-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwinmem_decompress.py
executable file
·262 lines (196 loc) · 6.27 KB
/
winmem_decompress.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
#!/usr/bin/env python3
# (c) Maxim Suhanov
import os
import sys
import time
import io
import struct
import multiprocessing
PROGRAM_VERSION = '20180925'
PARALLEL_TASKS = 4 # The number of decompression tasks to run in parallel.
PAGE_SIZE = 4096
COMPRESSED_DATA_CHUNK_SIZE = 16
DECOMPRESSED_DATA_SIZE_MIN = 1024 # Ignore decompressed data chunks which are smaller than this value.
def LZ77DecompressBuffer(Buffer):
"""Decompress data from Buffer using the plain LZ77 algorithm, return the decompressed data."""
def is_valid_write_request(offset):
return offset < 2*1024*1024*1024 # Reject obviously invalid write requests.
OutputObject = io.BytesIO()
BufferedFlags = 0
BufferedFlagCount = 0
InputPosition = 0
OutputPosition = 0
LastLengthHalfByte = 0
while True:
if BufferedFlagCount == 0:
BufferedFlags = Buffer[InputPosition : InputPosition + 4]
if len(BufferedFlags) != 4:
# Bogus data.
break
BufferedFlags, = struct.unpack('<L', BufferedFlags)
InputPosition += 4
BufferedFlagCount = 32
BufferedFlagCount -= 1
if BufferedFlags & (1 << BufferedFlagCount) == 0:
try:
OneByte = Buffer[InputPosition]
except IndexError:
# Bogus data.
break
if type(OneByte) is not int:
OneByte = ord(OneByte)
OneByte = bytearray([OneByte])
if is_valid_write_request(OutputPosition):
OutputObject.seek(OutputPosition)
OutputObject.write(OneByte)
else:
# Bogus data.
break
InputPosition += 1
OutputPosition += 1
else:
if InputPosition == len(Buffer):
# We are done.
OutputBuffer = OutputObject.getvalue()
OutputObject.close()
return OutputBuffer
MatchBytes = Buffer[InputPosition : InputPosition + 2]
if len(MatchBytes) != 2:
# Bogus data.
break
MatchBytes, = struct.unpack('<H', MatchBytes)
InputPosition += 2
MatchLength = MatchBytes % 8
MatchOffset = (MatchBytes // 8) + 1
if MatchLength == 7:
if LastLengthHalfByte == 0:
try:
MatchLength = Buffer[InputPosition]
except IndexError:
# Bogus data.
break
if type(MatchLength) is not int:
MatchLength = ord(MatchLength)
MatchLength = MatchLength % 16
LastLengthHalfByte = InputPosition
InputPosition += 1
else:
try:
MatchLength = Buffer[LastLengthHalfByte]
except IndexError:
# Bogus data.
break
if type(MatchLength) is not int:
MatchLength = ord(MatchLength)
MatchLength = MatchLength // 16
LastLengthHalfByte = 0
if MatchLength == 15:
try:
MatchLength = Buffer[InputPosition]
except IndexError:
# Bogus data.
break
if type(MatchLength) is not int:
MatchLength = ord(MatchLength)
InputPosition += 1
if MatchLength == 255:
MatchLength = Buffer[InputPosition : InputPosition + 2]
if len(MatchLength) != 2:
# Bogus data.
break
MatchLength, = struct.unpack('<H', MatchLength)
InputPosition += 2
if MatchLength < 15 + 7:
# Bogus data.
break
MatchLength -= (15 + 7)
MatchLength += 15
MatchLength += 7
MatchLength += 3
bogus_data = False
for i in range(0, MatchLength):
if OutputPosition - MatchOffset < 0:
# Bogus data.
bogus_data = True
break
OutputObject.seek(OutputPosition - MatchOffset)
OneByte = OutputObject.read(1)
if len(OneByte) != 1:
# Bogus data.
bogus_data = True
break
if is_valid_write_request(OutputPosition):
OutputObject.seek(OutputPosition)
OutputObject.write(OneByte)
else:
# Bogus data.
bogus_data = True
break
OutputPosition += 1
if bogus_data:
break
# We are done (but data is bogus).
OutputBuffer = OutputObject.getvalue()
OutputObject.close()
return OutputBuffer
def ScanBuffer(Buffer):
"""Scan Buffer for compressed data chunks, yield every decompressed data chunk."""
global pool
null_bytes_12 = b'\x00' * 12
compressed_data_to_process = []
pos = 0
while pos < len(Buffer):
compressed_data = Buffer[pos : pos + PAGE_SIZE]
if not compressed_data.startswith(null_bytes_12): # Check if data starts with many null bytes.
compressed_data_to_process.append(compressed_data)
pos += COMPRESSED_DATA_CHUNK_SIZE # Compressed memory pages are stored in chunks.
for decompressed_data in pool.imap_unordered(LZ77DecompressBuffer, compressed_data_to_process, chunksize = 8):
if len(decompressed_data) >= DECOMPRESSED_DATA_SIZE_MIN:
if len(decompressed_data) > PAGE_SIZE:
# Remove garbage after the decompressed page.
decompressed_data = decompressed_data[ : PAGE_SIZE]
elif len(decompressed_data) < PAGE_SIZE:
# Add padding bytes to keep the output aligned.
padding_length = PAGE_SIZE - len(decompressed_data)
decompressed_data += b'\x00' * padding_length
yield decompressed_data
def ScanFile(FilePath):
"""Scan a given file for compressed data chunks, yield every decompressed data chunk."""
read_chunk_size = 32 * PAGE_SIZE
with open(FilePath, 'rb') as file_obj:
file_obj.seek(0, 2)
file_size = file_obj.tell()
file_pos = 0
while file_pos < file_size:
file_obj.seek(file_pos)
buf = file_obj.read(read_chunk_size)
for data in ScanBuffer(buf):
yield data
if len(buf) != read_chunk_size:
# End of a file or a read error.
break
file_pos += read_chunk_size
def PrintUsage():
"""Print the usage information."""
print('winmem_decompress, version: {}'.format(PROGRAM_VERSION), file = sys.stderr)
print('', file = sys.stderr)
print('This program tries to extract compressed memory pages from page-aligned data.', file = sys.stderr)
print('Every decompressed page is written to the standard output.', file = sys.stderr)
print('', file = sys.stderr)
print('Usage: {} <input file>'.format(sys.argv[0]), file = sys.stderr)
if __name__ == '__main__':
if len(sys.argv) != 2:
PrintUsage()
sys.exit(1)
file_path = sys.argv[1]
if not os.path.isfile(file_path):
print('File doesn\'t exist: {}'.format(file_path), file = sys.stderr)
sys.exit(1)
pool = multiprocessing.Pool(processes = PARALLEL_TASKS)
time_start = round(time.time())
for data in ScanFile(file_path):
sys.stdout.buffer.write(data)
pool.close()
pool.join()
time_end = round(time.time())
print('Processing done in {} seconds.'.format(time_end - time_start), file = sys.stderr)