-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcarbon.py
247 lines (209 loc) · 7.88 KB
/
carbon.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
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
# Made by 0sir1ss @ https://github.com/0sir1ss/Carbon
import ast, re, random, io, tokenize, os, sys, platform, math
is_windows = True if platform.system() == "Windows" else False
if is_windows:
os.system("title Carbon @ github.com/0sir1ss/Carbon")
def clear():
if is_windows:
os.system("cls")
else:
os.system("clear")
def pause():
if is_windows:
os.system(f"pause >nul")
else:
input()
def leave():
try:
sys.exit()
except:
exit()
def error(error):
print(red(f" [>] Error: {error}"), end="")
pause(); clear(); leave()
def red(text):
os.system(""); faded = ""
for line in text.splitlines():
green = 250
for character in line:
green -= 5
if green < 0:
green = 0
faded += (f"\033[38;2;255;{green};0m{character}\033[0m")
faded += "\n"
return faded
def blue(text):
os.system(""); faded = ""
for line in text.splitlines():
green = 0
for character in line:
green += 3
if green > 255:
green = 255
faded += (f"\033[38;2;0;{green};255m{character}\033[0m")
faded += "\n"
return faded
def water(text):
os.system(""); faded = ""
green = 10
for line in text.splitlines():
faded += (f"\033[38;2;0;{green};255m{line}\033[0m\n")
if not green == 255:
green += 15
if green > 255:
green = 255
return faded
def purple(text):
os.system("")
faded = ""
down = False
for line in text.splitlines():
red = 40
for character in line:
if down:
red -= 3
else:
red += 3
if red > 254:
red = 255
down = True
elif red < 1:
red = 30
down = False
faded += (f"\033[38;2;{red};0;220m{character}\033[0m")
return faded
def remove_docs(source):
io_obj = io.StringIO(source)
out = ""
prev_toktype = tokenize.INDENT
last_lineno = -1
last_col = 0
for tok in tokenize.generate_tokens(io_obj.readline):
token_type = tok[0]
token_string = tok[1]
start_line, start_col = tok[2]
end_line, end_col = tok[3]
if start_line > last_lineno:
last_col = 0
if start_col > last_col:
out += (" " * (start_col - last_col))
if token_type == tokenize.COMMENT:
pass
elif token_type == tokenize.STRING:
if prev_toktype != tokenize.INDENT:
if prev_toktype != tokenize.NEWLINE:
if start_col > 0:
out += token_string
else:
out += token_string
prev_toktype = token_type
last_col = end_col
last_lineno = end_line
out = '\n'.join(l for l in out.splitlines() if l.strip())
return out
def do_rename(pairs, code):
for key in pairs:
code = re.sub(fr"\b({key})\b", pairs[key], code, re.MULTILINE)
return code
def rename(filename: str):
with open(filename, "r") as file:
code = file.read()
code = remove_docs(code)
parsed = ast.parse(code)
funcs = {
node for node in ast.walk(parsed) if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
}
classes = {
node for node in ast.walk(parsed) if isinstance(node, ast.ClassDef)
}
args = {
node.id for node in ast.walk(parsed) if isinstance(node, ast.Name) and not isinstance(node.ctx, ast.Load)
}
attrs = {
node.attr for node in ast.walk(parsed) if isinstance(node, ast.Attribute) and not isinstance(node.ctx, ast.Load)
}
for func in funcs:
if func.args.args:
for arg in func.args.args:
args.add(arg.arg)
if func.args.kwonlyargs:
for arg in func.args.kwonlyargs:
args.add(arg.arg)
if func.args.vararg:
args.add(func.args.vararg.arg)
if func.args.kwarg:
args.add(func.args.kwarg.arg)
pairs = {}
used = set()
for func in funcs:
if func.name == "__init__":
continue
newname = "".join(random.choice(["I", "l"]) for i in range(random.randint(8, 20)))
while newname in used:
newname = "".join(random.choice(["I", "l"]) for i in range(random.randint(8, 20)))
used.add(newname)
pairs[func.name] = newname
for _class in classes:
newname = "".join(random.choice(["I", "l"]) for i in range(random.randint(8, 20)))
while newname in used:
newname = "".join(random.choice(["I", "l"]) for i in range(random.randint(8, 20)))
used.add(newname)
pairs[_class.name] = newname
for arg in args:
newname = "".join(random.choice(["I", "l"]) for i in range(random.randint(8, 20)))
while newname in used:
newname = "".join(random.choice(["I", "l"]) for i in range(random.randint(8, 20)))
used.add(newname)
pairs[arg] = newname
for attr in attrs:
newname = "".join(random.choice(["I", "l"]) for i in range(random.randint(8, 20)))
while newname in used:
newname = "".join(random.choice(["I", "l"]) for i in range(random.randint(8, 20)))
used.add(newname)
pairs[attr] = newname
string_regex = r"('|\")[\x1f-\x7e]{1,}?('|\")"
original_strings = re.finditer(string_regex, code, re.MULTILINE)
originals = []
for matchNum, match in enumerate(original_strings, start=1):
originals.append(match.group().replace("\\", "\\\\"))
placeholder = os.urandom(16).hex()
code = re.sub(string_regex, f"'{placeholder}'", code, 0, re.MULTILINE)
for i in range(len(originals)):
for key in pairs:
originals[i] = re.sub(r"({.*)(" + key + r")(.*})", "\\1" + pairs[key] + "\\3", originals[i], re.MULTILINE)
while True:
found = False
code = do_rename(pairs, code)
for key in pairs:
if re.findall(fr"\b({key})\b", code):
found = True
if found == False:
break
replace_placeholder = r"('|\")" + placeholder + r"('|\")"
for original in originals:
code = re.sub(replace_placeholder, original, code, 1, re.MULTILINE)
return code
banner = f"""
/$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$
/$$__ $$ /$$__ $$| $$__ $$| $$__ $$ /$$__ $$| $$$ | $$
| $$ \__/| $$ \ $$| $$ \ $$| $$ \ $$| $$ \ $$| $$$$| $$
| $$ | $$$$$$$$| $$$$$$$/| $$$$$$$ | $$ | $$| $$ $$ $$
| $$ | $$__ $$| $$__ $$| $$__ $$| $$ | $$| $$ $$$$
| $$ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $$$
| $$$$$$/| $$ | $$| $$ | $$| $$$$$$$/| $$$$$$/| $$ \ $$
\______/ |__/ |__/|__/ |__/|_______/ \______/ |__/ \__/
{purple(f"[>] Running with Python {sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}")}
"""
clear()
print(water(banner))
while True:
file = input(purple(" [>] Enter the python file you wish to obfuscate [script.py] : ") + "\033[38;2;148;0;230m")
if not os.path.exists(file):
print(red(" [!] Error : That file does not exist"), end="")
else:
break
code = rename(file)
with open(f"{file[:-3]}-obf.py", "w") as f:
f.write(code)
print(blue(f" [>] Code has been successfully obfuscated @ {file[:-3]}-obf.py"), end="")
pause(); clear(); leave()