不知道如何输入一个函数的输出作为另一个函数的输入
问题描述:
我在这里有一个脚本,其中包含一些基本功能: 函数1-wget打开一个网页并将其保存到本地变量中,然后关闭。 函数2 - 为这个网页抓取md5散列值。 函数3 - 获取散列值并使用常用密码字典对它们进行破解。不知道如何输入一个函数的输出作为另一个函数的输入
我的问题是从函数2获取我的输出并将其插入到函数3中。部分原因是函数2的输出为列表,而函数3正在查找散列值。
你们很有可能通过阅读我的代码来了解更多,下面是我的代码到目前为止。
import sys, hashlib, re, urllib
def wget(url): # could import webpage_get and use wget() from there instead
'''Read the contents of a webpage from a specified URL'''
print '[+]---------------------------------------------------------------------------- ' #CHANGE THIS
# open URL
webpage = urllib.urlopen(url) # opens url like a file
# get page contents
page_contents = webpage.read() # reads content of webpage
return page_contents
page_contents = webpage.close() # close webpage
def findmd5(text):
'''Find all md5 hash values'''
md5value = re.findall(r'([a-fA-F\d]{32})', text)
count = len(md5value)
print "[+] Total number of md5 hash values found: %s" % count
for x in md5value:
print x
def dict_attack(passwd_hash):
dic = ['123','1234','12345','123456','1234567','12345678','password','qwerty','abc','abcd','abc123','111111','monkey','arsenal','letmein','trustno1','dragon','baseball','superman','iloveyou','starwars','montypython','cheese','123123','football','password','batman']
passwd_found = False
for value in dic:
hashvalue = hashlib.md5(value).hexdigest()
if hashvalue == passwd_hash:
passwd_found = True
recovered_password = value
if passwd_found == True:
print '[+] Password recovered: %s'% (recovered_password)
else:
print '[-] Password not recovered'
def main():
# temp testing url argument
sys.argv.append('URL HERE!')
# Check args
if len(sys.argv) != 2:
print '[-] Usage: email_analysis URL/filename'
return
#call functions
try:
print '[+] md5 values found: '
print findmd5(wget(sys.argv[1]))
print '[+] Cracking hash values: '
except IOError:
print 'Error'
if __name__ == '__main__':
main()
任何帮助,非常感谢!
答
wget:将return语句设置为最后一条语句。
findmd5:从打印它的结果变为将它们返回到主变量中。
main:在for循环中添加以遍历发现的散列并将dict_attack应用于每个值。
但是我确实没有建立任何中断或停止状态,所以即使找到了,程序也会继续运行。但它仍会打印找到的结果。
import sys, hashlib, re, urllib
def wget(url): # could import webpage_get and use wget() from there instead
'''Read the contents of a webpage from a specified URL'''
print ('[+]---------------------------------------------------------------------------- ') #CHANGE THIS
# open URL
webpage = urllib.urlopen(url) # opens url like a file
# get page contents
page_contents = webpage.read() # reads content of webpage
page_contents = webpage.close() # close webpage
return page_contents
def findmd5(text):
'''Find all md5 hash values'''
md5value = re.findall(r'([a-fA-F\d]{32})', text)
count = len(md5value)
print ("[+] Total number of md5 hash values found: %s" % count)
return md5value
def dict_attack(passwd_hash):
dic = ['123','1234','12345','123456','1234567','12345678','password','qwerty','abc','abcd','abc123','111111','monkey','arsenal','letmein','trustno1','dragon','baseball','superman','iloveyou','starwars','montypython','cheese','123123','football','password','batman']
passwd_found = False
for value in dic:
hashvalue = hashlib.md5(value).hexdigest()
if hashvalue == passwd_hash:
passwd_found = True
recovered_password = value
if passwd_found == True:
print ('[+] Password recovered: %s'% (recovered_password))
else:
print ('[-] Password not recovered')
def main():
# temp testing url argument
sys.argv.append('URL HERE!')
# Check args
if len(sys.argv) != 2:
print ('[-] Usage: email_analysis URL/filename')
return
#call functions
try:
md5Values = findmd5(wget(sys.argv[1]))
for md5value in md5values:
dict_attack(md5value)
print ('[+] Cracking hash values: ')
except IOError:
print ('Error')
if __name__ == '__main__':
main()
+0
非常感谢! –
答
比利,返回发现的哈希列表,而不是打印它们(它看起来像你认为它是bash,但你不需要“打印”一个函数的输出,就像你在bash,在python中,你可以从字面上返回一个包含元素的数组)。
您的散列表的正则表达式使用\d
,但也包括-
,它可能会带来不是MD5散列的东西。
我看到一个关于这个完全相同的任务不到一个小时前的问题。你能和你的同学一起工作吗? – TigerhawkT3