Tan Chew Keong in his Win2K/XP SDT Restore 0.1 uses a simple way to find changed SDT entries - he just compares SDT from memory with SDT from ntoskrnl.exe file, assuming that KeServiceDescriptorTable.Base is not changed. If it is, Tan's code that locates KiServiceTable on disk will fail when KeServiceDescriptorTable.Base points somewhere outside the ntoskrnl.
I've found a way how to locate original KiServiceTable in the ntoskrnl file even if KeServiceDescriptorTable.Base has been changed. Method works both in the user mode and in the kmode.
This may be useful to bypass SDT-patching hooks, and not only to restore old SDT. For example, KAV uses SDT relocation ;)
KeServiceDescriptorTable is initialized by KiInitSystem():
代码 |
KeServiceDescriptorTable[0].Base = &KiServiceTable[0]; KeServiceDescriptorTable[0].Count = NULL; KeServiceDescriptorTable[0].Limit = KiServiceLimit; KeServiceDescriptorTable[0].Number = &KiArgumentTable[0]; for (Index = 1; Index < NUMBER_SERVICE_TABLES; Index += 1) { KeServiceDescriptorTable[Index].Limit = 0; } |
Thus, we can find KiServiceTable by examining all xrefs to KeServiceDescriptorTable in the kernel. We will search for
C7 05 ..8 bytes.. mov ds:_KeServiceDescriptorTable.Base, offset _KiServiceTable
from which we will get _KiServiceTable rva.
It's easy to find KeServiceDescriptorTable xrefs by scanning the code, but this is dangerous and time-consuming. It's better to use ntoskrnl's relocation information - it is always present in all nt systems.
This "mov [mem32], imm32" instruction will have 2 relocs pointing in it, and the second is the one we're searching for. So, the usermode code will do these steps:
1. Load ntosknrl as a dll.
2. Locate KeServiceDescriptorTable - it is exported.
3. Enumerate all relocations to find xrefs to the KeServiceDescriptorTable.
4. Check these opcodes to be a "mov [mem32],imm32".
5. Get KiServiceTable - it's offset is +6 from the opcode beginning.
The example code dumps KiServiceTable data from file and does no comparings with the existing sdt.
So, it's a must to a SDT-patching rootkit to hook file system and show "patched" version of ntoskrnl to all readers. And of course, services hooks code must reside in the ntoskrnl region and have no codepaths outside it. Otherwise it may be tracked in seconds.
代码 |
#include <windows.h> #include <winnt.h> #include <stdio.h>
#define RVATOVA(base,offset) ((PVOID)((DWORD)(base)+(DWORD)(offset))) typedef struct {
#ifdef __cplusplus NTSTATUS #ifdef __cplusplus typedef struct _SYSTEM_MODULE_INFORMATION { //Information Class 11 typedef struct { #define SystemModuleInformation 11 DWORD GetHeaders(PCHAR ibase, {
GetHeaders((PBYTE)hModule,&pfh,&poh,&psh); // loop thru relocs to speed up the search bFirstChunk=TRUE; pfe=(PIMAGE_FIXUP_ENTRY)((DWORD)pbr+sizeof(IMAGE_BASE_RELOCATION)); for (i=0;i<(pbr->SizeOfBlock-sizeof(IMAGE_BASE_RELOCATION))>>1;i++,pfe++) { // does this reloc point to KeServiceDescriptorTable.Base? void main(int argc,char *argv[]) GlobalFree(pModules); // our own export walker is useless here - we have GetProcAddress :) // get KeServiceDescriptorTable rva printf("&KiServiceTable==%08X/n/nDumping 'old' ServiceTable:/n/n", printf("/n/nPossibly KiServiceLimit==%08X/n",dwServices); FreeLibrary(hKernel); } |