Session 03 - Python for Forensics
The code here is to support the Session 03 lecture.
passwordCrackZipExtD.py
import argparse, zipfile, time
def extract_zip(zFile, password):
try:
zFile.extractall(pwd=password.encode('utf-8'))
print("[+] Password Found: " + password + "\n")
return True
except Exception as e:
return False
def calc_times(time_zero, time_end):
difference = time_end - time_zero
return difference
def main():
parser = argparse.ArgumentParser(description="Zip file password cracker.")
parser.add_argument('-f', dest='zname', type=str, help='specify zipfile')
parser.add_argument('-d', dest='dname', type=str, help='specify dictionary file')
args = parser.parse_args()
if not (args.zname and args.dname):
parser.print_help()
exit(0)
else:
zname = args.zname
dname = args.dname
zFile = zipfile.ZipFile(zname)
t_zero = time.time()
password_found = False
with open(dname, 'r') as f:
for line in f.readlines():
password = line.strip('\n')
try:
if extract_zip(zFile, password):
password_found = True
break
except Exception as e:
pass
if password_found:
t_finish = time.time()
print('Seconds Taken to Crack Password and Extract Files: ' + str(calc_times(t_zero, t_finish)))
if __name__ == '__main__':
main()