我目前正处于软件开发学位的第三年,我正在考虑明年扩展到一些安全模块,我选择“Violent Python”作为介绍并学习一门新语言,但是我我坚持第一次练习。
我不是编程的新手,也许我只是累了 - 这是一个漫长的一天很少睡觉 - 但这个简单的剧本不会工作:
import crypt
def testPass(cryptPass):
salt = cryptPass[0:2]
dictFile = open('dictionary.txt')
for word in dictFile.readlines():
word = word.strip('\n')
print "[*] Attempting password: "+word+"\n"
cryptWord = crypt.crypt(word,salt)
if (cryptWord == cryptPass):
print "[+] Found Password: "+word+"\n"
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()
Passwords.txt有两个来自/ etc / passwd(受害者和root用户)的用户,它可以很好地循环它们。我的dictionary.txt中有三个密码,由于某种原因它只尝试第一个密码:
[*] Cracking Password For: victim
[*] Attempting password: break
[-] Password Not Found.
[*] Cracking Password For: root
[*] Attempting password: break
[-] Password Not Found.
有人可以解释为什么上面提到的代码没有起作用吗?我设法通过使用'with open'来解决问题:
with open('dictionary.txt') as f:
for word in f:
word = word.strip("\n")
cryptWord = crypt.crypt(word,salt)
if (cryptWord == cryptPass):
print "[+] Found Password: "+word+"\n"
return
print "[-] Password Not Found.\n"
return
答案 0 :(得分:2)
这是一个缩进错误。为了使事情正常工作,您的'testPass'功能应如下所示:
def testPass(cryptPass):
salt = cryptPass[0:2]
dictFile = open('dictionary.txt')
for word in dictFile.readlines():
word = word.strip('\n')
print "[*] Attempting password: "+word+"\n"
cryptWord = crypt.crypt(word,salt)
if (cryptWord == cryptPass):
print "[+] Found Password: "+word+"\n"
return
print "[-] Password Not Found.\n"
return
这样,它将循环遍历字典中的每个单词,并在找到密码后返回。如果它在没有找到它的情况下遍历所有内容,它应该然后打印“找不到密码”。你的问题是你的代码(为了清晰起见而略微间隔)实际上是这样缩进的:
def testPass(cryptPass):
salt = cryptPass[0:2]
dictFile = open('dictionary.txt')
for word in dictFile.readlines():
word = word.strip('\n')
print "[*] Attempting password: "+word+"\n"
cryptWord = crypt.crypt(word,salt)
if (cryptWord == cryptPass):
print "[+] Found Password: "+word+"\n"
return
print "[-] Password Not Found.\n"
return
看到区别?像这样,它只会检查第一个单词,因为第二个“返回”在循环内。请验证您的缩进(理想情况下为4个空格,如@jonrsharpe所说,或选项卡)并再次尝试。我怀疑这会解决你的问题。