-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertDLL.py
94 lines (77 loc) · 2.66 KB
/
ConvertDLL.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from ctypes import WinDLL
import sys
def LoadDLL(path, makeModule=False):
try:
testFile = open(path, "r")
testFile.close()
except FileNotFoundError:
try:
testFile = open("C:\\windows\\system32\\" + path)
testFile.close()
except FileNotFoundError as e:
e.filename = path
raise e
try:
os.system(r'("C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\bin\Hostx86\x86\dumpbin.exe" /exports ' + path + ') >> TEMP')
file = open('TEMP', 'r')
string =''.join(file.readlines())
#print(string)
file.close()
os.system('del TEMP')
return ParseDLLExportOutput(string, path, makeModule)
except PermissionError as e:
raise e
except FileNotFoundError as e:
raise e
def ParseDLLExportOutput(dllExportOuput, path, makeModule):
title, ext = os.path.splitext(os.path.basename(path))
title += "_dll"
d = dllExportOuput
c = "ordinal hint RVA name"
index1 = d.find(c) + len(c)
index2 = d.find("Summary")
strList = d[index1:index2]
fList = strList.split("\n")
nameList = []
for i in fList:
j = i[26:].split(" ")[0]
if j != "[NONAME]":
nameList.append(j)
#Try to read the ddll to check for errors such as FileNotFoundError
testerDLL = WinDLL(path)
file = open(title + ".py", "w")
file.write("from ctypes import WinDLL\nfrom time import sleep\n")
file.write("_dllHandle = WinDLL(r\"" + path + "\")\n\n")
for i in nameList:
func = None
try:
func = testerDLL.__getattr__(i)
file.write("def " + i + "(*args):\n")
file.write("\treturn _dllHandle." + i + "(*args)")
except AttributeError as e:
file.write("#function \"" + i + "\" could not be implemented")
finally:
file.write("\n\n")
file.close()
module = __import__(title)
if not makeModule:
p = os.path.dirname(os.path.realpath(__file__))
os.system("del " + p + "\\" + title + ".py")
return module
if __name__ == '__main__':
args = sys.argv
if len(args) > 1:
LoadDLL(args[1], makeModule=True)
else:
while True:
file = input('DLL: ')
if os.path.isfile(file):
LoadDLL(file, makeModule=True)
else:
if os.path.isfile('C:\\windows\\system32\\' + file):
LoadDLL('C:\\windows\\system32\\' + file, makeModule=True)
else:
print('Invalid DLL')