Violent Python Chapter #1 Final

by Marín

And that’s all for the chapter #1! The last exercise was quite easy after the ones before. It is a Zip File cracker. It uses a dictionary attack, hope you like it.


# Violent Python
# Chapter #1
# Exercise #3 Zip file cracker
# By: Marin Alcaraz

import zipfile
import sys


def decrypt_file(filename):
    zFile = zipfile.ZipFile(filename)
    dict_file = open("dict.txt", 'r')
    print "[*] Decrypting file..."
    for line in dict_file.readlines():
        password = line.strip('\n')
        try:
            zFile.extractall(pwd=password)
            return password
        except Exception, e:
            pass

def main():

    if (len(sys.argv) == 2):
        filename = sys.argv[1]
        p = decrypt_file(filename)
        if p:
            print "[+] Password[" + p + "]"
    else:
        print "Usage: python py-pass.py file.zip"
        exit(0)

if __name__ == '__main__':
    main()