1
我已经开始学习Python,并开始使用Violent Python这本书。其中第一章描述了一个脚本,在crypt
类的帮助下破解基于nix的密码哈希。下面是代码:相等的字符串不会返回true
import crypt
def testPass(cryptPass):
salt = cryptPass[0:2]
dictFile = open('dictionary.txt','r')
for word in dictFile.readlines():
word = word.strip('\n')
cryptWord = crypt.crypt(word,salt)
print cryptPass+":"cryptWord
if(cryptPass == cryptWord):
print "[+] Password found : "+word
return
print "[-] Password Not Found.\n"
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 Password For: "+user
testPass(cryptPass)
if __name__ == "__main__":
main()
我有一个password.txt的文件,其中包含用户名:密码(密码哈希)的字符串,并命名为另一个文件dictionary.txt其中包含词典words.These是密码的内容.txt文件:
apple:HXJintBqUVCEY
mango:HXInAjNhZF7YA
banana:HXKfazJbFHORc
grapes:HXtZSWbomS0xQ
和dictionary.txt:
apple
abcd
efgh
ijkl
mnop
从testpass计算出的密码哈希()方法和从password.txt的为用户N的密码哈希艾美苹果是平等的,当我打印他们两个。但是这里所有4个用户名的输出是“[ - ]密码找不到”。为什么==测试在这里失败?
使用'.strip( '\ n')'会比简单链接两个'strip'方法调用。 –
'.strip()'将适用于换行符和空格 –
非常感谢。这个技巧,来自Java背景,从来没有想过会有结尾的\ n结尾。 –