我正在为密码破解器制作一个多进程,上次运行它时,我的计算机真的死了。我有什么办法可以防止这种情况发生吗?我的内存使用量达到100%,我的电脑冻结了。我需要运行多个进程,以允许不同的程序一次测试部分猜测。如果我有更多的内存,这会起作用,但如果有人有任何解决方案,请这样说。
import hashlib
import itertools
import time
flag2=0
def getpass(comb, usechar, pass_hash):
global flag2
st = time.time()
for guess in comb:
guess = ''.join(guess)
digest = hashlib.md5(guess.encode('utf-8').strip()).hexdigest()
if digest == pass_hash:
print("password is: " + guess)
flag2=1
print("Time Completed(seconds): " + str(time.time() - st))
break;
if flag2==1:
break;
if __name__ == '__main__':
from multiprocessing import Process
flag = 0
lower = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
upper = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
nums = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
char = ["~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "[", "]", "{", "}", ":", ";", "'", """, "\", "|", ",", ".", "<", ">", "?", "/"]
pass_hash = input("Enter md5 hash: ")
#a=all, l=letters, n=numbers
pass_type = input(str("Enter Password Type: "))
usechar = []
processes = []
if pass_type == "a":
usechar = nums + lower + upper + char
elif pass_type == "l":
usechar = lower + upper
elif pass_type == "n":
usechar = nums
else:
usechar = nums + lower + upper + char
try:
pass_file = open("Passwords.txt", "r")
print("Accessing Passwords")
except:
print("No file found")
quit()
st = time.time()
for word in pass_file:
enc_word = word.encode('utf-8')
digest = hashlib.md5(enc_word.strip()).hexdigest()
if digest == pass_hash:
print("Password found")
print("Password is " + word)
print("Time Completed(seconds): " + str(time.time() - st))
flag = 1
break
if flag == 0:
print("Password is not in the list. Using brute-force")
for password_length in range(1, 20):
processes = []
comb = itertools.product(usechar, repeat=password_length)
thr = 0
leng = 0
for i in range(1,20):
leng += len(usechar)^i
for i in range(0,2):
processes.append(Process(target=getpass, args=[list(comb)[thr:round(leng/2)+thr], usechar, pass_hash]))
thr+=round(leng/2)
for process in processes:
process.start()
for process in processes:
process.join()
if flag2==0:
print("password is over 20 characters long")
首先,变量comb
是一个非常长的列表,您将把非常长的部分传递给每个子流程。这可能是你所有记忆的去向。
使用某种大小的线程池,并根据文本的前两个字母创建多个工作片段,可能会更明智。然后,每个工作人员都会用前两个字母生成所有密码(作为生成器,而不是一个完整的列表(,看看他们找到了什么。你会发现你根本不需要太多的记忆。
这是一个简短的大纲,只在第一个字符上分解。
def worker(start_character):
for length in range(1, max_length):
for password_suffix in itertools.product(usechar, length - 1)
password = start_character + password_suffix
......
with multiprocessing.Pool() as pool:
pool.map(cracker, usechar)
每个线程实际上根本不使用内存。您可以使用与您的机器有CPU一样多的线程。