forked from dwt1020/pyrarcr
/
pyrarcr.py
137 lines (121 loc) · 3.96 KB
/
pyrarcr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
##### ##### ##### ##### #### #### #
# # # # # # # # # # #### #### # # # ##
##### #### ##### ##### ##### # # # # #### #
# # # # # # # # # # # # # #
# # # # # # # # #### # #### # #### # #
#finds the password of a desired rar or zip file using a brute-force algorithm
#importing needed modules
import time,os,sys,itertools, re
from multiprocessing import Pool
import subprocess
from zipfile import ZipFile
name=os.path.basename(__file__)
#checking if the user's operating system is compatible with pyrarcr
if os.name!="posix":
print("ERROR:",name,"isn't compatible with your system.")
sys.exit(-1)
def zipFileUnzip (passwd):
global fileName
with ZipFile(fileName, 'r') as myzip:
try:
myzip.extractall('/tmp', None, passwd)
ret = True
except:
ret = False
else:
ret = True
return ret, passwd
def unrar(guessPass):
global fileName
cmd = ["unrar", "t", "-y" ,"-p"+guessPass, fileName]
proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
(stdout, stderr) = proc.communicate()
if not stderr:
print("Found password:",repr(guessPass))
global start
print "It took "+str(time.time()-start)+" seconds"
return True, guessPass
return False, guessPass
def unzip(guessPass):
global fileName
okOutput = '0\n'
cmd='7za t -y -p'+guessPass+' '+fileName+' > /dev/null ;echo $?'
kf=os.popen(cmd)
for rkf in kf.readlines():
if rkf == okOutput:
print("Found password:",repr(guessPass))
print("It took",round(time.time()-start,3),"seconds")
print("Exiting...")
return True, guessPass
return False, guessPass
#defining the function
def rc(rf, alphabet, numOfThreads):
tryn=0
counterTmp = 0
printCounter = 1000
listBasic = []
if rf.endswith('.rar'):
funcChosen = unrar
elif rf.endswith('.zip') or rf.endswith('.7z') :
funcChosen = zipFileUnzip
for a in range(1,len(alphabet)+1):
for b in itertools.product(alphabet,repeat=a):
k="".join(b)
k=re.escape(k)
listBasic.append(k)
tryn+=1
if len(listBasic) == numOfThreads:
pool = Pool(numOfThreads)
pool.map_async(funcChosen, listBasic, callback = exitPass)
pool.close()
if resultPass:
timeWasted = time.time()-start
print 'Found! Password is '+resultPass
print "It took " +str(round(time.time()-start,3))+" seconds"
print "Speed: "+str(round(tryn/float(timeWasted),2))+" passwords/sec"
print "Tried "+str(tryn)+" passwords"
exit()
listBasic = []
counterTmp+=1
if counterTmp >= printCounter:
print 'Trying combination number '+str(tryn)+':'+str(k)
timeWasted = round(time.time()-start,2)
if timeWasted > 0:
print "It took already " +str(timeWasted) +" seconds. Speed: "+str(round(tryn/float(timeWasted),2))+" passwords/sec"
counterTmp=0
def exitPass(result):
for results in result:
if True in results:
global resultPass
resultPass = results[1]
resultPass = None
defaultAlphabet = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890"
defaultThreadNumbers= 2
name = sys.argv[0]
helpMessage = '''Usage: '''+name+''' [rar file]
'''+name+''' [rar file] [alphabet]
Examples: '''+name+''' foobar.rar
'''+name+''' foobar.rar abc\~\(d1234567890\-'''
#checking if the file exists/running the function
argLen= len(sys.argv)
start=time.time()
if argLen >= 2:
if not os.path.exists(sys.argv[1]):
print("ERROR: File doesn't exist.\nExiting...")
else:
fileName = sys.argv[1]
if argLen ==4:
print ('Number of threads ='+str(sys.argv[3]))
rc(sys.argv[1], sys.argv[2], int(sys.argv[3]))
elif argLen ==3:
print ('Number of threads ='+str(defaultThreadNumbers))
rc(sys.argv[1], sys.argv[2], defaultThreadNumbers)
elif argLen == 2:
print ('Using default alphabet: '+defaultAlphabet)
print ('Number of threads ='+str(defaultThreadNumbers))
rc(sys.argv[1], defaultAlphabet, defaultThreadNumbers )
else:
print(helpMessage)
else:
print(helpMessage)