分享
 
 
 

A more stable way to locate real KiServiceTable

王朝other·作者佚名  2006-04-26
窄屏简体版  字體: |||超大  

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)))

#define ibaseDD *(PDWORD)&ibase

#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)

#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)

typedef struct {

WORD offset:12;

WORD type:4;

} IMAGE_FIXUP_ENTRY, *PIMAGE_FIXUP_ENTRY;

typedef LONG NTSTATUS;

#ifdef __cplusplus

extern "C" {

#endif

NTSTATUS

WINAPI

NtQuerySystemInformation(

DWORD SystemInformationClass,

PVOID SystemInformation,

ULONG SystemInformationLength,

PULONG ReturnLength

);

#ifdef __cplusplus

}

#endif

typedef struct _SYSTEM_MODULE_INFORMATION {//Information Class 11

ULONG Reserved[2];

PVOID Base;

ULONG Size;

ULONG Flags;

USHORT Index;

USHORT Unknown;

USHORT LoadCount;

USHORT ModuleNameOffset;

CHAR ImageName[256];

}SYSTEM_MODULE_INFORMATION,*PSYSTEM_MODULE_INFORMATION;

typedef struct {

DWORD dwNumberOfModules;

SYSTEM_MODULE_INFORMATION smi;

} MODULES, *PMODULES;

#define SystemModuleInformation 11

DWORD GetHeaders(PCHAR ibase,

PIMAGE_FILE_HEADER *pfh,

PIMAGE_OPTIONAL_HEADER *poh,

PIMAGE_SECTION_HEADER *psh)

{

PIMAGE_DOS_HEADER mzhead=(PIMAGE_DOS_HEADER)ibase;

if ((mzhead->e_magic!=IMAGE_DOS_SIGNATURE) ||

(ibaseDD[mzhead->e_lfanew]!=IMAGE_NT_SIGNATURE))

return FALSE;

*pfh=(PIMAGE_FILE_HEADER)&ibase[mzhead->e_lfanew];

if (((PIMAGE_NT_HEADERS)*pfh)->Signature!=IMAGE_NT_SIGNATURE)

return FALSE;

*pfh=(PIMAGE_FILE_HEADER)((PBYTE)*pfh+sizeof(IMAGE_NT_SIGNATURE));

*poh=(PIMAGE_OPTIONAL_HEADER)((PBYTE)*pfh+sizeof(IMAGE_FILE_HEADER));

if ((*poh)->Magic!=IMAGE_NT_OPTIONAL_HDR32_MAGIC)

return FALSE;

*psh=(PIMAGE_SECTION_HEADER)((PBYTE)*poh+sizeof(IMAGE_OPTIONAL_HEADER));

return TRUE;

}

DWORD FindKiServiceTable(HMODULE hModule,DWORD dwKSDT)

{

PIMAGE_FILE_HEADER pfh;

PIMAGE_OPTIONAL_HEADER poh;

PIMAGE_SECTION_HEADER psh;

PIMAGE_BASE_RELOCATION pbr;

PIMAGE_FIXUP_ENTRY pfe;

DWORD dwFixups=0,i,dwPointerRva,dwPointsToRva,dwKiServiceTable;

BOOL bFirstChunk;

GetHeaders((PBYTE)hModule,&pfh,&poh,&psh);

// loop thru relocs to speed up the search

if ((poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress) &&

(!((pfh->Characteristics)&IMAGE_FILE_RELOCS_STRIPPED))) {

pbr=(PIMAGE_BASE_RELOCATION)RVATOVA(poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress,hModule);

bFirstChunk=TRUE;

// 1st IMAGE_BASE_RELOCATION.VirtualAddress of ntoskrnl is 0

while (bFirstChunk || pbr->VirtualAddress) {

bFirstChunk=FALSE;

pfe=(PIMAGE_FIXUP_ENTRY)((DWORD)pbr+sizeof(IMAGE_BASE_RELOCATION));

for (i=0;i<(pbr->SizeOfBlock-sizeof(IMAGE_BASE_RELOCATION))>>1;i++,pfe++) {

if (pfe->type==IMAGE_REL_BASED_HIGHLOW) {

dwFixups++;

dwPointerRva=pbr->VirtualAddress+pfe->offset;

// DONT_RESOLVE_DLL_REFERENCES flag means relocs aren't fixed

dwPointsToRva=*(PDWORD)((DWORD)hModule+dwPointerRva)-(DWORD)poh->ImageBase;

// does this reloc point to KeServiceDescriptorTable.Base?

if (dwPointsToRva==dwKSDT) {

// check for mov [mem32],imm32. we are trying to find

// "mov ds:_KeServiceDescriptorTable.Base, offset _KiServiceTable"

// from the KiInitSystem.

if (*(PWORD)((DWORD)hModule+dwPointerRva-2)==0x05c7) {

// should check for a reloc presence on KiServiceTable here

// but forget it

dwKiServiceTable=*(PDWORD)((DWORD)hModule+dwPointerRva+4)-poh->ImageBase;

return dwKiServiceTable;

}

}

} else

if (pfe->type!=IMAGE_REL_BASED_ABSOLUTE)

// should never get here

printf("\trelo type %d found at .%X\n",pfe->type,pbr->VirtualAddress+pfe->offset);

}

*(PDWORD)&pbr+=pbr->SizeOfBlock;

}

}

if (!dwFixups)

// should never happen - nt, 2k, xp kernels have relocation data

printf("No fixups!\n");

return 0;

}

void main(int argc,char *argv[])

{

HMODULE hKernel;

DWORD dwKSDT; // rva of KeServiceDescriptorTable

DWORD dwKiServiceTable; // rva of KiServiceTable

PMODULES pModules=(PMODULES)&pModules;

DWORD dwNeededSize,rc;

DWORD dwKernelBase,dwServices=0;

PCHAR pKernelName;

PDWORD pService;

PIMAGE_FILE_HEADER pfh;

PIMAGE_OPTIONAL_HEADER poh;

PIMAGE_SECTION_HEADER psh;

// get system modules - ntoskrnl is always first there

rc=NtQuerySystemInformation(SystemModuleInformation,pModules,4,&dwNeededSize);

if (rc==STATUS_INFO_LENGTH_MISMATCH) {

pModules=GlobalAlloc(GPTR,dwNeededSize);

rc=NtQuerySystemInformation(SystemModuleInformation,pModules,dwNeededSize,NULL);

} else {

strange:

printf("strange NtQuerySystemInformation()!\n");

return;

}

if (!NT_SUCCESS(rc)) goto strange;

// imagebase

dwKernelBase=(DWORD)pModules->smi.Base;

// filename - it may be renamed in the boot.ini

pKernelName=pModules->smi.ModuleNameOffset+pModules->smi.ImageName;

// map ntoskrnl - hopefully it has relocs

hKernel=LoadLibraryEx(pKernelName,0,DONT_RESOLVE_DLL_REFERENCES);

if (!hKernel) {

printf("Failed to load! LastError=%i\n",GetLastError());

return;

}

GlobalFree(pModules);

// our own export walker is useless here - we have GetProcAddress :)

if (!(dwKSDT=(DWORD)GetProcAddress(hKernel,"KeServiceDescriptorTable"))) {

printf("Can't find KeServiceDescriptorTable\n");

return;

}

// get KeServiceDescriptorTable rva

dwKSDT-=(DWORD)hKernel;

// find KiServiceTable

if (!(dwKiServiceTable=FindKiServiceTable(hKernel,dwKSDT))) {

printf("Can't find KiServiceTable...\n");

return;

}

printf("&KiServiceTable==%08X\n\nDumping 'old' ServiceTable:\n\n",

dwKiServiceTable+dwKernelBase);

// let's dump KiServiceTable contents

// MAY FAIL!!!

// should get right ServiceLimit here, but this is trivial in the kernel mode

GetHeaders((PBYTE)hKernel,&pfh,&poh,&psh);

for (pService=(PDWORD)((DWORD)hKernel+dwKiServiceTable);

*pService-poh->ImageBase<poh->SizeOfImage;

pService++,dwServices++)

printf("%08X\n",*pService-poh->ImageBase+dwKernelBase);

printf("\n\nPossibly KiServiceLimit==%08X\n",dwServices);

FreeLibrary(hKernel);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有