Ik heb deze fout:
Traceback (most recent call last):
File "python_md5_cracker.py", line 27, in <module>
m.update(line)
TypeError: Unicode-objects must be encoded before hashing
wanneer ik deze code probeer uit te voeren in Python 3.2.2:
import hashlib, sys
m = hashlib.md5()
hash = ""
hash_file = input("What is the file name in which the hash resides? ")
wordlist = input("What is your wordlist? (Enter the file name) ")
try:
hashdocument = open(hash_file, "r")
except IOError:
print("Invalid file.")
raw_input()
sys.exit()
else:
hash = hashdocument.readline()
hash = hash.replace("\n", "")
try:
wordlistfile = open(wordlist, "r")
except IOError:
print("Invalid file.")
raw_input()
sys.exit()
else:
pass
for line in wordlistfile:
# Flush the buffer (this caused a massive problem when placed
# at the beginning of the script, because the buffer kept getting
# overwritten, thus comparing incorrect hashes)
m = hashlib.md5()
line = line.replace("\n", "")
m.update(line)
word_hash = m.hexdigest()
if word_hash == hash:
print("Collision! The word corresponding to the given hash is", line)
input()
sys.exit()
print("The hash given does not correspond to any supplied word in the wordlist.")
input()
sys.exit()
Antwoord 1, autoriteit 100%
Het is waarschijnlijk op zoek naar een tekencodering uit wordlistfile
.
wordlistfile = open(wordlist,"r",encoding='utf-8')
Of, als u regel voor regel werkt:
line.encode('utf-8')
BEWERKEN
Volgens de onderstaande opmerking en dit antwoord.
Mijn antwoord hierboven gaat ervan uit dat de gewenste uitvoer een str
is uit het bestand wordlist
. Als u vertrouwd bent met het werken in bytes
, kunt u beter open(wordlist, "rb")
gebruiken. Maar het is belangrijk om te onthouden dat uw hashfile
NIETrb
moet gebruiken als u het vergelijkt met de uitvoer van hexdigest
. hashlib.md5(value).hashdigest()
voert een str
uit en die kan niet direct worden vergeleken met een bytes-object: 'abc' != b'abc'
. (Er is nog veel meer over dit onderwerp, maar ik heb geen tijd ATM).
Er moet ook worden opgemerkt dat deze regel:
line.replace("\n", "")
Zou waarschijnlijk zijn
line.strip()
Dat werkt voor zowel bytes als str’s. Maar als u besluit om gewoon te converteren naar bytes
, dan kunt u de regel wijzigen in:
line.replace(b"\n", b"")
Antwoord 2, autoriteit 43%
U moet het encoding format
definiëren, zoals utf-8
,
Probeer deze makkelijke manier,
Dit voorbeeld genereert een willekeurig getal met behulp van het SHA256-algoritme:
>>> import hashlib
>>> hashlib.sha256(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
'cd183a211ed2434eac4f31b317c573c50e6c24e3a28b82ddcb0bf8bedf387a9f'
Antwoord 3, autoriteit 9%
import hashlib
string_to_hash = '123'
hash_object = hashlib.sha256(str(string_to_hash).encode('utf-8'))
print('Hash', hash_object.hexdigest())
Antwoord 4, autoriteit 5%
Om het wachtwoord (PY3) op te slaan:
import hashlib, os
password_salt = os.urandom(32).hex()
password = '12345'
hash = hashlib.sha512()
hash.update(('%s%s' % (password_salt, password)).encode('utf-8'))
password_hash = hash.hexdigest()
Antwoord 5, autoriteit 5%
De fout zegt al wat je moet doen. MD5 werkt op bytes, dus je moet Unicode-string coderen in bytes
, b.v. met line.encode('utf-8')
.
Antwoord 6, autoriteit 3%
Bekijk eerst datantwoord.
Nu is de foutmelding duidelijk: je kunt alleen bytes gebruiken, geen Python-strings (wat vroeger unicode
was in Python < 3), dus je moet de strings coderen met je voorkeur codering: utf-32
, utf-16
, utf-8
of zelfs een van de beperkte 8-bit-coderingen (wat sommigen codepagina’s noemen ).
De bytes in je woordenlijstbestand worden automatisch gedecodeerd naar Unicode door Python 3 terwijl je uit het bestand leest. Ik stel voor dat je het volgende doet:
m.update(line.encode(wordlistfile.encoding))
zodat de gecodeerde gegevens die naar het md5-algoritme worden gepusht, precies zoals het onderliggende bestand worden gecodeerd.
Antwoord 7, autoriteit 3%
het coderen van deze regel loste het voor mij op.
m.update(line.encode('utf-8'))
Antwoord 8, Autoriteit 3%
U kunt het bestand in binaire modus openen:
import hashlib
with open(hash_file) as file:
control_hash = file.readline().rstrip("\n")
wordlistfile = open(wordlist, "rb")
# ...
for line in wordlistfile:
if hashlib.md5(line.rstrip(b'\n\r')).hexdigest() == control_hash:
# collision
Antwoord 9
Als het een enkele lijnkoord is. wikkel het met B of B. E.G:
variable = b"This is a variable"
of
variable2 = B"This is also a variable"
Antwoord 10
Dit programma is de foutvrije en verbeterde versie van de bovenstaande MD5-cracker die het bestand bevat met de lijst met hashed-wachtwoorden en controleert het tegen Hashed Word uit de Engelse woordenlijst. Ik hoop dat het nuttig is.
Ik heb het Engelse woordenboek gedownload van de volgende link
https://github.com/dwyl/english-words
# md5cracker.py
# English Dictionary https://github.com/dwyl/english-words
import hashlib, sys
hash_file = 'exercise\hashed.txt'
wordlist = 'data_sets\english_dictionary\words.txt'
try:
hashdocument = open(hash_file,'r')
except IOError:
print('Invalid file.')
sys.exit()
else:
count = 0
for hash in hashdocument:
hash = hash.rstrip('\n')
print(hash)
i = 0
with open(wordlist,'r') as wordlistfile:
for word in wordlistfile:
m = hashlib.md5()
word = word.rstrip('\n')
m.update(word.encode('utf-8'))
word_hash = m.hexdigest()
if word_hash==hash:
print('The word, hash combination is ' + word + ',' + hash)
count += 1
break
i += 1
print('Itiration is ' + str(i))
if count == 0:
print('The hash given does not correspond to any supplied word in the wordlist.')
else:
print('Total passwords identified is: ' + str(count))
sys.exit()