-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathoutputter.py
399 lines (295 loc) · 8.38 KB
/
outputter.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/python
import colors
import distorm3
import optparse
import sys
import struct
from lib.darm import darm
def disas(buf, array_name = '', row_width = 16, fancy = False, sixtyfour = False):
parser = optparse.OptionParser()
if sixtyfour == True:
parser.set_defaults(dt=distorm3.Decode64Bits)
else:
parser.set_defaults(dt=distorm3.Decode32Bits)
options, args = parser.parse_args([])
disas = distorm3.Decode(0, buf, options.dt)
out = ''
for (offset, size, instruction, hexdump) in disas:
tmp = ''
if fancy:
tmp += colors.fg('cyan')
tmp += "%.8x: " % (offset)
if fancy:
tmp += colors.fg('red')
tmp += hexdump
tmp += " " * (20-len(hexdump))
if fancy:
tmp += colors.fg('green')
tmp += instruction
if fancy:
tmp += colors.end()
out += " " + tmp + "\n"
return out.lower()
def disas64(buf, array_name = '', row_width = 16, fancy = False):
return disas(buf, array_name, row_width, fancy, True)
def disas_arm(buf, array_name = '', row_width = 16, fancy = False):
insns = struct.unpack("I"*(len(buf)/4), buf)
out = ""
pos = 0
for insn in insns:
tmp = ""
if fancy:
tmp += colors.fg('cyan')
tmp += "%.8x: " % (pos)
if fancy:
tmp += colors.fg('red') + colors.bold()
tmp += "%08x " % (insn)
if fancy:
tmp += colors.end() + colors.fg('green')
tmp += str(darm.disasm_armv7(insn))
if fancy:
tmp += colors.end()
out += " " + tmp + "\n"
pos = pos+4
return out
def disas_thumb(buf, array_name = '', row_width = 16, fancy = False):
insns = struct.unpack("H"*(len(buf)/2), buf)
out = ""
pos = 0
for insn in insns:
tmp = ""
if fancy:
tmp += colors.fg('cyan')
tmp += "%.8x: " % (pos)
if fancy:
tmp += colors.fg('red') + colors.bold()
tmp += "%08x " % (insn)
if fancy:
tmp += colors.end() + colors.fg('green')
tmp += str(darm.disasm_thumb(insn))
if fancy:
tmp += colors.end()
out += " " + tmp + "\n"
pos = pos+2
return out
def bash(buf, array_name = 'shellcode', row_width = 16, fancy = False):
out = "$'"
badchars = [ 0x27, 0x5c ]
for c in buf:
o = ord(c)
if o >= 0x20 and o <= 0x7E and o not in badchars:
out += c
else:
out += "\\x%02x" % (o)
out += "'"
return out
def hexdump(buf, array_name = 'shellcode', row_width = 16, fancy = False):
# build horizontal marker
out = " | "
for i in range(0, row_width):
if fancy:
out += "%02x " % (i)
#out += colors.bold() + colors.fg('yellow') + ("%02x " % (i)) + colors.end()
else:
out += "%02x " % (i)
out += "|\n"
delim_row = " +--------+";
delim_row += "-" * (row_width*3 + 1) + "+" + "-" * (row_width+1) + "-+"
if fancy:
out += colors.bold() + delim_row + colors.end() + "\n"
else:
out += delim_row + "\n"
for i in range(0, len(buf), row_width):
if fancy:
out += colors.bold() + " | " + colors.fg("cyan") + ("%06x" % (i)) + " | " + colors.end()
else:
out += " | %06x | " % (i)
for j in range(0, row_width):
if i+j < len(buf):
if fancy:
str = colors.fg('red') + ("%02x " % (ord(buf[i+j])))
if (i+j)%8 >= 4:
out += colors.bold() + str + colors.end()
else:
out += str + colors.end()
else:
out += "%02x " % (ord(buf[i+j]))
else:
out += " "
asciiz = ''
for j in range(0, row_width):
if i+j < len(buf):
c = ord(buf[i+j])
if c >= 0x20 and c <= 0x7e:
asciiz += buf[i+j]
else:
asciiz += '.'
else:
asciiz += ' '
if fancy:
out += colors.bold() + "| " + colors.fg('green') + asciiz + colors.end() + colors.bold() + " |" + colors.end() + "\n"
else:
out += "| " + asciiz + " |\n"
if fancy:
out += colors.bold() + delim_row + colors.end() + "\n"
else:
out += delim_row + "\n"
return out
def code_array(buf, array_name = 'shellcode', row_width = 16, line_delimiter = '', fancy = False):
lines = []
out = array_name +" = \n"
for i in range(0, len(buf), row_width):
j = 0
linebuf = ''
while (j < row_width and (i+j) < len(buf)):
linebuf += "\\x%02x" % ( ord(buf[i+j]) )
j = j + 1
lines.append(linebuf);
for i in range(0, len(lines)-1):
if fancy:
out += "\t" + colors.bold() + colors.fg('magenta') + "\""
out += colors.fg("red") + lines[i]
out += colors.fg('magenta') + "\"" + colors.end()
out += line_delimiter + "\n"
else:
out += "\t\"%s\"%s\n" % ( lines[i], line_delimiter )
if fancy:
out += "\t" + colors.bold() + colors.fg('magenta') + "\""
out += colors.fg("red") + lines[len(lines)-1]
out += colors.fg('magenta') + "\"" + colors.end() + ";"
out += "\n\n"
# out += "\t\"%s\";\n\n" % ( lines[len(lines)-1] )
else:
out += "\t\"%s\";\n\n" % ( lines[len(lines)-1] )
return out
def c(buf, array_name = 'shellcode', row_width = 16, fancy = False):
if fancy:
name = colors.fg('green') + "unsigned " + colors.bold() + "char " + colors.end()
name += colors.bold() + array_name + "[]" + colors.end()
else:
name = "unsigned char " + array_name + "[]"
return code_array(buf, name, row_width, '', fancy);
def carray(buf, array_name = 'shellcode', row_width = 16, fancy = False):
out = "unsigned char %s[%d]={\n" % (array_name, len(buf))
n = 0
for c in buf:
if n == len(buf)-1:
out += "0x%02x " % (ord(c))
else:
out += "0x%02x, " % (ord(c))
n=n+1
if (n % row_width) == 0:
out += "\n"
out += "};\n"
return out
def python(buf, array_name = 'shellcode', row_width = 16, fancy = False):
lines = []
out = ""
for i in range(0, len(buf), row_width):
j = 0
linebuf = ''
while (j < row_width and (i+j) < len(buf)):
linebuf += "\\x%02x" % ( ord(buf[i+j]) )
j = j + 1
lines.append(linebuf);
for i in range(0, len(lines)-1):
if fancy:
if i == 0:
out += array_name + " = " + colors.bold() + colors.fg('magenta') + "\""
else:
out += array_name + " += " + colors.bold() + colors.fg('magenta') + "\""
out += colors.fg("red") + lines[i]
out += colors.fg('magenta') + "\"\n" + colors.end()
else:
if i == 0:
out += array_name + " = \"%s\"\n" % ( lines[i] )
else:
out += array_name + " += \"%s\"\n" % ( lines[i] )
if fancy:
out += array_name + " += " + colors.bold() + colors.fg('magenta') + "\""
out += colors.fg("red") + lines[len(lines)-1]
out += colors.fg('magenta') + "\"" + colors.end() + ";"
out += "\n\n"
# out += "\t\"%s\";\n\n" % ( lines[len(lines)-1] )
else:
out += array_name + " += \"%s\";\n\n" % ( lines[len(lines)-1] )
return out
def perl(buf, array_name = 'shellcode', row_width = 16, fancy = False):
return code_array(buf, '$' + array_name, row_width, ' . ', fancy)
def php(buf, array_name = 'shellcode', row_width = 16, fancy = False):
return perl(buf, array_name, row_width, fancy)
def raw(buf, array_name = '', row_width = 16, fancy = False):
if fancy:
return hexdump(buf, array_name, row_width, fancy)
else:
return buf
def hhex(buf, array_name = '', row_width = 16, fancy = False):
return buf.encode("hex")
def hwords(buf, array_name = '', row_width = 16, fancy = False):
i = 0
out = ""
if len(buf) % 2 != 0:
buf += "\x00" * (2 - (len(buf)%2))
while i < len(buf):
v = struct.unpack("<H", buf[i:i+2])
out += "%08x: 0x%04x\n" % (i, v[0])
i = i+2
return out
def dwords(buf, array_name = '', row_width = 16, fancy = False):
i = 0
out = ""
if len(buf) % 4 != 0:
buf += "\x00" * (4 - (len(buf)%4))
while i < len(buf):
v = struct.unpack("<L", buf[i:i+4])
out += "%08x: 0x%08x\n" % (i, v[0])
i = i+4
return out
def qwords(buf, array_name = '', row_width = 16, fancy = False):
i = 0
out = ""
if len(buf) % 8 != 0:
buf += "\x00" * (8 - (len(buf)%8))
while i < len(buf):
v = struct.unpack("<Q", buf[i:i+8])
out += "%08x: 0x%016x\n" % (i, v[0])
i = i+8
return out
def hex_bin(str):
return str.decode('hex')
outfunc = {
'c' : c,
'carray' : carray,
'php' : php,
'perl' : perl,
'hexdump' : hexdump,
'hex' : hhex,
'disas' : disas,
'disas64' : disas64,
'disas-arm' : disas_arm,
'disas-thumb' : disas_thumb,
'python' : python,
'bash' : bash,
'raw' : raw,
'dwords' : dwords,
'qwords' : qwords,
'hwords' : hwords
}
def main(args):
if len(args) > 2:
print "usage: moneyshot format [outformat] [fancy=0]"
return
if len(args) >= 1:
lang = args[0]
else:
lang = "c"
if lang not in outfunc:
print "Invalid outformat given: '%s' :(" % (lang)
return False
data = sys.stdin.readlines()
data = ''.join(data)
if len(args) == 2 and args[1] == "1":
do_fancy = True
else:
do_fancy = False
print outfunc[ lang ](data, fancy = do_fancy),