Session 03 - Python for Forensics
The code here is to support the Session 03 lecture.
passwordCrackZipExtE.py
import argparse, zipfile, time, csv
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 filenames_info(zFile, password):
for info in zFile.infolist():
print(info.filename, info.date_time, info.file_size)
def mkCSV(zFile, password):
with open('extractResult.csv', 'a', newline='') as csvfile:
outfile = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
outfile.writerow(['Filename', 'File DateTime', 'File Size'])
for info in zFile.infolist():
outfile.writerow([info.filename, info.date_time, info.file_size])
def main():
parser = argparse.ArgumentParser(description="Zip file password cracker and CSV generator.")
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 args.zname is None or args.dname is None:
print(parser.usage)
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
t_finish = time.time()
print('Seconds Taken to Crack Password and Extract Files: ' + str(calc_times(t_zero, t_finish)))
mkCSV(zFile, password)
break
except Exception as e:
pass
if __name__ == '__main__':
main()