Win10 x64 KeServiceDescriptorTable SSDT 偏移计算理解

Win10 x64 KeServiceDescriptorTable SSDT 偏移计算理解

引言

很久没写博客了,水个博客,KeServiceDescriptorTableKeServiceDescriptorTableShadow的原理我就不写了,百度很多,主要这篇写如何定位x64未导出KeServiceDescriptorTable

站在巨人的肩膀上

前辈们在之前已经找到了一个计算公式,通过读取msr寄存器的0xC0000082读取到KiSystemCall64,然后通过内存搜索的方式定位到KeServiceDescriptorTable
在这里插入图片描述
继续向下就可以看见KiSystemServiceRepeat,我将特征码使用红色框起来,偏移使用绿色框起来。
在这里插入图片描述

实现代码

如果你不理解x64下的RIP相对寻址,可以看看这篇文章。
REX(Register EXtension) 前缀 可以帮助你理解,代码中读取的偏移为什么从下一条指令位置开始计算

//老的方式
ULONG_PTR GetKeServiceDescriptorTable64()
{
	PUCHAR      pStartSearchAddress = (PUCHAR)__readmsr(0xC0000082);
	PUCHAR      pEndSearchAddress = pStartSearchAddress + 0x500;

	while (++pStartSearchAddress < pEndSearchAddress)
	{
		/*
		const unsigned char matchPattern[] = { 0x4C, 0x8D, 0x15 };
		if (RtlCompareMemory(pStartSearchAddress, matchPattern, 3) == 3)
		{
			//偏移字节码4为 读取成long保存
			LONG offset = *(PLONG)(pStartSearchAddress + 3);

			//此时跳过7位字节码(也就是下次RIP位置) + 偏移 就是 KeServiceDescriptorTable
			return (ULONG_PTR)pStartSearchAddress + 7 + offset;
		}
		*/


		//转为ulong指针读取后与0x00FFFFFF 取出低地址三字节
		//字节码转为小端存储后对比特征码
		if ((*(PULONG)pStartSearchAddress & 0x00FFFFFF) == 0x158D4C)
		{
			//偏移字节码4为 读取成long保存
			LONG offset = *(PLONG)(pStartSearchAddress + 3);

			//此时跳过7位字节码(也就是下次RIP位置) + 偏移 就是 KeServiceDescriptorTable
			return (ULONG_PTR)pStartSearchAddress + 7 + offset;
		}
	}

	return 0;
}

通用的偏移计算方法

但是在部分x64下(据说是1809之后),msr寄存器的0xC0000082有时可能读取到的是KiSystemCall64Shadow,虽然有人通过头部的几位字节判断到底读取的是KiSystemCall64Shadow还是KiSystemCall64,但是我在自己的系统查验时,发现字节码和他系统中并不相同,那么有没有一个办法可以通用呢。

同样有牛人mrexodia干了个更彻底的事,暴力搜索从头到尾,可以得到KiSystemServiceStart的下一条指令地址,这就在KiSystemServiceRepeat附近。在这里插入图片描述
https://github.com/mrexodia/TitanHide/blob/master/TitanHide/ssdt.cpp 从这我们可以看见他具体是如何搜索的。他使用了ZwQuerySystemInformation来查询模块进行导出Module[0].ImageBase,并且新的代码将搜索范围整个模块缩小到了.text段。
我们可以改一改使用LDR来导出ntoskrnl.exe

typedef struct _LDR_DATA_TABLE_ENTRY {
#ifndef _WIN64
	LIST_ENTRY InLoadOrderLinks;//这个成员把系统所有加载(可能是停止没被卸载)已经读取到内存中 本驱动的驱动对象就是一个节点
	LIST_ENTRY InMemoryOrderLinks;//系统已经启动 没有被初始化 没有调用DriverEntry这个历程的时候 通过这个链表进程串接起来
	LIST_ENTRY InInitializationOrderLinks;//已经调用DriverEntry这个函数的所有驱动程序
#else
	LIST_ENTRY64 InLoadOrderLinks;//这个成员把系统所有加载(可能是停止没被卸载)已经读取到内存中 本驱动的驱动对象就是一个节点
	LIST_ENTRY64 InMemoryOrderLinks;//系统已经启动 没有被初始化 没有调用DriverEntry这个历程的时候 通过这个链表进程串接起来
	LIST_ENTRY64 InInitializationOrderLinks;//已经调用DriverEntry这个函数的所有驱动程序
#endif

	ULONG_PTR DllBase;
	ULONG_PTR EntryPoint;//驱动的进入点 DriverEntry
	ULONG SizeOfImage;
	UNICODE_STRING FullDllName;//模块全路径名
	UNICODE_STRING BaseDllName;//不带路径的模块名
	ULONG Flags;
	USHORT LoadCount;
	USHORT TlsIndex;
	union {
#ifndef _WIN64
		LIST_ENTRY HashLinks;
#else
		LIST_ENTRY64 HashLinks;
#endif

		struct {
			ULONG_PTR SectionPointer;
			ULONG CheckSum;
		};
	};
	union {
		struct {
			ULONG TimeDateStamp;
		};
		struct {
			ULONG_PTR LoadedImports;
		};
	};
} LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;

ULONG_PTR GetKernelModuleBase(PDRIVER_OBJECT pDriver, PWCHAR wsModuleName,__out PULONG pImageSize)
{

	UNICODE_STRING moduleName = { 0 };
	RtlInitUnicodeString(&moduleName, wsModuleName);

	ULONG_PTR kernelBase = 0;
	PLDR_DATA_TABLE_ENTRY pEntry = (PLDR_DATA_TABLE_ENTRY)pDriver->DriverSection;
	ULONG_PTR nextEntry = pEntry->InLoadOrderLinks.Flink;
	__try {
		do {
			if (pEntry->BaseDllName.Buffer != NULL) {
				if (RtlCompareUnicodeString(&pEntry->BaseDllName, &moduleName, TRUE) == 0) {
					kernelBase = pEntry->DllBase;
					if (pImageSize) {
						*pImageSize = pEntry->SizeOfImage;
					}
					break;
				}
			}
			pEntry = (PLDR_DATA_TABLE_ENTRY)pEntry->InLoadOrderLinks.Flink;
		} while (pEntry->InLoadOrderLinks.Flink != nextEntry);
	}
	__except(EXCEPTION_EXECUTE_HANDLER) {
		return kernelBase;
	}

	return kernelBase;
}

NTKERNELAPI PIMAGE_NT_HEADERS NTAPI RtlImageNtHeader(_In_ PVOID Base);

ULONG_PTR GeneralKeServiceDescriptorTable64(PDRIVER_OBJECT pDriver)
{

#ifndef _WIN64
	//x86 code
	UNICODE_STRING routineName;
	RtlInitUnicodeString(&routineName, L"KeServiceDescriptorTable");
	return (ULONG_PTR)MmGetSystemRoutineAddress(&routineName);
#else
	//x64 code
	ULONG kernelSize = 0;
	ULONG_PTR kernelBase = (ULONG_PTR)GetKernelModuleBase(pDriver,L"ntoskrnl.exe",&kernelSize);
	if (kernelBase == 0 || kernelSize == 0)
		return 0;

	// Find .text section //需要导入ntimage.h
	PIMAGE_NT_HEADERS ntHeaders = RtlImageNtHeader((PVOID)kernelBase);
	PIMAGE_SECTION_HEADER textSection = NULL;
	PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(ntHeaders);
	for (ULONG i = 0; i < ntHeaders->FileHeader.NumberOfSections; ++i)
	{
		char sectionName[IMAGE_SIZEOF_SHORT_NAME + 1];
		RtlCopyMemory(sectionName, section->Name, IMAGE_SIZEOF_SHORT_NAME);
		sectionName[IMAGE_SIZEOF_SHORT_NAME] = '\0';
		if (strncmp(sectionName, ".text", sizeof(".text") - sizeof(char)) == 0)
		{
			textSection = section;
			break;
		}
		section++;
	}
	if (textSection == NULL)
		return 0;


	// Find KiSystemServiceStart in .text
	const unsigned char KiSystemServiceStartPattern[] = { 0x8B, 0xF8, 0xC1, 0xEF, 0x07, 0x83, 0xE7, 0x20, 0x25, 0xFF, 0x0F, 0x00, 0x00 };
	const ULONG signatureSize = sizeof(KiSystemServiceStartPattern);
	BOOLEAN found = FALSE;
	ULONG KiSSSOffset;
	for (KiSSSOffset = 0; KiSSSOffset < textSection->Misc.VirtualSize - signatureSize; KiSSSOffset++)
	{
		if (RtlCompareMemory(((unsigned char*)kernelBase + textSection->VirtualAddress + KiSSSOffset), KiSystemServiceStartPattern, signatureSize) == signatureSize)
		{
			found = TRUE;
			break;
		}
	}
	if (!found)
		return 0;

	//address = KiSystemServiceRepeat lea r10, KeServiceDescriptorTable
	ULONG_PTR address = kernelBase + textSection->VirtualAddress + KiSSSOffset + signatureSize;
	LONG relativeOffset = 0;
	if ((*(unsigned char*)address == 0x4c) &&
		(*(unsigned char*)(address + 1) == 0x8d) &&
		(*(unsigned char*)(address + 2) == 0x15))
	{
		relativeOffset = *(LONG*)(address + 3);
	}
	if (relativeOffset == 0)
		return 0;

	return (ULONG_PTR)(address + relativeOffset + 7);
#endif
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没事干写博客玩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值