-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswsusp2bin.cpp
513 lines (410 loc) · 15.3 KB
/
swsusp2bin.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include "swsusp2bin.h"
typedef struct _parameters_t {
char *swapfile;
char *memfile;
} parameters_t;
void *swsusp_header = NULL;
struct swsusp_header32 *swsusp_header32 = NULL;
struct swsusp_header64 *swsusp_header64 = NULL;
uint64_t last_highmem_page = 0;
bool no_compress_mode = false;
bool verbose_mode = false;
bool mode_32 = false;
void
help() {
printf(" --in swapfile\n");
printf(" --out output file. extracted memory view. (optional)\n");
printf(" --verbose display debug messages\n");
printf(" -32 32-bits mode. (default = 64-bits)\n");
}
bool
parse(int argc,
char **argv,
parameters_t *out) {
bool status = false;
for (int i = 0; i < argc; i++) {
string keyword = argv[i];
if ((keyword == "--in") && ((i + 1) < argc)) {
out->swapfile = argv[++i];
status = true;
}
else if ((keyword == "--out") && ((i + 1) < argc)) {
out->memfile = argv[++i];
}
else if ((keyword == "--verbose")) {
verbose_mode = true;
}
else if ((keyword == "-32")) {
mode_32 = true;
}
}
return status;
}
int
get_sector_size() {
return mode_32 ? sizeof(uint32_t) : sizeof(uint64_t);
}
int
get_max_page_entries() {
return (PAGE_SIZE / get_sector_size()) - 1;
}
uint64_t
get_page_entry_by_index(
void *table,
int index
) {
if (!table) return 0;
swap_map_page32 *table32 = NULL;
swap_map_page64 *table64 = NULL;
if (index >= get_max_page_entries()) return 0;
table32 = (swap_map_page32 *)table;
table64 = (swap_map_page64 *)table;
return mode_32 ? table32->entries[index] : table64->entries[index];
}
uint64_t
get_page_map_next_swap(
void *table
) {
if (!table) return 0;
swap_map_page32 *table32 = NULL;
swap_map_page64 *table64 = NULL;
table32 = (swap_map_page32 *)table;
table64 = (swap_map_page64 *)table;
return mode_32 ? table32->next_swap : table64->next_swap;
}
bool
get_compress_mode()
{
if (swsusp_header32) no_compress_mode = !!(swsusp_header32->flags & SF_NOCOMPRESS_MODE);
else if (swsusp_header64) no_compress_mode = !!(swsusp_header64->flags & SF_NOCOMPRESS_MODE);
else false;
return no_compress_mode;
}
void
print_header() {
if (!swsusp_header32 && !swsusp_header64) return;
if (!mode_32) {
char signature[10 + 1] = { 0 };
memcpy_s(signature, sizeof(signature), swsusp_header64->orig_sig, sizeof(swsusp_header64->orig_sig));
printf("swsusp_header.orig_sig = %s\n", signature);
memset(signature, 0, sizeof(signature));
memcpy_s(signature, sizeof(signature), swsusp_header64->sig, sizeof(swsusp_header64->sig));
printf("swsusp_header.sig = %s\n", signature);
printf("swsusp_header.image = %lx\n", swsusp_header64->image);
printf("swsusp_header.flags = %x\n", swsusp_header64->flags);
printf("swsusp_header.flags[SF_PLATFORM_MODE] = %s\n", swsusp_header64->flags & SF_PLATFORM_MODE ? "TRUE" : "FALSE");
printf("swsusp_header.flags[SF_NOCOMPRESS_MODE] = %s\n", swsusp_header64->flags & SF_NOCOMPRESS_MODE ? "TRUE" : "FALSE");
printf("swsusp_header.flags[SF_CRC32_MODE] = %s\n", swsusp_header64->flags & SF_CRC32_MODE ? "TRUE" : "FALSE");
if (swsusp_header64->flags & SF_CRC32_MODE) printf("swsusp_header.crc32 = 0x%x\n", swsusp_header64->crc32);
}
else {
char signature[10 + 1] = { 0 };
memcpy_s(signature, sizeof(signature), swsusp_header32->orig_sig, sizeof(swsusp_header32->orig_sig));
printf("swsusp_header.orig_sig = %s\n", signature);
memset(signature, 0, sizeof(signature));
memcpy_s(signature, sizeof(signature), swsusp_header32->sig, sizeof(swsusp_header32->sig));
printf("swsusp_header.sig = %s\n", signature);
printf("swsusp_header.image = %x\n", swsusp_header32->image);
printf("swsusp_header.flags = %x\n", swsusp_header32->flags);
printf("swsusp_header.flags[SF_PLATFORM_MODE] = %s\n", swsusp_header32->flags & SF_PLATFORM_MODE ? "TRUE" : "FALSE");
printf("swsusp_header.flags[SF_NOCOMPRESS_MODE] = %s\n", swsusp_header32->flags & SF_NOCOMPRESS_MODE ? "TRUE" : "FALSE");
printf("swsusp_header.flags[SF_CRC32_MODE] = %s\n", swsusp_header32->flags & SF_CRC32_MODE ? "TRUE" : "FALSE");
if (swsusp_header32->flags & SF_CRC32_MODE) printf("swsusp_header.crc32 = 0x%x\n", swsusp_header32->crc32);
}
}
bool
is_swapfile_valid()
{
char signature[10 + 1] = { 0 };
memset(signature, 0, sizeof(signature));
if (swsusp_header32) memcpy_s(signature, sizeof(signature), swsusp_header32->sig, sizeof(swsusp_header32->sig));
else if (swsusp_header64) memcpy_s(signature, sizeof(signature), swsusp_header64->sig, sizeof(swsusp_header64->sig));
else false;
if (memcmp(HIBERNATE_SIG, signature, 10) == 0) {
printf("debug: valid swsusp file.\n");
}
else {
printf("error: invalid swsusp file. Swap header not found!\n");
return false;
}
return true;
}
uint64_t
get_map_table_offset(
)
{
uint64_t map_table_offset = 0;
if (swsusp_header32) map_table_offset = swsusp_header32->image * PAGE_SIZE;
else if (swsusp_header64) map_table_offset = swsusp_header64->image * PAGE_SIZE;
return map_table_offset;
}
uint64_t
get_last_highmem_page(
FILE *handle
) {
swap_map_page *map_page = NULL;
size_t result = 0;
uint64_t map_table_offset = 0;
if (!swsusp_header32 && !swsusp_header64) return 0;
if (last_highmem_page) return last_highmem_page;
map_table_offset = get_map_table_offset();
if (!map_table_offset) goto cleanup;
map_page = (struct swap_map_page *)malloc(PAGE_SIZE);
if (map_page == NULL) goto cleanup;
while (map_table_offset) {
if (fseek(handle, (long)map_table_offset, SEEK_SET)) {
printf("%s: error: can't change the offset.\n", __FUNCTION__);
goto cleanup;
}
result = fread(map_page, 1, PAGE_SIZE, handle);
if (result != PAGE_SIZE) {
printf("%s: error: can't read page table (result = 0x%lx, map_table_offset = 0x%lx).\n", __FUNCTION__, result, (long)map_table_offset);
goto cleanup;
}
int i = 0;
while (get_page_entry_by_index(map_page, i) && (i < MAP_PAGE_ENTRIES)) {
uint64_t page_offset = get_page_entry_by_index(map_page, i) * PAGE_SIZE;
if (page_offset > last_highmem_page) last_highmem_page = page_offset;
i++;
}
map_table_offset = map_page->next_swap * PAGE_SIZE;
if (!map_page->next_swap) break;
}
cleanup:
if (map_page) free(map_page);
return last_highmem_page;
}
bool
read_file_at(
FILE *file,
uint64_t offset,
void *buffer,
uint32_t buffer_size
)
{
size_t size = PAGE_SIZE;
size_t result = 0;
bool status = false;
if (buffer_size) size = buffer_size;
if (fseek(file, (long)offset, SEEK_SET)) {
printf("error: can't change the offset to 0x%lx.\n", offset);
goto cleanup;
}
result = fread(buffer, 1, size, file);
if (result != size) {
printf("error: can't read data at 0x%lx. result = 0x%lx\n", offset, result);
goto cleanup;
}
status = true;
cleanup:
return status;
}
bool
write_file_at(
FILE *file,
uint64_t offset,
void *buffer,
uint32_t buffer_size
)
{
size_t size = PAGE_SIZE;
size_t result = 0;
bool status = false;
if (buffer_size) size = buffer_size;
if (fseek(file, (long)offset, SEEK_SET)) {
printf("error: can't change the offset to 0x%lx.\n", offset);
goto cleanup;
}
result = fwrite(buffer, 1, size, file);
if (result != size) {
printf("error: can't write data at 0x%lx. result = 0x%lx\n", offset, result);
goto cleanup;
}
if (verbose_mode) printf("debug: wrote data at 0x%lx.\n", offset);
status = true;
cleanup:
return status;
}
bool
readwrite(
FILE *in,
FILE *out,
uint64_t offset,
void *buffer,
uint32_t buffer_size
) {
bool status = false;
if (!out || !in || !buffer_size || !buffer) return false;
if (!read_file_at(in, offset, buffer, buffer_size)) {
printf("error: Can't read memory block.\n");
goto cleanup;
}
if (!write_file_at(out, offset, buffer, buffer_size)) {
printf("error: Can't write memory block.\n");
goto cleanup;
}
if (verbose_mode) printf("debug: wrote 0x%x bytes at %lx.\n", buffer_size, offset);
status = true;
cleanup:
return status;
}
bool
read_swsusp_header(
FILE *file
)
{
bool status = false;
swsusp_header = (void *)malloc(PAGE_SIZE);
if (!swsusp_header) {
printf("error: not enough memory..\n");
goto cleanup;
}
if (!read_file_at(file, 0, swsusp_header, PAGE_SIZE)) {
printf("error: can't read header.\n");
goto cleanup;
}
if (mode_32) swsusp_header32 = (struct swsusp_header32 *)swsusp_header;
else swsusp_header64 = (struct swsusp_header64 *)swsusp_header;
status = true;
cleanup:
return status;
}
int main(
int argc,
char **argv)
{
parameters_t params = { 0 };
char *buffer = NULL;
void *map_page = NULL;
FILE * file = NULL;
FILE * out = NULL;
uint64_t highmem = 0;
unsigned char *uncompressed_buffer = NULL;
unsigned char *compressed_buffer = NULL;
printf("swsusp2bin\n");
wprintf(L"\n"
L" swsusp2bin v1.0\n"
L" Copyright (C) 2017, Comae Technologies DMCC <http://www.comae.io>\n\n");
if (!parse(argc, argv, ¶ms)) {
help();
goto cleanup;
}
printf("analyzing %s...\n", params.swapfile);
file = fopen(params.swapfile, "rb");
if (file == NULL) {
printf("error: can't open file.\n");
goto cleanup;
}
if (params.memfile) {
out = fopen(params.memfile, "wb");
if (out == NULL) {
printf("error: can't create file.\n");
goto cleanup;
}
}
if (!read_swsusp_header(file)) goto cleanup;
get_compress_mode();
print_header();
if (!is_swapfile_valid()) goto cleanup;
highmem = get_last_highmem_page(file);
uint64_t map_table_offset;
map_table_offset = get_map_table_offset();
if (verbose_mode) printf("image: 0x%lx\n", map_table_offset);
map_page = (void *)malloc(PAGE_SIZE);
if (map_page == NULL) goto cleanup;
buffer = (char *)malloc(PAGE_SIZE);
if (!buffer) goto cleanup;
if (out) {
uncompressed_buffer = (unsigned char *)malloc(LZO_UNC_SIZE);
if (!uncompressed_buffer) goto cleanup;
compressed_buffer = (unsigned char *)malloc(LZO_UNC_SIZE);
if (!compressed_buffer) goto cleanup;
}
int pages_count;
pages_count = 0;
int err_pages_count;
err_pages_count = 0;
while (map_table_offset) {
if (!read_file_at(file, map_table_offset, map_page, PAGE_SIZE)) {
printf("error: Can't read page table (map_table_offset = 0x%lx).\n", (long)map_table_offset);
goto cleanup;
}
int i = 0;
while (get_page_entry_by_index(map_page, i) && (i < get_max_page_entries())) {
uint64_t map_page_entry = get_page_entry_by_index(map_page, i);
if (verbose_mode) printf("[0x%lx][%4d] = 0x%lx / 0x%lx.\n", map_table_offset, i, map_page_entry, map_page_entry * PAGE_SIZE);
uint64_t compressed_size = 0;
uint64_t page_offset = map_page_entry * PAGE_SIZE;
if (!no_compress_mode) {
if (!read_file_at(file, page_offset, &compressed_size, get_sector_size())) {
printf("error: Can't read compressed size in the block header.\n");
goto cleanup;
}
if (verbose_mode) printf("debug: compressed buffer size = 0x%lx\n", compressed_size);
if (!compressed_size || (compressed_size > lzo1x_worst_compress(LZO_UNC_SIZE))) {
if (verbose_mode) printf("error: Invalid LZO compressed length (worse: 0x%x)\n", lzo1x_worst_compress(LZO_UNC_SIZE));
err_pages_count++;
if (out) {
readwrite(file, out, page_offset, buffer, PAGE_SIZE);
}
i += 1;
pages_count++;
} else {
int compressed_size_in_pages = (int)DIV_ROUND_UP(compressed_size, PAGE_SIZE);
if (verbose_mode) printf("skip %d pages...\n", compressed_size_in_pages);
if (out) {
if (!read_file_at(file, page_offset + get_sector_size(), compressed_buffer, (uint32_t)compressed_size)) {
printf("error: Can't read compressed block.\n");
goto cleanup;
}
size_t ucmp_len = LZO_UNC_SIZE;
memset(uncompressed_buffer, 0, ucmp_len);
if (lzo1x_decompress_safe(compressed_buffer, (size_t)compressed_size, uncompressed_buffer, &ucmp_len) != LZO_E_OK) {
if (verbose_mode) printf("%s: decompression failed.\n", __FUNCTION__);
// page isn't compressed. we force write it.
readwrite(file, out, page_offset, buffer, PAGE_SIZE);
}
else {
for (int j = 0; j < compressed_size_in_pages; j += 1) {
uint64_t dst_offset = get_page_entry_by_index(map_page, i + j) * PAGE_SIZE;
if (!write_file_at(out, dst_offset, uncompressed_buffer + (j + PAGE_SIZE), PAGE_SIZE)) {
printf("error: Can't write uncompressed block.\n");
goto cleanup;
}
}
}
}
i += compressed_size_in_pages;
pages_count += LZO_UNC_PAGES;
continue;
}
} else {
if (out) {
readwrite(file, out, page_offset, buffer, PAGE_SIZE);
}
i += 1;
pages_count += 1;
}
}
uint64_t next_swap = get_page_map_next_swap(map_page);
if (verbose_mode) printf("map_page->next_swap = 0x%lx\n", next_swap);
if (next_swap) {
if (verbose_mode) printf("gap between the two page map table is %d pages\n", (uint32_t)(next_swap - (map_table_offset / PAGE_SIZE)));
else printf(".");
}
map_table_offset = next_swap * PAGE_SIZE;
}
printf("\n");
printf("debug: highmem = 0x%lx (%d MB)\n", highmem, (int)(highmem / (1024 * 1024)));
printf("debug: total page count = 0x%x (%d MB)\n", pages_count, (pages_count * PAGE_SIZE) / (1024 * 1024));
printf("debug: total non compressed pages = 0x%x (%d MB)\n", err_pages_count, (err_pages_count * PAGE_SIZE) / (1024 * 1024));
cleanup:
if (uncompressed_buffer) free(uncompressed_buffer);
if (compressed_buffer) free(compressed_buffer);
if (swsusp_header) free(swsusp_header);
if (map_page) free(map_page);
if (buffer) free(buffer);
if (out) fclose(out);
if (file) fclose(file);
return true;
}