Repository navigation
Expand file tree
/
Copy pathreverse.py
More file actions
110 lines (92 loc) · 3.29 KB
/
Copy pathreverse.py
File metadata and controls
110 lines (92 loc) · 3.29 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
import sys
import os
import shutil
import tempfile
import importlib.util
import logging
from contextlib import redirect_stdout, redirect_stderr
from io import StringIO
from pathlib import Path
def load_pyinstxtractor():
# In a PyInstaller bundle, data files land in sys._MEIPASS
if hasattr(sys, "_MEIPASS"):
base_dir = Path(sys._MEIPASS)
else:
base_dir = Path(__file__).parent
spec = importlib.util.spec_from_file_location("pyinstxtractor", base_dir / "pyinstxtractor.py")
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def main():
if len(sys.argv) < 2:
print("Usage: reverse.exe <exe_path>")
sys.exit(1)
exe_path = Path(sys.argv[1]).resolve()
if not exe_path.exists():
print(f"Error: {exe_path} does not exist")
sys.exit(1)
# When bundled by PyInstaller, use the exe's directory; otherwise script dir
if hasattr(sys, "_MEIPASS"):
script_dir = Path(sys.executable).parent
else:
script_dir = Path(__file__).parent
output_dir = script_dir / "output"
output_dir.mkdir(exist_ok=True)
original_cwd = os.getcwd()
extract_dir = exe_path.parent / (exe_path.name + "_extracted")
print(f"[+] 解包中 ...")
pyinstxtractor = load_pyinstxtractor()
os.chdir(str(exe_path.parent))
arch = pyinstxtractor.PyInstArchive(str(exe_path))
with redirect_stdout(StringIO()), redirect_stderr(StringIO()):
ok = arch.open() and arch.checkFile() and arch.getCArchiveInfo()
if not ok:
arch.close()
os.chdir(original_cwd)
print(f"[-] 失败 原因: 无法解析 PyInstaller 归档")
sys.exit(1)
with redirect_stdout(StringIO()), redirect_stderr(StringIO()):
arch.parseTOC()
arch.extractFiles()
arch.close()
os.chdir(original_cwd)
if not extract_dir.exists():
print(f"[-] 失败 原因: 解包目录不存在 {extract_dir}")
sys.exit(1)
pyc_candidates = list(extract_dir.glob("main.pyc")) or list(extract_dir.rglob("main.pyc"))
if not pyc_candidates:
print(f"[-] 失败 原因: 未找到 main.pyc")
sys.exit(1)
pyc_path = pyc_candidates[0]
print(f"[+] 解包成功")
print(f"[+] 反编译中 ...")
from pylingual.decompiler import decompile
# suppress transformers/pylingual logging noise
logging.disable(logging.CRITICAL)
tmp_out = output_dir / "decompiled_main.py"
try:
with redirect_stdout(StringIO()), redirect_stderr(StringIO()):
decompile(
pyc=pyc_path,
save_to=tmp_out,
config_file=None,
version=None,
top_k=10,
trust_lnotab=False,
)
except Exception as e:
logging.disable(logging.NOTSET)
print(f"[-] 失败 原因: {e}")
sys.exit(1)
finally:
logging.disable(logging.NOTSET)
if not tmp_out.exists():
print(f"[-] 失败 原因:未生成输出文件")
sys.exit(1)
dest_py = output_dir / "main.py"
lines = tmp_out.read_text(encoding="utf-8").splitlines(keepends=True)
tmp_out.write_text("".join(lines[5:]), encoding="utf-8")
tmp_out.rename(dest_py)
print(f"[+] 成功 保存到 {dest_py}")
if __name__ == "__main__":
main()