-
Notifications
You must be signed in to change notification settings - Fork 12
/
exports.py
83 lines (53 loc) · 2.15 KB
/
exports.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
#!/usr/bin/env python3
import sys
import glob
import json
import pefile
import argparse
from prettytable import PrettyTable
parser = argparse.ArgumentParser(description='Tool to show exports of a given DLL.')
parser.add_argument('filename', help='filename of binary to analyse',type=argparse.FileType('r'))
#parser.add_argument('-d', '--deff', help='Output in DEF format instead of pretty table', required=False, action='store_true')
parser.add_argument("-d", "--deff", metavar='<forwardtarget>', help="Instead of pretty table, print a DEF file with exports to a given target", required=False, action='store', default=None, type=str)
args = parser.parse_args()
def printTable(pe):
print('binary: ' + args.filename.name)
imageBase = pe.OPTIONAL_HEADER.ImageBase
print('image base: ' + '0x' + hex(imageBase)[2:].zfill(16) + ' (' + str(imageBase) + ')')
is32bit = hex(pe.FILE_HEADER.Machine) == '0x14c'
if is32bit:
print('architecture: 32-bit')
else:
print('architecture: 64-bit')
d = [pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_EXPORT"]]
pe.parse_data_directories(directories=d)
exports = [(e) for e in pe.DIRECTORY_ENTRY_EXPORT.symbols]
x = PrettyTable()
x.field_names = ['name', 'address (imagebase + relative)', 'relative address', 'ordinal']
for n in x.field_names:
x.align[n] = "l"
for e in exports:
if is32bit:
address = '0x' + hex(imageBase + e.address)[2:].zfill(8)
else:
address = '0x' + hex(imageBase + e.address)[2:].zfill(16)
if e.forwarder:
address = str(e.forwarder.decode())
name = ''
if e.name:
name = str(e.name.decode())
x.add_row([name, address, '0x' + hex(e.address)[2:].zfill(8), str(e.ordinal) + ' (' + hex(e.ordinal) + ')'])
print(x)
def printDeff(pe):
print('EXPORTS')
d = [pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_EXPORT"]]
pe.parse_data_directories(directories=d)
exports = [(e) for e in pe.DIRECTORY_ENTRY_EXPORT.symbols]
for export in exports:
export = export.name.decode('utf-8')
print(' ' + export + '="' + args.deff[:-4] + '".' + export)
pe = pefile.PE(args.filename.name)
if args.deff:
printDeff(pe)
else:
printTable(pe)