如何传递参数使代码正常工作
我正在做一本叫《暴力Python》的书里的练习,这是一个UNIX密码破解器。书里假设你已经在某个地方传入了密码名称和盐值作为参数,这样它才能正常工作,但原本需要的参数在哪里都没有说明。我应该在哪里传入这些参数或者声明一个变量呢?
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()
1 个回答
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
和 root
是用户名,而 HX9LLTdc/jiDE
和 DFNFxgW7C05fo
是对应的加密密码)。接着,加密密码会被传递给 testPass
函数,并且假设它的前两个字符是“盐”。