I am trying to build an md5 cracker for practice. Before I go any further here is my code:
def offline_wordlist_attack(list_path):
with fileinput.input(files=(list_path)) as wordlist:
for word in wordlist:
md5_hash_object = hashlib.md5() # constructing an md5 hash object
md5_hash_object.update(binascii.a2b_uu(word))
word_digest = md5_hash_object.digest() # performing the md5 digestion of the word
print(word_digest) # Debug
My issue is with md5_hash_object.update(binascii.a2b_uu(word))
. The hashlib Python 3 documentation states that the string passed to update()
should be in binary representation. The documentation uses m.update(b"Nobody inspects")
as an example. In my code, I can not simply attach b
in front of the variable word
. So I tried to use the binascii library, but that library too, has a note in the documentation stating:
Note
Encoding and decoding functions do not accept Unicode strings. Only bytestring and bytearray objects can be processed.
Could somebody help me out with this? It is getting the better of me.
Method #1 : Using join() + ord() + format() The ord function converts the character to it's ASCII equivalent, format converts this to binary number and join is used to join each converted character to form a string.
In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.
Python String encode() MethodThe encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.
You need to pass in a bytes
object, rather than a str
. The typical way to go from str
(a unicode string in Python 3) to bytes
is to use the .encode()
method on the string and specify the encoding you wish to use.
my_bytes = my_string.encode('utf-8')
Just call fileinput.input(...,mode='rb')
to open files in binary mode. Such files produce binary strings instead of Unicode strings as files opened in text mode do.
It allows you to skip an unnecessary (implicit) decoding of bytes read from disk followed by immediate encoding them back to bytes using .encode()
before passing them to md5()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With