-2

I have developed a zipfile password cracker which does a brute force attack. The password of the zipfile is 1234. When ever I run the program it gives me this error:

Traceback (most recent call last):
  File "C:\Users\Kartikey\Desktop\cracking\bruteforce\bruteforce.py", line 51, in <module>
    zf.extractall(password)
  File "C:\Python27\lib\zipfile.py", line 1040, in extractall
    self.extract(zipinfo, path, pwd)
  File "C:\Python27\lib\zipfile.py", line 1028, in extract
    return self._extract_member(member, path, pwd)
  File "C:\Python27\lib\zipfile.py", line 1069, in _extract_member
    targetpath = os.path.join(targetpath, arcname)
  File "C:\Python27\lib\ntpath.py", line 84, in join
    result_path = result_path + '\\'
TypeError: can only concatenate tuple (not "str") to tuple

This is the code:

from zipfile import ZipFile
import itertools
#--------------------------------------CHARECTER SET--------------------------------------
pincharsonlynums = '1234'      
passcharswithnonumsnocap = 'abcdefghijklmnopqrstuvwxyz'
passcharswithnumsbtnocap = 'abcdefghijklmnopqrstuvwxyz1234567890'
passcharswithnumsandcap = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
count = 1

passwdlength = 0

#--------------------------------------CONFIGURATION--------------------------------------
print "\n1. What charecter set do you want to use?"
print "1 - 1234567890"
print "2 - abcdefghijklmnopqrstuvwxyz"
print "3 - abcdefghijklmnopqrstuvwxyz1234567890"
print "4 - abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

#charsetchoise = input("Your choise (1/2/3/4): ")

passwdlength = input("\n2. Enter the max length of the password you want to generate: ")

#zipfile = input("\n3. Enter the name of the zipfile with path: ")


#--------------------------------------START CRACKING--------------------------------------

while (count != 0):
    gen = itertools.combinations_with_replacement(pincharsonlynums,passwdlength) #1
    for password in gen:                                                      #2 
        with ZipFile('downloads.zip') as zf:
           # try:
                zf.extractall(password)
           # except:
              #  print "Failed."

Any ideas?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

3

itertools.combinations_with_replacement() produces tuples with individual characters, not strings:

>>> import itertools
>>> gen = itertools.combinations_with_replacement('1234', 3)
>>> next(gen)
('1', '1', '1')

Use ''.join() to form these into one string:

password = ''.join(password)

Note however that the first argument to zf.extractall() is a path, not a password. You are trying to extract the contents to the path named by the generated password. I doubt that that is what you wanted to do.

Specify a password with the pwd keyword argument:

zf.extractall(pwd=password)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    @KartikeyKushwaha: please don't change your question to ask what is a new problem. That'd be a subject for a new post. Your password is wrong, catch that exception and try a new one. – Martijn Pieters Jun 01 '16 at 06:40
  • @machineyearning: I know you mean well but your wording is not quite correct. Posting an error and your relevant code is **excellent**. Yes, more research would have been helpful but many beginners don't know how to do so. This isn't that bad a question. – Martijn Pieters Jun 01 '16 at 06:46
  • @MartijnPieters Thanks for your clarification, sorry my wording was imprecise. I was thinking not in terms of the rules of the site, but just in terms of developing the long-term skill of engineering research. Learning to refine a problem statement to the point where you can ask a good question is, in my opinion, a critical skill for any programmer. Anyway I won't drag on too much in the comments section. Comment retracted though. – machine yearning Jun 01 '16 at 06:58