2014-04-16 34 views
0

我正在做这本书“Violent Python”的练习,它是一个UNIX密码破解程序。这本书让人理所当然地认为,你已经将密码名称和salt作为参数传递给某个地方,这样它就可以工作,但所需的原始参数不会在任何地方声明。我应该在哪里传递/声明一个变量?如何将此代码的参数传递到工作

import crypt 



def testPass(cryptPass): 
    salt = cryptPass[0:2] 
    dictFile = open('/home/cf/Downloads/CH1/dictionary.txt','r') 
    for word in dictFile.readlines(): 
     word = word.strip('\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('/home/cf/Downloads/CH1/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() 
+2

您似乎是从'passwords.txt'中提取它们。 –

+1

根据你的代码,'salt'需要'cryptPass'的前两个字符('salt = cryptPass [0:2]') – Don

回答

2

说你passwords.txt看起来像

victim: HX9LLTdc/jiDE: 503:100:Iama Victim:/home/victim:/bin/sh 
root: DFNFxgW7C05fo: 504:100: Markus Hess:/root:/bin/bash 

那么main函数打开这个文件,包含它分析出用户和加密的密码(冒号每一行victim/rootHX9LLTdc/jiDE /分别为DFNFxgW7C05fo)。加密的密码然后被传递给testPass函数,并且它的前两个字符被假定为盐。

+0

谢谢你进一步解释,现在我明白了,但看起来像是有问题检查字典文件的func,因为它不会破解受害者密码。 –

+0

你有特定的错误吗?你在使用Linux吗(crypt模块只能在Linux上使用)?你的'dictionary.txt'中是否包含'egg'这个词('HX9LLTdc/jiDE'用salt'HX'解密'''egg')解密? – BioGeek

+0

是的,它确实...可能是路线问题?在执行时或调试时我都没有遇到任何错误 –

相关问题