Skip to content

Instantly share code, notes, and snippets.

@robbert1978
Created March 7, 2018 09:01
Show Gist options
  • Save robbert1978/ea4de5984531a7ec09455d866635e1f8 to your computer and use it in GitHub Desktop.
Save robbert1978/ea4de5984531a7ec09455d866635e1f8 to your computer and use it in GitHub Desktop.
Crack zip or rar
import zipfile
import rarfile
import os
import sys
from threading import Thread
import argparse
from itertools import product
import time
parser = argparse.ArgumentParser(description='CompressedCrack v1.0.0 by ThanhMinh', epilog='Use the -h for help')
parser.add_argument('-i','--input', help='Insert the file path of compressed file', required=True)
parser.add_argument('rules', nargs='*', help='<min> <max> <character>')
CHARACTER = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()'
class Check:
def __init__(self, Arg):
self.type = None
self.rules = False
self.minLength = None
self.maxLength = None
self.character = None
if len(Arg) >= 4:
self.getData(Arg)
self.rules = True
elif len(Arg) == 0 or len(Arg) > 2:
parser.print_help()
parser.exit()
if (self.CheckFileExist(Arg)):
self.getType(Arg)
else:
print ('No such file or directory: ',Arg[1])
parser.exit()
def CheckFileExist(self, Arg):
if (os.path.isfile(Arg[1])):
return True
else:
return False
def getData(self, Arg):
try:
self.minLength = int(Arg[2])
self.maxLength = int(Arg[3])
except ValueError:
print ('Value Error')
parser.exit()
if self.minLength > self.maxLength:
print ('Length Error')
parser.exit()
if len(Arg) == 5:
self.character = Arg[4]
def getType(self, Arg):
if os.path.splitext(Arg[1])[1] == ".rar" or os.path.splitext(Arg[1])[1]==".zip":
self.type = os.path.splitext(Arg[1])[1]
else:
print ('Extension Error')
parser.exit()
class Handler:
def __init__(self, rules, typeCompress, minLength, maxLength, character):
self.rules = rules
self.location = sys.argv[2]
self.type = typeCompress
self.minLength = minLength
self.maxLength = maxLength
if not character:
self.character = CHARACTER
else:
self.character = character
self.result = False
self.GetFile()
self.CheckRules()
def GetFile(self):
if self.type == '.zip':
self.FileCrack = zipfile.ZipFile(self.location)
else:
self.FileCrack = rarfile.RarFile(self.location)
def Brute(self,password):
try:
if self.type == '.zip':
tryPass = password.encode()
else:
tryPass = password
print (tryPass)
self.FileCrack.extractall(pwd=tryPass)
print ('Complete')
print('Time:',time.clock() - self.start_time,'s')
print ('Password:',password)
self.result = True
except:
pass
def CheckRules(self):
self.start_time = time.clock()
print ('Cracking...')
if not self.rules:
length = 1
while True:
self.SendRequest(length)
if self.result:
return
length += 1
else:
for length in range(self.minLength, self.maxLength + 1):
self.SendRequest(length)
if self.result:
return
if not self.result:
print ('Cannot find password with this rules')
return
def SendRequest(self,length):
listPass = product(self.character, repeat=length)
for Pass in listPass:
tryPass = ''.join(Pass)
# Multi Thread:
# nThread = Thread(target=self.Brute, args=(tryPass, ))
# nThread.start()
# Single Thread:
self.Brute(tryPass)
if self.result:
return
def main():
check = Check(sys.argv[1:])
args = parser.parse_args()
rarfile.UNRAR_TOOL = "UnRAR.exe"
Handling = Handler(check.rules, check.type, check.minLength, check.maxLength, check.character)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment