forked from grevutiu-gabriel/python-md5-collision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_coll_c.py
executable file
·63 lines (46 loc) · 1.67 KB
/
gen_coll_c.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
#!/usr/bin/env python3
from coll import Collider, md5pad, filter_disallow_binstrings
import os, sys
# First compile the C code into a binary
temp = 'out_c_demo_temp'
os.system('gcc c_demo.c -o {}'.format(temp))
with open(temp, 'rb') as tempfile:
compdata = bytearray(tempfile.read())
first = None
second = None
# Find strings in binary:
# We find where in the first one we can put a collision pair (aligned to 64 bytes).
# The second string gets a copy of the first from the pair,
# and it is put at the same offset into the first string.
for i in range(0, len(compdata), 64):
s = compdata[i:i+128]
if s != b'%' * 128:
continue
for q in range(i,i+(64*3+2)):
if compdata[q] == ord('+') or compdata[q] == ord('-'):
startchars = q-(64*3)
if not first:
first = i
offset = i - startchars
else:
second = startchars + offset
compdata[q] = 0
break
if not (first and second):
raise Exception('error: did not find marker strings')
# Splice in the collision blocks according to the obtained offsets
collider = Collider(blockfilter=filter_disallow_binstrings([b'\0']))
collider.bincat(compdata[:first])
collider.safe_diverge()
c1, c2 = collider.get_last_coll()
collider.bincat(compdata[first+128:second] + c1 + compdata[second+128:])
# Write out good and evil binaries
cols = collider.get_collisions()
GOOD = 'out_c_good'
EVIL = 'out_c_evil'
with open(GOOD, 'wb') as good:
good.write(next(cols))
with open(EVIL, 'wb') as evil:
evil.write(next(cols))
os.system('chmod +x {} {}'.format(GOOD, EVIL))
os.remove(temp)