forked from wapiflapi/villoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
villoc.py
executable file
·521 lines (362 loc) · 12.2 KB
/
villoc.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env python3
# @wapiflapi
import re
import html
import random
import codecs
class State(list):
def __init__(self, *args, **kwargs):
self.errors = []
self.info = []
super().__init__(*args, **kwargs)
def boundaries(self):
bounds = set()
for block in self:
lo, hi = block.boundaries()
bounds.add(lo)
bounds.add(hi)
return bounds
class Printable():
unit_width = 10
classes = ["block"]
def boundaries(self):
return (self.start(), self.end())
def gen_html(self, out, width, color=""):
out.write('<div class="%s" style="width: %dem; %s;">' %
(" ".join(self.classes), 10 * width, color))
if self.details:
out.write('<strong>%#x</strong><br />' % self.start())
out.write(self.more_html())
else:
out.write(' ')
out.write('</div>\n')
def more_html(self):
return ""
def __repr__(self):
return "%s(start=%#x, end=%#x)" % (self.__class__.__name__,
self.start(), self.end())
class Empty(Printable):
classes = Printable.classes + ["empty"]
def __init__(self, start, end, display=True):
self._start = start
self._end = end
self.details = display
def start(self):
return self._start
def end(self):
return self._end
def set_end(self, end):
self._end = end
def more_html(self):
return "+ %#x" % (self.end() - self.start())
class Block(Printable):
header = 8
footer = 0
round = 0x10
minsz = 0x20
classes = Printable.classes + ["normal"]
def __init__(self, addr, size, error=False, tmp=False, **kwargs):
self.color = kwargs.get('color', random_color())
self.uaddr = addr
self.usize = size
self.details = True
self.error = error
self.tmp = tmp
def start(self):
return self.uaddr - self.header
def end(self):
size = max(self.minsz, self.usize + self.header + self.footer)
rsize = size + (self.round - 1)
rsize = rsize - (rsize % self.round)
return self.uaddr - self.header + rsize
def gen_html(self, out, width):
if self.color:
color = ("background-color: rgb(%d, %d, %d);" % self.color)
else:
color = ""
if self.error:
color += ("background-image: repeating-linear-gradient("
"120deg, transparent, transparent 1.40em, "
"#A85860 1.40em, #A85860 2.80em);")
super().gen_html(out, width, color)
def more_html(self):
return "+ %#x (%#x)" % (self.end() - self.start(), self.usize)
def __repr__(self):
return "%s(start=%#x, end=%#x, tmp=%s)" % (
self.__class__.__name__, self.start(), self.end(), self.tmp)
class Marker(Block):
def __init__(self, addr, error=False, **kwargs):
super().__init__(addr, 0x0, tmp=True, error=error, *kwargs)
def more_html(self):
return "unknown"
def match_ptr(state, ptr):
if ptr is None:
return None, None
s, smallest_match = None, None
for i, block in enumerate(state):
if block.uaddr != ptr:
continue
if smallest_match is None or smallest_match.usize >= block.usize:
s, smallest_match = i, block
if smallest_match is None:
state.errors.append("Couldn't find block at %#x, added marker." %
(ptr - Block.header))
# We'll add a small tmp block here to show the error.
state.append(Marker(ptr, error=True))
return s, smallest_match
def malloc(state, ret, size):
if not ret:
state.errors.append("Failed to allocate %#x bytes." % size)
else:
state.append(Block(ret, size))
def calloc(state, ret, nmemb, size):
malloc(state, ret, nmemb * size)
def free(state, ret, ptr):
if ptr is 0:
return
s, match = match_ptr(state, ptr)
if match is None:
return
elif ret is None:
state[s] = Block(match.uaddr, match.usize,
error=True, color=match.color)
else:
del state[s]
def realloc(state, ret, ptr, size):
if not ptr:
return malloc(state, ret, size)
elif not size:
return free(state, ret, ptr)
s, match = match_ptr(state, ptr)
if match is None:
return
elif ret is None:
state[s] = Block(match.uaddr, match.usize, color=match.color)
state[s].error = True
else:
state[s] = Block(ret, size, color=match.color)
operations = {
'free': free,
'malloc': malloc,
'calloc': calloc,
'realloc': realloc,
}
def sanitize(x):
if x is None:
return None
if x == "<void>":
return 0
return int(x, 0)
def parse_ltrace(ltrace):
match_call = r"^([a-z_]+)\((.*)\) += (.*)"
match_err = r"^([a-z_]+)\((.*) <no return \.\.\.>"
for line in ltrace:
# if the trace file contains PID (for ltrace -f)
head, _, tail = line.partition(" ")
if head.isdigit():
line = tail
if not any(line.startswith(f) for f in operations):
continue
try:
func, args, ret = re.findall(match_call, line)[0]
except Exception:
try:
# maybe this stopped the program
func, args = re.findall(match_err, line)[0]
ret = None
except Exception:
print("ignoring line: %s" % line, file=sys.stderr)
continue
args = list(map(sanitize, args.split(", ")))
ret = sanitize(ret)
yield func, args, ret
def build_timeline(events):
boundaries = set()
timeline = [State()]
for func, args, ret in events:
try:
op = operations[func]
except KeyError:
continue
state = State(b for b in timeline[-1] if not b.tmp)
call = "%s(%s)" % (func, ", ".join("%#x" % a for a in args))
if ret is None:
state.errors.append("%s = <error>" % call)
else:
state.info.append("%s = %#x" % (call, ret))
op(state, ret, *args)
boundaries.update(state.boundaries())
timeline.append(state)
return timeline, boundaries
def random_color(r=200, g=200, b=125):
red = (random.randrange(0, 256) + r) / 2
green = (random.randrange(0, 256) + g) / 2
blue = (random.randrange(0, 256) + b) / 2
return (red, green, blue)
def print_state(out, boundaries, state):
out.write('<div class="state %s">\n' % ("error" if state.errors else ""))
known_stops = set()
todo = state
while todo:
out.write('<div class="line" style="">\n')
done = []
current = None
last = 0
for i, b in enumerate(boundaries):
# If this block has size 0; make it continue until the
# next boundary anyway. The size will be displayed as
# 0 or unknown anyway and it shouldn't be too confusing.
if current and current.end() != b and current.start() != current.end():
continue
if current: # stops here.
known_stops.add(i)
current.gen_html(out, i - last)
done.append(current)
last = i
current = None
for block in todo:
if block.start() == b:
current = block
break
else:
continue
if last != i:
# We want to show from previous known_stop.
for s in reversed(range(last, i+1)):
if s in known_stops:
break
if s != last:
Empty(boundaries[last], boundaries[s],
display=False).gen_html(out, s - last)
known_stops.add(s)
if s != i:
Empty(boundaries[s], b).gen_html(out, i - s)
known_stops.add(i)
last = i
if current:
raise RuntimeError("Block was started but never finished.")
if not done:
raise RuntimeError("Some block(s) don't match boundaries.")
out.write('</div>\n')
todo = [x for x in todo if x not in done]
out.write('<div class="log">')
for msg in state.info:
out.write('<p>%s</p>' % html.escape(str(msg)))
for msg in state.errors:
out.write('<p>%s</p>' % html.escape(str(msg)))
out.write('</div>\n')
out.write('</div>\n')
def gen_html(timeline, boundaries, out):
if timeline and not timeline[0]:
timeline.pop(0)
boundaries = list(sorted(boundaries))
out.write('<style>')
out.write('''body {
font-size: 12px;
background-color: #EBEBEB;
font-family: "Lucida Console", Monaco, monospace;
width: %dem;
}
''' % ((len(boundaries) - 1) * (Printable.unit_width + 1)))
out.write('''p {
margin: 0.8em 0 0 0.1em;
}
''')
out.write('''.block {
float: left;
padding: 0.5em 0;
text-align: center;
color: black;
}
''')
out.write('''.normal {
-webkit-box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.80);
-moz-box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.80);
box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.80);
}
''')
out.write('''.empty + .empty {
border-left: 1px solid gray;
margin-left: -1px;
}
''')
out.write('''.empty {
color: gray;
}
''')
out.write('.line { }')
out.write('''.line:after {
content:"";
display:table;
clear:both;
}''')
out.write('''.state {
margin: 0.5em; padding: 0;
background-color: white;
border-radius: 0.3em;
-webkit-box-shadow: inset 2px 2px 4px 0px rgba(0,0,0,0.80);
-moz-box-shadow: inset 2px 2px 4px 0px rgba(0,0,0,0.80);
box-shadow: inset 2px 2px 4px 0px rgba(0,0,0,0.80);
padding: 0.5em;
}''')
out.write('''.log {
}''')
out.write('''.error {
color: white;
background-color: #8b1820;
}''')
out.write('''.error .empty {
color: white;
}''')
out.write('</style>\n')
out.write('<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>')
out.write('''<script>
var scrollTimeout = null;
$(window).scroll(function(){
if (scrollTimeout) clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(function(){
$('.log').stop();
$('.log').animate({
'margin-left': $(this).scrollLeft()
}, 100);
}, 200);
});
</script>
''')
out.write('<body>\n')
out.write('<div class="timeline">\n')
for state in timeline:
print_state(out, boundaries, state)
out.write('</div>\n')
out.write('</body>\n')
if __name__ == '__main__':
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("ltrace", type=argparse.FileType("rb"))
parser.add_argument("out", type=argparse.FileType("w"))
parser.add_argument("--header", type=int, default=8,
help="size of malloc metadata before user data")
parser.add_argument("--footer", type=int, default=0,
help="size of malloc metadata after user data")
parser.add_argument("--round", type=int, default=0x10,
help="size of malloc chunks are a multiple of this value")
parser.add_argument("--minsz", type=int, default=0x20,
help="size of a malloc chunk is at least this value")
parser.add_argument("--raw", action="store_true",
help="disables header, footer, round and minsz")
# Some values that work well: 38, 917, 190, 226
parser.add_argument("-s", "--seed", type=int, default=226)
parser.add_argument("-S", "--show-seed", action="store_true")
args = parser.parse_args()
random.seed(args.seed)
if args.show_seed:
args.out.write('<h2>seed: %d</h2>' % args.seed)
# Set malloc options
if args.raw:
Block.header, Block.footer, Block.round, Block.minsz = 0, 0, 1, 0
Block.header, Block.footer, Block.round, Block.minsz = (
args.header, args.footer, args.round, args.minsz)
noerrors = codecs.getreader('utf8')(args.ltrace.detach(), errors='ignore')
timeline, boundaries = build_timeline(parse_ltrace(noerrors))
gen_html(timeline, boundaries, args.out)