Search code examples
pythonzip

Brute Force zip file with password returning syntax error


I was given a problem to solve, asking to add 3 more letters to 'Super' and then use it to unlock a zip file. My code is as follows:

import zipfile
import itertools
import time

# Function for extracting zip files to test if the password works!
def extractFile(zip_file, password):
    try:
        zip_file.extractall(pwd=password)
        return True
    except KeyboardInterrupt:
        exit(0)
    except Exception, e:
        pass

# Main code starts here...
# The file name of the zip file.
zipfilename = 'planz.zip'
# The first part of the password. We know this for sure!
first_half_password = 'Super'
# We don't know what characters they add afterwards...
# This is case sensitive!
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
zip_file = zipfile.ZipFile(zipfilename)

# We know they always have 3 characters after Super...
# For every possible combination of 3 letters from alphabet...
for c in itertools.product(alphabet, repeat=3):
    # Slowing it down on purpose to make it work better with the web terminal
    # Remove at your peril
    time.sleep(0.001)
    # Add the three letters to the first half of the password.
    password = first_half_password+''.join(c)
    # Try to extract the file.
    print "Trying: %s" % password
    # If the file was extracted, you found the right password.
    if extractFile(zip_file, password):
        print '*' * 20
        print 'Password found: %s' % password
        print 'Files extracted...'
        exit(0)

# If no password was found by the end, let us know!
print 'Password not found.'

But my code returns

./code.py: line 6: syntax error near unexpected token ('./code.py: line 6: def extractFile(zip_file, password):'

What is the syntax error, because I'm not able to find it?


Solution

  • You have to add a shebang: In the first line add

    #! /usr/bin/env python2