-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathccl_chrome_pickle.py
308 lines (249 loc) · 9.29 KB
/
ccl_chrome_pickle.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python3
"""
Copyright (c) 2016, CCL Forensics
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 the CCL Forensics 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 CCL FORENSICS 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.
"""
import struct
import datetime
import io
from enum import Enum
__contact__ = "Alex Caithness"
__version__ = "0.4.1"
__description__ = "A reimplementation of the Google PickleIterator"
__outputtype__ = 1
__outputext__ = None
class PickleType(Enum):
Bool = 1
Int16 = 2
UInt16 = 3
Int32 = 4
UInt32 = 5
Int64 = 6
UInt64 = 7
Single = 8
Double = 9
Blob = 10
String = 11
String16 = 12
DateTime = 13
Pickle = 14
String16ByteCount = 15
class PickleReader:
def __init__(self, blob):
self._stream = io.BytesIO(blob)
try:
self.pickle_length = self._read_int32()
except struct.error as e:
raise EOFError("End of stream when reading pickle length", )
if len(blob) - 4 != self.pickle_length:
raise ValueError("Declared pickle length not equal to blob length")
self.current = None
self.length = len(blob)
def _read_int16(self):
x = self._stream.read(4)
return struct.unpack("<h", x[0:2])[0]
def _read_int32(self):
x = self._stream.read(4)
return struct.unpack("<i", x)[0]
def _read_int64(self):
x = self._stream.read(8)
return struct.unpack("<q", x)[0]
def _read_uint16(self):
x = self._stream.read(4)
return struct.unpack("<H", x[0:2])[0]
def _read_uint32(self):
x = self._stream.read(4)
return struct.unpack("<I", x)[0]
def _read_uint64(self):
x = self._stream.read(8)
return struct.unpack("<Q", x)[0]
def _read_single(self):
x = self._stream.read(4)
return struct.unpack("<f", x)[0]
def _read_double(self):
x = self._stream.read(8)
return struct.unpack("<d", x)[0]
def _read_raw(self, length):
# NB will always align the buffer after the read
start = self._stream.tell()
blob = self._stream.read(length)
if len(blob) < length:
raise EOFError(
"End of file reached when reading {0} bytes from offset {1} in the pickle")
alignment = (4 - (length % 4)) if length % 4 != 0 else 0
self._stream.seek(alignment, 1)
return blob
def read_short(self):
try:
self.current = self._read_int16()
except struct.error:
self.current = None
return False
return True
def read_int(self):
try:
self.current = self._read_int32()
except struct.error:
self.current = None
return False
return True
def read_long(self):
try:
self.current = self._read_int64()
except struct.error:
self.current = None
return False
return True
def read_ushort(self):
try:
self.current = self._read_uint16()
except struct.error:
self.current = None
return False
return True
def read_uint(self):
try:
self.current = self._read_uint32()
except struct.error:
self.current = None
return False
return True
def read_ulong(self):
try:
self.current = self._read_uint64()
except struct.error:
self.current = None
return False
return True
def read_bool(self):
try:
self.current = self._read_int32() != 0
except struct.error:
self.current = None
return False
return True
def read_single(self):
try:
self.current = self._read_single()
except struct.error:
self.current = None
return False
return True
def read_double(self):
try:
self.current = self._read_double()
except struct.error:
self.current = None
return False
return True
def read_timestamp(self):
try:
x = self._read_int64()
except struct.error:
self.current = None
return False
self.current = datetime.datetime(1601, 1, 1) + datetime.timedelta(microseconds=x)
return True
def read_blob(self, is_doubled_length=False):
try:
length = self._read_int32()
except struct.error:
self.current = None
return False
if length == -1:
self.current = None
return True
try:
self.current = self._read_raw(length * (2 if is_doubled_length else 1))
except EOFError:
self.current = None
return False
return True
def read_str(self):
success = self.read_blob()
if success and self.current is not None:
self.current = self.current.decode("utf-8")
return success
def read_str16_with_byte_count(self):
success = self.read_blob(is_doubled_length=False)
if success and self.current is not None:
self.current = self.current.decode("utf-16-le")
return success
def read_str16_with_char_count(self):
success = self.read_blob(is_doubled_length=True)
if success and self.current is not None:
self.current = self.current.decode("utf-16-le")
return success
def read_pickle(self):
length_buff = self._stream.read(4)
if len(length_buff) != 4:
self.current = None
return False
length, = struct.unpack("<i", length_buff)
pickle_buff = self._stream.read(length)
if len(pickle_buff) != length:
self.current = None
return False
self.current = PickleReader(length_buff + pickle_buff)
return True
def deserialise_into_dict(self, fields, raise_on_missing=False):
"""
fields: an iterable of tuples (attribute_name, pickle_type)
"""
result = {}
for field, field_type in fields:
if not isinstance(field, str):
raise TypeError("field must be str")
if not isinstance(field_type, PickleType):
raise TypeError("field_type must be a PickleType")
success = _func_lookup[field_type](self)
if not success and raise_on_missing:
raise ValueError("Field: {0} couldn't be read.")
if field not in result or success:
result[field] = self.current
return result
def iter_deserialise(self, fields, raise_on_missing=False):
"""
fields: an iterable of pickle_types
"""
for field_type in fields:
if not isinstance(field_type, PickleType):
raise TypeError("field_type must be a PickleType")
if not _func_lookup[field_type](self) and raise_on_missing:
raise ValueError("Field: {0} couldn't be read.".format(field_type))
yield self.current
_func_lookup = {PickleType.Bool: PickleReader.read_bool,
PickleType.Int16: PickleReader.read_short,
PickleType.UInt16: PickleReader.read_ushort,
PickleType.Int32: PickleReader.read_int,
PickleType.UInt32: PickleReader.read_uint,
PickleType.Int64: PickleReader.read_long,
PickleType.UInt64: PickleReader.read_ulong,
PickleType.Single: PickleReader.read_single,
PickleType.Double: PickleReader.read_double,
PickleType.Blob: PickleReader.read_blob,
PickleType.String: PickleReader.read_str,
PickleType.String16: PickleReader.read_str16_with_char_count,
PickleType.DateTime: PickleReader.read_timestamp,
PickleType.Pickle: PickleReader.read_pickle,
PickleType.String16ByteCount: PickleReader.read_str16_with_byte_count}