forked from ufrisk/MemProcFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infodb.c
667 lines (635 loc) · 23.4 KB
/
infodb.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
// infodb.c : implementation of the information read-only sqlite database.
//
// (c) Ulf Frisk, 2021-2023
// Author: Ulf Frisk, [email protected]
//
#include "infodb.h"
#include "pe.h"
#include "charutil.h"
#include "util.h"
#include "sqlite/sqlite3.h"
#define INFODB_SQL_POOL_CONNECTION_NUM 4
typedef struct tdOB_INFODB_CONTEXT {
OB ObHdr;
DWORD dwPdbId_NT;
DWORD dwPdbId_TcpIp;
BOOL fPdbId_TcpIp_TryComplete;
HANDLE hEventIngestPhys[INFODB_SQL_POOL_CONNECTION_NUM];
sqlite3 *hSql[INFODB_SQL_POOL_CONNECTION_NUM];
} OB_INFODB_CONTEXT, *POB_INFODB_CONTEXT;
// ----------------------------------------------------------------------------
// SQLITE GENERAL FUNCTIONALITY:
// ----------------------------------------------------------------------------
/*
* Retrieve an SQLITE database handle. The retrieved handle must be
* returned with Fc_SqlReserveReturn(H, ).
* -- H
* -- ctx
* -- return = an SQLITE handle, or NULL on error.
*/
_Success_(return != NULL)
sqlite3 *InfoDB_SqlReserve(_In_ VMM_HANDLE H, _In_ POB_INFODB_CONTEXT ctx)
{
DWORD iWaitNum = 0;
iWaitNum = WaitForMultipleObjects(INFODB_SQL_POOL_CONNECTION_NUM, ctx->hEventIngestPhys, FALSE, INFINITE) - WAIT_OBJECT_0;
if(iWaitNum >= INFODB_SQL_POOL_CONNECTION_NUM) {
VmmLog(H, MID_INFODB, LOGLEVEL_CRITICAL, "DATABASE ERROR: WaitForMultipleObjects ERROR: 0x%08x", (DWORD)(iWaitNum + WAIT_OBJECT_0));
return NULL;
}
return ctx->hSql[iWaitNum];
}
/*
* Return a SQLITE database handle previously retrieved with Fc_SqlReserve()
* so that other threads may use it.
* -- ctx
* -- hSql = the SQLITE database handle.
* -- return = always NULL.
*/
_Success_(return != NULL)
sqlite3 *InfoDB_SqlReserveReturn(_In_opt_ POB_INFODB_CONTEXT ctx, _In_opt_ sqlite3 *hSql)
{
DWORD i;
if(!ctx || !hSql) { return NULL; }
for(i = 0; i < INFODB_SQL_POOL_CONNECTION_NUM; i++) {
if(ctx->hSql[i] == hSql) {
SetEvent(ctx->hEventIngestPhys[i]);
break;
}
}
return NULL;
}
/*
* Execute a single SQLITE database SQL query and return the SQLITE result code.
* -- H
* -- ctx
* -- szSql
* -- return = sqlite return code.
*/
_Success_(return == SQLITE_OK)
int InfoDB_SqlExec(_In_ VMM_HANDLE H, _In_ POB_INFODB_CONTEXT ctx, _In_ LPSTR szSql)
{
int rc = SQLITE_ERROR;
sqlite3 *hSql = InfoDB_SqlReserve(H, ctx);
if(hSql) {
rc = sqlite3_exec(hSql, szSql, NULL, NULL, NULL);
InfoDB_SqlReserveReturn(ctx, hSql);
}
return rc;
}
/*
* Execute a single SQLITE database SQL query and return all results as numeric
* 64-bit results in an array that must have capacity to hold all values.
* result and the SQLITE result code.
* -- H
* -- ctx
* -- szSql
* -- cQueryValue = nummber of numeric query arguments-
* -- pqwQueryValues = array of 64-bit query arguments-
* -- cResultValues = max number of numeric query results.
* -- pqwResultValues = array to receive 64-bit query results.
* -- pcResultValues = optional to receive number of query results read.
* -- return = sqlite return code.
*/
_Success_(return == SQLITE_OK)
int InfoDB_SqlQueryN(_In_ VMM_HANDLE H, _In_ POB_INFODB_CONTEXT ctx, _In_ LPSTR szSql, _In_ DWORD cQueryValues, _In_reads_(cQueryValues) PQWORD pqwQueryValues, _In_ DWORD cResultValues, _Out_writes_(cResultValues) PQWORD pqwResultValues, _Out_opt_ PDWORD pcResultValues)
{
int rc = SQLITE_ERROR;
DWORD i, iMax;
sqlite3 *hSql = InfoDB_SqlReserve(H, ctx);
sqlite3_stmt *hStmt = NULL;
if(hSql) {
rc = sqlite3_prepare_v2(hSql, szSql, -1, &hStmt, 0);
if(rc != SQLITE_OK) { goto fail; }
for(i = 0; i < cQueryValues; i++) {
sqlite3_bind_int64(hStmt, i + 1, pqwQueryValues[i]);
}
rc = sqlite3_step(hStmt);
if(rc != SQLITE_ROW) { goto fail; }
iMax = sqlite3_column_count(hStmt);
if(pcResultValues) {
*pcResultValues = iMax;
}
if(iMax > cResultValues) {
rc = SQLITE_ERROR;
goto fail;
}
for(i = 0; i < iMax; i++) {
pqwResultValues[i] = sqlite3_column_int64(hStmt, i);
}
rc = SQLITE_OK;
}
fail:
sqlite3_finalize(hStmt);
InfoDB_SqlReserveReturn(ctx, hSql);
if(pcResultValues) { *pcResultValues = 0; }
return rc;
}
// ----------------------------------------------------------------------------
// GENERAL INTERNAL FUNCTIONALITY BELOW:
// ----------------------------------------------------------------------------
DWORD InfoDB_GetPdbId(_In_ VMM_HANDLE H, _In_ POB_INFODB_CONTEXT ctx, _In_ QWORD vaModuleBase)
{
PVMM_PROCESS pObSystemProcess = NULL;
PE_CODEVIEW_INFO CodeViewInfo = { 0 };
QWORD qwEndGUID;
DWORD dwPdbId = 0;
CHAR szAgeGUID[0x40] = { 0 };
int rc;
sqlite3_stmt *hStmt = NULL;
sqlite3 *hSql = InfoDB_SqlReserve(H, ctx);
if(!(pObSystemProcess = VmmProcessGet(H, 4))) { goto fail; }
if(!PE_GetCodeViewInfo(H, pObSystemProcess, vaModuleBase, NULL, &CodeViewInfo)) { goto fail; }
qwEndGUID = *(PQWORD)(CodeViewInfo.CodeView.Guid + 8);
_snprintf_s(szAgeGUID, sizeof(szAgeGUID), _TRUNCATE, "%08X%04X%04X%016llX%X",
*(PDWORD)(CodeViewInfo.CodeView.Guid + 0),
*(PWORD)(CodeViewInfo.CodeView.Guid + 4),
*(PWORD)(CodeViewInfo.CodeView.Guid + 6),
(QWORD)_byteswap_uint64(qwEndGUID),
CodeViewInfo.CodeView.Age);
VmmLog(H, MID_INFODB, LOGLEVEL_TRACE, "AGEGUID=%s va=0x%llx", szAgeGUID, vaModuleBase);
rc = sqlite3_prepare_v2(hSql, "SELECT id FROM pdb WHERE guidage = ?", -1, &hStmt, 0);
if(rc != SQLITE_OK) { goto fail; }
sqlite3_bind_text(hStmt, 1, szAgeGUID, -1, NULL);
rc = sqlite3_step(hStmt);
if(rc != SQLITE_ROW) { goto fail; }
dwPdbId = (DWORD)sqlite3_column_int(hStmt, 0);
fail:
VmmLog(H, MID_INFODB, LOGLEVEL_VERBOSE, "INIT: %s: va=0x%llx", (dwPdbId ? "SUCCESS" : "FAIL"), vaModuleBase);
sqlite3_finalize(hStmt);
InfoDB_SqlReserveReturn(ctx, hSql);
Ob_DECREF(pObSystemProcess);
return dwPdbId;
}
DWORD InfoDB_EnsureTcpIp(_In_ VMM_HANDLE H, _In_ POB_INFODB_CONTEXT ctx)
{
PVMMOB_MAP_MODULE pObModuleMap = NULL;
PVMM_MAP_MODULEENTRY peModuleTcpip;
if(!ctx->fPdbId_TcpIp_TryComplete) {
EnterCriticalSection(&H->vmm.LockMaster);
if(!ctx->fPdbId_TcpIp_TryComplete && VmmMap_GetModuleEntryEx(H, NULL, 4, "tcpip.sys", 0, &pObModuleMap, &peModuleTcpip)) {
ctx->dwPdbId_TcpIp = InfoDB_GetPdbId(H, ctx, peModuleTcpip->vaBase);
ctx->fPdbId_TcpIp_TryComplete = TRUE;
Ob_DECREF_NULL(&pObModuleMap);
}
LeaveCriticalSection(&H->vmm.LockMaster);
}
return ctx->dwPdbId_TcpIp;
}
// ----------------------------------------------------------------------------
// INFO QUERY FUNCTIONALITY BELOW:
// ----------------------------------------------------------------------------
/*
* Check if a certificate is well know against the database.
* -- H
* -- qwThumbprintEndSHA1 = QWORD representation of the last 64 bits of the SHA-1 certificate thumbprint.
* -- return
*/
_Success_(return)
BOOL InfoDB_CertIsWellKnown(_In_ VMM_HANDLE H, _In_ QWORD qwThumbprintEndSHA1)
{
QWORD qwResult = 0;
POB_INFODB_CONTEXT pObCtx = NULL;
qwThumbprintEndSHA1 = qwThumbprintEndSHA1 & 0x7fffffffffffffff;
if(!(pObCtx = ObContainer_GetOb(H->vmm.pObCInfoDB)) || !pObCtx->dwPdbId_NT) { goto fail; }
InfoDB_SqlQueryN(H, pObCtx, "SELECT count(*) FROM cert WHERE hash = ?", 1, &qwThumbprintEndSHA1, 1, &qwResult, NULL);
fail:
Ob_DECREF(pObCtx);
return (1 == qwResult);
}
/*
* Query the InfoDB for the offset of a symbol.
* Currently only szModule values of 'nt', 'ntoskrnl', 'tcpip' is supported.
* -- H
* -- szModule
* -- szSymbolName
* -- pdwSymbolOffset
* -- return
*/
_Success_(return)
BOOL InfoDB_SymbolOffset(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ LPSTR szSymbolName, _Out_ PDWORD pdwSymbolOffset)
{
BOOL fResult = FALSE;
POB_INFODB_CONTEXT pObCtx = NULL;
QWORD qwHash, qwResult = 0, qwPdbId = 0;
*pdwSymbolOffset = 0;
if(!(pObCtx = ObContainer_GetOb(H->vmm.pObCInfoDB))) { goto fail; }
if(!strcmp(szModule, "nt") || !strcmp(szModule, "ntoskrnl")) {
qwPdbId = pObCtx->dwPdbId_NT;
} else if(!strcmp(szModule, "tcpip")) {
qwPdbId = InfoDB_EnsureTcpIp(H, pObCtx);
}
if(!qwPdbId) { goto fail; }
qwHash = CharUtil_Hash32A(szSymbolName, FALSE) + (qwPdbId << 32);
if(SQLITE_OK == InfoDB_SqlQueryN(H, pObCtx, "SELECT data FROM symbol_offset WHERE hash = ?", 1, &qwHash, 1, &qwResult, NULL)) {
*pdwSymbolOffset = (DWORD)qwResult;
fResult = TRUE;
}
fail:
Ob_DECREF(pObCtx);
if(!fResult) {
VmmLog(H, MID_INFODB, LOGLEVEL_TRACE, "Missing SymbolOffset: %s", szSymbolName);
}
return fResult;
}
/*
* Read memory at the symbol offset.
* -- H
* -- szModule
* -- vaModuleBase
* -- szSymbolName
* -- pProcess
* -- pb
* -- cb
* -- return
*/
_Success_(return)
BOOL InfoDB_SymbolPBYTE(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ QWORD vaModuleBase, _In_ LPSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_writes_(cb) PBYTE pb, _In_ DWORD cb)
{
DWORD dwSymbolOffset = 0;
if(!InfoDB_SymbolOffset(H, szModule, szSymbolName, &dwSymbolOffset)) { return FALSE; }
return VmmRead(H, pProcess, vaModuleBase + dwSymbolOffset, pb, cb);
}
/*
* Read memory pointed to at the symbol offset.
* -- H
* -- szModule
* -- vaModuleBase
* -- szSymbolName
* -- pProcess
* -- pqw
* -- return
*/
_Success_(return)
BOOL InfoDB_GetSymbolQWORD(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ QWORD vaModuleBase, _In_ LPSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PQWORD pqw)
{
return InfoDB_SymbolPBYTE(H, szModule, vaModuleBase, szSymbolName, pProcess, (PBYTE)pqw, sizeof(QWORD));
}
/*
* Read memory pointed to at the symbol offset.
* -- H
* -- szModule
* -- vaModuleBase
* -- szSymbolName
* -- pProcess
* -- pdw
* -- return
*/
_Success_(return)
BOOL InfoDB_SymbolDWORD(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ QWORD vaModuleBase, _In_ LPSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PDWORD pdw)
{
return InfoDB_SymbolPBYTE(H, szModule, vaModuleBase, szSymbolName, pProcess, (PBYTE)pdw, sizeof(DWORD));
}
/*
* Read memory pointed to at the symbol offset.
* -- H
* -- szModule
* -- vaModuleBase
* -- szSymbolName
* -- pProcess
* -- pv = PDWORD on 32-bit and PQWORD on 64-bit _operating_system_ architecture.
* -- return
*/
_Success_(return)
BOOL InfoDB_SymbolPTR(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ QWORD vaModuleBase, _In_ LPSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PVOID pv)
{
return InfoDB_SymbolPBYTE(H, szModule, vaModuleBase, szSymbolName, pProcess, (PBYTE)pv, (H->vmm.f32 ? sizeof(DWORD) : sizeof(QWORD)));
}
/*
* Query the InfoDB for a static size populated in the static_type_size table.
* -- H
* -- szModule
* -- szTypeName
* -- pdwTypeSize
* -- return
*/
_Success_(return)
BOOL InfoDB_TypeSize_Static(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ LPSTR szTypeName, _Out_ PDWORD pdwTypeSize)
{
int r, rc = SQLITE_ERROR;
sqlite3_stmt *hStmt = NULL;
sqlite3 *hSql = NULL;
POB_INFODB_CONTEXT pObCtx = NULL;
DWORD dwArch = H->vmm.f32 ? 32 : 64;
*pdwTypeSize = 0;
if(!(pObCtx = ObContainer_GetOb(H->vmm.pObCInfoDB))) { goto fail; }
hSql = InfoDB_SqlReserve(H, pObCtx);
if(!H->vmm.f32 && (szModule[0] == 'w')) {
// wow64 modules start with 'w' (wntdll == 32-bit ntdll on a 64-bit system) -> use the 32-bit offset instead.
szModule = szModule + 1;
dwArch = 32;
}
if(hSql) {
rc = sqlite3_prepare_v2(hSql, "SELECT value FROM static_type_size WHERE module = ? AND type = ? AND arch = ? AND build <= ? ORDER BY build DESC LIMIT 1", -1, &hStmt, 0);
if(rc != SQLITE_OK) { goto fail; }
sqlite3_bind_text(hStmt, 1, szModule, -1, NULL);
sqlite3_bind_text(hStmt, 2, szTypeName, -1, NULL);
sqlite3_bind_int(hStmt, 3, dwArch);
sqlite3_bind_int(hStmt, 4, H->vmm.kernel.dwVersionBuild);
rc = sqlite3_step(hStmt);
if(rc != SQLITE_ROW) { goto fail; }
r = sqlite3_column_int(hStmt, 0);
if(r < 0) { rc = SQLITE_ERROR; goto fail; }
*pdwTypeSize = r;
rc = SQLITE_OK;
}
fail:
sqlite3_finalize(hStmt);
InfoDB_SqlReserveReturn(pObCtx, hSql);
Ob_DECREF(pObCtx);
if(rc == SQLITE_OK) {
return TRUE;
}
VmmLog(H, MID_INFODB, LOGLEVEL_TRACE, "Missing TypeSize(Static): %s!%s", szModule, szTypeName);
return FALSE;
}
/*
* Query the InfoDB for the size of a type.
* Support for 'nt', 'tcpip'.
* -- H
* -- szModule
* -- szTypeName
* -- pdwTypeSize
* -- return
*/
_Success_(return)
BOOL InfoDB_TypeSize_Dynamic(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ LPSTR szTypeName, _Out_ PDWORD pdwTypeSize)
{
DWORD dwPdbId = 0;
QWORD qwHash, qwResult = 0;
POB_INFODB_CONTEXT pObCtx = NULL;
if(!(pObCtx = ObContainer_GetOb(H->vmm.pObCInfoDB))) { goto fail; }
if(!strcmp(szModule, "nt") || !strcmp(szModule, "ntoskrnl")) {
dwPdbId = pObCtx->dwPdbId_NT;
} else if(!strcmp(szModule, "tcpip")) {
dwPdbId = InfoDB_EnsureTcpIp(H, pObCtx);
}
if(!dwPdbId) { goto fail; }
qwHash = CharUtil_Hash32A(szTypeName, FALSE) + ((QWORD)dwPdbId << 32);
if(SQLITE_OK == InfoDB_SqlQueryN(H, pObCtx, "SELECT data FROM type_size WHERE hash = ?", 1, &qwHash, 1, &qwResult, NULL)) {
*pdwTypeSize = (DWORD)qwResult;
Ob_DECREF(pObCtx);
return TRUE;
}
fail:
VmmLog(H, MID_INFODB, LOGLEVEL_TRACE, "Missing TypeSize(Dynamic): %s!%s", szModule, szTypeName);
Ob_DECREF(pObCtx);
return FALSE;
}
/*
* Query the InfoDB for the static offset of a child inside a type - often inside a struct.
* Support for nt/ntoskrnl/tcpip.
* -- H
* -- szModule
* -- szTypeName
* -- uszTypeChildName
* -- pdwTypeOffset = offset relative to type base.
* -- return
*/
_Success_(return)
BOOL InfoDB_TypeChildOffset_Static(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ LPSTR szTypeName, _In_ LPSTR uszTypeChildName, _Out_ PDWORD pdwTypeOffset)
{
int r, rc = SQLITE_ERROR;
sqlite3_stmt *hStmt = NULL;
sqlite3 *hSql = NULL;
POB_INFODB_CONTEXT pObCtx = NULL;
DWORD dwArch = H->vmm.f32 ? 32 : 64;
*pdwTypeOffset = 0;
if(!(pObCtx = ObContainer_GetOb(H->vmm.pObCInfoDB))) { goto fail; }
hSql = InfoDB_SqlReserve(H, pObCtx);
if(!H->vmm.f32 && (szModule[0] == 'w')) {
// wow64 modules start with 'w' (wntdll == 32-bit ntdll on a 64-bit system) -> use the 32-bit offset instead.
szModule = szModule + 1;
dwArch = 32;
}
if(hSql) {
rc = sqlite3_prepare_v2(hSql, "SELECT value FROM static_type_child WHERE module = ? AND type = ? AND child = ? AND arch = ? AND build <= ? ORDER BY build DESC LIMIT 1", -1, &hStmt, 0);
if(rc != SQLITE_OK) { goto fail; }
rc = sqlite3_bind_text(hStmt, 1, szModule, -1, NULL);
rc = sqlite3_bind_text(hStmt, 2, szTypeName, -1, NULL);
rc = sqlite3_bind_text(hStmt, 3, uszTypeChildName, -1, NULL);
rc = sqlite3_bind_int(hStmt, 4, dwArch);
rc = sqlite3_bind_int(hStmt, 5, H->vmm.kernel.dwVersionBuild);
rc = sqlite3_step(hStmt);
if(rc != SQLITE_ROW) { goto fail; }
r = sqlite3_column_int(hStmt, 0);
if(r < 0) { rc = SQLITE_ERROR; goto fail; }
*pdwTypeOffset = r;
rc = SQLITE_OK;
}
fail:
sqlite3_finalize(hStmt);
InfoDB_SqlReserveReturn(pObCtx, hSql);
Ob_DECREF(pObCtx);
if(rc == SQLITE_OK) {
return TRUE;
}
VmmLog(H, MID_INFODB, LOGLEVEL_TRACE, "Missing TypeChildOffset(Static): %s.%s", szTypeName, uszTypeChildName);
return FALSE;
}
/*
* Query the InfoDB for the offset of a child inside a type - often inside a struct.
* Support for nt/ntoskrnl/tcpip.
* -- H
* -- szModule
* -- szTypeName
* -- uszTypeChildName
* -- pdwTypeOffset = offset relative to type base.
* -- return
*/
_Success_(return)
BOOL InfoDB_TypeChildOffset_Dynamic(_In_ VMM_HANDLE H, _In_ LPSTR szModule, _In_ LPSTR szTypeName, _In_ LPSTR uszTypeChildName, _Out_ PDWORD pdwTypeOffset)
{
DWORD dwPdbId = 0;
POB_INFODB_CONTEXT pObCtx = NULL;
QWORD qwHash, qwHash1, qwHash2, qwResult = 0;
if(!(pObCtx = ObContainer_GetOb(H->vmm.pObCInfoDB))) { goto fail; }
if(!strcmp(szModule, "nt") || !strcmp(szModule, "ntoskrnl")) {
dwPdbId = pObCtx->dwPdbId_NT;
} else if(!strcmp(szModule, "tcpip")) {
dwPdbId = InfoDB_EnsureTcpIp(H, pObCtx);
}
if(!dwPdbId) { goto fail; }
qwHash1 = CharUtil_Hash32A(szTypeName, FALSE);
qwHash2 = CharUtil_Hash32U(uszTypeChildName, FALSE);
qwHash = ((qwHash2 << 32) + qwHash1 + dwPdbId + ((QWORD)dwPdbId << 32)) & 0x7fffffffffffffff;
if(SQLITE_OK == InfoDB_SqlQueryN(H, pObCtx, "SELECT data FROM type_child WHERE hash = ?", 1, &qwHash, 1, &qwResult, NULL)) {
*pdwTypeOffset = (DWORD)qwResult;
Ob_DECREF(pObCtx);
return TRUE;
}
fail:
VmmLog(H, MID_INFODB, LOGLEVEL_TRACE, "Missing TypeChildOffset(Dynamic): %s.%s", szTypeName, uszTypeChildName);
Ob_DECREF(pObCtx);
return FALSE;
}
/*
* Return whether the InfoDB symbols are ok or not.
* -- H
* -- pfNtos
* -- pfTcpIp
*/
VOID InfoDB_IsValidSymbols(_In_ VMM_HANDLE H, _Out_opt_ PBOOL pfNtos, _Out_opt_ PBOOL pfTcpIp)
{
BOOL fNtos = FALSE, fTcpIp = FALSE;
POB_INFODB_CONTEXT pObCtx = NULL;
if((pObCtx = ObContainer_GetOb(H->vmm.pObCInfoDB))) {
fNtos = pObCtx->dwPdbId_NT ? TRUE : FALSE;
fTcpIp = pObCtx->dwPdbId_TcpIp ? TRUE : FALSE;
}
if(pfNtos) { *pfNtos = fNtos; }
if(pfTcpIp) { *pfTcpIp = fTcpIp; }
Ob_DECREF(pObCtx);
}
/*
* Lookup well known SIDs from the database.
* This is preferred over system lookups due to english names.
* -- H
* -- szSID = a SID in string format (i.e. S-1-5-19)
* -- szName = buffer of length *pcbName to receive user name on success.
* -- pcbName
* -- szDomain = buffer of length *pcbDomain to receive domain name on success.
* -- pcbDomain
* -- return = the well known username on success, NULL on fail.
*/
_Success_(return)
BOOL InfoDB_SidToUser_Wellknown(
_In_ VMM_HANDLE H,
_In_ LPSTR szSID,
_Out_writes_to_opt_(*pcbName, *pcbName + 1) LPSTR szName,
_Inout_ LPDWORD pcbName,
_Out_writes_to_opt_(*pcbDomain, *pcbDomain + 1) LPSTR szDomain,
_Inout_ LPDWORD pcbDomain
) {
BOOL fResult = FALSE;
int rc = SQLITE_ERROR;
POB_INFODB_CONTEXT pObCtx = NULL;
sqlite3 *hSql = NULL;
sqlite3_stmt *hStmt = NULL;
DWORD cbDomain = 0, cbName = 0;
LPSTR szRID, szQueryResult;
// 1: check built-in well known domain RIDs
if(CharUtil_StrStartsWith(szSID, "S-1-5-21-", FALSE)) {
szRID = NULL;
if(!szRID && CharUtil_StrEndsWith(szSID, "-500", FALSE)) { szRID = "DOMAIN_USER_RID_ADMIN"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-501", FALSE)) { szRID = "DOMAIN_USER_RID_GUEST"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-513", FALSE)) { szRID = "DOMAIN_GROUP_RID_USERS"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-514", FALSE)) { szRID = "DOMAIN_GROUP_RID_GUESTS"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-515", FALSE)) { szRID = "DOMAIN_GROUP_RID_COMPUTERS"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-516", FALSE)) { szRID = "DOMAIN_GROUP_RID_CONTROLLERS"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-517", FALSE)) { szRID = "DOMAIN_GROUP_RID_CERT_ADMINS"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-518", FALSE)) { szRID = "DOMAIN_GROUP_RID_SCHEMA_ADMINS"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-519", FALSE)) { szRID = "DOMAIN_GROUP_RID_ENTERPRISE_ADMINS"; }
if(!szRID && CharUtil_StrEndsWith(szSID, "-520", FALSE)) { szRID = "DOMAIN_GROUP_RID_POLICY_ADMINS"; }
if(szRID) {
cbDomain = (DWORD)strlen("DOMAIN") + 1;
cbName = (DWORD)strlen(szRID) + 1;
if((szName && (*pcbName < cbName)) || (szDomain && (*pcbDomain < cbDomain))) {
goto fail_size;
}
if(szDomain) {
strncpy_s(szDomain, *pcbDomain, "DOMAIN", cbDomain);
}
if(szName) {
szQueryResult = (LPSTR)sqlite3_column_text(hStmt, 1);
strncpy_s(szName, *pcbName, szRID, cbName);
}
goto finish;
}
}
// 2: lookup from well known SIDs in database
if(!(pObCtx = ObContainer_GetOb(H->vmm.pObCInfoDB))) { goto fail; }
if(!(hSql = InfoDB_SqlReserve(H, pObCtx))) { goto fail; }
rc = sqlite3_prepare_v2(hSql, "SELECT domain, name FROM sid WHERE sid = ?", -1, &hStmt, 0);
if(rc != SQLITE_OK) { goto fail; }
rc = sqlite3_bind_text(hStmt, 1, szSID, -1, NULL);
rc = sqlite3_step(hStmt);
if(rc != SQLITE_ROW) { goto fail; }
cbDomain = sqlite3_column_bytes(hStmt, 0) + 1;
cbName = sqlite3_column_bytes(hStmt, 1) + 1;
if((szName && (*pcbName < cbName)) || (szDomain && (*pcbDomain < cbDomain))) {
goto fail_size;
}
if(szDomain) {
szQueryResult = (LPSTR)sqlite3_column_text(hStmt, 0);
strncpy_s(szDomain, *pcbDomain, szQueryResult, cbDomain);
}
if(szName) {
szQueryResult = (LPSTR)sqlite3_column_text(hStmt, 1);
strncpy_s(szName, *pcbName, szQueryResult, cbName);
}
finish:
fResult = TRUE;
fail_size:
*pcbName = cbName;
*pcbDomain = cbDomain;
fail:
sqlite3_finalize(hStmt);
InfoDB_SqlReserveReturn(pObCtx, hSql);
Ob_DECREF(pObCtx);
return fResult;
}
/*
* Return if the InfoDB have been successfully initialized.
* Will return fail on no-init or failure to init (missing info.db file).
* -- H
* -- return;
*/
BOOL InfoDB_IsInitialized(_In_ VMM_HANDLE H)
{
return ObContainer_Exists(H->vmm.pObCInfoDB);
}
/*
* Object cleanup callback for the InfoDB context.
*/
VOID InfoDB_Context_CleanupCB(POB_INFODB_CONTEXT pOb)
{
DWORD i;
for(i = 0; i < INFODB_SQL_POOL_CONNECTION_NUM; i++) {
if(pOb->hEventIngestPhys[i]) {
WaitForSingleObject(pOb->hEventIngestPhys[i], INFINITE);
CloseHandle(pOb->hEventIngestPhys[i]);
pOb->hEventIngestPhys[i] = NULL;
}
if(pOb->hSql[i]) { sqlite3_close(pOb->hSql[i]); }
}
}
VOID InfoDB_Initialize_DoWork(_In_ VMM_HANDLE H)
{
DWORD i;
POB_INFODB_CONTEXT pObCtx = NULL;
CHAR szDbPathFile[MAX_PATH] = { 0 };
// 1: INIT
if(!(pObCtx = Ob_AllocEx(H, OB_TAG_INFODB_CTX, LMEM_ZEROINIT, sizeof(OB_INFODB_CONTEXT), (OB_CLEANUP_CB)InfoDB_Context_CleanupCB, NULL))) { goto fail; }
// 2: SQLITE INIT:
Util_GetPathLib(szDbPathFile);
strncat_s(szDbPathFile, sizeof(szDbPathFile), "info.db", _TRUNCATE);
if(SQLITE_CONFIG_MULTITHREAD != sqlite3_threadsafe()) {
VmmLog(H, MID_INFODB, LOGLEVEL_CRITICAL, "WRONG SQLITE THREADING MODE - TERMINATING!");
ExitProcess(0);
}
for(i = 0; i < INFODB_SQL_POOL_CONNECTION_NUM; i++) {
if(!(pObCtx->hEventIngestPhys[i] = CreateEvent(NULL, FALSE, TRUE, NULL))) { goto fail; }
if(SQLITE_OK != sqlite3_open_v2(szDbPathFile, &pObCtx->hSql[i], SQLITE_OPEN_URI | SQLITE_OPEN_READONLY | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_NOMUTEX, NULL)) { goto fail; }
}
// 3: QUERY CURRENT 'NTOSKRNL.EXE' IMAGE
pObCtx->dwPdbId_NT = InfoDB_GetPdbId(H, pObCtx, H->vmm.kernel.vaBase);
ObContainer_SetOb(H->vmm.pObCInfoDB, pObCtx);
fail:
Ob_DECREF(pObCtx);
}
/*
* Initialize the InfoDB (if possible):
*/
VOID InfoDB_Initialize(_In_ VMM_HANDLE H)
{
if(ObContainer_Exists(H->vmm.pObCInfoDB)) { return; }
if(H->cfg.fDisableInfoDB) {
VmmLog(H, MID_INFODB, LOGLEVEL_INFO, "Info database disabled by user");
return;
}
EnterCriticalSection(&H->vmm.LockMaster);
if(!ObContainer_Exists(H->vmm.pObCInfoDB)) {
InfoDB_Initialize_DoWork(H);
}
LeaveCriticalSection(&H->vmm.LockMaster);
}