Skip to content
Permalink
Browse files
Test List Code added
  • Loading branch information
aa9863 committed Oct 15, 2021
1 parent 060e742 commit edd4366b172fe699e3490780040fad356f4c1fa2
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
@@ -37,7 +37,7 @@ def simpleCrackReturn(wordlist, target):
"""
Code to crack an md5 hash using a dictionary
More efficent version as it returns as soon as it finds the match
More efficient version as it returns as soon as it finds the match
@param wordlist: List of dictionary Words
@param target: The Hash we want to crack
@@ -0,0 +1,120 @@
"""
Test Cases, and code for the list cracking task
"""

import unittest
import time
import hashlib

import test_function

#Store the correct MD5 for the later checks
CORRECT_MATCHES = {"24eb05d18318ac2db8b2b959315d10f2": "coffee",
"ab4f63f9ac65152575886860dde480a1": "azerty",
"11a7f956c37bf0459e9c80b16cc72107":"spitfire",
"0e2b0bb3a925c7001d953983f9de9823":"f00tball",
"bd1e13bdaab82581d4dc299eb9a3da0f":"1qazxsw23edc"}

#And the Hashes we want to crack
TARGETS = ["24eb05d18318ac2db8b2b959315d10f2",
"ab4f63f9ac65152575886860dde480a1",
"11a7f956c37bf0459e9c80b16cc72107",
"0e2b0bb3a925c7001d953983f9de9823",
"bd1e13bdaab82581d4dc299eb9a3da0f"]



def crackList_Iterative(wordlist, targets):
"""
Crack a list of hashes.
This approach is iterative, we do the hash cracking for each
of the items in turn.
May as well reuse the Simple Crack from the test_function code
"""

matches = {} #Store the matches

for item in targets:
#Reuse the cracked function from before
out = test_function.simpleCrack(wordlist, item)
matches[item] = out

return matches


def crackList_Lookup(wordlist, targets):
"""
This time we hash everything and stash in a dictionry (or DB),
then do a lookup
"""

#Somewhere to store the hashes
hashLookup = {}

#Go through the wordlist and get the hash for each item.
for plaintext in wordlist:
plaintext = plaintext.strip()
theHash = hashlib.md5(plaintext.encode()).hexdigest()
#Store in the "Database"
hashLookup[theHash] = plaintext

#Now look for the cracked passwords using the dict
matches = {}
for item in targets:
matches[item] = hashLookup.get(item)

return matches



class TestCases(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""
A bit of magic to keep the stats.
Called the first time the class is run
"""

cls.statsDict = {}

@classmethod
def tearDownClass(cls):
"""
And a bit more magic to print the stats.
"""

print("\n\n{0} STATS (List) {0}".format("-"*20))

print("Crack Iteratively: {0}".format(cls.statsDict["iter"]))
print("Crack Lookup Tab: {0}".format(cls.statsDict["lookup"]))

def setUp(self):
"""
Load the wordlist each time we run a test case
Here we open the wordlist file, then store it as an array
This lets us reuse the list multiple times
"""

with open("10-million-password-list-top-10000.txt") as fd:
#Store as an array
self.wordlist = fd.readlines()


def testList_Iterative(self):
t1 = time.time()
out = crackList_Iterative(self.wordlist, TARGETS)
t2 = time.time()
self.assertEqual(out, CORRECT_MATCHES) #check we were successful
self.statsDict["iter"] = t2-t1


def testList_Lookup(self):
t1 = time.time()
out = crackList_Lookup(self.wordlist, TARGETS)
t2 = time.time()
self.assertEqual(out, CORRECT_MATCHES) #check we were successful
self.statsDict["lookup"] = t2-t1

0 comments on commit edd4366

Please sign in to comment.