-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
73 lines (58 loc) · 1.94 KB
/
cli.py
File metadata and controls
73 lines (58 loc) · 1.94 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
import ast
import astor
import argparse
import sys
from obfuscate import Obfuscator
def obfuscate_code(filename, output_filename=None):
import os
with open(filename, 'r') as f:
source_code = f.read()
root_node = ast.parse(source_code)
node = ast.fix_missing_locations(Obfuscator().visit(root_node))
# Extract directory and basename
dirname = os.path.dirname(filename)
basename = os.path.basename(filename)
# Create output path in the same directory
if output_filename is not None:
output_path = output_filename
elif dirname:
output_path = os.path.join(dirname, f'obfuscated_{basename}')
else:
output_path = f'obfuscated_{basename}'
with open(output_path, 'w') as f:
f.write(astor.to_source(node))
return node, output_path
def main():
parser = argparse.ArgumentParser(
description='Obfuscate Python code using lambda calculus transformations'
)
parser.add_argument(
'input_file',
help='Python file to obfuscate'
)
parser.add_argument(
'-o', '--output',
help='Output file name (default: obfuscated_<input_file>)',
default=None
)
parser.add_argument(
'--execute',
action='store_true',
help='Execute the obfuscated code after generation'
)
args = parser.parse_args()
try:
tree, output_file = obfuscate_code(args.input_file, args.output)
print(f"Successfully obfuscated {args.input_file} -> {output_file}")
if args.execute:
print("\nExecuting obfuscated code...")
code = compile(tree, filename="<ast>", mode="exec")
exec(code)
except FileNotFoundError:
print(f"Error: File '{args.input_file}' not found", file=sys.stderr)
sys.exit(1)
# except Exception as e:
# print(f"Error: {e}", file=sys.stderr)
# sys.exit(1)
if __name__ == '__main__':
main()