-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjdump2shellcode.py
More file actions
executable file
·131 lines (104 loc) · 3.5 KB
/
objdump2shellcode.py
File metadata and controls
executable file
·131 lines (104 loc) · 3.5 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
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
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python2
import re
import sys
import argparse
def nullbytes(shellcode):
c = 0
for byte in shellcode.split('\\x'):
if ('00' in byte):
c += 1
return c
def show_info(shellcode, endiannes, arch, quiet_mode):
if not quiet_mode:
print "-" * 64
print "Architecture: %s" % (arch)
print "Endiannes: %s" % (endiannes)
print "Length: %d" % (len(shellcode))
print "Null bytes: %d" % (nullbytes(shellcode))
print shellcode
else:
print shellcode
def detectArch(line):
string = line.split(' ')[-1]
string = string.lower()
if 'arm' in string:
return 'arm'
elif 'x86' in string:
return 'x86'
else:
print 'Could not detect architecture! Exiting.'
sys.exit(0)
def arm(fd, endiannes, quiet_mode):
shellcode = ''
code_section = False
for line in fd:
line = line.strip()
if "_start" in line:
code_section = True
continue
if code_section == True:
if not quiet_mode:
print line
if len(line) > 5:
if line.endswith('>:'):
continue
columns = line.replace('\t',' ').split(' ')
columns = filter(None, columns)
code_col = columns[1].strip()
pairs = re.findall('..',code_col)
if endiannes == 'little':
pairs = pairs[::-1]
for pair in pairs:
shellcode += '\\x'+pair
return shellcode
def x86(fd, endiannes, quiet_mode):
shellcode = ''
code_section = False
for line in fd:
line = line.strip()
if "_start" in line:
if not quiet_mode:
print line
code_section = True
continue
if code_section == True:
if not quiet_mode:
print line
if len(line) > 5:
if line.endswith('>:'):
continue
columns = line.replace('\t',' ').split(' ')
code_col = columns[1:columns.index('')]
code_col = "".join(code_col)
pairs = re.findall('..',code_col)
if endiannes == 'little':
pairs = pairs[::-1]
for pair in pairs:
shellcode += '\\x'+pair
return shellcode
def main():
ap = argparse.ArgumentParser()
ap.add_argument('-c', '--cpu', required=False, help='set CPU architechture [arm, x86]')
ap.add_argument('-q', '--quiet', required=False, default=False, help='reduced output', action='store_true')
ap.add_argument('-e', '--endiannes', required=False, default='little', help='change endiannes [big, little]')
ap.add_argument("file")
args = vars(ap.parse_args())
endiannes = args["endiannes"]
shellcode = ''
arch = args["cpu"]
filepath = args["file"]
quiet_mode = args["quiet"]
with open(filepath) as fd:
for line in fd:
line = line.strip()
if not arch:
if len(line.strip()) != 0 :
arch = detectArch(line)
else:
if arch == 'arm':
shellcode = arm(fd, endiannes, quiet_mode)
if arch == 'x86':
shellcode = x86(fd, endiannes, quiet_mode)
show_info(shellcode, endiannes, arch, quiet_mode)
if __name__ == '__main__':
main()