-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
59 lines (51 loc) · 2.03 KB
/
Copy pathpipeline.py
File metadata and controls
59 lines (51 loc) · 2.03 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
import os
import ast
import traceback
from obfuscator import (
variable_mangler,
string_encryptor,
control_flow_flattener,
dead_code_injector,
opaque_predicates,
metadata_stripper
)
def process_directory(directory, level, verbose=False):
"""Walk through the directory and process all .py files."""
if os.path.isdir(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".py"):
file_path = os.path.join(root, file)
if verbose:
print(f"Processing file: {file_path}")
process_file(file_path, level, verbose)
else:
# Single file mode.
process_file(directory, level, verbose)
def process_file(file_path, level, verbose=False):
"""Parse, transform, and write back a Python file."""
try:
with open(file_path, "r", encoding="utf-8") as f:
source_code = f.read()
tree = ast.parse(source_code, filename=file_path)
# Apply transformations based on obfuscation level.
if level in ["light", "medium", "heavy"]:
tree = variable_mangler.mangle(tree, verbose)
if level in ["medium", "heavy"]:
tree = string_encryptor.encrypt_strings(tree, verbose)
if level == "heavy":
tree = control_flow_flattener.flatten(tree, verbose)
tree = dead_code_injector.inject_dead_code(tree, verbose)
tree = opaque_predicates.inject_opaque_predicates(tree, verbose)
tree = metadata_stripper.strip_metadata(tree)
# Fix missing location information.
tree = ast.fix_missing_locations(tree)
# Unparse AST back into source code.
new_code = ast.unparse(tree)
with open(file_path, "w", encoding="utf-8") as f:
f.write(new_code)
if verbose:
print(f"Obfuscation complete for {file_path}")
except Exception as e:
print(f"Error processing {file_path}: {e}")
traceback.print_exc()