forked from corkami/collisions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pngStd.py
executable file
·123 lines (95 loc) · 2.59 KB
/
pngStd.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
#!/usr/bin/env python
# A script to collide PNGs with the same header
# and optionally craft the prefix and launch UniColl
import sys
import struct
import hashlib
import glob
import os
import shutil
def get_data(args):
fn1, fn2 = args
with open(fn1, "rb") as f:
d1 = f.read()
with open(fn2, "rb") as f:
d2 = f.read()
assert d1.startswith("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")
assert d2.startswith("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")
assert d1[:0x21] == d2[:0x21]
return d1, d2
d1, d2 = get_data(sys.argv[1:3])
hash = hashlib.sha256(d1[:0x21]).hexdigest()[:8]
print "Header hash: %s" % hash
if not glob.glob("png1-%s.bin" % hash):
print "Not found! Launching computation..."
# make the complete prefix
with open("prefix", "wb") as f:
f.write("".join([
# 00-20 - original common header
d1[:0x21],
# 21-46 - padding chunk
"\0\0\0\x1a", "aNGE", ":MD5 ISREALLY DEAD NOW!!1!", "ROFL",
# 47-C7 - collision chunk
# 47-4F
# this is the end of the collision prefix,
# => lengths of 0x75 and 0x175
"\0\0\0\x75", "mARC", "!",
# the end of the collision blocks if they're not computed
# 50-BF
# " " * 0x70,
]))
# Note: make sure poc_no.sh is unmodified (ie, N=1)
os.system("../hashclash/scripts/poc_no.sh prefix")
shutil.copyfile("collision1.bin", "png1-%s.bin" % hash)
shutil.copyfile("collision2.bin", "png2-%s.bin" % hash)
with open("png1-%s.bin" % hash, "rb") as f:
block1 = f.read()
with open("png2-%s.bin" % hash, "rb") as f:
block2 = f.read()
assert len(block1) == 0xC0
assert len(block2) == 0xC0
ascii_art = """
/==============\\
|* *|
| PNG IMAGE |
| with |
| identical |
| -prefix |
| MD5 collision|
| |
| by |
| Marc Stevens |
| and |
|Ange Albertini|
| |
|* *|
\\==============/
BRK!
""".replace("\n", "").replace("\r","")
assert len(ascii_art) == 0xF4
suffix = "".join([
# C0-C7
"RealHash", # the remaining of the mARC chunk
# C8-1C3 the tricky fake chunk
# the length, the type and the data should all take 0x100
struct.pack(">I", 0x100 - 4*2 + len(d2[0x21:])),
"jUMP",
# it will cover all data chunks of d2,
# and the 0x100 buffer
ascii_art,
"\xDE\xAD\xBE\xEF", # fake CRC for mARC
# 1C8 - Img2 + 4
d2[0x21:],
"\x5E\xAF\x00\x0D", # fake CRC for jUMP after d2's IEND
d1[0x21:],
])
with open("%s-1.png" % hash, "wb") as f:
f.write("".join([
block1,
suffix
]))
with open("%s-2.png" % hash, "wb") as f:
f.write("".join([
block2,
suffix
]))