-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathobfuscator.py
More file actions
executable file
·350 lines (266 loc) · 10 KB
/
obfuscator.py
File metadata and controls
executable file
·350 lines (266 loc) · 10 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python3
# Python Code Obfuscator
# by Brandon Asuncion
#
# Questions/Comments? me@brandonasuncion.tech
import string
import sys
import argparse
# How strings are encoded
# Turning off will remove all numbers in the code,
# but will increase output size by a lot!
USE_HEXSTRINGS = False
# Obfuscate Python's built-in function calls
OBFUSCATE_BUILTINS = False
# Remove comments from code
REMOVE_COMMENTS = True
# Special code replacements
REPLACEMENTS = {
'True': '(()==())',
'False': '(()==[])',
}
# Ignore the following two constants if you don't know what they mean
RESERVED_VAR = "__RSV" # Name of variable for internal actions (such as string decryption)
BUILTINS_CONST = "__B" # name used in the header for storing the "builtins" string
_RESERVED = [
# Python reserved keywords
'None', 'and', 'as', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not',
'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield',
]
# Python Built-in functions
# can be called like: getattr(__import__('builtins'), 'abs')(1)
_BUILT_IN = [
'abs', 'dict', 'help', 'min', 'setattr',
'all', 'dir', 'hex', 'next', 'slice',
'any', 'divmod', 'id', 'object', 'sorted',
'ascii', 'enumerate', 'input', 'oct', 'staticmethod',
'bin', 'eval', 'int', 'open', 'str',
'bool', 'exec', 'isinstance', 'ord', 'sum',
'bytearray', 'filter', 'issubclass', 'pow', 'super',
'bytes', 'float', 'iter', 'print', 'tuple',
'callable', 'format', 'len', 'property', 'type',
'chr', 'frozenset', 'list', 'range', 'vars',
'classmethod', 'getattr', 'locals', 'repr', 'zip',
'compile', 'globals', 'map', 'reversed', '__import__',
'complex', 'hasattr', 'max', 'round',
'delattr', 'hash', 'memoryview', 'set'
]
# Might not be a complete list...
_PREPAD = [';', ':', '=', '+', '-', '*', '%', '^', '<<', '>>', '|', '^', '/', ',', '{', '}', '[', ']']
class Obfuscator(object):
def __init__(self):
self.header_variables = {}
self.variables = {}
# Header includes stuff like pre-processed integers
def getHeader(self):
return ";".join("{}={}".format(self.header_variables[number], self.variables[self.header_variables[number]]) for number in sorted(self.header_variables, key = lambda x: len(self.header_variables[x]))) + "\n"
def getVariable(self, variableName):
pass
def addHeaderVar(self, varName, expression):
if varName in self.header_variables:
return self.header_variables[varName]
# if the variable list is already too big,
# it is better to just use the expression
if len(expression) < (len(self.variables) + 1) + 2:
return expression
variable_name = '_' * (len(self.variables) + 1)
self.variables[variable_name] = expression
self.header_variables[varName] = variable_name
return variable_name
def encodeNumber(self, number, addToHeader = False):
if int(number) < 0:
return "(~([]==())*{})".format(self.encodeNumber(abs(int(number)))) # Not working for some reason
#return "(({}-{})*{})".format(self.encodeNumber('0'), self.encodeNumber('1'), self.encodeNumber(-int(number)))
number = str(number)
if number in self.header_variables:
return self.variables[self.header_variables[number]]
if number == '0':
return '((()==[])+(()==[]))'
elif number == '1':
return '((()==())+(()==[]))'
else:
# Try to avoid adding a header unless you really need to
if ('0' not in self.header_variables):
self.addHeaderVar('0', '((()==[])+(()==[]))')
self.addHeaderVar('1', '({0}**{0})'.format(self.header_variables['0']))
# Convert a number to binary, then encode
# eg. 13 => 1101 (binary) => (1 << 3)+(1 << 2)+(1 << 0)
bin_number = bin(int(number))[2:]
shifts = 0
obf_number = ''
while bin_number != '':
if bin_number[-1] == '1':
if shifts == 0:
obf_number += self.encodeNumber(1)
elif str(1<<shifts) in self.header_variables:
obf_number += self.header_variables[str(1<<shifts)]
elif str(shifts) in self.header_variables:
encode_bitshift = self.header_variables[str(shifts)]
obf_number += '({}<<{})'.format(self.header_variables['1'], encode_bitshift)
else:
bit_m1 = self.encodeNumber(str(1 << (shifts-1)), True)
obf_number += '({}<<{})'.format(bit_m1, self.encodeNumber('1'))
obf_number += '+'
bin_number = bin_number[:-1]
shifts += 1
if bin_number.count('1') == 1:
obf_number = obf_number[:-1]
else:
obf_number = "({})".format(obf_number[:-1])
if addToHeader:
return self.addHeaderVar(number, obf_number)
return obf_number
def encodeString(self, string, addToHeader = False, forceHexstrings = False):
if USE_HEXSTRINGS or forceHexstrings:
#byte_array = "[{}]".format(",".join([hex(ord(c)) for c in string]))
#result = "str(''.join(chr({0}) for {0} in {1}))".format(RESERVED_VAR, byte_array)
result = "'{}'".format("".join("\\x{:02x}".format(ord(c)) for c in string))
else:
byte_array = "[{}]".format(",".join([self.encodeNumber(ord(c)) for c in string]))
result = "str(''.join(chr({0}) for {0} in {1}))".format(RESERVED_VAR, byte_array)
if addToHeader:
return self.addHeaderVar(string, result)
return result
def obfuscate(self, code, append_header = True):
# import statements should just be returned
if code.split()[0] in ['import', 'from']:
return code
# Pad certain characters so they can be parsed properly
prepadded = code
for p in _PREPAD:
prepadded = prepadded.replace(p, " {} ".format(p))
prepadded = prepadded.replace('(', "( ").replace(')', ' )')
result = ''
parsingQuote = ''
lineCommented = False
for symbol in prepadded.split():
# Check if the rest of the line is commented
if symbol[0] == '#':
if REMOVE_COMMENTS:
return
lineCommented = True
if lineCommented:
result += symbol + ' '
continue
# If you encounter a string
if (parsingQuote == '') and (symbol[0] in ["\"", "\'"]):
parsingQuote = symbol + ' '
continue
# when it reaches the end of the string
if parsingQuote != '':
if (symbol.find(parsingQuote[0]) != -1):
parsingQuote += symbol[:symbol.find(parsingQuote[0])+1]
result += self.encodeString(parsingQuote[1:-1])
parsingQuote = ''
else:
parsingQuote += symbol + ' '
continue
# Reserved words are passed along with spacing
if symbol in _RESERVED:
result += " {} ".format(symbol)
continue
# arithmetic and similar symbols are passed along as well
if symbol in _PREPAD:
result += symbol
continue
# special replacements
if symbol in REPLACEMENTS:
result += REPLACEMENTS[symbol]
continue
# if we find a number
if symbol.isdigit():
result += self.encodeNumber(int(symbol))
continue
# Try to find the name of a variable / function
name = ""
for s in symbol:
if s in string.ascii_letters + '_':
name += s
elif name:
#if name in self.variables
if name[0] in string.digits:
name = ""
if name in _BUILT_IN:
if OBFUSCATE_BUILTINS:
if BUILTINS_CONST not in self.header_variables:
self.addHeaderVar(BUILTINS_CONST, self.encodeString('builtins'))
enc_name = self.addHeaderVar(name, self.encodeString(name))
result += "getattr(__import__({}), {})".format(self.header_variables[BUILTINS_CONST], enc_name)
result += symbol[len(name):]
else:
result += symbol
continue
# If it is a variable/function, replace the old variable name with a new one
if (name != "") and (name not in _RESERVED) and (name not in _BUILT_IN):
if name not in self.variables:
self.variables[name] = '_' * (len(self.variables) + 1)
result += self.variables[name] + symbol[len(name):]
continue
# If there aren't any changes, just use the original code
result += symbol
# restore original indentation
indents = ""
i = 0
while code[i] in ['\t', ' ']:
indents += code[i]
i += 1
result = indents + result.strip()
if append_header and (len(self.header_variables) > 0):
return getHeader() + result
return result
# For processing multiple lines at once
def obfuscate_lines(self, code):
str_start = -1
strings = []
# get all strings in the code
for i, c in enumerate(code):
if (c in ['\'', '\"']) and (code[i-1] != '\\'):
if str_start == -1:
str_start = i
elif c == code[str_start]:
strings.append(code[str_start : i+1])
str_start = -1
# encode all the strings, and store them as variables in the header
string_vars = {}
for s in strings:
encoded_str = self.encodeString(s[1:-1])
string_vars[s] = self.addHeaderVar(s, encoded_str)
for s in string_vars:
code = code.replace(s, string_vars[s])
result = ""
for line in code.split('\n'):
if not line:
continue
result += self.obfuscate(line, False) + "\n"
return self.getHeader() + result
class MyArgParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('Error: {}\n'.format(message))
self.print_help()
sys.exit(2)
def main():
parser = MyArgParser(description='Python Code Obfuscator by Brandon Asuncion (me@brandonasuncion.tech)')
parser.add_argument('inputfile', help="Name of the input file")
parser.add_argument('outputfile', help="Name of the output file")
parser.add_argument('--debug', help="Show debug info", action="store_true")
args = vars(parser.parse_args())
print('Opening {} for obfuscation'.format(args['inputfile']))
with open(args['inputfile'], 'r') as fh:
lines = fh.read()
obf = Obfuscator()
output = obf.obfuscate_lines(lines)
with open(args['outputfile'], 'w') as fh:
fh.write(output)
print('Written to {}\n'.format(args['outputfile']))
if args['debug']:
print('CONVERTED VARIABLES')
for v in obf.variables:
print("{}\t=> {}".format(v, obf.variables[v]))
print('\nVARIABLES IN HEADER')
for n in sorted(obf.header_variables, key=len):
print("{}\t=> {}".format(n, obf.header_variables[n]))
print()
if __name__ == "__main__":
main()