Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Session 03 - Python for Forensics

The code here is to support the Session 03 lecture.

passwordCrackZipExtC.py

import argparse, zipfile, time

def extract_zip(zFile, password):
    try:
        zFile.extractall(pwd=password.encode('utf-8'))
        print("[+] Password Found: " + password + "\n")
    except Exception as e:
        pass

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()
    with open(dname, 'r') as f:
        for line in f.readlines():
            password = line.strip('\n')
            try:
                extract_zip(zFile, password)
                t_finish = time.time()
                print('Seconds Taken to Crack Password and Extract Files: ' + str(calc_times(t_zero, t_finish)))
            except Exception as e:
                pass

if __name__ == '__main__':
    main()
[ Last updated: Tuesday 13 February 2024 @ 13:15:52 +0000 // branch: master // commit: 7a80d61 ]