-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutility.cpp
More file actions
54 lines (45 loc) · 1.67 KB
/
Copy pathutility.cpp
File metadata and controls
54 lines (45 loc) · 1.67 KB
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
#include "stdafx.h"
#include "utility.h"
PVOID GetNtosKernelBaseAddress()
{
UNICODE_STRING strPsLoadedModuleList = RTL_CONSTANT_STRING(L"PsLoadedModuleList");
UNICODE_STRING strNtoskrnl = RTL_CONSTANT_STRING(L"ntoskrnl.exe");
PVOID pPsLoadedModuleList = MmGetSystemRoutineAddress(&strPsLoadedModuleList);
if (pPsLoadedModuleList == NULL)
return NULL;
PLDR_DATA_TABLE_ENTRY pCurrentEntry = (PLDR_DATA_TABLE_ENTRY)((PLDR_DATA_TABLE_ENTRY)pPsLoadedModuleList)->InLoadOrderLinks.Flink;
while (pCurrentEntry != pPsLoadedModuleList)
{
if (RtlCompareUnicodeString(&pCurrentEntry->BaseDllName, &strNtoskrnl, TRUE) == 0)
{
return pCurrentEntry->DllBase;
}
pCurrentEntry = (PLDR_DATA_TABLE_ENTRY)pCurrentEntry->InLoadOrderLinks.Flink;
}
return NULL;
}
LPWSTR GetNearestDriverNameFromOffset(
_In_ PVOID pOffset
)
{
UNICODE_STRING strPsLoadedModuleList = RTL_CONSTANT_STRING(L"PsLoadedModuleList");
PVOID pPsLoadedModuleList = MmGetSystemRoutineAddress(&strPsLoadedModuleList);
if (pPsLoadedModuleList == NULL)
return NULL;
PLDR_DATA_TABLE_ENTRY pFoundEntry = NULL;
PLDR_DATA_TABLE_ENTRY pCurrentEntry = (PLDR_DATA_TABLE_ENTRY)((PLDR_DATA_TABLE_ENTRY)pPsLoadedModuleList)->InLoadOrderLinks.Flink;
while (pCurrentEntry != pPsLoadedModuleList)
{
// Check offset is larger than DllBase and nearest than any others.
if (((ULONG_PTR)pCurrentEntry->DllBase <= (ULONG_PTR)pOffset) && ((ULONG_PTR)pOffset <= (ULONG_PTR)pCurrentEntry->DllBase + pCurrentEntry->SizeOfImage))
{
pFoundEntry = pCurrentEntry;
}
pCurrentEntry = (PLDR_DATA_TABLE_ENTRY)pCurrentEntry->InLoadOrderLinks.Flink;
}
if (pFoundEntry != NULL)
{
return pFoundEntry->BaseDllName.Buffer;
}
return NULL;
}