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
| #include <windows.h> #include <stdio.h>
#define SystemModuleInformation 11
typedef NTSTATUS(NTAPI *kZwQuerySystemInformation)( _In_ DWORD SystemInformationClass, _Inout_ PVOID SystemInformation, _In_ ULONG SystemInformationLength, _Out_opt_ PULONG ReturnLength );
typedef NTSTATUS(NTAPI *kPsLookupProcessByProcessId)( IN HANDLE ProcessId, OUT PVOID Process );
typedef struct _SYSTEM_MODULE { HANDLE Reserved1; PVOID Reserved2; PVOID ImageBaseAddress; ULONG ImageSize; ULONG Flags; USHORT Id; USHORT Rank; USHORT w018; USHORT NameOffset; BYTE Name[256]; } SYSTEM_MODULE, *PSYSTEM_MODULE;
typedef struct _SYSTEM_MODULE_INFORMATION { ULONG ModulesCount; SYSTEM_MODULE Modules[0]; } SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION;
kPsLookupProcessByProcessId pPsLookupProcessByProcessId = NULL;
int main() { char szNtName[256]; PVOID NtBase; HMODULE hNtdll = LoadLibraryA("ntdll"); if (hNtdll == NULL) { printf("[-] Load Ntdll fail!"); return 0; } kZwQuerySystemInformation pZwQuerySystemInformation = (kZwQuerySystemInformation)GetProcAddress(hNtdll, "ZwQuerySystemInformation"); if (pZwQuerySystemInformation == NULL) { printf("[-] Can not found ZwQuerySystemInformation!"); return 0; } ULONG SystemInfoBufferSize; pZwQuerySystemInformation(SystemModuleInformation, &SystemInfoBufferSize, 0, &SystemInfoBufferSize); if (SystemInfoBufferSize == 0) { printf("[-] SystemInfoBufferSize is 0!"); } PULONG pSystemInfoBuffer = (PULONG)LocalAlloc(LMEM_ZEROINIT, SystemInfoBufferSize); printf("[+] LocalAlloc:0x%p\n", pSystemInfoBuffer); if (pSystemInfoBuffer == 0) { printf("[-] LocalAlloc is fail!"); return -1; } int ret = pZwQuerySystemInformation(SystemModuleInformation, pSystemInfoBuffer, SystemInfoBufferSize, &SystemInfoBufferSize); if (ret) { printf("[-] ZwQuerySystemInformation is fail!"); return -1; }
_SYSTEM_MODULE_INFORMATION* smi = (_SYSTEM_MODULE_INFORMATION *)pSystemInfoBuffer;
printf("[+] Kernel Modle found %d\n", smi->ModulesCount);
memset(szNtName, 0, 256); int i = 0; while (i < smi->ModulesCount) { SYSTEM_MODULE* sm = (SYSTEM_MODULE *)(smi->Modules + i); printf("[*] module %p\n", smi->Modules); printf("[*] Reserved1 0x%p\n", sm->Reserved1); printf("[*] Reserved2 0x%p\n", sm->Reserved2); printf("[*] ImageBaseAddress 0x%p\n", sm->ImageBaseAddress); printf("[*] ImageSize 0x%p\n", sm->ImageSize); printf("[*] Flags 0x%x\n", sm->Flags); printf("[*] Id 0x%x\n", sm->Id); printf("[*] Rank 0x%x\n", sm->Rank); printf("[*] w018 0x%x\n", sm->w018); printf("[*] NameOffset 0x%p\n", sm->NameOffset); printf("[*] Name: %s\n", sm->Name); printf("===========================================\n"); if (strstr((char*)sm->Name, ".exe") && strstr((char*)sm->Name, "nt")) { NtBase = sm->ImageBaseAddress; strncpy_s(szNtName, 256, strstr((char*)sm->Name, "nt"), _TRUNCATE); break; } } printf("name:%s-0x%p\n", szNtName, NtBase); HMODULE nt = LoadLibraryA(szNtName); kPsLookupProcessByProcessId PLPBP = (kPsLookupProcessByProcessId)GetProcAddress(nt, "PsLookupProcessByProcessId"); pPsLookupProcessByProcessId = (kPsLookupProcessByProcessId)((_int64)NtBase + ((_int64)PLPBP - (_int64)nt)); printf("PsLookupProcessByProcessId Address in 0x%p\n",pPsLookupProcessByProcessId);
}
|