-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
gz.py
278 lines (226 loc) · 7.06 KB
/
gz.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
#!/usr/bin/env python3
# Reusable MD5 collision for .GZ
# via pre-computed UniColl prefixes
# Ange Albertini 2021
# 2021/10/17: - first release
# 2021/10/18: - splitting members of top file if too big.
# 2021/10/20: - splitting bottom file for warning-less decompression.
# - prefix ordering
# Archive: T (Trampoline header) + [gap] + LP (LandingPad)
# T: 16 bytes
# LP: 2 + 4 + 4 = 10 bytes
# Sa0 = Size of member A0
# 000: Aalign vvv Prefix vvv
# 03E: Tunicoll
# / \ (jumps short or long)
# 0C0: LPs_ \ vvv Suffix vvv
# Ta \ (0x100) (truncated)
# / \ <gap>
# 1C0: / LPl_
# / T (10 + Sa1 + 16) vvv no more gap vvv
# LP /
# Aa0 /
# T / (10 + 16)
# | LP_
# | T (10 + Sa1 + 16)
# LP /
# Aa1 /
# ... ...
# T / (10+16)
# | LP__
# | T (10 + SaN + 16)
# LP /
# An / <-- archive A is over
# -> end the parsing cleanly
# T / (10 + Sb0 + 16)
# / LP
# / Ba0
# / T (10 + 16)
# LP__ |
# ... ...
# T | (10 + Sb1 + 16)
# / LP
# / Ba1
# / T (10 + 16)
# LP_ |
# T | (10 + SbN + 16)
# \ LP
# \ BaN
# \_T (0) \ common end
# LP /
# Between each Ai..Aj:
# T(10+16), , LP
# LP, T(10+Saj+16),
# Acknowledgment:
# Yann Droneaud - https://twitter.com/ydroneaud/status/1449079965853573127
import argparse
import gzip
import hashlib
import io
import sys
def makeHeader(length, mtime=b"Ange",subfieldID=b"cb"):
# 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 -A -9 -8 -7 -6 -5 -4 -3 -2 -1 EOF
# Sig |CM|Fl|Mtime |XF|OS|Len |SuID |SuLen| SubData |Body |CRC32 |Size32 |
# 1F 8B|08|04|.A .n .g .e|02|FF|LL LL|Id Id|Sl Sl| ... |03 00|00 00 00 00|00 00 00 00|
# ^^
# \- Unicoll applied here
result = b"".join([
b"\x1f\x8b", # Signature
b"\x08", # Compression Method = Deflate
b"\4", # Flags = Extra Field
mtime,
b"\2", # Extra flags: compression max
b"\xFF", # OS: Unknown
(length+4).to_bytes(2, "little"),
subfieldID,
(length).to_bytes(2, "little"),
])
assert len(result) == 16
return result
parser = argparse.ArgumentParser(description="Generate GZip MD5 collisions.")
parser.add_argument('file1',
help="first 'top' input file - will be cut in smaller chunks if needed.")
parser.add_argument('file2',
help="second 'bottom' input file.")
args = parser.parse_args()
filename_a = args.file1
filename_b = args.file2
# All landing pads are identical
LP = b"".join([
b"\3\0", # Body [empty data]
b"\0\0\0\0", # CRC32
b"\0\0\0\0", # USize32
])
# Gzip terminates when indicating 2 characters - since Gzip signature is 2 bytes
# should be non null, should not be 1F 8B
# Make Boom a length of 16 to simplify the loop
boom = 8 * b"AA"
MAXEF = 0xffff - 2 - 2
def slice_archive(archive, chunk_l=int(MAXEF * 2.0)):
"""slice in members of arbitrary - small - uncompressed length"""
uncomp_data = gzip.decompress(archive)
uncomp_l = len(uncomp_data)
comp_factor = uncomp_l // chunk_l
members = []
for i in range(comp_factor - 1):
members.append(gzip.compress(uncomp_data[chunk_l * i:chunk_l * (i+1)], mtime=0))
members.append(gzip.compress(uncomp_data[chunk_l * (comp_factor-1):], mtime=0))
return members
def split_member(member):
"""split members by compressing data in halves"""
data = gzip.decompress(member)
l = len(data)
d1 = data[:l//2]
d2 = data[l//2:]
a1 = gzip.compress(d1, mtime=0)
a2 = gzip.compress(d2, mtime=0)
return a1, a2
def split_members(members):
"""split members until they're small enough"""
while True:
for i, member in enumerate(members):
if len(member) > MAXEF:
member1, member2 = split_member(member)
members[i] = member2
members.insert(i, member1)
break
else:
break
return members
def test_members(archive, members):
concat = b""
for member in members:
assert len(member) <= 0xfffe
concat += member
assert gzip.decompress(archive) == gzip.decompress(concat)
def process(contents):
if len(contents) <= MAXEF:
members = [contents]
else:
members = slice_archive(contents)
members = split_members(members)
test_members(contents, members)
return members
with open(filename_a, "rb") as f:
contents_a = f.read()
membersA = process(contents_a)
if len(membersA) > 1:
print("%s (%i bytes): split in %i members" % (args.file1, len(contents_a), len(membersA)))
with open(filename_b, "rb") as f:
contents_b = f.read()
membersB = process(contents_b)
if len(membersB) > 1:
print("%s (%i bytes): split in %i members" % (args.file2, len(contents_b), len(membersB)))
suffix = LP
suffix += makeHeader(0x1DA - 0xDA, mtime=b"JMPa")
def deco(s, c=b"|"):
"""align a string on 16 bytes"""
return c + s[:16].center(16 - 2) + c
suffix += b"".join([
b"\1\2\3\4\5\6",
b"+--------------+",
deco(b"reusable"),
deco(b""),
deco(b"GZIP"),
deco(b""),
deco(b"collision"),
deco(b""),
deco(b"for MD5"),
deco(b""),
deco(b"2021"),
deco(b""),
deco(b"Ange"),
deco(b"Albertini"),
b"+______________+",
])
# suffix += (0x100 - len(suffix)) * b"\0"
assert len(suffix) == 0x100
suffix += LP
suffix += makeHeader(len(membersA[0]) + 10 + 16, mtime=b"JUMP")
suffix += LP
suffix += membersA[0]
for member in membersA[1:]:
suffix += makeHeader(10 + 16, mtime=b"JMPb")
suffix += LP
suffix += makeHeader(len(member) + 10 + 16, mtime=b"JMPc")
suffix += LP
suffix += member
for member in membersB[:-1]:
suffix += makeHeader(len(member) + 10 + 16, mtime=b"JMPe")
suffix += LP
suffix += member
suffix += makeHeader(10 + 16, mtime=b"JMPd")
suffix += LP
suffix += makeHeader(len(membersB[-1]) + 10 + 16, mtime=b"JMPe")
suffix += LP
suffix += membersB[-1]
suffix += makeHeader(0, mtime=b"END_") # required to end on the same LP
suffix += LP
with open("prefix1.gz", "rb") as f:
prefix1 = f.read()
with open("prefix2.gz", "rb") as f:
prefix2 = f.read()
assert len(prefix1) % 64 == 0
def check_pair(d1, d2):
assert prefix1 != prefix2
assert len(prefix1) == len(prefix2)
assert hashlib.md5(prefix1).hexdigest() == hashlib.md5(prefix2).hexdigest()
check_pair(prefix1, prefix2)
DIFF_o = -0x77 # first diff in first UniColl block
# is prefix1 the short jump ?
if prefix1[DIFF_o] - 1 == prefix2[DIFF_o]:
prefix1, prefix2 = prefix2, prefix1
assert prefix1[DIFF_o] + 1 == prefix2[DIFF_o]
data1 = prefix1 + suffix
data2 = prefix2 + suffix
assert gzip.decompress(contents_a) == gzip.decompress(data1)
assert gzip.decompress(contents_b) == gzip.decompress(data2)
check_pair(data1, data2)
print("Success!")
with open("coll-1.gz", "wb") as f:
f.write(data1)
with open("coll-2.gz", "wb") as f:
f.write(data2)
print(hashlib.md5(data1).hexdigest())
print("coll-1.gz => %s" % filename_a)
print("coll-2.gz => %s" % filename_b)