本書第一章主要介紹了python的基礎(chǔ)知識(shí),包括變量、數(shù)據(jù)類型、函數(shù)、迭代、判斷語句、語句塊、異常處理及常用模塊的介紹。介紹了第三方庫的安裝方式,并在最后使用兩個(gè)案例實(shí)現(xiàn)了密碼暴力破解的功能,由于python筆者已經(jīng)較為熟悉,此處不再贅述python的基礎(chǔ)知識(shí)了。
python第三方模塊的安裝方法總結(jié):
1. 源代碼安裝
- 下載源代碼安裝包,使用wget工具直接從網(wǎng)絡(luò)下載對(duì)應(yīng)的數(shù)據(jù)包
wget http://XXXXXXXXXXX/XXX/XX/XX.tar.gz - 使用tar命令解壓縮
tar -xzf xxx.tar.gz
cd xxx - 利用python setup.py install 在解壓縮的目錄下完成安裝
python setup.py install
2. pip安裝
pip install xxx
3. easy_install安裝
easy_install xxx
4. 使用pycharm安裝
在pycharm setting中

image.png
點(diǎn)擊+號(hào),在彈出窗口中搜索需要的包

image.png
點(diǎn)擊install package完成安裝即可
unix口令破解器
- crypt加密版
需要導(dǎo)入crypt包,然后使用crypt.crypt(word,salt)將字典中的明文密碼加密,salt是加鹽后的密碼前兩位,word為字典中的密碼,cryptPass為待破的密碼.
def checkPassCrypt(cryptPass):
salt = cryptPass[0:2]
dictFile = open('dictionary.txt', 'r')
for word in dictFile.readlines():
word = word.strip("\n")
cryptWord = crypt.crypt(word, salt)
if cryptWord == cryptPass:
print("[+] Found Password : %s \n" % (word))
return
print("[-] Password Not Found.\n")
return
```
- sha-512加密版
sha512或者其他sha256等hash加密的均可以使用python的hashlib
將字典的密碼使用sha512加密然后與原加密的hash比對(duì)進(jìn)行破解
hashlib.sha512(passwrod).hexdigest()
def chechPassSHA512(cryptPass):
dictFile = open('dictionary.txt', 'r')
for word in dictFile.readlines():
word = word.strip("\n")
cryptWord = hashlib.sha512(word).hexdigest()
if cryptWord == cryptPass:
print("[+] Found Password : %s \n" % (word))
return
print("[-] Password Not Found.\n")
return
zip文件口令破解機(jī)
主要使用zipfile庫,使用ZipFile類中的extratall()完成指定密碼后的zipfile文件解壓,解壓路徑是當(dāng)前路徑,也可以使用path進(jìn)行路徑指定
還用到了optparser庫進(jìn)行命令行參數(shù)的解析
用到threading進(jìn)行多線程操作
import zipfile
from threading import Thread
import optparse
def extractZipFile(zFile, password):
try:
zFile.extractall(pwd=password)
print('[+] Found Password:%s\n' % (password))
except:
pass
def main():
parser = optparse.OptionParser("usage%prog -f <zipfile> -d <dictionary>")
parser.add_option('-f', dest='zname', type="string", help='specify zip file')
parser.add_option('-d', dest='dname', type='string', help='specify dictionary file')
(options, args) = parser.parse_args()
if options.zname == None or options.dname == None:
print(parser.usage)
exit(0)
else:
zname = options.zname
dname = options.dname
zFile = zipfile.ZipFile(zname)
passFile = open(dname)
for line in passFile.readlines():
password = line.strip('\n')
t = Thread(target=extractZipFile, args=(zFile, password))
t.start()
if __name__ == '__main__':
main()