-
Notifications
You must be signed in to change notification settings - Fork 8
/
pe_import_resolv.py
69 lines (54 loc) · 1.84 KB
/
pe_import_resolv.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
# Copyright (C) 2021 Iru Cai <mytbk920423@gmail.com>
# SPDX-License-Identifier: MIT
import os
win32_libpath = '/usr/i686-w64-mingw32/lib/'
def openlib(lib):
return os.popen('nm --format=bsd ' + win32_libpath + lib)
def search_imports(pipe, functions):
res = {}
for L in pipe:
record = L.split()
if len(record) != 3 or record[1] != 'I':
continue
for imp in functions:
if '__imp__' + imp + '@' in record[2]:
res[imp] = record[2]
break
elif '__imp__' + imp == record[2]:
res[imp] = record[2]
break
return res
# dllname is the DLL file name without ".dll" extension
def resolv_imports(dllname, functions):
ar_name = 'lib' + dllname + '.a'
return search_imports(openlib(ar_name), functions)
"""
r2_pe_import_info: get the following PE import information
- addr_sym_map: address to symbol name mapping
- all_imps: the import name to symbol name mapping (e.g. ExitProcess -> __imp__ExitProcess@4)
- libs: all the dll libraries needed (for linking libs)
"""
def r2_pe_import_info(r2):
imports = r2.cmdj('iij')
dll_imp_map = {}
for i in imports:
func = i["name"]
dll = i["libname"].lower()
if len(dll) > 4 and dll[-4:] == '.dll':
dll = dll[0:-4] # trim ".dll"
else:
print("DLL name {} does not end with .dll.".format(dll))
continue
if dll in dll_imp_map:
dll_imp_map[dll].append(func)
else:
dll_imp_map[dll] = [func]
all_imps = {}
libs = dll_imp_map.keys()
for dll in dll_imp_map:
r = resolv_imports(dll, dll_imp_map[dll])
all_imps.update(r)
addr_sym_map = {}
for i in imports:
addr_sym_map[i["plt"]] = all_imps[i["name"]]
return addr_sym_map, all_imps, libs