This repository was archived by the owner on Nov 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalypsisOBF.py
More file actions
506 lines (426 loc) · 20.7 KB
/
CalypsisOBF.py
File metadata and controls
506 lines (426 loc) · 20.7 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import sys
import ast
import random
import string
import marshal
import zlib
import lzma
import base64
import binascii
import time
import os
import subprocess
import importlib
def install_dependencies():
required_packages = ['colorama']
for package in required_packages:
try:
importlib.import_module(package)
except ImportError:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
install_dependencies()
from colorama import init, Fore, Back, Style
init()
except:
class MockColorama:
RESET = ''
WHITE = ''
BLACK = ''
LIGHTBLACK_EX = ''
LIGHTWHITE_EX = ''
Fore = MockColorama()
Style = MockColorama()
def get_gradient_color(progress):
progress = max(0, min(1, progress))
gray_levels = [
(255, 255, 255), # Pure white
(230, 230, 230), # Very light gray
(205, 205, 205), # Light gray
(180, 180, 180), # Light-medium gray
(155, 155, 155), # Medium gray
(130, 130, 130), # Medium-dark gray
(105, 105, 105), # Dark gray
(80, 80, 80), # Darker gray
]
index = progress * (len(gray_levels) - 1)
lower_index = int(index)
upper_index = min(lower_index + 1, len(gray_levels) - 1)
if lower_index == upper_index:
r, g, b = gray_levels[lower_index]
else:
fraction = index - lower_index
r1, g1, b1 = gray_levels[lower_index]
r2, g2, b2 = gray_levels[upper_index]
r = int(r1 + (r2 - r1) * fraction)
g = int(g1 + (g2 - g1) * fraction)
b = int(b1 + (b2 - b1) * fraction)
return f'\033[38;2;{r};{g};{b}m'
def clear_terminal():
os.system('cls' if os.name == 'nt' else 'clear')
def create_gradient_text(text, width=80):
lines = text.split('\n')
result = []
for line_index, line in enumerate(lines):
if line.strip():
progress = line_index / max(1, len(lines) - 1)
gradient_line = apply_character_gradient(line, progress)
padded_line = gradient_line.center(width + len(gradient_line) - len(line))
result.append(padded_line)
else:
result.append(line.center(width))
return '\n'.join(result)
def apply_character_gradient(text, start_progress=0, reverse=False):
if not text.strip():
return text
gradient_chars = []
non_space_chars = [i for i, char in enumerate(text) if char != ' ']
if not non_space_chars:
return text
for char_index, char in enumerate(text):
if char == ' ':
gradient_chars.append(char)
else:
non_space_index = non_space_chars.index(char_index)
progress = start_progress + (non_space_index / max(1, len(non_space_chars) - 1)) * 0.6
if reverse:
progress = 1 - progress
color = get_gradient_color(progress)
gradient_chars.append(color + char + '\033[0m')
return ''.join(gradient_chars)
def apply_text_gradient(text, reverse=False):
return apply_character_gradient(text, 0, reverse)
def display_header():
banner_text = """
/$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$$ /$$$$$$$$
/$$__ $$ | $$ |__/ /$$__ $$| $$__ $$| $$_____/
| $$ \__/ /$$$$$$ | $$ /$$ /$$ /$$$$$$ /$$$$$$$ /$$ /$$$$$$$| $$ \ $$| $$ \ $$| $$
| $$ |____ $$| $$| $$ | $$ /$$__ $$ /$$_____/| $$ /$$_____/| $$ | $$| $$$$$$$ | $$$$$
| $$ /$$$$$$$| $$| $$ | $$| $$ \ $$| $$$$$$ | $$| $$$$$$ | $$ | $$| $$__ $$| $$__/
| $$ $$ /$$__ $$| $$| $$ | $$| $$ | $$ \____ $$| $$ \____ $$| $$ | $$| $$ \ $$| $$
| $$$$$$/| $$$$$$$| $$| $$$$$$$| $$$$$$$/ /$$$$$$$/| $$ /$$$$$$$/| $$$$$$/| $$$$$$$/| $$
\______/ \_______/|__/ \____ $$| $$____/ |_______/ |__/|_______/ \______/ |_______/ |__/
/$$ | $$| $$
| $$$$$$/| $$
\______/ |__/
Python Script Obfuscator
========================
""".strip()
clear_terminal()
gradient_banner = create_gradient_text(banner_text)
print(gradient_banner)
time.sleep(0.5)
def update_progress(current_step, total_steps, bar_length=60):
progress_fraction = current_step / total_steps
filled_length = int(progress_fraction * bar_length)
progress_bar = []
for position in range(bar_length):
if position < filled_length:
progress = position / max(1, bar_length - 1)
color = get_gradient_color(progress)
progress_bar.append(color + '█' + '\033[0m')
else:
progress_bar.append(get_gradient_color(0.3) + '░' + '\033[0m')
bar_display = ''.join(progress_bar)
percentage = progress_fraction * 100
if percentage >= 100:
status_text = apply_character_gradient("Complete")
elif percentage >= 80:
status_text = apply_character_gradient("Finalizing")
elif percentage >= 40:
status_text = apply_character_gradient("Processing")
else:
status_text = apply_character_gradient("Starting")
progress_label = apply_character_gradient("Progress:")
print(f'\r{progress_label} {bar_display} {percentage:.1f}% {status_text}', end='', flush=True)
def generate_variable_name(name_length=10):
first_character = random.choice(string.ascii_letters + '_')
remaining_characters = ''.join(random.choices(string.ascii_letters + string.digits + '_', k=name_length-1))
return first_character + remaining_characters
class VariableRenamer(ast.NodeTransformer):
def __init__(self):
self.scope_stack = [{}]
self.protected_names = set([
'print', 'len', 'str', 'int', 'float', 'bool', 'list', 'dict', 'tuple', 'set',
'range', 'enumerate', 'zip', 'map', 'filter', 'sorted', 'max', 'min', 'sum',
'abs', 'round', 'pow', 'divmod', 'isinstance', 'issubclass', 'hasattr',
'getattr', 'setattr', 'delattr', 'callable', 'iter', 'next', 'reversed',
'slice', 'super', 'type', 'vars', 'dir', 'id', 'hash', 'repr', 'ascii',
'ord', 'chr', 'bin', 'oct', 'hex', 'any', 'all', 'format', 'input',
'open', 'exec', 'eval', 'compile', '__import__', 'globals', 'locals',
'Exception', 'ValueError', 'TypeError', 'AttributeError', 'KeyError',
'IndexError', 'NameError', 'ImportError', 'ModuleNotFoundError',
'SyntaxError', 'IndentationError', 'TabError', 'UnicodeDecodeError',
'requests', 'json', 'socket', 'platform', 'os', 'datetime',
'marshal', 'zlib', 'lzma', 'base64', 'binascii', 'sys', 'ast',
'random', 'string', 'time'
])
def enter_new_scope(self):
self.scope_stack.append({})
def exit_current_scope(self):
self.scope_stack.pop()
def lookup_name(self, original_name):
if original_name in self.protected_names:
return None
for scope_dict in reversed(self.scope_stack):
if original_name in scope_dict:
return scope_dict[original_name]
return None
def register_name(self, original_name, new_name):
if original_name not in self.protected_names:
self.scope_stack[-1][original_name] = new_name
def visit_FunctionDef(self, node):
if node.name not in self.protected_names:
existing_name = self.lookup_name(node.name)
if not existing_name:
existing_name = generate_variable_name()
self.register_name(node.name, existing_name)
node.name = existing_name
self.enter_new_scope()
for function_arg in node.args.args:
if function_arg.arg not in self.protected_names:
new_arg_name = generate_variable_name()
self.register_name(function_arg.arg, new_arg_name)
function_arg.arg = new_arg_name
for keyword_arg in node.args.kwonlyargs:
if keyword_arg.arg not in self.protected_names:
new_keyword_name = generate_variable_name()
self.register_name(keyword_arg.arg, new_keyword_name)
keyword_arg.arg = new_keyword_name
if node.args.vararg and node.args.vararg.arg not in self.protected_names:
new_vararg_name = generate_variable_name()
self.register_name(node.args.vararg.arg, new_vararg_name)
node.args.vararg.arg = new_vararg_name
if node.args.kwarg and node.args.kwarg.arg not in self.protected_names:
new_kwarg_name = generate_variable_name()
self.register_name(node.args.kwarg.arg, new_kwarg_name)
node.args.kwarg.arg = new_kwarg_name
self.generic_visit(node)
self.exit_current_scope()
return node
def visit_Lambda(self, node):
self.enter_new_scope()
for lambda_arg in node.args.args:
if lambda_arg.arg not in self.protected_names:
new_lambda_arg = generate_variable_name()
self.register_name(lambda_arg.arg, new_lambda_arg)
lambda_arg.arg = new_lambda_arg
self.generic_visit(node)
self.exit_current_scope()
return node
def visit_ListComp(self, node):
self.enter_new_scope()
self.generic_visit(node)
self.exit_current_scope()
return node
def visit_SetComp(self, node):
self.enter_new_scope()
self.generic_visit(node)
self.exit_current_scope()
return node
def visit_DictComp(self, node):
self.enter_new_scope()
self.generic_visit(node)
self.exit_current_scope()
return node
def visit_GeneratorExp(self, node):
self.enter_new_scope()
self.generic_visit(node)
self.exit_current_scope()
return node
def visit_Name(self, node):
if node.id in self.protected_names:
return node
if isinstance(node.ctx, ast.Store):
replacement_name = self.lookup_name(node.id)
if not replacement_name:
replacement_name = generate_variable_name()
self.register_name(node.id, replacement_name)
node.id = replacement_name
else:
replacement_name = self.lookup_name(node.id)
if replacement_name:
node.id = replacement_name
return node
class CodeNoiseInjector(ast.NodeTransformer):
def generic_visit(self, node_element):
if hasattr(node_element, 'body') and isinstance(node_element.body, list):
for noise_iteration in range(random.randint(0, 2)):
insertion_position = random.randint(0, len(node_element.body))
noise_statement = self.create_noise_statement()
node_element.body.insert(insertion_position, noise_statement)
if hasattr(node_element, 'orelse') and isinstance(node_element.orelse, list):
for else_noise_iteration in range(random.randint(0, 1)):
else_insertion_position = random.randint(0, len(node_element.orelse))
else_noise_statement = self.create_noise_statement()
node_element.orelse.insert(else_insertion_position, else_noise_statement)
if hasattr(node_element, 'handlers') and isinstance(node_element.handlers, list):
for exception_handler in node_element.handlers:
for handler_noise_iteration in range(random.randint(0, 1)):
handler_insertion_position = random.randint(0, len(exception_handler.body))
handler_noise_statement = self.create_noise_statement()
exception_handler.body.insert(handler_insertion_position, handler_noise_statement)
if hasattr(node_element, 'finalbody') and isinstance(node_element.finalbody, list):
for finally_noise_iteration in range(random.randint(0, 1)):
finally_insertion_position = random.randint(0, len(node_element.finalbody))
finally_noise_statement = self.create_noise_statement()
node_element.finalbody.insert(finally_insertion_position, finally_noise_statement)
return super().generic_visit(node_element)
def create_noise_statement(self):
noise_options = [
ast.Assign(
targets=[ast.Name(id=generate_variable_name(), ctx=ast.Store())],
value=ast.Constant(value=random.randint(1, 100))
),
ast.If(
test=ast.Constant(value=False),
body=[ast.Expr(value=ast.Call(
func=ast.Name(id='print', ctx=ast.Load()),
args=[ast.Constant(value=generate_variable_name())],
keywords=[]
))],
orelse=[]
),
ast.Assert(
test=ast.Constant(value=True),
msg=ast.Constant(value=generate_variable_name())
),
]
return random.choice(noise_options)
if __name__ == "__main__":
total_processing_steps = 11
current_step = 0
display_header()
print(apply_text_gradient("Drag a Python (.py) file into this console window and press Enter."))
print(apply_text_gradient("Or type the path to a Python file and press Enter:", reverse=True))
input_file_path = input().strip()
if not input_file_path:
print(apply_text_gradient("Error: No file path provided."))
input(apply_text_gradient("Press any key to exit...", reverse=True))
sys.exit(1)
cleaned_file_path = input_file_path.strip('"').strip("'")
if not cleaned_file_path.endswith('.py'):
display_header()
print(apply_text_gradient("Error: Please provide a valid Python (.py) file."))
input(apply_text_gradient("Press any key to exit...", reverse=True))
sys.exit(1)
if not os.path.exists(cleaned_file_path):
display_header()
print(apply_text_gradient(f"Error: File '{cleaned_file_path}' not found."))
input(apply_text_gradient("Press any key to exit...", reverse=True))
sys.exit(1)
display_header()
update_progress(current_step, total_processing_steps)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
try:
with open(cleaned_file_path, 'r', encoding='utf-8') as source_file:
original_source_code = source_file.read()
except UnicodeDecodeError:
print(f"\n{apply_text_gradient(f'Error: Cannot decode {cleaned_file_path} with UTF-8 encoding.')}")
print(apply_text_gradient("The file may use a different encoding (e.g., UTF-16, Latin-1)."))
print(apply_text_gradient("Please convert the file to UTF-8 or check its encoding.", reverse=True))
input(apply_text_gradient("Press any key to exit...", reverse=True))
sys.exit(1)
except Exception as file_error:
print(f"\n{apply_text_gradient(f'Error: Failed to read {cleaned_file_path}: {str(file_error)}')}")
input(apply_text_gradient("Press any key to exit...", reverse=True))
sys.exit(1)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
try:
syntax_tree = ast.parse(original_source_code)
except SyntaxError:
print(f"\n{apply_text_gradient(f'Error: Invalid Python syntax in {cleaned_file_path}.')}")
input(apply_text_gradient("Press any key to exit...", reverse=True))
sys.exit(1)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
variable_renamer = VariableRenamer()
renamed_syntax_tree = variable_renamer.visit(syntax_tree)
noise_injector = CodeNoiseInjector()
obfuscated_tree = noise_injector.visit(renamed_syntax_tree)
ast.fix_missing_locations(obfuscated_tree)
obfuscated_source_code = ast.unparse(obfuscated_tree)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
try:
compiled_bytecode = compile(obfuscated_source_code, '<obfuscated>', 'exec')
except SyntaxError as syntax_error:
print(f"\n{apply_text_gradient(f'Error: Invalid syntax in obfuscated code: {str(syntax_error)}')}")
print(apply_text_gradient("This may be due to invalid variable names or code modifications."))
print(apply_text_gradient("Generated code snippet causing the error:", reverse=True))
source_lines = obfuscated_source_code.split('\n')
error_line_number = syntax_error.lineno - 1
start_line = max(0, error_line_number - 2)
end_line = min(len(source_lines), error_line_number + 3)
for line_index, source_line in enumerate(source_lines[start_line:end_line], start=start_line+1):
error_marker = ' <--- Error here' if line_index == syntax_error.lineno else ''
print(apply_text_gradient(f"Line {line_index}: {source_line}{error_marker}"))
input(apply_text_gradient("Press any key to exit...", reverse=True))
sys.exit(1)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
marshaled_bytecode = marshal.dumps(compiled_bytecode)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
encryption_key = random.randint(1, 255)
xor_encrypted_data = bytes(byte_value ^ encryption_key for byte_value in marshaled_bytecode)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
hex_encoded_data = binascii.hexlify(xor_encrypted_data)
base64_encoded_data = base64.b64encode(hex_encoded_data)
zlib_compressed_data = zlib.compress(base64_encoded_data)
lzma_compressed_data = lzma.compress(zlib_compressed_data)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
output_file_path = cleaned_file_path.replace('.py', '_obfuscated.py')
data_variable_name = generate_variable_name()
key_variable_name = generate_variable_name()
lzma_variable_name = generate_variable_name()
zlib_variable_name = generate_variable_name()
base64_variable_name = generate_variable_name()
hex_variable_name = generate_variable_name()
xor_variable_name = generate_variable_name()
code_variable_name = generate_variable_name()
decoder_template = f"""import marshal, zlib, lzma, base64, binascii
{data_variable_name} = {lzma_compressed_data!r}
{key_variable_name} = {encryption_key}
{lzma_variable_name} = lzma.decompress({data_variable_name})
{zlib_variable_name} = zlib.decompress({lzma_variable_name})
{base64_variable_name} = base64.b64decode({zlib_variable_name})
{hex_variable_name} = binascii.unhexlify({base64_variable_name})
{xor_variable_name} = bytes(b ^ {key_variable_name} for b in {hex_variable_name})
{code_variable_name} = marshal.loads({xor_variable_name})
exec({code_variable_name})
"""
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.1)
try:
with open(output_file_path, 'w', encoding='utf-8') as output_file:
output_file.write(decoder_template)
except Exception as write_error:
print(f"\n{apply_text_gradient(f'Error: Failed to write obfuscated file {output_file_path}: {str(write_error)}')}")
input(apply_text_gradient("Press any key to exit...", reverse=True))
sys.exit(1)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.2)
current_step += 1
update_progress(current_step, total_processing_steps)
time.sleep(0.3)
print(f"\n\n{apply_text_gradient('Obfuscation Complete')}")
print(apply_text_gradient(f"Obfuscated script saved to: {output_file_path}"))
print(apply_text_gradient("Your code is now protected with multi-layer obfuscation", reverse=True))
print(apply_text_gradient("Layers applied: AST Transform + Noise + Marshal + XOR + Hex + Base64 + Zlib + LZMA"))
input(f"\n{apply_text_gradient('Press any key to exit...', reverse=True)}")