So in order to teach myself how to create multi-threaded python apps I decided to have a go this morning at writing a simple MD5 bruteforcer (using a wordlist).

The way this works is that you create worker threads and each worker thread pulls an item from the queue and processes it; when finished it pulls another item from the queue and so on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
# md5brute.py

import sys,Queue,threading,hashlib,os

NumOfThreads=100  # Number of threads
queue = Queue.Queue()

# get hash and wordlist input
try:
    TargetHash=sys.argv[1].lower()
    WordList=open(sys.argv[2],'r')
except:
    print "Usage: %s hash wordlist" % sys.argv[0]
    sys.exit(1)

class checkHash(threading.Thread):
    def __init__(self,queue):
        threading.Thread.__init__(self)
        self.queue=queue
    def run(self):
        while True:
            self.clear=self.queue.get()
            if hashlib.md5(self.clear).hexdigest() == TargetHash:
                print "Hash found!!! %s = %s" % (self.clear,TargetHash)
                os._exit(0)
            self.queue.task_done()

#spawn a pool of threads
for i in range(NumOfThreads):
    t=checkHash(queue)
    t.setDaemon(True)
    t.start()

#populate queue with wordlist
for word in WordList.readlines():
    queue.put(word.strip())

queue.join()

We will try to crack the MD5 hash b0b129991a71c1ba6e8b6a280c5fbed2 using the rockyou wordlist.

1
2
3
4
Matthews-MacBook-Pro:Desktop phillips321$ ./md5brute.py
Usage: ./md5brute.py hash wordlist
Matthews-MacBook-Pro:Desktop phillips321$ ./md5brute.py b0b129991a71c1ba6e8b6a280c5fbed2 rockyou.txt
Hash found!!! a6_123 = b0b129991a71c1ba6e8b6a280c5fbed2

Voila! It worked.

What’s important to note here is that although python is running multiple threads it is only running on 1 core so it’s not making full use of the CPU.
Screen Shot 2013-08-31 at 17.09.17
Screen Shot 2013-08-31 at 17.09.36
In order to fully utilise the multiple cores on the CPU I would need to use the multiprocessing module in python… but i’ll save that for another day.

Leave a Reply