forked from ufrisk/MemProcFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmmnet.c
1190 lines (1153 loc) · 48.1 KB
/
vmmnet.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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vmmnet.c : implementation of functionality related to the Windows networking.
//
// (c) Ulf Frisk, 2019-2022
// Author: Ulf Frisk, [email protected]
//
#ifdef _WIN32
#include <ws2tcpip.h>
#endif /* _WIN32 */
#include "vmmnet.h"
#include "pe.h"
#include "pdb.h"
#include "infodb.h"
#include "util.h"
typedef struct _RTL_DYNAMIC_HASH_TABLE {
DWORD Flags; // +000
DWORD Shift; // +004
DWORD TableSize; // +008
DWORD Pivot; // +00c
DWORD DivisorMask; // +010
DWORD NumEntries; // +014
DWORD NonEmptyBuckets; // +018
DWORD NumEnumerators; // +01c
QWORD Directory; // +020
} RTL_DYNAMIC_HASH_TABLE, *PRTL_DYNAMIC_HASH_TABLE;
// INTERNAL TYPEDEF's BELOW:
typedef struct tdVMMNET_OFFSET_TcpE {
BOOL _fValid;
BOOL _fProcessedTry;
WORD _Size;
WORD INET_AF;
WORD INET_AF_AF;
WORD INET_Addr;
WORD FLink;
WORD State;
WORD PortSrc;
WORD PortDst;
WORD EProcess;
WORD Time;
} VMMNET_OFFSET_TcpE, *PVMMNET_OFFSET_TcpE;
// TCP_TIMEWAIT
typedef struct tdVMMNET_OFFSET_TcTW {
WORD _Size;
WORD INET_AF;
WORD INET_Addr;
WORD PortSrc;
WORD PortDst;
WORD AddrDst;
WORD Time;
} VMMNET_OFFSET_TcTW, *PVMMNET_OFFSET_TcTW;
typedef struct tdVMMNET_OFFSET_TcpL_UdpA {
WORD _Size;
WORD INET_AF;
WORD INET_AF_AF;
WORD SrcAddr;
WORD SrcPort;
WORD DstPort;
WORD FLink;
WORD EProcess;
WORD Time;
} VMMNET_OFFSET_TcpL_UdpA, *PVMMNET_OFFSET_TcpL_UdpA;
typedef struct tdVMMNET_CONTEXT {
QWORD vaModuleTcpip;
DWORD cPartition;
QWORD vaPartitionTable;
VMMNET_OFFSET_TcpE oTcpE;
VMMNET_OFFSET_TcTW oTcTW;
VMMNET_OFFSET_TcpL_UdpA oTcpL;
VMMNET_OFFSET_TcpL_UdpA oUdpA;
QWORD vaTcpPortPool;
QWORD vaUdpPortPool;
} VMMNET_CONTEXT, *PVMMNET_CONTEXT;
typedef struct tdVMMNET_ASYNC_CONTEXT {
PVMMNET_CONTEXT ctx;
POB_MAP pmNetEntries;
PVMMOB_MAP_POOL pPoolMap;
PVMM_PROCESS pSystemProcess;
} VMMNET_ASYNC_CONTEXT, *PVMMNET_ASYNC_CONTEXT;
#define VMMNET_PARTITIONTABLE_OFFSET20(pbPT, vaPT) (*(PQWORD)pbPT && !*(PQWORD)(pbPT + 0x30) && ((vaPT + 0x20) == *(PQWORD)(pbPT + 0x20)) && ((vaPT + 0x20) == *(PQWORD)(pbPT + 0x28)))
#define VMMNET_PARTITIONTABLE_OFFSET18(pbPT, vaPT) (*(PQWORD)pbPT && !*(PQWORD)(pbPT + 0x28) && ((vaPT + 0x18) == *(PQWORD)(pbPT + 0x18)) && ((vaPT + 0x18) == *(PQWORD)(pbPT + 0x20)))
#define VMMNET_PARTITIONTABLE_WIN10_1903_ABOVE(pbPT) (VMM_KADDR64_16(*(PQWORD)(pbPT + 0x00)) && VMM_KADDR64_16(*(PQWORD)(pbPT + 0x08)) && VMM_KADDR64_16(*(PQWORD)(pbPT + 0x10)) && (*(PQWORD)(pbPT + 0x08) - *(PQWORD)(pbPT + 0x00) < 0x200) && (*(PQWORD)(pbPT + 0x10) - *(PQWORD)(pbPT + 0x08) < 0x200))
// ----------------------------------------------------------------------------
// TCP ENDPOINT FUNCTIONALITY BELOW:
// ----------------------------------------------------------------------------
/*
* qsort compare function for sorting the TCP connection list
*/
int VmmNet_TcpE_CmpSort(PVMM_MAP_NETENTRY a, PVMM_MAP_NETENTRY b)
{
if((a->dwPoolTag != b->dwPoolTag) && (a->dwPoolTag == 'UdpA' || b->dwPoolTag == 'UdpA')) {
return (a->dwPoolTag == 'UdpA') ? 1 : -1;
}
if(a->dwPID != b->dwPID) {
return a->dwPID - b->dwPID;
}
if(memcmp(a->Src.pbAddr, b->Src.pbAddr, 16)) {
return memcmp(a->Src.pbAddr, b->Src.pbAddr, 16);
}
if(a->Src.port != b->Src.port) {
return a->Src.port - b->Src.port;
}
if(a->AF != b->AF) {
return a->AF - b->AF;
}
return memcmp(a->Dst.pbAddr, b->Dst.pbAddr, 16);
}
/*
* Fuzz offsets in TcpE if required. Upon a successful fuzz values will be stored
* in the vmm handle.
* -- H
* -- ctx
* -- pSystemProcess
* -- vaTcpE - virtual address of a TCP ENDPOINT entry (TcpE).
* -- return
*/
_Success_(return)
BOOL VmmNet_TcpE_Fuzz(_In_ VMM_HANDLE H, _In_ PVMMNET_CONTEXT ctx, _In_ PVMM_PROCESS pSystemProcess, _In_ QWORD vaTcpE)
{
BOOL f;
QWORD o, va;
DWORD dwPoolTagInNl;
BYTE pb[0x380];
PVMMNET_OFFSET_TcpE po = &ctx->oTcpE;
POB_MAP pmObProcessAll = NULL;
if(po->_fValid || po->_fProcessedTry) { goto fail; }
po->_fProcessedTry = TRUE;
if(!VmmRead2(H, pSystemProcess, vaTcpE, pb, 0x380, VMM_FLAG_ZEROPAD_ON_FAIL)) { goto fail; }
if(!(pmObProcessAll = VmmProcessGetAll(H, TRUE, VMM_FLAG_PROCESS_SHOW_TERMINATED))) { goto fail; }
// Search for EPROCESS value in TcpE struct
for(o = 0x80; o < 0x380; o += 8) {
va = *(PQWORD)(pb + o);
if(!VMM_KADDR64_16(va)) { continue; }
if(ObMap_ExistsKey(pmObProcessAll, va)) {
po->EProcess = (WORD)o;
// INET_AF offset:
f = VMM_KADDR64_16(*(PQWORD)(pb + 0x10)) &&
VmmRead(H, pSystemProcess, *(PQWORD)(pb + 0x10) - 0x0c, (PBYTE)&dwPoolTagInNl, 4) &&
(dwPoolTagInNl == 'lNnI');
po->INET_AF = f ? 0x10 : 0x18;
// INET_AF AF offset
po->INET_AF_AF = (H->vmm.kernel.dwVersionBuild < 9200) ? 0x14 : 0x18; // VISTA-WIN7 or WIN8+
// check for state offset
po->State = (*(PDWORD)(pb + 0x6c) <= 13) ? 0x6c : 0x68;
if(H->vmm.kernel.dwVersionBuild == 22000) {
po->State = 0x70;
}
// static or relative offsets
po->INET_Addr = po->INET_AF + 0x08;
po->FLink = 0x40;
po->PortSrc = po->State + 0x04;
po->PortDst = po->State + 0x06;
po->Time = po->EProcess + 0x10;
po->_Size = po->Time + 8;
po->_fValid = TRUE;
// print result
if(H->cfg.fVerboseExtra) {
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "FuzzTcpE: 0x%016llx", vaTcpE);
VmmLog(H, MID_NET, LOGLEVEL_DEBUG,
" _Size %03X, InetAF %03X, InetAFAF %03X, InetAddr %03X, FLinkAll %03X",
po->_Size, po->INET_AF, po->INET_AF_AF, po->INET_Addr, po->FLink);
VmmLog(H, MID_NET, LOGLEVEL_DEBUG,
" State %03X, SrcPort %03X, DstPort %03X, EProcess %03X, Time %03X",
po->State, po->PortSrc, po->PortDst, po->EProcess, po->Time);
VmmLogHexAsciiEx(H, MID_NET, LOGLEVEL_DEBUG, pb, 0x300, 0, "");
}
Ob_DECREF(pmObProcessAll);
return TRUE;
}
}
fail:
Ob_DECREF(pmObProcessAll);
return FALSE;
}
/*
* Retrieve the virtual addresses of the TCP ENDPOINT structs in memory (TcpE).
* The virtual addresses will be put into the pObSet_TcpEndpoints set upon success.
* -- H
* -- ctx
* -- pSystemProcess
* -- pPoolMap
* -- psvaOb_TcpE
* -- psvaOb_TcTW
* -- return
*/
_Success_(return)
BOOL VmmNet_TcpE_GetAddressEPs(_In_ VMM_HANDLE H, _In_ PVMMNET_CONTEXT ctx, _In_ PVMM_PROCESS pSystemProcess, _In_opt_ PVMMOB_MAP_POOL pPoolMap, _Inout_ POB_SET psvaOb_TcpE, _Inout_ POB_SET psvaOb_TcTW)
{
BOOL f, f32 = H->vmm.f32, fResult = FALSE;
QWORD va, va2, va3;
DWORD i, j, o, iEntry, oStartHT, oListPT = 0, cbRead, cbTcpHT;
BYTE pb[0x810] = { 0 };
PBYTE pbPartitionTable = NULL, pbTcHT = NULL;
POB_SET pObTcHT = NULL, pObHTab_TcpE = NULL, pObTcpE = NULL;
PRTL_DYNAMIC_HASH_TABLE pTcpHT;
DWORD dwPoolTag;
PVMM_MAP_POOLENTRYTAG pePoolTag;
if(!(pObTcHT = ObSet_New(H))) { goto fail; }
if(!(pObHTab_TcpE = ObSet_New(H))) { goto fail; }
if(!(pObTcpE = ObSet_New(H))) { goto fail; }
if(!(pbPartitionTable = LocalAlloc(LMEM_ZEROINIT, 0x4000))) { goto fail; }
// 1: enumerate possible TcHT by walking tcpip.sys!PartitionTable
VmmReadEx(H, pSystemProcess, ctx->vaPartitionTable, pbPartitionTable, 0x4000, NULL, 0);
oStartHT = (DWORD)(*(PQWORD)(pbPartitionTable + 0x10) - *(PQWORD)(pbPartitionTable + 0x00));
if(oStartHT < 0x40) { goto fail; }
cbTcpHT = 0x10 + oStartHT + ctx->cPartition * sizeof(RTL_DYNAMIC_HASH_TABLE);
if(cbTcpHT > 0x10000) { goto fail; }
if(!(pbTcHT = LocalAlloc(LMEM_ZEROINIT, cbTcpHT))) { goto fail; }
oListPT = VMMNET_PARTITIONTABLE_OFFSET20(pbPartitionTable, ctx->vaPartitionTable) ? 0x20 : oListPT;
oListPT = VMMNET_PARTITIONTABLE_OFFSET18(pbPartitionTable, ctx->vaPartitionTable) ? 0x18 : oListPT;
if(oListPT) {
for(o = 0; o < 0x1000 - oListPT - 8; o += 8) {
f = *(PQWORD)(pbPartitionTable + o + 0x00) &&
(*(PQWORD)(pbPartitionTable + o + 0x10) - *(PQWORD)(pbPartitionTable + o + 0x00) == oStartHT) &&
((ctx->vaPartitionTable + o + oListPT) == *(PQWORD)(pbPartitionTable + o + oListPT)) &&
((ctx->vaPartitionTable + o + oListPT) == *(PQWORD)(pbPartitionTable + o + oListPT + 8));
if(!f) { continue; }
ObSet_Push(pObTcHT, *(PQWORD)(pbPartitionTable + o + 0x00) - 0x10); // store address in set & adjust for prepended pool header
o += 0x70;
}
}
if(VMMNET_PARTITIONTABLE_WIN10_1903_ABOVE(pbPartitionTable)) {
for(o = 0; o < 0x4000 - 0xc0 && VMMNET_PARTITIONTABLE_WIN10_1903_ABOVE(pbPartitionTable + o); o += 0xc0) {
ObSet_Push(pObTcHT, *(PQWORD)(pbPartitionTable + o + 0x00) - 0x10); // store address in set & adjust for prepended pool header
}
}
if(0 == ObSet_Size(pObTcHT)) { goto fail; }
VmmCachePrefetchPages3(H, pSystemProcess, pObTcHT, cbTcpHT, 0);
// 2: enumerate possible/interesting TCP hash tables - TcHT.
while((va = ObSet_Pop(pObTcHT))) {
ZeroMemory(pbTcHT, cbTcpHT);
VmmReadEx(H, pSystemProcess, va, pbTcHT, cbTcpHT, &cbRead, VMM_FLAG_FORCECACHE_READ);
if((cbTcpHT != cbRead) || !VMM_POOLTAG_PREPENDED(f32, pbTcHT, 0x10, 'TcHT')) { continue; }
// Common TCP Hash Table: Contains TcTW
if(H->vmm.kernel.dwVersionBuild >= 20348) {
o = oStartHT - sizeof(RTL_DYNAMIC_HASH_TABLE);
if(((PRTL_DYNAMIC_HASH_TABLE)(pbTcHT + o - 0x10))->DivisorMask == 0x7f) { o -= 0x10; }
for(; o >= 2 * sizeof(RTL_DYNAMIC_HASH_TABLE); o -= sizeof(RTL_DYNAMIC_HASH_TABLE)) {
pTcpHT = (PRTL_DYNAMIC_HASH_TABLE)(pbTcHT + o);
if(!VMM_KADDR64_16(pTcpHT->Directory) || (pTcpHT->TableSize != 0x80) || (pTcpHT->DivisorMask != 0x7f)) { continue; }
if(!pTcpHT->NonEmptyBuckets) { continue; }
ObSet_Push(pObHTab_TcpE, pTcpHT->Directory - 0x10); // store address in set & account for prepended pool header
}
}
// Partition related TCP Hash Tables: Contains TcpE
for(i = 0; i < ctx->cPartition; i++) {
pTcpHT = (PRTL_DYNAMIC_HASH_TABLE)(pbTcHT + 0x10 + oStartHT) + i;
if(!VMM_KADDR64_16(pTcpHT->Directory) || (pTcpHT->TableSize != 0x80) || (pTcpHT->DivisorMask != 0x7f)) { break; }
if(!pTcpHT->NonEmptyBuckets) { continue; }
ObSet_Push(pObHTab_TcpE, pTcpHT->Directory - 0x10); // store address in set & account for prepended pool header
}
}
if(0 == ObSet_Size(pObHTab_TcpE)) { goto fail; }
VmmCachePrefetchPages3(H, pSystemProcess, pObHTab_TcpE, 0x810, 0);
// 3: Enumerate TCP Endpoints 'TcpE' out of the potential 'HTab'
while((va = ObSet_Pop(pObHTab_TcpE))) {
VmmReadEx(H, pSystemProcess, va, pb, 0x810, &cbRead, VMM_FLAG_FORCECACHE_READ);
if(0x810 != cbRead) { continue; }
if((*(PDWORD)(pb + 0x04) != 'baTH') && (H->vmm.kernel.dwVersionBuild != 10240)) {
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "UNEXPECTED POOL HDR: '%c%c%c%c' EXPECT: 'HTab' AT VA: 0x%016llx", pb[4], pb[5], pb[6], pb[7], va);
continue;
}
for(o = 0x10; o < 0x800; o += 0x10) {
va2 = *(PQWORD)(pb + o);
if((va + o == va2) || !VMM_KADDR64_16(va2)) { continue; }
ObSet_Push(pObTcpE, va2 - 0x50);
va3 = *(PQWORD)(pb + o + 8);
if((va + o == va3) || (va2 == va3) || !VMM_KADDR64_16(va2)) { continue; }
ObSet_Push(pObTcpE, va3 - 0x50);
}
}
if(0 == ObSet_Size(pObTcpE)) { goto fail; }
VmmCachePrefetchPages3(H, pSystemProcess, pObTcpE, 0x50, 0);
// 4: Verify and transfer to outgoing result set pObTcpE_Located
while((va = ObSet_Pop(pObTcpE))) {
VmmReadEx(H, pSystemProcess, va, pb, 0x50, &cbRead, VMM_FLAG_FORCECACHE_READ);
if(0x50 != cbRead) { continue; }
if(VMM_POOLTAG_PREPENDED(f32, pb, 0x10, 'TcpE') || VMM_POOLTAG_PREPENDED(f32, pb, 0x10, 'TTcb')) {
ObSet_Push(psvaOb_TcpE, va + 0x10);
continue;
}
if(VMM_POOLTAG_PREPENDED(f32, pb, 0x50, 'TcTW')) {
ObSet_Push(psvaOb_TcTW, va + 0x50);
continue;
}
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "UNEXPECTED POOL HDR: '%c%c%c%c' EXPECT: 'TcpE/TTcb/TcTW' AT VA: 0x%016llx", pb[4], pb[5], pb[6], pb[7], va);
}
// 5: fill extra candidates from pool headers:
if(pPoolMap) {
for(i = 0; i < 3; i++) {
switch(i) {
case 0: o = 0x00; dwPoolTag = 'TTcb'; break;
case 1: o = 0x00; dwPoolTag = 'TcpE'; break;
default: o = 0x40; dwPoolTag = 'TcTW'; break;
}
if(VmmMap_GetPoolTag(H, pPoolMap, dwPoolTag, &pePoolTag)) {
for(j = 0; j < pePoolTag->cEntry; j++) {
iEntry = pPoolMap->piTag2Map[pePoolTag->iTag2Map + j];
ObSet_Push(psvaOb_TcpE, pPoolMap->pMap[iEntry].va + o);
}
}
}
}
if(!ObSet_Size(psvaOb_TcpE) && !ObSet_Size(psvaOb_TcTW)) { goto fail; }
fResult = TRUE;
fail:
Ob_DECREF(pObTcHT);
Ob_DECREF(pObHTab_TcpE);
Ob_DECREF(pObTcpE);
LocalFree(pbPartitionTable);
LocalFree(pbTcHT);
return fResult;
}
_Success_(return)
BOOL VmmNet_TcpE_Enumerate(_In_ VMM_HANDLE H, _In_ PVMMNET_CONTEXT ctx, _In_ PVMM_PROCESS pSystemProcess, _In_ POB_SET pSet_TcpE, _Inout_ POB_MAP pmTcpE)
{
BOOL f;
QWORD va, ftTime, vaEPROCESS;
DWORD cbRead, c = 0, i;
BYTE pb[0x400] = { 0 };
PVMM_MAP_NETENTRY pe;
POB_SET pObPrefetch = NULL;
PVMM_PROCESS pObProcess = NULL;
PVMMNET_OFFSET_TcpE po = &ctx->oTcpE;
QWORD vaINET_AF, vaINET_Addr, vaINET_Src, vaINET_Dst;
if(!(pObPrefetch = ObSet_New(H))) { goto fail; }
VmmCachePrefetchPages3(H, pSystemProcess, pSet_TcpE, po->_Size, 0);
// 1: retrieve general info from main struct (TcpE)
while((va = ObSet_Pop(pSet_TcpE))) {
VmmReadEx(H, pSystemProcess, va, pb, po->_Size, &cbRead, VMM_FLAG_FORCECACHE_READ);
if(po->_Size != cbRead) { continue; }
ftTime = *(PQWORD)(pb + po->Time);
if(!ftTime || (ftTime > 0x0200000000000000)) { continue; }
if(!VMM_KADDR64_8(*(PQWORD)(pb + po->EProcess)) || !VMM_KADDR64_8(*(PQWORD)(pb + po->INET_AF)) || !VMM_KADDR64_8(*(PQWORD)(pb + po->INET_Addr))) { continue; }
if(!(pe = LocalAlloc(LMEM_ZEROINIT, sizeof(VMM_MAP_NETENTRY)))) { continue; }
pe->dwPoolTag = 'TcpE';
pe->Dst.port = _byteswap_ushort(*(PWORD)(pb + po->PortDst));
pe->Src.port = _byteswap_ushort(*(PWORD)(pb + po->PortSrc));
pe->dwState = *(PWORD)(pb + po->State);
if(pe->dwState > 13) { continue; }
pe->vaObj = va;
pe->ftTime = ftTime;
pe->_Reserved1 = *(PQWORD)(pb + po->INET_AF); // vaINET_AF
pe->_Reserved2 = *(PQWORD)(pb + po->INET_Addr); // vaINET_Addr
vaEPROCESS = *(PQWORD)(pb + po->EProcess);
while((pObProcess = VmmProcessGetNext(H, pObProcess, VMM_FLAG_PROCESS_SHOW_TERMINATED))) {
if(vaEPROCESS == pObProcess->win.EPROCESS.va) {
pe->dwPID = pObProcess->dwPID;
Ob_DECREF_NULL(&pObProcess);
break;
}
}
ObSet_Push(pObPrefetch, pe->_Reserved1 - 0x10);
ObSet_Push(pObPrefetch, pe->_Reserved2);
ObMap_Push(pmTcpE, va, pe);
}
// 2: retrieve address family and ptr to address
VmmCachePrefetchPages3(H, pSystemProcess, pObPrefetch, 0x30, 0);
Ob_DECREF_NULL(&pObPrefetch);
if(!(pObPrefetch = ObSet_New(H))) { goto fail; }
for(i = 0, c = ObMap_Size(pmTcpE); i < c; i++) {
pe = ObMap_GetByIndex(pmTcpE, i);
vaINET_AF = pe->_Reserved1;
vaINET_Addr = pe->_Reserved2;
pe->_Reserved1 = 0;
pe->_Reserved2 = 0;
// 2.1 fetch INET_AF
VmmReadEx(H, pSystemProcess, vaINET_AF - 0x10, pb, 0x30, &cbRead, VMM_FLAG_FORCECACHE_READ);
if(0x30 != cbRead) { continue; }
if(*(PDWORD)(pb + 0x04) != 'lNnI') {
if(H->vmm.kernel.dwVersionBuild < 22000) {
// on win11 this is very common (and expected). This happens
// when a non-supported 'TcpE' is parsed instead of a 'TTcb'.
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "UNEXPECTED POOL HDR: '%c%c%c%c' EXPECT: 'InNl' AT VA: 0x%016llx", pb[4], pb[5], pb[6], pb[7], vaINET_AF);
}
continue;
}
pe->AF = *(PWORD)(pb + 0x10 + po->INET_AF_AF);
if((pe->AF != AF_INET) && (pe->AF != AF_INET6)) {
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "UNEXPECTED INET_AF: %i EXPECT: %i or %i AT VA: 0x%016llx", pe->AF, AF_INET, AF_INET6, vaINET_AF);
continue;
}
// 2.2 fetch ptrs to INET_ADDR SRC/DST and queue for prefetch
VmmReadEx(H, pSystemProcess, vaINET_Addr, pb, 0x18, &cbRead, VMM_FLAG_FORCECACHE_READ);
if((0x18 != cbRead) || !VMM_KADDR64_8(*(PQWORD)(pb + 0x00)) || !VMM_KADDR64_8(*(PQWORD)(pb + 0x10))) { continue; }
pe->_Reserved1 = *(PQWORD)(pb + 0x00); // vaINET_Src
pe->_Reserved2 = *(PQWORD)(pb + 0x10); // vaINET_Dst
ObSet_Push(pObPrefetch, pe->_Reserved1);
ObSet_Push(pObPrefetch, pe->_Reserved2);
}
// 3: retrieve src / dst addresses
VmmCachePrefetchPages3(H, pSystemProcess, pObPrefetch, 0x18, 0);
Ob_DECREF_NULL(&pObPrefetch);
for(i = 0, c = ObMap_Size(pmTcpE); i < c; i++) {
pe = ObMap_GetByIndex(pmTcpE, i);
vaINET_Src = pe->_Reserved1;
vaINET_Dst = pe->_Reserved2;
pe->_Reserved1 = 0;
pe->_Reserved2 = 0;
if((pe->AF == AF_INET) || (pe->AF == AF_INET6)) {
// 3.1 src address
VmmReadEx(H, pSystemProcess, vaINET_Src, pb, 0x18, &cbRead, VMM_FLAG_FORCECACHE_READ);
f = (0x18 == cbRead) &&
VMM_KADDR64_8(*(PQWORD)(pb + 0x10)) &&
VmmRead(H, pSystemProcess, *(PQWORD)(pb + 0x10), pb, 0x08) &&
VMM_KADDR64_8(*(PQWORD)pb) &&
VmmRead(H, pSystemProcess, *(PQWORD)pb, pb, 0x20);
if(f) {
pe->Src.fValid = TRUE;
memcpy(pe->Src.pbAddr, pb, (pe->AF == AF_INET) ? 4 : 16);
}
// 3.2 dst address
VmmReadEx(H, pSystemProcess, vaINET_Dst, pb, 0x20, &cbRead, VMM_FLAG_FORCECACHE_READ);
if(0x20 == cbRead) {
pe->Dst.fValid = TRUE;
memcpy(pe->Dst.pbAddr, pb, (pe->AF == AF_INET) ? 4 : 16);
}
}
}
return TRUE;
fail:
Ob_DECREF(pObPrefetch);
return FALSE;
}
_Success_(return)
BOOL VmmNet_TcpTW_Enumerate(_In_ VMM_HANDLE H, _In_ PVMMNET_CONTEXT ctx, _In_ PVMM_PROCESS pSystemProcess, _In_ POB_SET pSet_TcpTW, _Inout_ POB_MAP pmTcpE)
{
BOOL f;
QWORD va;
DWORD cbRead, c = 0, i;
BYTE pb[0x400] = { 0 };
PVMM_MAP_NETENTRY pe;
POB_SET pObPrefetch = NULL;
PVMMNET_OFFSET_TcTW po = &ctx->oTcTW;
QWORD vaINET_AF, vaINET_Addr, vaINET_Src;
if(!ObSet_Size(pSet_TcpTW)) { goto fail; }
if(!(pObPrefetch = ObSet_New(H))) { goto fail; }
VmmCachePrefetchPages3(H, pSystemProcess, pSet_TcpTW, po->_Size, 0);
// 1: retrieve general info from main struct (TcpE)
while((va = ObSet_Pop(pSet_TcpTW))) {
if(!VmmRead2(H, pSystemProcess, va, pb, po->_Size, VMM_FLAG_FORCECACHE_READ)) { continue; }
if(!VMM_KADDR64_8(*(PQWORD)(pb + po->INET_AF)) || !VMM_KADDR64_8(*(PQWORD)(pb + po->INET_Addr))) {
continue;
}
if(!(pe = LocalAlloc(LMEM_ZEROINIT, sizeof(VMM_MAP_NETENTRY)))) { continue; }
pe->dwPoolTag = 'TcTW';
pe->Dst.fValid = TRUE;
memcpy(pe->Dst.pbAddr, pb + po->AddrDst, 16);
pe->Dst.port = _byteswap_ushort(*(PWORD)(pb + po->PortDst));
pe->Src.port = _byteswap_ushort(*(PWORD)(pb + po->PortSrc));
pe->dwState = 12;
pe->vaObj = va;
pe->ftTime = *(PQWORD)(pb + po->Time);
pe->_Reserved1 = *(PQWORD)(pb + po->INET_AF); // vaINET_AF
pe->_Reserved2 = *(PQWORD)(pb + po->INET_Addr); // vaINET_Addr
ObSet_Push(pObPrefetch, pe->_Reserved1 - 0x10);
ObSet_Push(pObPrefetch, pe->_Reserved2);
ObMap_Push(pmTcpE, va, pe);
}
// 2: retrieve address family and ptr to source address
VmmCachePrefetchPages3(H, pSystemProcess, pObPrefetch, 0x30, 0);
Ob_DECREF_NULL(&pObPrefetch);
if(!(pObPrefetch = ObSet_New(H))) { goto fail; }
for(i = 0, c = ObMap_Size(pmTcpE); i < c; i++) {
pe = ObMap_GetByIndex(pmTcpE, i);
vaINET_AF = pe->_Reserved1;
vaINET_Addr = pe->_Reserved2;
pe->_Reserved1 = 0;
pe->_Reserved2 = 0;
// 2.1 fetch INET_AF
VmmReadEx(H, pSystemProcess, vaINET_AF - 0x10, pb, 0x30, &cbRead, VMM_FLAG_FORCECACHE_READ);
if(0x30 != cbRead) { continue; }
if(*(PDWORD)(pb + 0x04) != 'lNnI') {
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "UNEXPECTED POOL HDR: '%c%c%c%c' EXPECT: 'InNl' AT VA: 0x%016llx", pb[4], pb[5], pb[6], pb[7], vaINET_AF);
continue;
}
pe->AF = *(PWORD)(pb + 0x10 + ctx->oTcpE.INET_AF_AF);
if((pe->AF != AF_INET) && (pe->AF != AF_INET6)) {
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "UNEXPECTED INET_AF: %i EXPECT: %i or %i AT VA: 0x%016llx", pe->AF, AF_INET, AF_INET6, vaINET_AF);
continue;
}
// 2.2 fetch ptrs to INET_ADDR SRC and queue for prefetch
VmmReadEx(H, pSystemProcess, vaINET_Addr, pb, 0x18, &cbRead, VMM_FLAG_FORCECACHE_READ);
if((0x18 != cbRead) || !VMM_KADDR64_8(*(PQWORD)(pb + 0x10))) { continue; }
pe->_Reserved1 = *(PQWORD)(pb + 0x10); // vaINET_Src
ObSet_Push(pObPrefetch, pe->_Reserved1);
}
// 3: retrieve src address
VmmCachePrefetchPages3(H, pSystemProcess, pObPrefetch, 0x38, 0);
Ob_DECREF_NULL(&pObPrefetch);
for(i = 0, c = ObMap_Size(pmTcpE); i < c; i++) {
pe = ObMap_GetByIndex(pmTcpE, i);
if(!pe->_Reserved1) { continue; }
vaINET_Src = pe->_Reserved1;
pe->_Reserved1 = 0;
if((pe->AF == AF_INET) || (pe->AF == AF_INET6)) {
// 3.1 src address
VmmReadEx(H, pSystemProcess, vaINET_Src, pb, 0x38, &cbRead, VMM_FLAG_FORCECACHE_READ);
f = (0x38 == cbRead) &&
VMM_KADDR64_8(*(PQWORD)(pb + 0x00)) &&
VMM_KADDR64_8(*(PQWORD)(pb + 0x10)) &&
VMM_KADDR64_8(*(PQWORD)(pb + 0x18));
if(f) {
pe->Src.fValid = TRUE;
memcpy(pe->Src.pbAddr, pb + 0x28, (pe->AF == AF_INET) ? 4 : 16);
}
}
}
return TRUE;
fail:
Ob_DECREF(pObPrefetch);
return FALSE;
}
/*
* Retrieve active TCP connections.
* NB! Function may be started asynchronously.
*/
DWORD VmmNet_TcpE_DoWork(_In_ VMM_HANDLE H, PVOID lpThreadParameter)
{
PVMMNET_ASYNC_CONTEXT actx = (PVMMNET_ASYNC_CONTEXT)lpThreadParameter;
PVMMNET_CONTEXT ctx = actx->ctx;
PVMM_PROCESS pSystemProcess = actx->pSystemProcess;
POB_MAP pmNetEntries = actx->pmNetEntries;
POB_SET pObTcpE = NULL, pObTcpTW = NULL;
if(!(pObTcpE = ObSet_New(H))) { goto fail; }
if(!(pObTcpTW = ObSet_New(H))) { goto fail; }
if(!VmmNet_TcpE_GetAddressEPs(H, ctx, pSystemProcess, actx->pPoolMap, pObTcpE, pObTcpTW)) { goto fail; }
VmmNet_TcpE_Fuzz(H, ctx, pSystemProcess, ObSet_Get(pObTcpE, 0));
if(!ctx->oTcpE._fValid) { goto fail; }
VmmNet_TcpE_Enumerate(H, ctx, pSystemProcess, pObTcpE, pmNetEntries);
VmmNet_TcpTW_Enumerate(H, ctx, pSystemProcess, pObTcpTW, pmNetEntries);
fail:
Ob_DECREF(pObTcpE);
Ob_DECREF(pObTcpTW);
return 0;
}
// ----------------------------------------------------------------------------
// UDP ENDPOINT AND TCP LISTENER FUNCTIONALITY (VIA PORT POOL InPP) BELOW:
// ----------------------------------------------------------------------------
#define VMMNET_EP_OFFSET 0xC0
#define VMMNET_EP_SIZE 0x380
/*
* Filter for TcpL / UdpA
*/
VOID VmmNet_InPP_FilterTcpLUdpA(_In_ QWORD k, _In_ PVOID v, _Inout_opt_ PVOID ctx)
{
POB_MAP pm = ctx;
if((((PVMM_MAP_NETENTRY)v)->dwPoolTag == 'TcpL') || (((PVMM_MAP_NETENTRY)v)->dwPoolTag == 'UdpA')) {
ObMap_Push(pm, k, v);
}
}
/*
* Perform post processing of already enumerated UdpA / TcpL entries in map
* pmNetEntriesPre. Upon completion valid entries will be transferred to map
* pmNetEntries.
* -- H
* -- ctx
* -- pSystemProcess
* -- pmNetEntriesPre = map of partial not-yet completed entries.
* -- pmNetEntries = result map of completed net entries.
*/
VOID VmmNet_InPP_PostTcpLUdpA(_In_ VMM_HANDLE H, _In_ PVMMNET_CONTEXT ctx, _In_ PVMM_PROCESS pSystemProcess, _Inout_ POB_MAP pmNetEntriesPre, _Inout_ POB_MAP pmNetEntries)
{
DWORD o, cbRead;
BYTE pb[0x30] = { 0 };
POB_SET psObPrefetch = NULL;
POB_MAP pmOb = NULL;
QWORD vaINET_AF, vaLocal_Addr;
PVMM_MAP_NETENTRY pe = NULL;
if(!(pmOb = ObMap_New(H, 0))) { goto fail; }
if(!(psObPrefetch = ObSet_New(H))) { goto fail; }
if(!ObMap_Filter(pmNetEntriesPre, pmOb, VmmNet_InPP_FilterTcpLUdpA)) { goto fail; }
// 1: prefetch
while((pe = ObMap_GetNext(pmOb, pe))) {
if(pe->_Reserved1) { ObSet_Push(psObPrefetch, pe->_Reserved1 - 0x10); }
if(pe->_Reserved2) { ObSet_Push(psObPrefetch, pe->_Reserved2); }
}
if(!ObSet_Size(psObPrefetch)) { goto fail; }
// 2: retrieve address family and ptr to address
VmmCachePrefetchPages3(H, pSystemProcess, psObPrefetch, 0x30, 0);
ObSet_Clear(psObPrefetch);
while((pe = ObMap_GetNext(pmOb, pe))) {
vaINET_AF = pe->_Reserved1;
vaLocal_Addr = pe->_Reserved2;
pe->_Reserved1 = 0;
pe->_Reserved2 = 0;
// 2.1 fetch INET_AF
VmmReadEx(H, pSystemProcess, vaINET_AF - 0x10, pb, 0x30, &cbRead, VMM_FLAG_FORCECACHE_READ);
if(0x30 != cbRead) { continue; }
if(*(PDWORD)(pb + 0x04) != 'lNnI') {
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "UNEXPECTED POOL HDR: '%c%c%c%c' EXPECT: 'InNl' AT VA: 0x%016llx", pb[4], pb[5], pb[6], pb[7], vaINET_AF);
continue;
}
pe->AF = *(PWORD)(pb + 0x10 + ctx->oTcpL.INET_AF_AF);
if((pe->AF != AF_INET) && (pe->AF != AF_INET6)) {
VmmLog(H, MID_NET, LOGLEVEL_DEBUG, "UNEXPECTED INET_AF: %i EXPECT: %i or %i AT VA: 0x%016llx", pe->AF, AF_INET, AF_INET6, vaINET_AF);
continue;
}
// 2.2 fetch ptrs to INET_ADDR SRC and queue for prefetch
if(vaLocal_Addr) {
o = ((pe->dwPoolTag == 'UdpA') && H->vmm.kernel.dwVersionBuild >= 10240) ? 0x18 : 0x10; // UDP-Win10 special offset
VmmReadEx(H, pSystemProcess, vaLocal_Addr, pb, 0x20, &cbRead, VMM_FLAG_FORCECACHE_READ);
if((0x20 != cbRead) || !VMM_KADDR64_8(*(PQWORD)(pb + o))) { continue; }
pe->_Reserved2 = *(PQWORD)(pb + o); // vaSrc
ObSet_Push(psObPrefetch, pe->_Reserved2);
} else {
pe->Src.fValid = TRUE; // address 0.0.0.0
}
}
// 3: retrieve addr ptr
VmmCachePrefetchPages3(H, pSystemProcess, psObPrefetch, 8, 0);
ObSet_Clear(psObPrefetch);
while((pe = ObMap_GetNext(pmOb, pe))) {
if(pe->_Reserved2) {
vaLocal_Addr = pe->_Reserved2;
pe->_Reserved2 = 0;
VmmReadEx(H, pSystemProcess, vaLocal_Addr, pb, 8, &cbRead, VMM_FLAG_FORCECACHE_READ);
if((8 != cbRead) || !VMM_KADDR64_8(*(PQWORD)(pb))) { continue; }
pe->_Reserved2 = *(PQWORD)(pb); // vaSrc
ObSet_Push(psObPrefetch, pe->_Reserved2);
}
}
// 4: retrieve addr
VmmCachePrefetchPages3(H, pSystemProcess, psObPrefetch, 0x20, 0);
while((pe = ObMap_GetNext(pmOb, pe))) {
if(pe->_Reserved2) {
o = ((pe->dwPoolTag == 'UdpA') && H->vmm.kernel.dwVersionBuild >= 10240) ? 0x18 : 0; // UDP-Win10 special offset
if(VmmRead2(H, pSystemProcess, pe->_Reserved2 + o, pb, 16, VMM_FLAG_FORCECACHE_READ)) {
pe->Src.fValid = TRUE;
memcpy(pe->Src.pbAddr, pb, (pe->AF == AF_INET) ? 4 : 16);
}
pe->_Reserved2 = 0;
}
ObMap_Remove(pmNetEntriesPre, pe);
ObMap_Push(pmNetEntries, pe->vaObj, pe);
}
fail:
Ob_DECREF(pmOb);
Ob_DECREF(psObPrefetch);
}
/*
* Enumerate PortPool TcpE entries. Enumeration is currently limited and will at
* this moment only look at forward linked entries.
* TODO: IMPLEMENT InPP-TcpE SUPPORT:
*/
PVMM_MAP_NETENTRY VmmNet_InPP_TcpE(_In_ PVMMNET_CONTEXT ctx, _In_ PVMM_PROCESS pSystemProcess, _In_ QWORD vaTcpE, _In_reads_(cb) PBYTE pb, _In_ DWORD cb, _Inout_ POB_SET psEP_Next)
{
QWORD vaNext = *(PQWORD)(pb + 0x70);
if(VMM_KADDR64(vaNext)) {
ObSet_Push(psEP_Next, (vaNext & ~7) - VMMNET_EP_OFFSET);
}
return NULL;
}
/*
* Enumerate PortPool UdpA / TcpL entries.
*/
PVMM_MAP_NETENTRY VmmNet_InPP_TcpL_UdpA(_In_ VMM_HANDLE H, _In_ PVMMNET_CONTEXT ctx, _In_ PVMM_PROCESS pSystemProcess, _In_ DWORD dwPoolTag, PVMMNET_OFFSET_TcpL_UdpA po, _In_ QWORD vaTcpE_UdpA, _In_reads_(cb) PBYTE pb, _In_ DWORD cb, _Inout_ POB_SET psEP_Next)
{
DWORD c = 0;
QWORD ftTime, vaNext, vaEPROCESS;
PVMM_MAP_NETENTRY pe;
PVMM_PROCESS pObProcess = NULL;
vaNext = *(PQWORD)(pb + po->FLink);
if(VMM_KADDR64(vaNext)) {
ObSet_Push(psEP_Next, (vaNext & ~7) - VMMNET_EP_OFFSET);
}
if(!VMM_KADDR64_8(*(PQWORD)(pb + po->INET_AF))) { return NULL; }
if(!(pe = LocalAlloc(LMEM_ZEROINIT, sizeof(VMM_MAP_NETENTRY)))) { return NULL; }
pe->dwPoolTag = dwPoolTag;
pe->dwState = (dwPoolTag == 'TcpL') ? 1 : 13;
pe->Src.port = _byteswap_ushort(*(PWORD)(pb + po->SrcPort));
if(po->DstPort) {
pe->Dst.port = _byteswap_ushort(*(PWORD)(pb + po->DstPort));
}
pe->vaObj = vaTcpE_UdpA;
ftTime = *(PQWORD)(pb + po->Time);
if(1 == (ftTime >> 56)) {
pe->ftTime = ftTime;
}
pe->_Reserved1 = *(PQWORD)(pb + po->INET_AF); // vaINET_AF
if(VMM_KADDR64_8(*(PQWORD)(pb + po->SrcAddr))) {
pe->_Reserved2 = *(PQWORD)(pb + po->SrcAddr); // vaLocalAddr
}
vaEPROCESS = *(PQWORD)(pb + po->EProcess);
if(VMM_KADDR64_16(vaEPROCESS)) {
while((pObProcess = VmmProcessGetNext(H, pObProcess, VMM_FLAG_PROCESS_SHOW_TERMINATED))) {
if(vaEPROCESS == pObProcess->win.EPROCESS.va) {
pe->dwPID = pObProcess->dwPID;
Ob_DECREF_NULL(&pObProcess);
break;
}
}
}
return pe;
}
/*
* Dispatch a PortPool entry to its enumeration function.
*/
VOID VmmNet_InPP_Dispatch(_In_ VMM_HANDLE H, _In_ PVMMNET_CONTEXT ctx, _In_ PVMM_PROCESS pSystemProcess, _In_ DWORD tag, _In_ QWORD va, _In_reads_(cb) PBYTE pb, _In_ DWORD cb, _In_ DWORD oFLink, _Inout_ POB_SET psEP_Next, _Inout_ POB_MAP pmNetEntriesPre)
{
PVMM_MAP_NETENTRY pe;
PVMMNET_OFFSET_TcpL_UdpA po = &ctx->oTcpL;
if(ObMap_ExistsKey(pmNetEntriesPre, va)) { return; }
if(tag == 'TcpE') {
// TODO: IMPLEMENT SUPPORT FOR InPP-TcpE
VmmNet_InPP_TcpE(ctx, pSystemProcess, va, pb, cb, psEP_Next);
}
if(tag == 'TcpL') {
if((pe = VmmNet_InPP_TcpL_UdpA(H, ctx, pSystemProcess, 'TcpL', &ctx->oTcpL, va, pb, cb, psEP_Next))) {
ObMap_Push(pmNetEntriesPre, va, pe);
}
}
if(tag == 'UdpA') {
if((pe = VmmNet_InPP_TcpL_UdpA(H, ctx, pSystemProcess, 'UdpA', &ctx->oUdpA, va, pb, cb, psEP_Next))) {
ObMap_Push(pmNetEntriesPre, va, pe);
}
}
}
/*
* Enumerate PortPool entries and put valid ones into the result map.
* NB! Function may be started asynchronously.
*/
DWORD VmmNet_InPP_DoWork(_In_ VMM_HANDLE H, PVOID lpThreadParameter)
{
BOOL f32 = H->vmm.f32;
DWORD dwVersionBuild = H->vmm.kernel.dwVersionBuild;
PVMMNET_ASYNC_CONTEXT actx = (PVMMNET_ASYNC_CONTEXT)lpThreadParameter;
PVMMNET_CONTEXT ctx = actx->ctx;
PVMM_PROCESS pSystemProcess = actx->pSystemProcess;
POB_MAP pmNetEntries = actx->pmNetEntries;
DWORD cbInPPe, oInPPe, oInPA = 0, o, oFLink, tag, iEntry;
QWORD i, j, va;
BYTE pb[0x2000], pb2[0x20];
POB_SET psObPA = NULL, psObPreEP = NULL, psObEP = NULL, psObEP_Next = NULL, psObEP_SWAP;
POB_MAP pmObNetEntriesPre = NULL;
PVMM_MAP_POOLENTRYTAG pePoolTag;
if(!(psObPA = ObSet_New(H))) { goto fail; }
if(!(psObPreEP = ObSet_New(H))) { goto fail; }
if(!(psObEP = ObSet_New(H))) { goto fail; }
if(!(psObEP_Next = ObSet_New(H))) { goto fail; }
if(!(pmObNetEntriesPre = ObMap_New(H, OB_MAP_FLAGS_OBJECT_LOCALFREE))) { goto fail; }
// set offsets
if(dwVersionBuild <= 9200) {
oInPPe = 0x08;
cbInPPe = 0x10;
} else if(dwVersionBuild <= 18363) {
oInPPe = 0x08;
cbInPPe = 0x18;
} else {
oInPPe = 0x10;
cbInPPe = 0x20;
}
// fetch InPA
VmmCachePrefetchPages2(H, pSystemProcess, 2, ctx->vaTcpPortPool, ctx->vaUdpPortPool);
for(i = 0; i < 2; i++) {
if(!VmmRead2(H, pSystemProcess, i ? ctx->vaTcpPortPool : ctx->vaUdpPortPool, pb, 0x1000, VMM_FLAG_FORCECACHE_READ)) { continue; }
// offet for ptrs into InPA starts at +0a0 + extra, each InPA is responsible for 256 ports.
if(!oInPA) {
for(o = 0x0a0; o < 0x100; o += 8) {
va = *(PQWORD)(pb + o);
if(VMM_KADDR64_16(va) && !VMM_KADDR64_PAGE(va) && VmmRead(H, pSystemProcess, va - 0x10, pb2, 0x20) && VMM_POOLTAG_PREPENDED(f32, pb2, 0x10, 'InPA')) {
oInPA = o;
break;
}
}
if(!oInPA) { goto fail; }
}
for(j = 0; j < 256; j++) {
va = *(PQWORD)(pb + oInPA + j * 8);
if(VMM_KADDR64_16(va)) {
ObSet_Push(psObPA, va - 0x10);
}
}
}
if(!ObSet_Size(psObPA)) { goto fail; }
// fetch InPA tables
VmmCachePrefetchPages3(H, pSystemProcess, psObPA, 0x40, 0);
while((va = ObSet_Pop(psObPA))) {
if(VmmRead2(H, pSystemProcess, va, pb, 0x40, VMM_FLAG_FORCECACHE_READ) && VMM_POOLTAG_PREPENDED(f32, pb, 0x10, 'InPA')) {
va = *(PQWORD)(pb + 0x28);
if(!VMM_KADDR64_PAGE(va)) {
va = *(PQWORD)(pb + 0x30);
}
if(VMM_KADDR64_PAGE(va)) {
ObSet_Push(psObPreEP, va);
}
}
}
if(!ObSet_Size(psObPreEP)) { goto fail; }
// fetch initial addresses for endpoints / listeners.
VmmCachePrefetchPages(H, pSystemProcess, psObPreEP, 0);
while((va = ObSet_Pop(psObPreEP))) {
VmmRead2(H, pSystemProcess, va, pb, 256 * cbInPPe, VMM_FLAG_FORCECACHE_READ | VMM_FLAG_ZEROPAD_ON_FAIL);
for(i = 0; i < 256; i++) { // TODO - 0-255
va = *(PQWORD)(pb + i * cbInPPe + oInPPe) & ~7;
if(VMM_KADDR64_8(va)) {
ObSet_Push(psObEP, va - VMMNET_EP_OFFSET);
}
}
}
// fetch candidate addresses for endpoints / listeners from pool tagging
if(actx->pPoolMap) {
for(i = 0; i < 2; i++) {
if(VmmMap_GetPoolTag(H, actx->pPoolMap, (i ? 'TcpL' : 'UdpA'), &pePoolTag)) {
for(j = 0; j < pePoolTag->cEntry; j++) {
iEntry = actx->pPoolMap->piTag2Map[pePoolTag->iTag2Map + j];
ObSet_Push(psObEP, actx->pPoolMap->pMap[iEntry].va);
}
}
}
}
// fetch and process endpoints / listeners
while(ObSet_Size(psObEP) || ObSet_Size(psObEP_Next)) {
VmmCachePrefetchPages3(H, pSystemProcess, psObEP, VMMNET_EP_SIZE, 0);
while((va = ObSet_Pop(psObEP))) {
oFLink = VMMNET_EP_OFFSET;
if(va & 8) {
oFLink -= 8;
va += 8;
}
VmmRead2(H, pSystemProcess, va, pb, VMMNET_EP_SIZE, VMM_FLAG_FORCECACHE_READ | VMM_FLAG_ZEROPAD_ON_FAIL);
for(o = 0x10; o < 0x80; o += 0x10) {
tag = 0;
if(VMM_POOLTAG_PREPENDED(f32, pb, o, 'TcpL')) { tag = 'TcpL'; }
if(VMM_POOLTAG_PREPENDED(f32, pb, o, 'TcpE')) { tag = 'TcpE'; }
if(VMM_POOLTAG_PREPENDED(f32, pb, o, 'UdpA')) { tag = 'UdpA'; }
if(tag) {
VmmNet_InPP_Dispatch(H, ctx, pSystemProcess, tag, va + o, pb + o, VMMNET_EP_SIZE - 8 - o, oFLink - o, psObEP_Next, pmObNetEntriesPre);
break;
}
}
}
psObEP_SWAP = psObEP;
psObEP = psObEP_Next;
psObEP_Next = psObEP_SWAP;
}
// post processing of entries
// TODO: add post processing of InPP-TcpE
VmmNet_InPP_PostTcpLUdpA(H, ctx, pSystemProcess, pmObNetEntriesPre, pmNetEntries);
fail:
Ob_DECREF(psObEP_Next);
Ob_DECREF(psObEP);
Ob_DECREF(psObPreEP);
Ob_DECREF(psObPA);
Ob_DECREF(pmObNetEntriesPre);
return 0;
}
// ----------------------------------------------------------------------------
// INITIALIZATION / MAP CREATE FUNCTIONALITY BELOW:
// ----------------------------------------------------------------------------
VOID VmmNet_CallbackCleanup_ObMapNet(PVMMOB_MAP_NET pOb)
{
LocalFree(pOb->pbMultiText);
}
/*
* Set static offsets for the 'TcpL' / 'UdpA' / 'TcTW' structs depending on OS version.
*/
VOID VmmNet_Initialize_Context_Fuzz_TcpL_UdpA_TcTW(_In_ VMM_HANDLE H, _In_ PVMMNET_CONTEXT ctx)
{
PVMMNET_OFFSET_TcTW potw;
PVMMNET_OFFSET_TcpL_UdpA po;
DWORD dwBuild = H->vmm.kernel.dwVersionBuild;
// TcpL
po = &ctx->oTcpL;
if(dwBuild >= 10240) {
// WIN10+
po->INET_AF = 0x28;
po->EProcess = 0x30;
po->Time = 0x40;
po->SrcAddr = 0x60;
po->SrcPort = 0x72;
po->FLink = 0x78;
} else {
// VISTA - WIN8.1
po->INET_AF = 0x60;
po->EProcess = 0x28;
po->SrcAddr = 0x58;
po->SrcPort = 0x6a;
po->FLink = 0x70;
}
po->INET_AF_AF = (dwBuild < 9200) ? 0x14 : 0x18; // VISTA-WIN7 or WIN8+
po->_Size = po->FLink + 8;
// UdpA
po = &ctx->oUdpA;
if(dwBuild >= 19041) { // WIN10 / WIN11 / SERVER2022
po->SrcAddr = 0xa8;
po->SrcPort = 0xa0;
po->DstPort = 0x110;
po->FLink = 0x70; // ??
} else if(dwBuild >= 10240) { // WIN10
po->SrcAddr = 0x80;
po->SrcPort = 0x78;
po->FLink = 0x70;
} else { // VISTA - WIN8.1
po->SrcAddr = 0x60;
po->SrcPort = 0x80;
po->FLink = 0x88;
}
po->INET_AF = 0x20;
po->EProcess = 0x28;
po->Time = 0x58;
po->INET_AF_AF = (dwBuild < 9200) ? 0x14 : 0x18; // VISTA-WIN7 or WIN8+
po->_Size = max(max(po->SrcAddr, po->SrcPort), max(po->DstPort, po->FLink)) + 8;
// TcTW
potw = &ctx->oTcTW;
potw->_Size = 0xA0;
potw->INET_AF = 0x30;
potw->INET_Addr = 0x50;
potw->PortSrc = 0x4A;
potw->PortDst = 0x4C;
potw->AddrDst = 0x58;
potw->Time = 0x98;
}
/*
* Helper function for VmmNet_Initialize_Context in order to locate the TCP/UDP
* port pool structures in memory on more recent Windows 10 systems.
* The port pool may be located by traversing pool allocations such as:
* tcpip!TcpCompartmentSet [InCS] -> [InCo] -> [TcCo/UdCo] -> [InPP large].
* -- H
* -- pSystemProcess
* -- vaInCS
* -- dwTagTcpUdp
* -- return = address of TcpPortPool / UdpPortPool on success, zero on fail.
*/
QWORD VmmNet_Initialize_Context_PortPool(_In_ VMM_HANDLE H, _In_ PVMM_PROCESS pSystemProcess, _In_ QWORD vaInCS, _In_ DWORD dwTagTcpUdp)
{
BOOL f, f32 = H->vmm.f32;
BYTE pb[0x200];
POB_SET psvaOb = NULL;
QWORD o, va, vaTcCo = 0, vaInPP = 0;
// 1: InCS struct
if(!(psvaOb = ObSet_New(H))) { goto fail; }
if(!VmmRead(H, pSystemProcess, vaInCS - 0x10, pb, 0x180)) { goto fail; }
if(!VMM_POOLTAG_PREPENDED(f32, pb, 0x10, 'InCS')) { goto fail; }
for(o = 0x10; o < 0x180; o += 8) {
va = *(PQWORD)(pb + o);
if(VMM_KADDR64_16(va)) {
ObSet_Push(psvaOb, va - 0x10);
}
}
// 2: InCo struct
VmmCachePrefetchPages3(H, pSystemProcess, psvaOb, 0x40, 0);
while((va = ObSet_Pop(psvaOb))) {
f = VmmRead2(H, pSystemProcess, va, pb, 0x40, VMM_FLAG_FORCECACHE_READ) &&
VMM_POOLTAG_PREPENDED(f32, pb, 0x10, 'InCo') &&
(va = *(PQWORD)(pb + 0x30)) &&
VMM_KADDR64_16(va);
if(f) {
vaTcCo = va;
break;
}
}
if(!vaTcCo) { goto fail; }
// 3: TcCo/UdCo struct
f = VmmRead(H, pSystemProcess, vaTcCo - 0x10, pb, 0x20) &&
VMM_POOLTAG_PREPENDED(f32, pb, 0x10, dwTagTcpUdp) &&
(vaInPP = *(PQWORD)(pb + 0x10)) &&
VMM_KADDR64_PAGE(vaInPP);
if(!f) { vaInPP = 0; }
fail:
Ob_DECREF(psvaOb);
return vaInPP;
}
/*
* Initialize the network context containing various offsets and other
* required data to look up networking information.
* -- pSystemProcess
*/
VOID VmmNet_Initialize_Context(_In_ VMM_HANDLE H, _In_ PVMM_PROCESS pSystemProcess)