-
Notifications
You must be signed in to change notification settings - Fork 3
/
obf.py
50 lines (40 loc) · 1.44 KB
/
obf.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
import os
import base64
from sys import argv
# configuration
OFFSET = 33
VARIABLE_NAME = '__python_PiP' * 87
def obfuscate(content):
b64_content = base64.b64encode(content.encode()).decode()
index = 0
code = f'{VARIABLE_NAME} = ""\n'
for _ in range(int(len(b64_content) / OFFSET) + 1):
_str = ''
for char in b64_content[index:index + OFFSET]:
byte = str(hex(ord(char)))[2:]
if len(byte) < 2:
byte = '0' + byte
_str += '\\x' + str(byte)
code += f'{VARIABLE_NAME} += "{_str}"\n'
index += OFFSET
code += f'exec(__import__("\\x62\\x61\\x73\\x65\\x36\\x34").b64decode({VARIABLE_NAME}.encode("\\x75\\x74\\x66\\x2d\\x38")).decode("\\x75\\x74\\x66\\x2d\\x38"))'
return code
def main():
try:
path = argv[1]
if not os.path.exists(path):
print('[-] File not found')
exit()
if not os.path.isfile(path) or not path.endswith('.py'):
print('[-] Invalid file')
exit()
with open(path, 'r', encoding='utf-8', errors='ignore') as file:
file_content = file.read()
obfuscated_content = obfuscate(file_content)
with open(f'{path.split(".")[0]}.py', 'w') as file:
file.write(obfuscated_content)
print('OBFUSCATED')
except:
print(f'ERROR')
if __name__ == '__main__':
main()