SHA256 암호 크래커-무차별 대입
Nov 16 2020
SHA256 암호 크래커를 작성했습니다. 나는 처음에 정보를 저장하는 목록을 사용 list1.txt
하고 list2.txt
있지만 메모리 과부하를 그만 둘 것이다. 그래서 지금은 파일을 사용합니다. 이 전환을 수행했을 때 프로그램 속도가 크게 감소했음을 알았습니다. 메모리 과부하 문제없이이 프로그래밍의 속도를 최적화 할 수있는 방법이 있습니까?
감사
import hashlib
import time
import os
def extract_password(fname):
if fname == 0:
filename = "list2.txt"
else:
filename = "list1.txt"
for line in open(filename, "r"):
yield line.strip()
def save_password(fname, password):
if fname == 0:
filename = "list1.txt"
else:
filename = "list2.txt"
file = open(filename, "a")
file.write(password)
file.write("\n")
file.close()
def next_password(lsta, item,next_char, secure_password):
found = 0
guess = item + chr(next_char)
if hash_password(guess) == secure_password:
print(guess)
found = 1
save_password(lsta, guess)
return found
def hash_password(pwrd):
#Generates hash of original password
pwrd = pwrd.encode("UTF-8")
password = hashlib.sha256()
password.update(pwrd)
return password.hexdigest()
def delete_file(ltsa):
try:
if ltsa == 1:
os.remove("list2.txt")
else:
os.remove("list1.txt")
except:
pass
def reset_file_status():
try:
os.remove("list2.txt")
os.remove("list1.txt")
except:
pass
def find_password(secure_password):
#Brute force to find original password
found = 0
lsta = 1
for length in range(1, 15):
if found == 1: break
lsta = lsta^1
delete_file(lsta)
for next_char in range(65, 123):
if found == 1: break
if length == 1:
found = next_password(lsta, "", next_char, secure_password)
if found == 1: break
else:
for item in extract_password(lsta):
found = next_password(lsta, item, next_char, secure_password)
if found == 1: break
if __name__ == "__main__":
reset_file_status()
start = time.time()
secure_password = hash_password("AAAA")
find_password(secure_password)
print(f"{(time.time() - start)} seconds")
답변
4 LevM. Nov 16 2020 at 06:40
당신이 찾고있는 최적화는 매우 간단합니다.
스스로에게 물어보세요 . 내가 정말로 유지해야 할 것은 무엇입니까?
대답 : 현재 확인중인 암호 만 추측합니다.
모든 오래된 추측을 저장할 필요가 없으므로 파일이나 방대한 목록이 필요하지 않습니다.
정말 필요한 것은 계속 업데이트 할 하나의 문자열입니다.
이를 시각화하려면 각 문자가 58 자리 숫자로 늘어나는 숫자로 문자열을 생각해보십시오.
지금. 정말 필요한 것은 첫 번째 숫자에 +1을 수행하고, 일반적인 추가와 마찬가지로 필요한 경우 캐리를 확인하고 다음 숫자를 업데이트하는 것입니다.
불행히도 Python 문자열은 색인 별 할당을 허용하지 않지만 slicing을 지원 합니다.
다음은 모든 문자를 통해 실행되는 순차 암호를 생성하고 필요에 따라 길이를 늘리는 함수입니다 (호출 당 하나의 암호!).
def make_next_guess(guess):
carry = 1
next_guess = guess
for i in range(len(guess)):
cur_char = ord(guess[i]) + carry
if cur_char > ord('z'):
cur_char = ord('A')
carry = 1
else:
carry = 0
next_guess = next_guess[:i] + chr(cur_char) + guess[i + 1:]
if carry == 0:
break
if carry = 1:
next_guess += 'A'
return next_guess
이를 통해 최대 길이까지 모든 가능성에 대해 하나의 루프를 사용할 수 있습니다.
guess = 'A'
for _ in range(58 ** 14): #password maximum length 14 and there are 58 characters that can be used
if hash_password(guess) == secure_password:
print(guess)
break
guess = make_next_guess(guess)