-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjdump_instruction_extractor.py
More file actions
73 lines (49 loc) · 2.25 KB
/
objdump_instruction_extractor.py
File metadata and controls
73 lines (49 loc) · 2.25 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
''' Extracts from objdump -d output the hexcode for each op+operands '''
import sys
import re
exe_info_line = 2 # objdump -d second line has ej: "program.exe: file format pei-i386"
section_start_line = 5 # contains: \'Dissasembly of section .text: '''
function_dissasembly_line = 7 # First line containing the header for
# first function ej: 00401000 <_main>:
# match it with: function_code_re '''
line_code_re = r'(?P<addr>\w+):\s+'\
'(?P<hexrep>(\w{2}\s)+)\s+'\
'(?P<op>\w+){1}\s*'\
'(?P<loperand>[\w$%\[\]*]+)?,'\
'?(?P<roperand>[\w$%\[\]*]+)?'
FIRST_ARG = 1 # First commandline argument
function_code_re = '(?P<address>[0-9a-b]+)\s<(?P<label>\w+)>:'
cline_code_re = re.compile(line_code_re)
cfunction_code_re = re.compile(function_code_re)
def hex_for_each_op(string):
for hgroup in string.split():
yield '\\' + 'x' + hgroup
pass
def main():
if len(sys.argv) < 2:
return
with open(sys.argv[FIRST_ARG], 'r') as objdump_file :
output = ''
lines = objdump_file.read().split('\n')
# Get the hex representation for each line of objdump
for line in lines:
if '' or '(bad)' in line or '...' in line:
continue
elif 'Dissasembly of section' in line:
output += line + '\n\n'
print('Processing:\n' + line)
match_section = cfunction_code_re.search(line)
match_code = cline_code_re.search(line)
if match_section != None:
section = match_section.groupdict()['label']
output += '\nSection: {0}'.format(section) + '\n\n'
elif match_code != None:
for hexrep in hex_for_each_op(match_code.groupdict()['hexrep']):
output += hexrep
output += '\n'
# Store the hexcode of each instruction + operands into new file
result_file = open(sys.argv[FIRST_ARG] + '.shellcode', 'w')
result_file.writelines(output)
result_file.close()
if __name__ == '__main__':
main()