-
Notifications
You must be signed in to change notification settings - Fork 11
/
genbadzip.py
203 lines (174 loc) · 5.44 KB
/
genbadzip.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
import struct
import crcmod.predefined
crc32 = crcmod.predefined.mkPredefinedCrcFun('crc-32')
"""
Construct zip files with various kinds of bad behaviour.
Current:
- vary long paths
- paths with ..
- paths pointing to symlinks.
"""
def getdottedpath(i):
p = ""
while i:
p += "/" + "." * (i%3)
i //= 3
return p[1:] or '/'
class Writer:
def __init__(self, fh):
self.fh = fh
def tell(self):
return self.fh.tell()
def write(self, data):
self.fh.write(data)
def write16le(self, val):
self.write(struct.pack("<H", val))
def write32le(self, val):
self.write(struct.pack("<L", val))
class FileEnt:
def __init__(self, name, data):
self.name = name
self.data = data
self.crc = crc32(data)
self.xattr = b""
self.islink = False
def encode(self, w):
w.write(b"PK\x03\x04")
w.write16le(10) # need
w.write16le(0) # flags
w.write16le(0) # method
w.write32le(0x571658d4) # stamp
w.write32le(self.crc)
w.write32le(len(self.data)) # compsize
w.write32le(len(self.data)) # fullsize
w.write16le(len(self.name)) # namelen
w.write16le(len(self.xattr)) # xlen
w.write(self.name)
w.write(self.xattr)
w.write(self.data)
class DirEnt:
def __init__(self, name, crc, datalen):
self.name = name
self.crc = crc
self.datalen = datalen
self.osattr = 0o100644<<16
self.xattr = b""
def makedir(self):
self.osattr = (0o040755<<16) + 0x10
def makelink(self):
self.osattr = 0o120777<<16
def encode(self, w):
w.write(b"PK\x01\x02")
w.write16le(0x31e) # crea
w.write16le(10) # need
w.write16le(0) # flags
w.write16le(0) # method
w.write32le(0x571658d4) # stamp
w.write32le(self.crc)
w.write32le(self.datalen) # compsize
w.write32le(self.datalen) # fullsize
w.write16le(len(self.name)) # namelen
w.write16le(len(self.xattr)) # xlen
w.write16le(0) # clen
w.write16le(0) # dsk0
w.write16le(0) # atr
w.write32le(self.osattr)
w.write32le(self.dataptr)
w.write(self.name)
w.write(self.xattr)
class EODEnt:
def __init__(self, nrentries, dirsize, diroffset):
self.nrentries = nrentries
self.dirsize = dirsize
self.diroffset = diroffset
def encode(self, w):
w.write(b"PK\x05\x06")
w.write16le(0) # diskid
w.write16le(0) # diskstart
w.write16le(self.nrentries)
w.write16le(self.nrentries)
w.write32le(self.dirsize)
w.write32le(self.diroffset)
w.write16le(0) # comment size
class Zipfile:
def __init__(self):
self.entries = []
def addfile(self, name, src):
if type(src)==bytes:
data = src
elif type(src)==str:
data = src.encode('utf-8')
elif hasattr(src, 'read'):
data = src.read()
else:
raise Exception(f"dont know how to read {src}")
self.entries.append(FileEnt(name.encode('utf-8'), data))
def addsymlink(self, name, dst):
e = FileEnt(name.encode('utf-8'), dst.encode('utf-8'))
e.islink = True
self.entries.append(e)
def save(self, w):
for e in self.entries:
o = w.tell()
e.dataptr = o
e.encode(w)
dirofs = w.tell()
for e in self.entries:
de = DirEnt(e.name, e.crc, len(e.data))
de.dataptr = e.dataptr
if e.islink:
de.makelink()
de.encode(w)
dirend = w.tell()
eod = EODEnt(len(self.entries), dirend-dirofs, dirofs)
eod.encode(w)
def oldmakezip(zipfilename):
z = Zipfile()
for i in range(27):
# create symlinks
z.addsymlink(f"s{i:02d}", getdottedpath(i))
for i in range(27):
# file in symlinked dir
z.addfile(f"s{i:02d}/badfile_1{i:02d}.txt", b"")
# file with dotted path directly
p = getdottedpath(i)
z.addfile(f"{p}/badfile_2{i:02d}.txt", b"")
for l in range(230, 270):
# long filename
z.addfile(f"{l+3+4}{'x'*l}.txt", b"")
# long dirname
z.addfile(f"{l+3}{'y'*l}/x.txt", b"")
with open(zipfilename, "wb") as fh:
z.save(Writer(fh))
def zipgenerators():
for i in range(27):
# create symlinks
yield lambda z: z.addsymlink(f"s{i:02d}", getdottedpath(i))
for i in range(27):
# file in symlinked dir
yield lambda z:z.addfile(f"s{i:02d}/badfile_1{i:02d}.txt", b"")
# file with dotted path directly
p = getdottedpath(i)
yield lambda z:z.addfile(f"{p}/badfile_2{i:02d}.txt", b"")
for l in range(230, 270):
# long filename
yield lambda z:z.addfile(f"{l+3+4}{'x'*l}.txt", b"")
# long dirname
yield lambda z:z.addfile(f"{l+3}{'y'*l}/x.txt", b"")
def makezip(zipfilename):
z = Zipfile()
for gen in zipgenerators():
gen(z)
with open(zipfilename, "wb") as fh:
z.save(Writer(fh))
def makezips(zipbase):
for i, gen in enumerate(zipgenerators()):
z = Zipfile()
gen(z)
with open(f"{zipbase}-{i:04d}.zip", "wb") as fh:
z.save(Writer(fh))
def main():
makezip("escapedzip.zip")
makezips("esc")
if __name__=='__main__':
main()