-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtechniques.py
87 lines (71 loc) · 2.62 KB
/
techniques.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
import re
import ast
import random
import time
from .helpers import VariableNameGenerator, RandomDataTypeGenerator
import regex
def one_liner(code):
# TODO: strings with \n at top
formatted_code = re.sub(
r"(;)\1+",
";",
"""exec(\"\"\"{};\"\"\")""".format(
code.replace("\n", ";").replace('"""', '\\"\\"\\"')
),
)
if formatted_code[0] == ';':
return formatted_code[1:]
return formatted_code
def variable_renamer(code):
# add \n so regex picks it up
code = "\n" + code
variable_names = re.findall(r"(\w+)(?=( |)=( |))", code)
name_generator = VariableNameGenerator()
for i in range(len(variable_names)):
obfuscated_name = name_generator.get_random(i + 1)
code = re.sub(
r"(?<=[^.])(\b{}\b)".format(variable_names[i][0]), obfuscated_name, code
)
return code
def add_random_variables(code):
useless_variables_to_add = random.randint(100, 400)
name_generator = VariableNameGenerator()
data_generator = RandomDataTypeGenerator()
for v in range(1, useless_variables_to_add):
rand_data = data_generator.get_random()
if type(rand_data) == str:
rand_data = '"{}"'.format(rand_data)
if v % 2 == 0:
code = "{} = {}\n".format(name_generator.get_random(v), rand_data) + code
else:
code = code + "\n{} = {}".format(name_generator.get_random(v), rand_data)
return code
def str_to_hex_bytes(code):
# ((?<=(( | |)\w+( |)=( |))("""|"|'))[\W\w]*?(?=("""|"|')))
# TODO: Fix still buggy and kinda just wanna publish this to github rn
python_string_decoraters = ['"""', "'''", '"', "'"]
for s in python_string_decoraters:
pattern = r"((?<=(( | |\n)\w+( |)=( |))({}))[\W\w]*?(?=({})))".format(s, s)
t = regex.findall(pattern, code)
for v in t:
string_contents = v[0]
if s == '"' and string_contents == '"':
continue
if s == "'" and string_contents == "'":
continue
hex_bytes = "\\" + "\\".join(
x.encode("utf-8").hex() for x in string_contents
)
code = regex.sub(pattern, str(hex_bytes).replace("\\", "\\\\"), code)
return code
def obfuscate(code, remove_techniques=[]):
if len(remove_techniques) == 0:
methods = all_methods
else:
methods = all_methods.copy()
for technique in remove_techniques:
methods.remove(technique)
for technique in methods:
code = technique(code)
return code
all_methods = [variable_renamer, add_random_variables, one_liner]