forked from tywang2006/Chaos2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-opt-in.py
240 lines (198 loc) · 6.74 KB
/
add-opt-in.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
#!/usr/bin/env python
# Copyright (c) 2012, Adobe Systems Incorporated
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of Adobe Systems Incorporated nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''See readme or run with no args for usage'''
import os
import sys
import tempfile
import shutil
import struct
import zlib
import hashlib
import inspect
supportsLZMA = False
try:
import pylzma
supportsLZMA = True
except:
pass
####################################
# Helpers
####################################
class stringFile(object):
def __init__(self, data):
self.data = data
def read(self, num=-1):
result = self.data[:num]
self.data = self.data[num:]
return result
def close(self):
self.data = None
def flush(self):
pass
def consumeSwfTag(f):
tagBytes = b""
recordHeaderRaw = f.read(2)
tagBytes += recordHeaderRaw
if recordHeaderRaw == "":
raise Exception("Bad SWF: Unexpected end of file")
recordHeader = struct.unpack("BB", recordHeaderRaw)
tagCode = ((recordHeader[1] & 0xff) << 8) | (recordHeader[0] & 0xff)
tagType = (tagCode >> 6)
tagLength = tagCode & 0x3f
if tagLength == 0x3f:
ll = f.read(4)
longlength = struct.unpack("BBBB", ll)
tagLength = ((longlength[3]&0xff) << 24) | ((longlength[2]&0xff) << 16) | ((longlength[1]&0xff) << 8) | (longlength[0]&0xff)
tagBytes += ll
tagBytes += f.read(tagLength)
return (tagType, tagBytes)
def outputInt(o, i):
o.write(struct.pack('I', i))
def outputTelemetryTag(o, passwordClear):
lengthBytes = 2 # reserve
if passwordClear:
sha = hashlib.sha256()
sha.update(passwordClear)
passwordDigest = sha.digest()
lengthBytes += len(passwordDigest)
# Record header
code = 93
if lengthBytes >= 63:
o.write(struct.pack('<HI', code << 6 | 0x3f, lengthBytes))
else:
o.write(struct.pack('<H', code << 6 | lengthBytes))
# Reserve
o.write(struct.pack('<H', 0))
# Password
if passwordClear:
o.write(passwordDigest)
####################################
# main()
####################################
if __name__ == "__main__":
####################################
# Parse command line
####################################
if len(sys.argv) < 2:
print("Usage: %s SWF_FILE [PASSWORD]" % os.path.basename(inspect.getfile(inspect.currentframe())))
print("\nIf PASSWORD is provided, then a password will be required to view advanced telemetry in Adobe 'Monocle'.")
sys.exit(-1)
infile = sys.argv[1]
passwordClear = sys.argv[2] if len(sys.argv) >= 3 else None
####################################
# Process SWF header
####################################
swfFH = open(infile, 'rb')
signature = swfFH.read(3)
swfVersion = swfFH.read(1)
struct.unpack("<I", swfFH.read(4))[0] # uncompressed length of file
if signature == b"FWS":
pass
elif signature == b"CWS":
decompressedFH = stringFile(zlib.decompressobj().decompress(swfFH.read()))
swfFH.close()
swfFH = decompressedFH
elif signature == b"ZWS":
if not supportsLZMA:
raise Exception("You need the PyLZMA package to use this script on \
LZMA-compressed SWFs. http://www.joachim-bauch.de/projects/pylzma/")
swfFH.read(4) # compressed length
decompressedFH = stringFile(pylzma.decompress(swfFH.read()))
swfFH.close()
swfFH = decompressedFH
else:
raise Exception("Bad SWF: Unrecognized signature: %s" % signature)
f = swfFH
o = tempfile.TemporaryFile()
o.write(signature)
o.write(swfVersion)
outputInt(o, 0) # FileLength - we'll fix this up later
# FrameSize - this is nasty to read because its size can vary
rs = f.read(1)
r = struct.unpack("B", rs)
rbits = (r[0] & 0xff) >> 3
rrbytes = (7 + (rbits*4) - 3) / 8;
o.write(rs)
o.write(f.read((int)(rrbytes)))
o.write(f.read(4)) # FrameRate and FrameCount
####################################
# Process each SWF tag
####################################
while True:
(tagType, tagBytes) = consumeSwfTag(f)
if tagType == 93:
raise Exception("Bad SWF: already has EnableTelemetry tag")
elif tagType == 92:
raise Exception("Bad SWF: Signed SWFs are not supported")
elif tagType == 69:
# FileAttributes tag
o.write(tagBytes)
# Look ahead for Metadata tag. If present, put our tag after it
(nextTagType, nextTagBytes) = consumeSwfTag(f)
writeAfterNextTag = nextTagType == 77
if writeAfterNextTag:
o.write(nextTagBytes)
outputTelemetryTag(o, passwordClear)
# If there was no Metadata tag, we still need to write that tag out
if not writeAfterNextTag:
o.write(nextTagBytes)
(tagType, tagBytes) = consumeSwfTag(f)
o.write(tagBytes)
if tagType == 0:
break
####################################
# Finish up
####################################
# Fix the FileLength header
uncompressedLength = o.tell()
o.seek(4)
o.write(struct.pack("I", uncompressedLength))
o.flush()
o.seek(0)
# Copy the temp file to the outFile, compressing if necessary
outFile = open(infile, "wb")
if signature == b"FWS":
shutil.copyfileobj(o, outFile)
else:
outFile.write(o.read(8)) # File is compressed after header
if signature == b"CWS":
outFile.write(zlib.compress(o.read()))
elif signature == b"ZWS":
compressed = pylzma.compress(o.read())
outputInt(outFile, len(compressed)-5) # LZMA SWF has CompressedLength header field
outFile.write(compressed)
else:
assert(false)
outFile.close()
if passwordClear:
print("Added opt-in flag with encrypted password " + passwordClear)
else:
print("Added opt-in flag with no password")