Repository navigation
Expand file tree
/
Copy pathbasecrack.py
More file actions
332 lines (296 loc) · 14.8 KB
/
Copy pathbasecrack.py
File metadata and controls
332 lines (296 loc) · 14.8 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Mufeed VH'
__version__ = '2.0'
__email__ = 'contact@mufeedvh.com'
__github__ = 'https://github.com/mufeedvh/basecrack'
# importing some required stuff
import os
import re
import sys
import time
import argparse
from colorama import init
from termcolor import colored
import base36
import base58
import base62
import base64
import base91
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
import base92
class BaseCrack:
def __init__(self, output=None):
self.output = output
# initial bools
self.api_call = False
# main decode function
def decode_base(self, encoded_base):
"""
contains_replacement_char() checks whether the decoded base
contains an unknown unicode, ie: invalid character.
these are replaced with 'replacement character',
which is '�' and 'U+FFFD' in unicode.
"""
def contains_replacement_char(res):
return True if u'\ufffd' in res else False
# to store the encoding schemes which haven't caused errors
encoding_type = []
# to store the decoded results which haven't caused errors
results = []
# checking if input is not empty
if len(encoded_base) != 0:
# decoding as base16
try:
base16_decode = base64.b16decode(encoded_base, casefold=False).decode('utf-8', 'replace')
if not contains_replacement_char(base16_decode):
encoding_type.append('Base16')
results.append(base16_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base16: ', 'blue')+colored(base16_decode, 'green'))
except:
pass
# decoding as base32
try:
base32_decode = base64.b32decode(encoded_base, casefold=False, map01=None).decode('utf-8', 'replace')
if not contains_replacement_char(base32_decode):
encoding_type.append('Base32')
results.append(base32_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base32: ', 'blue')+colored(base32_decode, 'green'))
except:
pass
# decoding as base36
try:
base36_decode = base36.dumps(encoded_base).decode('utf-8', 'replace')
if not contains_replacement_char(base36_decode):
encoding_type.append('Base36')
results.append(base36_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base36: ', 'blue')+colored(base36_decode, 'green'))
except:
pass
# decoding as base58
try:
base58_decode = base58.b58decode(encoded_base.encode()).decode('utf-8', 'replace')
if not contains_replacement_char(base58_decode):
encoding_type.append('Base58')
results.append(base58_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base58: ', 'blue')+colored(base58_decode, 'green'))
except:
pass
# decoding as base62
try:
base62_decode = base62.decodebytes(encoded_base).decode('utf-8', 'replace')
if not contains_replacement_char(base62_decode):
encoding_type.append('Base62')
results.append(base62_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base62: ', 'blue')+colored(base62_decode, 'green'))
except:
pass
# decoding as base64
try:
base64_decode = base64.b64decode(encoded_base).decode('utf-8', 'replace')
if not contains_replacement_char(base64_decode):
encoding_type.append('Base64')
results.append(base64_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base64: ', 'blue')+colored(base64_decode, 'green'))
except:
pass
# decoding as base64url
try:
base64url_decode = base64.urlsafe_b64decode(encoded_base + '=' * (4 - len(encoded_base) % 4)).decode('utf-8', 'replace')
if not contains_replacement_char(base64url_decode):
encoding_type.append('Base64URL')
results.append(base64url_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base64URL: ', 'blue')+colored(base64url_decode, 'green'))
except:
pass
# decoding as base85
try:
base85_decode = base64.b85decode(encoded_base).decode('utf-8', 'replace')
if not contains_replacement_char(base85_decode):
encoding_type.append('Base85')
results.append(base85_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base85: ', 'blue')+colored(base85_decode, 'green'))
except:
pass
# decoding as ascii85
try:
ascii85_decode = base64.a85decode(encoded_base).decode('utf-8', 'replace')
if not contains_replacement_char(ascii85_decode):
encoding_type.append('Ascii85')
results.append(ascii85_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Ascii85: ', 'blue') + colored(ascii85_decode, 'green'))
except:
pass
# decoding as base91
try:
base91_decode = base91.decode(encoded_base).decode('utf-8', 'replace')
if not contains_replacement_char(base91_decode):
encoding_type.append('Base91')
results.append(base91_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base91: ', 'blue')+colored(base91_decode, 'green'))
except:
pass
# decoding as base92
try:
base92_decode = base92.decode(encoded_base)
if not contains_replacement_char(base92_decode):
encoding_type.append('Base92')
results.append(base92_decode)
if not self.api_call:
print(colored('\n[>] Decoding as Base92: ', 'blue')+colored(base92_decode, 'green'))
except:
pass
if not results and not self.api_call:
quit(colored('\n[!] Not a valid encoding.\n', 'red'))
# algorithm to identify which type of base encoding the input is
for x in range(len(results)):
"""
It runs through all the results and compares them
with a regex pattern of 'alphabets, numbers, and special characters'
thus ending up with the right result as false results will
contain invalid characters.
"""
if re.match('[A-Za-z0-9$&+,:;=?@#|\'<>.^*()%!_-]', results[x]):
if not self.api_call:
# printing the predicted encoding type
print(colored('\n[-] The Encoding Scheme Is ', 'blue')+colored(encoding_type[x], 'green'))
# generating the wordlist/output file with the decoded bases
if self.output != None:
open(self.output, 'a').write(results[x]+'\n')
else:
# return a tuple with the decoded base and encoding scheme if it's an api call
return (results[x], encoding_type[x])
"""
this function fetches the set of base encodings from the input file
and passes it to 'decode_base()' function to decode it all
"""
def decode_base_from_file(self, file):
print(colored('[-] Decoding Base Data From ', 'cyan')+colored(file, 'yellow'))
# opening the input file
with open(file) as input_file:
# reading each line from the file
for line in input_file:
# checking if the line/base is not empty
if len(line) > 1:
# printing the encoded base
print(colored('\n[-] Encoded Base: ', 'yellow')+str(line.strip()))
# calling the 'decode_base()' function to decode the current line
self.decode_base(line)
# separating each decode results with some lines yo
print(colored('\n{{<<', 'red')+colored('='*70, 'yellow')+colored('>>}}', 'red'))
# api decode function
"""
API FUNCTION
------------
the decode() function returns a tuple
with the structure:
('DECODED_STRING', 'ENCODING SCHEME')
For example:
>> from basecrack import BaseCrack
>> BaseCrack().decode('c3BhZ2hldHRp')
('spaghetti', 'Base64')
ie:
result[0] is the decoded string
result[1] is the encoding scheme
"""
def decode(self, encoded_base):
self.api_call = True
# api calls returns a tuple with the decoded base and the encoding scheme
return self.decode_base(encoded_base)
# magic mode tries to decode multi-encoded bases
def magic_mode(self, encoded_base):
# magic mode uses the api function to decode every iteration
self.api_call = True
iteration = 0
result = None
encoding_pattern = []
start_time = time.time()
while True:
if self.decode(encoded_base) is not None:
iteration += 1
result = self.decode(encoded_base)
decoded_string = result[0]
encoding_scheme = result[1]
encoding_pattern.append(encoding_scheme)
print(colored('\n[-] Iteration: ', 'green')+colored(iteration, 'blue'))
print(colored('\n[-] Heuristic Found Encoding To Be: ', 'yellow')+colored(encoding_scheme, 'green'))
print(colored('\n[-] Decoding as {}: '.format(encoding_scheme), 'blue')+colored(decoded_string, 'green'))
print(colored('\n{{<<', 'red')+colored('='*70, 'yellow')+colored('>>}}', 'red'))
# setting the encoded bases and the current result for the next iteration
encoded_base = decoded_string
else:
break
if result is not None:
end_time = time.time()
print(colored('\n[-] Total Iterations: ', 'green')+colored(iteration, 'blue'))
# show the encoding pattern in order and comma-seperated
pattern = ' -> '.join(map(str, encoding_pattern))
print(colored('\n[-] Encoding Pattern: ', 'green')+colored(pattern, 'blue'))
print(colored('\n[-] Magic Decode Finished With Result: ', 'green')+colored(decoded_string, 'yellow', attrs=['bold']))
completion_time = str(end_time-start_time)[:6]
print(colored('\n[-] Finished in ', 'green')+colored(completion_time, 'cyan', attrs=['bold'])+colored(' seconds\n', 'green'))
quit()
else:
quit(colored('\n[!] Not a valid encoding.\n', 'red'))
# print a kickass banner to look cool
def banner():
banner = '''
██████╗ █████╗ ███████╗███████╗ ██████╗██████╗ █████╗ ██████╗██╗ ██╗
██╔══██╗██╔══██╗██╔════╝██╔════╝██╔════╝██╔══██╗██╔══██╗██╔════╝██║ ██╔╝
██████╔╝███████║███████╗█████╗ ██║ ██████╔╝███████║██║ █████╔╝
██╔══██╗██╔══██║╚════██║██╔══╝ ██║ ██╔══██╗██╔══██║██║ ██╔═██╗
██████╔╝██║ ██║███████║███████╗╚██████╗██║ ██║██║ ██║╚██████╗██║ ██╗
╚═════╝ ╚═╝ ╚═╝╚══════╝╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝
'''
print(colored(banner, 'red')+colored('\n\t\tpython basecrack.py -h [FOR HELP]\n', 'green'))
def main():
banner()
# setting up argparse module to accept arguments
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--base', help='Decode a single encoded base from argument.')
parser.add_argument('-f', '--file', help='Decode multiple encoded bases from a file.')
parser.add_argument('-m', '--magic', help='Decode multi-encoded bases in one shot.', action='store_true')
parser.add_argument('-o', '--output', help='Generate a wordlist/output with the decoded bases, enter filename as the value.')
args = parser.parse_args()
if args.output:
print(colored('\n[>] ', 'yellow')+colored('Enabled Wordlist Generator Mode :: ', 'green')+colored(args.output+'\n', 'blue'))
"""
decodes base encodings from file if argument is given
else it accepts a single encoded base from user
"""
if args.file:
if args.magic:
quit(colored('\n[!] Magic Mode doesn\'t work with DECODE_FROM_FILE mode.\n', 'red'))
# calling the 'decode_base_from_file()' with the input file
BaseCrack(args.output).decode_base_from_file(str(args.file))
if args.output:
print(colored('\n[-] Output Generated Successfully > ', 'green')+colored(args.output+'\n', 'yellow'))
elif args.base:
print(colored('[-] Encoded Base: ', 'yellow')+colored(str(args.base), 'red'))
if args.magic:
BaseCrack().magic_mode(str(args.base))
else:
BaseCrack().decode_base(str(args.base))
else:
# input() for python2 is actually eval() :face_palm:
if sys.version_info >= (3, 0):
encoded_base = input(colored('[>] Enter Encoded Base: ', 'yellow'))
else:
encoded_base = raw_input(colored('[>] Enter Encoded Base: ', 'yellow'))
if args.magic:
BaseCrack().magic_mode(encoded_base)
else:
BaseCrack().decode_base(encoded_base)
if __name__ == '__main__':
init()
main()