-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
122 lines (112 loc) · 4.19 KB
/
main.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
"""
DllToLib&Inc v1.3 26.03.2023
(c) Mantissa для wasm.in
"""
import os
import sys
import subprocess
import re
"""
Используется регулярное выражение для поиска имен функций.
Проверить корректность выражения можно на сайте https://regex101.com/
"""
def parse_funName(input_text):
lines = input_text.split("\n")
exports = []
for line in lines:
match = re.search(r'^\s*\d+\s+[0-9a-fA-F]+\s+(?:[0-9a-fA-F]+\s+)?(.+?)(?=\s+\(forwarded|\s*$)', line)
if match:
if not match.group(1).startswith('['):
exports.append(match.group(1))
return exports
def main():
os.chdir(directory)
count = 0
empty_count = 0
for file in os.listdir(directory):
os.chdir(directory)
if file.endswith(".dll"):
def_file = _libdir + file[:-4] + ".def"
inc_file = _incdir + file[:-4] + ".inc"
#print(inc_file)
try:
subprocess.run(f"dumpbin /nologo /exports {directory}/{file} > {def_file}",
shell=True,
check=True,
stderr=subprocess.PIPE,
text=True)
except subprocess.CalledProcessError as e:
print(f"Error - {e.stderr}\n")
if e.returncode == 1:
print("Make sure set path to dumpbin.exe to PATH variable.\n")
exit(-1)
else:
print(f"Error with {def_file}\n")
count += 1
continue
with open(def_file, "r") as f:
exports = parse_funName(f.read())
if len(exports) == 0:
os.remove(def_file)
empty_count += 1
continue
with open(def_file, "w") as f:
f.write("EXPORTS\n")
for export in exports:
f.write(export + "\n")
lib_file = _libdir + file[:-4] + ".lib"
try:
subprocess.run(f"lib /nologo /def:{def_file} /MACHINE:x64 /out:{lib_file} > nul",
shell=True,
check=True,
stderr=subprocess.PIPE,
text=True)
except subprocess.CalledProcessError as e:
print(f"Error - {e.stderr}\n")
if e.returncode == 1:
print("Make sure set path to lib.exe to PATH variable.\n")
exit(-1)
else:
print(f"Error with {lib_file}\n")
count += 1
continue
with open(inc_file, "w") as f:
for export in exports:
f.write("extern __imp_" + export + ":qword \n" + export + " TEXTEQU <__imp_" + export + ">\n")
os.chdir(_libdir)
os.remove(def_file)
count += 1
if count % 10 == 0:
print(f"Progress {count} / {len([i for i in os.listdir(directory) if i.endswith('.dll')])}", end="\r", flush=True)
print("\nFinished.\n"
f"Empty count - {empty_count}\n")
if __name__ == "__main__":
if len(sys.argv) < 4:
print("Select parameters correctly:\n"
"1 - path to Dll \n"
"2 - path to save .lib\n"
"3 - path to save .inc\n")
exit(-1)
proc = subprocess.run(f"dumpbin /logo", shell=True)
if proc.returncode != 0:
print("Make sure set path to dumpbin.exe to PATH variable.\n")
exit(-1)
else:
print("dumpbin is preset in PATH")
proc = subprocess.run(f"lib /logo", shell=True)
if proc.returncode != 0:
print("Make sure set path to lib.exe to PATH variable.\n")
exit(-1)
else:
print("lib is preset in PATH")
directory = sys.argv[1] + '\\'
_libdir = sys.argv[2] + '\\'
print(_libdir)
_incdir = sys.argv[3] + '\\'
print(_incdir)
#os.system('chcp 65001')
try:
pass
main()
except Exception as E:
print(E)