This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
wintools.c
850 lines (684 loc) · 23.1 KB
/
wintools.c
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
#include "wintools.h"
#include "mem.h"
#include "pmparser.h"
#include <errno.h>
#include <string.h>
#ifdef KMOD_MEMMAP
#include "kmem.h"
#include <sys/mman.h>
#include <sys/ioctl.h>
#endif
#if (LMODE() != MODE_QEMU_INJECT())
#include <sys/types.h>
#include <dirent.h>
#endif
char* strdup(const char*);
static int CheckLow(const WinCtx* ctx, uint64_t* pml4, uint64_t* kernelEntry);
static void FindNTKernel(WinCtx* ctx, uint64_t kernelEntry);
static uint16_t GetNTVersion(const WinCtx* ctx);
static uint32_t GetNTBuild(const WinCtx* ctx);
static int SetupOffsets(WinCtx* ctx);
static WinModule GetBaseModule(const WinCtx* ctx, const WinProc* process);
static void FillAnyModuleList64(const WinCtx* ctx, const WinProc* process, WinModuleList* list, size_t* maxSize, uint64_t head, int inMemoryOrder);
static void FillModuleList64(const WinCtx* ctx, const WinProc* process, WinModuleList* list, size_t* maxSize, char* x86);
static void FillModuleList32(const WinCtx* ctx, const WinProc* process, WinModuleList* list, size_t* maxSize);
extern uint64_t KFIXC;
extern uint64_t KFIXO;
FILE* vmread_dfile = NULL;
#ifndef HEADER_SIZE
#define HEADER_SIZE 0x1000
#endif
#if (LMODE() != MODE_QEMU_INJECT())
static int RecursFind(const char* path, int level) {
if (level > 2)
return 0;
DIR* dir;
struct dirent* entry;
int ret = 0;
if (!(dir = opendir(path)))
return 0;
while (!ret && (entry = readdir(dir))) {
char npath[512];
if (entry->d_type == DT_DIR && ((level == 1 && entry->d_name[0] == 'f') ||
(entry->d_name[0] >= '0' && entry->d_name[0] <= '9'))) {
snprintf(npath, sizeof(npath), "%s/%s", path, entry->d_name);
ret = RecursFind(npath, level + 1);
} else if (level >= 2) {
snprintf(npath, sizeof(npath), "%s/%s", path, entry->d_name);
uint64_t dirv = 0;
const uint64_t kvm = *(uint64_t*)(void*)"/dev/kvm";
if (readlink(npath, (char*)&dirv, 8) == 8 && dirv == kvm)
sscanf(path, "/proc/%d/", &ret);
}
}
closedir(dir);
return ret;
}
#endif
static pid_t FindKVMProcess()
{
#if (LMODE() == MODE_QEMU_INJECT())
return getpid();
#else
pid_t ret = RecursFind("/proc", 0);
return ret > 0 ? ret : 0;
#endif
}
int InitializeContext(WinCtx* ctx, pid_t pid)
{
memset(ctx, 0, sizeof(WinCtx));
uint64_t pml4, kernelEntry;
if (pid == 0 && (pid = FindKVMProcess()) == 0)
return -1;
procmaps_struct* maps = pmparser_parse(pid);
if (!maps)
return 1;
procmaps_struct* tempMaps = NULL;
procmaps_struct* largestMaps = NULL;
while ((tempMaps = pmparser_next()) != NULL)
if (!largestMaps || tempMaps->length > largestMaps->length)
largestMaps = tempMaps;
if (!largestMaps)
return 2;
ctx->process.pid = pid;
ctx->process.mapsStart = (uint64_t)largestMaps->addr_start;
ctx->process.mapsSize = largestMaps->length;
pmparser_free(maps);
#ifdef KMOD_MEMMAP
MSG(2, "Mapping VM memory, this will take a second...\n");
int fd = open("/proc/vmread", O_RDWR);
if (fd != -1) {
int ret = ioctl(fd, VMREAD_IOCTL_MAPVMMEM, &ctx->process);
close(fd);
if (ret)
return 101;
} else
return 100;
#endif
MSG(2, "Mem:\t%lx\t| Size:\t%lx\n", ctx->process.mapsStart, ctx->process.mapsSize);
if (!CheckLow(ctx, &pml4, &kernelEntry))
return 3;
MSG(2, "PML4:\t%lx\t| KernelEntry:\t%lx\n", pml4, kernelEntry);
ctx->initialProcess.dirBase = pml4;
FindNTKernel(ctx, kernelEntry);
if (!ctx->ntKernel) {
/* Test in case we are running XP (QEMU AddressSpace is different) */
#if (LMODE() != MODE_DMA())
KFIXC = 0x40000000ll * 4;
KFIXO = 0x40000000;
FindNTKernel(ctx, kernelEntry);
#endif
if (!ctx->ntKernel)
return 4;
}
MSG(2, "Kernel Base:\t%lx (%lx)\n", ctx->ntKernel, VTranslate(&ctx->process, ctx->initialProcess.dirBase, ctx->ntKernel));
uint64_t initialSystemProcess = FindProcAddress(ctx->ntExports, "PsInitialSystemProcess");
if (!initialSystemProcess)
return 6;
MSG(2, "PsInitialSystemProcess:\t%lx (%lx)\n", initialSystemProcess, VTranslate(&ctx->process, ctx->initialProcess.dirBase, initialSystemProcess));
VMemRead(&ctx->process, ctx->initialProcess.dirBase, (uint64_t)&ctx->initialProcess.process, initialSystemProcess, sizeof(uint64_t));
if (!ctx->initialProcess.process)
return 7;
ctx->initialProcess.physProcess = VTranslate(&ctx->process, ctx->initialProcess.dirBase, ctx->initialProcess.process);
MSG(2, "System (PID 4):\t%lx (%lx)\n", ctx->initialProcess.process, ctx->initialProcess.physProcess);
ctx->ntVersion = GetNTVersion(ctx);
if (!ctx->ntVersion)
return 8;
MSG(2, "NT Version:\t%hu\n", ctx->ntVersion);
ctx->ntBuild = GetNTBuild(ctx);
if (!ctx->ntBuild)
return 8;
MSG(2, "NT Build:\t%u\n", ctx->ntBuild);
if (SetupOffsets(ctx))
return 9;
return 0;
}
int FreeContext(WinCtx* ctx)
{
FreeExportList(ctx->ntExports);
return 0;
}
IMAGE_NT_HEADERS* GetNTHeader(const WinCtx* ctx, const WinProc* process, uint64_t address, uint8_t* header, uint8_t* is64Bit)
{
if (VMemRead(&ctx->process, process->dirBase, (uint64_t)header, address, HEADER_SIZE) == -1)
return NULL;
//TODO: Allow the compiler to properly handle alignment
IMAGE_DOS_HEADER* dosHeader = (IMAGE_DOS_HEADER*)(void*)header;
if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE)
return NULL;
IMAGE_NT_HEADERS* ntHeader = (IMAGE_NT_HEADERS*)(void*)(header + dosHeader->e_lfanew);
if ((uint8_t*)ntHeader - header > HEADER_SIZE - 0x200 || ntHeader->Signature != IMAGE_NT_SIGNATURE)
return NULL;
if(ntHeader->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC && ntHeader->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC)
return NULL;
*is64Bit = ntHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC;
return ntHeader;
}
int ParseExportTable(const WinCtx* ctx, const WinProc* process, uint64_t moduleBase, IMAGE_DATA_DIRECTORY* exports, WinExportList* outList)
{
if (exports->Size < sizeof(IMAGE_EXPORT_DIRECTORY) || exports->Size > 0x7fffff || exports->VirtualAddress == moduleBase)
return 1;
char* buf = (char*)malloc(exports->Size + 1);
IMAGE_EXPORT_DIRECTORY* exportDir = (IMAGE_EXPORT_DIRECTORY*)(void*)buf;
if (VMemRead(&ctx->process, process->dirBase, (uint64_t)buf, moduleBase + exports->VirtualAddress, exports->Size) == -1) {
free(buf);
return 2;
}
buf[exports->Size] = 0;
if (!exportDir->NumberOfNames || !exportDir->AddressOfNames) {
free(buf);
return 3;
}
uint32_t exportOffset = exports->VirtualAddress;
uint32_t* names = (uint32_t*)(void*)(buf + exportDir->AddressOfNames - exportOffset);
if (exportDir->AddressOfNames - exportOffset + exportDir->NumberOfNames * sizeof(uint32_t) > exports->Size) {
free(buf);
return 4;
}
uint16_t* ordinals = (uint16_t*)(void*)(buf + exportDir->AddressOfNameOrdinals - exportOffset);
if (exportDir->AddressOfNameOrdinals - exportOffset + exportDir->NumberOfNames * sizeof(uint16_t) > exports->Size) {
free(buf);
return 5;
}
uint32_t* functions = (uint32_t*)(void*)(buf + exportDir->AddressOfFunctions - exportOffset);
if (exportDir->AddressOfFunctions - exportOffset + exportDir->NumberOfFunctions * sizeof(uint32_t) > exports->Size) {
free(buf);
return 6;
}
outList->size = exportDir->NumberOfNames;
outList->list = (WinExport*)malloc(sizeof(WinExport) * outList->size);
size_t sz = 0;
for (uint32_t i = 0; i < exportDir->NumberOfNames; i++) {
if (names[i] > exports->Size + exportOffset || names[i] < exportOffset || ordinals[i] > exportDir->NumberOfNames)
continue;
outList->list[sz].name = strdup(buf + names[i] - exportOffset);
outList->list[sz].address = moduleBase + functions[ordinals[i]];
sz++;
}
outList->size = sz;
free(buf);
return 0;
}
int GenerateExportList(const WinCtx* ctx, const WinProc* process, uint64_t moduleBase, WinExportList* outList)
{
uint8_t is64 = 0;
uint8_t headerBuf[HEADER_SIZE];
int ret = 0;
IMAGE_NT_HEADERS64* ntHeader64 = GetNTHeader(ctx, process, moduleBase, headerBuf, &is64);
if (!ntHeader64)
return -1;
IMAGE_NT_HEADERS32* ntHeader32 = (IMAGE_NT_HEADERS32*)ntHeader64;
IMAGE_DATA_DIRECTORY* exportTable = NULL;
if (is64)
exportTable = ntHeader64->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_EXPORT;
else
exportTable = ntHeader32->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_EXPORT;
ret = ParseExportTable(ctx, process, moduleBase, exportTable, outList);
return ret;
}
void FreeExportList(WinExportList list)
{
if (!list.list)
return;
for (uint32_t i = 0; i < list.size; i++)
free((char*)list.list[i].name);
free(list.list);
list.list = NULL;
}
/* TODO: do without the table parsing, we are just wasting a lot of string duplication etc. */
uint64_t GetProcAddress(const WinCtx* ctx, const WinProc* process, uint64_t module, const char* procName)
{
WinExportList exports;
if (GenerateExportList(ctx, process, module, &exports))
return 1;
uint64_t ret = FindProcAddress(exports, procName);
FreeExportList(exports);
return ret;
}
uint64_t FindProcAddress(const WinExportList exports, const char* procName)
{
for (uint32_t i = 0; i < exports.size; i++)
if (!strcmp(procName, exports.list[i].name))
return exports.list[i].address;
return 0;
}
WinProcList GenerateProcessList(const WinCtx* ctx)
{
WinProcList list;
uint64_t curProc = ctx->initialProcess.physProcess;
uint64_t virtProcess = ctx->initialProcess.process;
list.list = (WinProc*)malloc(sizeof(WinProc) * 25);
list.size = 0;
size_t maxSize = 25;
while (!list.size || (curProc != ctx->initialProcess.physProcess && virtProcess != ctx->initialProcess.process)) {
uint64_t stackCount = MemReadU64(&ctx->process, curProc + ctx->offsets.stackCount);
uint64_t dirBase = MemReadU64(&ctx->process, curProc + ctx->offsets.dirBase);
uint64_t pid = MemReadU64(&ctx->process, curProc + ctx->offsets.apl - 8);
/* The end of the process list usually has corrupted values, some sort of address, and we avoid the issue by checking the PID (which shouldn't be over 32 bit limit anyways) */
if (pid < 1u << 31 && stackCount) {
list.list[list.size] = (WinProc){
.process = virtProcess,
.physProcess = curProc,
.dirBase = dirBase,
.pid = pid,
};
WinModule baseMod = GetBaseModule(ctx, &list.list[list.size]);
if (baseMod.name)
list.list[list.size].name = baseMod.name;
else {
list.list[list.size].name = (char*)malloc(16);
MemRead(&ctx->process, (uint64_t)list.list[list.size].name, curProc + ctx->offsets.imageFileName, 15);
list.list[list.size].name[15] = '\0';
}
list.size++;
if (list.size > 1000 || pid == 0)
break;
}
if (list.size >= maxSize) {
maxSize = list.size * 2;
WinProc* newProc = (WinProc*)realloc(list.list, sizeof(WinProc) * maxSize);
if (!newProc)
break;
list.list = newProc;
}
virtProcess = MemReadU64(&ctx->process, curProc + ctx->offsets.apl) - ctx->offsets.apl;
if (!virtProcess)
break;
curProc = VTranslate(&ctx->process, dirBase, virtProcess);
if (!curProc)
break;
}
return list;
}
void FreeProcessList(WinProcList list)
{
size_t i;
if (list.list) {
for (i = 0; i < list.size; i++)
free(list.list[i].name);
free(list.list);
}
}
WinModuleList GenerateModuleList(const WinCtx* ctx, const WinProc* process)
{
WinModuleList list;
list.size = 0;
list.list = (WinModule*)malloc(sizeof(WinModule) * 25);
size_t maxSize = 25;
char x86 = 0;
FillModuleList64(ctx, process, &list, &maxSize, &x86);
if (x86)
FillModuleList32(ctx, process, &list, &maxSize);
return list;
}
WinModuleList GenerateKernelModuleList(const WinCtx* ctx)
{
WinModuleList list;
list.size = 0;
list.list = (WinModule*)malloc(sizeof(WinModule) * 25);
size_t maxSize = 25;
uint64_t psLoadedModuleList = FindProcAddress(ctx->ntExports, "PsLoadedModuleList");
FillAnyModuleList64(ctx, &ctx->initialProcess, &list, &maxSize, psLoadedModuleList, 0);
return list;
}
void FreeModuleList(WinModuleList list)
{
if (!list.list)
return;
for (size_t i = 0; i < list.size; i++)
free(list.list[i].name);
free(list.list);
list.list = NULL;
}
const WinModule* GetModuleInfo(const WinModuleList list, const char* moduleName)
{
for (size_t i = 0; i < list.size; i++)
if (!strcmp(list.list[i].name, moduleName))
return list.list + i;
return NULL;
}
PEB GetPeb(const WinCtx* ctx, const WinProc* process)
{
PEB peb;
uint64_t ppeb = MemReadU64(&ctx->process, process->physProcess + ctx->offsets.peb);
VMemRead(&ctx->process, process->dirBase, (uint64_t)&peb, ppeb, sizeof(PEB));
return peb;
}
PEB32 GetPeb32(const WinCtx* ctx, const WinProc* process)
{
PEB32 peb;
uint64_t ethread = MemReadU64(&ctx->process, process->physProcess + ctx->offsets.threadListHead) - ctx->offsets.threadListEntry;
uint64_t teb = VMemReadU64(&ctx->process, process->dirBase, ethread + ctx->offsets.teb) + 0x2000;
uint32_t ppeb;
VMemRead(&ctx->process, process->dirBase, (uint64_t)&ppeb, teb + ctx->offsets.peb32, sizeof(ppeb));
VMemRead(&ctx->process, process->dirBase, (uint64_t)&peb, ppeb, sizeof(PEB32));
return peb;
}
/*
The low stub (if exists), contains PML4 (kernel DirBase) and KernelEntry point.
Credits: PCILeech
*/
static int CheckLow(const WinCtx* ctx, uint64_t* pml4, uint64_t* kernelEntry)
{
int i, o;
char buf[0x10000];
for (i = 0; i < 10; i++) {
MemRead(&ctx->process, (uint64_t)buf, i * 0x10000, 0x10000);
for (o = 0; o < 0x10000; o += 0x1000) {
if(0x00000001000600E9 ^ (0xffffffffffff00ff & *(uint64_t*)(void*)(buf + o)))
continue;
if(0xfffff80000000000 ^ (0xfffff80000000000 & *(uint64_t*)(void*)(buf + o + 0x70)))
continue;
if(0xffffff0000000fff & *(uint64_t*)(void*)(buf + o + 0xa0))
continue;
*pml4 = *(uint64_t*)(void*)(buf + o + 0xa0);
*kernelEntry = *(uint64_t*)(void*)(buf + o + 0x70);
return 1;
}
}
return 0;
}
static void FindNTKernel(WinCtx* ctx, uint64_t kernelEntry)
{
uint64_t i, o, p, u, mask = 0xfffff;
char buf[0x10000];
ctx->ntKernel = 0;
while (mask >= 0xfff) {
for (i = (kernelEntry & ~0x1fffff) + 0x20000000; i > kernelEntry - 0x20000000; i -= 0x200000) {
for (o = 0; o < 0x20; o++) {
VMemRead(&ctx->process, ctx->initialProcess.dirBase, (uint64_t)buf, i + 0x10000 * o, 0x10000);
for (p = 0; p < 0x10000; p += 0x1000) {
if (((i + 0x1000 * o + p) & mask) == 0 && *(short*)(void*)(buf + p) == IMAGE_DOS_SIGNATURE) {
int kdbg = 0, poolCode = 0;
for (u = 0; u < 0x1000; u++) {
kdbg = kdbg || *(uint64_t*)(void*)(buf + p + u) == 0x4742444b54494e49;
poolCode = poolCode || *(uint64_t*)(void*)(buf + p + u) == 0x45444f434c4f4f50;
if (kdbg & poolCode) {
ctx->ntKernel = i + 0x10000 * o + p;
if (GenerateExportList(ctx, &ctx->initialProcess, ctx->ntKernel, &ctx->ntExports)) {
ctx->ntKernel = 0;
break;
}
return;
}
}
}
}
}
}
mask = mask >> 4;
}
}
static uint16_t GetNTVersion(const WinCtx* ctx)
{
uint64_t getVersion = FindProcAddress(ctx->ntExports, "RtlGetVersion");
if (!getVersion)
return 0;
char buf[0x100];
VMemRead(&ctx->process, ctx->initialProcess.dirBase, (uint64_t)buf, getVersion, 0x100);
char major = 0, minor = 0;
/* Find writes to rcx +4 and +8 -- those are our major and minor versions */
for (char* b = buf; b - buf < 0xf0; b++) {
if (!major && !minor)
if (*(uint32_t*)(void*)b == 0x441c748)
return ((uint16_t)b[4]) * 100 + (b[5] & 0xf);
if (!major && (*(uint32_t*)(void*)b & 0xfffff) == 0x441c7)
major = b[3];
if (!minor && (*(uint32_t*)(void*)b & 0xfffff) == 0x841c7)
minor = b[3];
}
if (minor >= 100)
minor = 0;
return ((uint16_t)major) * 100 + minor;
}
static uint32_t GetNTBuild(const WinCtx* ctx)
{
uint64_t ntBuild = FindProcAddress(ctx->ntExports, "NtBuildNumber");
if (ntBuild) {
uint32_t build = 0;
VMemRead(&ctx->process, ctx->initialProcess.dirBase, (uint64_t)&build, ntBuild, sizeof(build));
if (build)
return build & 0xffffff;
}
uint64_t getVersion = FindProcAddress(ctx->ntExports, "RtlGetVersion");
if (!getVersion)
return 0;
char buf[0x100];
VMemRead(&ctx->process, ctx->initialProcess.dirBase, (uint64_t)buf, getVersion, 0x100);
/* Find writes to rcx +12 -- that's where the version number is stored. These instructions are not on XP, but that is simply irrelevant. */
for (char* b = buf; b - buf < 0xf0; b++) {
uint32_t val = *(uint32_t*)(void*)b & 0xffffff;
if (val == 0x0c41c7 || val == 0x05c01b)
return *(uint32_t*)(void*)(b + 3);
}
return 0;
}
static int SetupOffsets(WinCtx* ctx)
{
switch (ctx->ntVersion) {
case 502: /* XP SP2 */
ctx->offsets = (WinOffsets){
.apl = 0xe0,
.session = 0x260,
.stackCount = 0xa0,
.imageFileName = 0x268,
.dirBase = 0x28,
.peb = 0x2c0,
.peb32 = 0x30,
.threadListHead = 0x290,
.threadListEntry = 0x3d0,
.teb = 0xb0
};
break;
case 601: /* W7 */
ctx->offsets = (WinOffsets){
.apl = 0x188,
.session = 0x2d8,
.stackCount = 0xdc,
.imageFileName = 0x2e0,
.dirBase = 0x28,
.peb = 0x338,
.peb32 = 0x30,
.threadListHead = 0x300,
.threadListEntry = 0x420, /* 0x428 on later SP1 */
.teb = 0xb8
};
/* SP1 */
if (ctx->ntBuild == 7601)
ctx->offsets.imageFileName = 0x2d8;
break;
case 602: /* W8 */
ctx->offsets = (WinOffsets){
.apl = 0x2e8,
.session = 0x430,
.stackCount = 0x234,
.imageFileName = 0x438,
.dirBase = 0x28,
.peb = 0x338, /*peb will be wrong on Windows 8 and 8.1*/
.peb32 = 0x30,
.threadListHead = 0x470,
.threadListEntry = 0x400,
.teb = 0xf0
};
break;
case 603: /* W8.1 */
ctx->offsets = (WinOffsets){
.apl = 0x2e8,
.session = 0x430,
.stackCount = 0x234,
.imageFileName = 0x438,
.dirBase = 0x28,
.peb = 0x338,
.peb32 = 0x30,
.threadListHead = 0x470,
.threadListEntry = 0x688, /* 0x650 on previous builds */
.teb = 0xf0
};
break;
case 1000: /* W10 */
ctx->offsets = (WinOffsets){
.apl = 0x2e8,
.session = 0x448,
.stackCount = 0x23c,
.imageFileName = 0x450,
.dirBase = 0x28,
.peb = 0x3f8,
.peb32 = 0x30,
.threadListHead = 0x488,
.threadListEntry = 0x6a8,
.teb = 0xf0
};
if (ctx->ntBuild >= 18362) { /* Version 1903 or higher */
ctx->offsets.apl = 0x2f0;
ctx->offsets.threadListEntry = 0x6b8;
}
if (ctx->ntBuild >= 19041) { /* Version 2004 or higher */
ctx->offsets.apl = 0x448;
ctx->offsets.stackCount = 0x348;
ctx->offsets.imageFileName = 0x5a8;
ctx->offsets.peb = 0x550;
ctx->offsets.threadListHead = 0x5e0;
ctx->offsets.threadListEntry = 0x4e8;
}
break;
default:
return 1;
}
return 0;
}
static void FillModuleList64(const WinCtx* ctx, const WinProc* process, WinModuleList* list, size_t* maxSize, char* x86)
{
PEB peb = GetPeb(ctx, process);
PEB_LDR_DATA ldr;
memset(&ldr, 0, sizeof(ldr));
VMemRead(&ctx->process, process->dirBase, (uint64_t)&ldr, peb.Ldr, sizeof(ldr));
uint64_t head = ldr.InMemoryOrderModuleList.f_link;
size_t i = list->size ? list->size - 1 : list->size;
FillAnyModuleList64(ctx, process, list, maxSize, head, 1);
for (; i < list->size; i++) {
if (!strcmp(list->list[i].name, "wow64.dll")) {
*x86 = 1;
break;
}
}
}
static int FillModuleInfo64(const WinCtx* ctx, const WinProc* process, uint64_t* head, int inMemoryOrder, WinModule* modinfo)
{
const size_t NAME_LEN = 128;
wchar_t buf[128];
LDR_MODULE mod;
memset(&mod, 0, sizeof(mod));
VMemRead(&ctx->process, process->dirBase, (uint64_t)&mod, *head - sizeof(LIST_ENTRY) * inMemoryOrder, sizeof(mod));
VMemRead(&ctx->process, process->dirBase, (uint64_t)head, *head, sizeof(*head));
if (!mod.BaseDllName.length || !mod.SizeOfImage)
return 1;
/* Cap the module size */
if (mod.BaseDllName.length >= NAME_LEN)
mod.BaseDllName.length = NAME_LEN;
VMemRead(&ctx->process, process->dirBase, (uint64_t)buf, mod.BaseDllName.buffer, mod.BaseDllName.length * sizeof(wchar_t));
char* buf2 = (char*)malloc(mod.BaseDllName.length);
for (int i = 0; i < mod.BaseDllName.length; i++)
buf2[i] = ((char*)buf)[i*2];
buf2[mod.BaseDllName.length-1] = '\0';
if (*(short*)(void*)buf2 == 0x53) { /* 'S\0', a bit of magic, but it works */
free(buf2);
return 2;
}
modinfo->name = buf2;
modinfo->baseAddress = mod.BaseAddress;
modinfo->entryPoint = mod.EntryPoint;
modinfo->sizeOfModule = mod.SizeOfImage;
modinfo->loadCount = mod.LoadCount;
return 0;
}
static WinModule GetBaseModule(const WinCtx* ctx, const WinProc* process)
{
WinModule mod;
mod.name = NULL;
PEB peb = GetPeb(ctx, process);
PEB_LDR_DATA ldr;
memset(&ldr, 0, sizeof(ldr));
VMemRead(&ctx->process, process->dirBase, (uint64_t)&ldr, peb.Ldr, sizeof(ldr));
uint64_t head = ldr.InMemoryOrderModuleList.f_link;
uint64_t end = head;
uint64_t prev = head+1;
do {
prev = head;
if (!FillModuleInfo64(ctx, process, &head, 1, &mod)) {
if (mod.baseAddress == peb.ImageBaseAddress) {
break;
} else {
free(mod.name);
mod.name = NULL;
}
}
} while (head != end && head != prev);
return mod;
}
static void FillAnyModuleList64(const WinCtx* ctx, const WinProc* process, WinModuleList* list, size_t* maxSize, uint64_t head, int inMemoryOrder)
{
uint64_t end = head;
uint64_t prev = head+1;
if (inMemoryOrder)
inMemoryOrder = 1;
do {
if (list->size >= *maxSize) {
*maxSize *= 2;
WinModule* newList = (WinModule*)realloc(list->list, sizeof(WinModule) * *maxSize);
if (!newList)
break;
list->list = newList;
}
prev = head;
if (!FillModuleInfo64(ctx, process, &head, inMemoryOrder, list->list + list->size))
list->size++;
} while (head != end && head != prev);
}
static void FillModuleList32(const WinCtx* ctx, const WinProc* process, WinModuleList* list, size_t* maxSize)
{
PEB32 peb = GetPeb32(ctx, process);
PEB_LDR_DATA32 ldr;
memset(&ldr, 0, sizeof(ldr));
VMemRead(&ctx->process, process->dirBase, (uint64_t)&ldr, peb.Ldr, sizeof(ldr));
uint32_t head = ldr.InMemoryOrderModuleList.f_link;
uint32_t end = head;
uint32_t prev = head+1;
size_t nameBufSize = 128;
wchar_t* buf = (wchar_t*)malloc(sizeof(wchar_t) * nameBufSize);
do {
if (list->size >= *maxSize) {
*maxSize *= 2;
WinModule* newList = (WinModule*)realloc(list->list, sizeof(WinModule) * *maxSize);
if (!newList)
break;
list->list = newList;
}
prev = head;
LDR_MODULE32 mod;
memset(&mod, 0, sizeof(mod));
VMemRead(&ctx->process, process->dirBase, (uint64_t)&mod, head - sizeof(LIST_ENTRY32), sizeof(mod));
VMemRead(&ctx->process, process->dirBase, (uint64_t)&head, head, sizeof(head));
if (!mod.BaseDllName.length || !mod.SizeOfImage)
continue;
if (mod.BaseDllName.length >= nameBufSize) {
nameBufSize = mod.BaseDllName.length * 2;
buf = (wchar_t*)realloc(buf, sizeof(wchar_t) * nameBufSize);
}
VMemRead(&ctx->process, process->dirBase, (uint64_t)buf, mod.BaseDllName.buffer, mod.BaseDllName.length * sizeof(wchar_t));
char* buf2 = (char*)malloc(mod.BaseDllName.length);
for (int i = 0; i < mod.BaseDllName.length; i++)
buf2[i] = ((char*)buf)[i*2];
buf2[mod.BaseDllName.length-1] = '\0';
if (*(short*)(void*)buf2 == 0x53) {
free(buf2);
continue;
}
list->list[list->size].name = buf2;
list->list[list->size].baseAddress = mod.BaseAddress;
list->list[list->size].entryPoint = mod.EntryPoint;
list->list[list->size].sizeOfModule = mod.SizeOfImage;
list->list[list->size].loadCount = mod.LoadCount;
list->size++;
} while (head != end && head != prev);
free(buf);
}