-
Notifications
You must be signed in to change notification settings - Fork 3
/
shadow.py
executable file
·211 lines (188 loc) · 5.13 KB
/
shadow.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
#!/usr/bin/python3
import base64;
import sys;
class Crafter:
def __init__(self, jpg1, jpg2, w, h):
self.jpg1 = jpg1
self.jpg2 = jpg2
self.w = w
self.h = h
def craft(self, out1, out2):
size1 = len(self.jpg1) - 4 # we remove leading 0xffd8 and trailing 0xffd9
size2 = len(self.jpg2) - 2 # we remove leading 0xffd8
jmp1 = size1 + 10 + 4 # jump over file 1 from 0x234
jmp2 = size2 # jump over file 2
imgStart = 0x95
imgEnd = 0x23e + size1 + 4 + size2
out1.write(base64.b64decode(h1))
out2.write(base64.b64decode(h2))
duplicator = Duplicator([out1, out2])
duplicator.write(jmp1.to_bytes(2, 'big'))
duplicator.write(b'\0' * 8)
duplicator.write(memoryview(self.jpg1)[2:-2])
duplicator.write(b'\xff\xfe')
duplicator.write(jmp2.to_bytes(2, 'big'))
duplicator.write(memoryview(self.jpg2)[2:])
# write PDF sections and auto-compute their xref index
footer = Footer()
footer.append(
"""endstream
endobj
""")
footer.mark()
footer.append(
"""2 0 obj
{w}
endobj
""".format(w=self.w))
footer.mark()
footer.append(
"""3 0 obj
{h}
endobj
""".format(h=self.h))
footer.mark()
footer.append(
"""4 0 obj
/XObject
endobj
""")
footer.mark()
footer.append(
"""5 0 obj
/Image
endobj
""")
footer.mark()
footer.append(
"""6 0 obj
/DCTDecode
endobj
""")
footer.mark()
footer.append(
"""7 0 obj
/DeviceRGB
endobj
""")
footer.mark()
footer.append(
"""8 0 obj
{length}
endobj
""".format(length=imgEnd-imgStart))
footer.mark()
footer.append(
"""9 0 obj
<<
/Type /Catalog
/Pages 10 0 R
>>
endobj
""")
footer.mark()
footer.append(
"""10 0 obj
<<
/Type /Pages
/Count 1
/Kids [11 0 R]
>>
endobj
""")
footer.mark()
footer.append(
"""11 0 obj
<<
/Type /Page
/Parent 10 0 R
/MediaBox [0 0 {w} {h}]
/CropBox [0 0 {w} {h}]
/Contents 12 0 R
/Resources
<<
/XObject <</Im0 1 0 R>>
>>
>>
endobj
""".format(w=self.w, h=self.h))
footer.mark()
footer.append(
"""12 0 obj
<</Length 38>>
stream
q
{w:05d} 0 0 {h:05d} 0 0 cm
/Im0 Do
Q
endstream
endobj
""".format(w=self.w, h=self.h))
xrefPos = footer.position()
footer.append(
"""xref
0 13
0000000000 65535 f
0000000017 00000 n
""")
for index in footer.indices:
footer.append('{:010d} 00000 n \n'.format(imgEnd + index))
footer.append(
"""trailer << /Root 9 0 R /Size 13>>
startxref
{xrefpos}
%%EOF
""".format(xrefpos=imgEnd+xrefPos))
# write the footer to the file
duplicator.write(bytes(footer.data, "UTF-8"));
class Footer:
def __init__(self):
self.data = ""
self.indices = []
def append(self, text):
self.data += text
def mark(self):
self.indices.append(self.position())
def position(self):
return len(self.data)
class Duplicator:
def __init__(self, outputs):
self.outputs = outputs
def write(self, *args):
for output in self.outputs:
output.write(*args)
def main():
if len(sys.argv) < 5:
sys.exit('Usage: {} <jpg1> <jpg2> <width> <height>'.format(sys.argv[0]))
with open(sys.argv[1], 'rb') as file1, open(sys.argv[2], 'rb') as file2:
w = int(sys.argv[3])
h =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