Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Fixing Code
#1
I'm doing a cybersecurity course and one of the levels requires editing a python script to brute force a .zip file. I edited some very easy errors like adding parenthesis etc. But when I run the code I get these errors and I dont know how to fix them.
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)
# 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.')
Error:
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ================== RESTART: C:\Users\kianw\Desktop\zipcrack.py ================= Trying: Superaaa Traceback (most recent call last): File "C:\Users\kianw\Desktop\zipcrack.py", line 33, in <module> if extractFile(zip_file, password): File "C:\Users\kianw\Desktop\zipcrack.py", line 8, in extractFile zip_file.extractall(pwd=password) File "C:\Users\kianw\AppData\Local\Programs\Python\Python38\lib\zipfile.py", line 1647, in extractall self._extract_member(zipinfo, path, pwd) File "C:\Users\kianw\AppData\Local\Programs\Python\Python38\lib\zipfile.py", line 1700, in _extract_member with self.open(member, pwd=pwd) as source, \ File "C:\Users\kianw\AppData\Local\Programs\Python\Python38\lib\zipfile.py", line 1497, in open raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__) TypeError: pwd: expected bytes, got str >>>
Reply
#2
Post your code here on the forum - in python tags, full traceback - in error tags.
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
extractFile need pwd paramenter to be bytes not string.
Can do it in the function call.
if extractFile(zip_file, password.encode()):
The basic is.
>>> s = 'Secret'
>>> s
'Secret'
>>> type(s)
<class 'str'>
>>> 
>>> s_byte = s.encode() #Same as s.encode('utf-8')
>>> s_byte
b'Secret'
>>> type(s_byte)
<class 'bytes'>
Reply
#4
(May-12-2020, 11:26 AM)buran Wrote: Post your code here on the forum - in python tags, full traceback - in error tags.
See BBcode help for more info.

Should i do it in a separate post or in a reply?
Reply
#5
(May-12-2020, 11:58 AM)kianwalters05 Wrote: Should i do it in a separate post or in a reply?
I edited your post to include the code and error
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(May-12-2020, 12:05 PM)buran Wrote:
(May-12-2020, 11:58 AM)kianwalters05 Wrote: Should i do it in a separate post or in a reply?
I edited your post to include the code and error
Wasnt showing up for me, thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need help with fixing data vulnarabilities anaconda99 1 963 May-03-2025, 05:08 PM
Last Post: buran
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 8 4,343 Dec-03-2024, 10:21 AM
Last Post: skylartyler
  Invalid syntax error - need help fixing calgk01 3 5,431 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Fixing a code error Twoshawns 5 4,631 May-14-2020, 11:56 AM
Last Post: Twoshawns
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 108,353 Mar-09-2020, 03:20 PM
Last Post: syssy
  Fixing the code khanhcao 2 130,671 Feb-06-2020, 07:24 AM
Last Post: buran
  Fixing a problem with file.io ThickTac 2 4,078 Mar-13-2019, 10:13 PM
Last Post: ThickTac
  Fixing indentation issues. MuntyScruntfundle 9 6,978 Feb-02-2019, 05:55 PM
Last Post: snippsat
  Can anyone Please help on fixing this python code to connect and save things on sqlit Roscoes 2 4,196 Mar-06-2018, 04:48 AM
Last Post: Roscoes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020