驅動開發:內核無痕隱藏自身分析

在筆者前面有一篇文章《驅動開發:斷鏈隱藏驅動程序自身》通過摘除驅動的鏈表實現了斷鏈隱藏自身的目的,但此方法恢復時會觸發PG會藍屏,偶然間在網上找到了一個作者介紹的一種方法,覺得有必要詳細分析一下他是如何實現的驅動隱藏的,總體來說作者的思路是最終尋找到MiProcessLoaderEntry的入口地址,該函數的作用是將驅動信息加入鏈表和移除鏈表,運用這個函數即可動態處理驅動的添加和移除問題。

  • MiProcessLoaderEntry(pDriverObject->DriverSection, 1) 添加
  • MiProcessLoaderEntry(pDriverObject->DriverSection, 0) 移除

那麼如何找到MiProcessLoaderEntry函數入口地址就是下一步的目標,尋找入口可以總結爲;

  • 1.尋找MmUnloadSystemImage函數地址,可通過MmGetSystemRoutineAddress函數得到。
  • 2.在MmUnloadSystemImage裏面尋找MiUnloadSystemImage函數地址。
  • 3.在MiUnloadSystemImage裏面繼續尋找MiProcessLoaderEntry即可。

搜索MmUnloadSystemImage可定位到call nt!MiUnloadSystemImage地址。

搜索MiUnloadSystemImage定位到call nt!MiProcessLoaderEntry即得到了我們想要的。

根據前面枚舉篇系列文章,定位這段特徵很容易實現,如下是一段參考代碼。

// PowerBy: LyShark
// Email: [email protected]

#include <ntddk.h>
#include <ntstrsafe.h>

typedef NTSTATUS(__fastcall *MiProcessLoaderEntry)(PVOID pDriverSection, BOOLEAN bLoad);

// 取出指定函數地址
PVOID GetProcAddress(WCHAR *FuncName)
{
	UNICODE_STRING u_FuncName = { 0 };
	PVOID ref = NULL;

	RtlInitUnicodeString(&u_FuncName, FuncName);
	ref = MmGetSystemRoutineAddress(&u_FuncName);

	if (ref != NULL)
	{
		return ref;
	}

	return ref;
}

// 特徵定位 MiUnloadSystemImage
ULONG64 GetMiUnloadSystemImageAddress()
{
	// 在MmUnloadSystemImage函數中搜索的Code
	/*
	lyshark.com: kd> uf MmUnloadSystemImage
		fffff801`37943512 83caff          or      edx,0FFFFFFFFh
		fffff801`37943515 488bcf          mov     rcx,rdi
		fffff801`37943518 488bd8          mov     rbx,rax
		fffff801`3794351b e860b4ebff      call    nt!MiUnloadSystemImage (fffff801`377fe980)
	*/
	CHAR MmUnloadSystemImage_Code[] = "\x83\xCA\xFF"  // or      edx, 0FFFFFFFFh
		"\x48\x8B\xCF"                                // mov     rcx, rdi
		"\x48\x8B\xD8"                                // mov     rbx, rax
		"\xE8";                                       // call    nt!MiUnloadSystemImage (fffff801`377fe980)

	ULONG_PTR MmUnloadSystemImageAddress = 0;
	ULONG_PTR MiUnloadSystemImageAddress = 0;
	ULONG_PTR StartAddress = 0;

	MmUnloadSystemImageAddress = (ULONG_PTR)GetProcAddress(L"MmUnloadSystemImage");
	if (MmUnloadSystemImageAddress == 0)
	{
		return 0;
	}

	// 在MmUnloadSystemImage中搜索特徵碼尋找MiUnloadSystemImage
	StartAddress = MmUnloadSystemImageAddress;
	while (StartAddress < MmUnloadSystemImageAddress + 0x500)
	{
		if (memcmp((VOID*)StartAddress, MmUnloadSystemImage_Code, strlen(MmUnloadSystemImage_Code)) == 0)
		{
			// 跳過call之前的指令
			StartAddress += strlen(MmUnloadSystemImage_Code);

			// 取出 MiUnloadSystemImage地址
			MiUnloadSystemImageAddress = *(LONG*)StartAddress + StartAddress + 4;
			break;
		}
		++StartAddress;
	}

	if (MiUnloadSystemImageAddress != 0)
	{
		return MiUnloadSystemImageAddress;
	}
	return 0;
}

// 特徵定位 MiProcessLoaderEntry
MiProcessLoaderEntry GetMiProcessLoaderEntry(ULONG64 StartAddress)
{
	if (StartAddress == 0)
	{
		return NULL;
	}

	while (StartAddress < StartAddress + 0x600)
	{
		// 操作數MiProcessLoaderEntry內存地址是動態變化的
		/*
		lyshark.com: kd> uf MiUnloadSystemImage
			fffff801`377fed19 33d2            xor     edx,edx
			fffff801`377fed1b 488bcb          mov     rcx,rbx
			fffff801`377fed1e e84162b4ff      call    nt!MiProcessLoaderEntry (fffff801`37344f64)
			fffff801`377fed23 8b05d756f7ff    mov     eax,dword ptr [nt!PerfGlobalGroupMask (fffff801`37774400)]
			fffff801`377fed29 a804            test    al,4
			fffff801`377fed2b 7440            je      nt!MiUnloadSystemImage+0x3ed (fffff801`377fed6d)  Branch
			E8 call | 8B 05 mov eax
		*/

		// fffff801`377fed1e   | fffff801`377fed23
		// 判斷特徵 0xE8(call) | 0x8B 0x05(mov eax)
		if (*(UCHAR*)StartAddress == 0xE8 && *(UCHAR *)(StartAddress + 5) == 0x8B && *(UCHAR *)(StartAddress + 6) == 0x05)
		{
			// 跳過一個字節call的E8
			StartAddress++;

			// StartAddress + 1 + 4
			return (MiProcessLoaderEntry)(*(LONG*)StartAddress + StartAddress + 4);
		}
		++StartAddress;
	}
	return NULL;
}

VOID UnDriver(PDRIVER_OBJECT driver)
{
	DbgPrint("卸載完成... \n");
}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
	DbgPrint("hello lyshark.com \n");

	ULONG64 MiUnloadSystemImageAddress = GetMiUnloadSystemImageAddress();
	DbgPrint("MiUnloadSystemImageAddress = %p \n", MiUnloadSystemImageAddress);

	MiProcessLoaderEntry MiProcessLoaderEntryAddress = GetMiProcessLoaderEntry(MiUnloadSystemImageAddress);
	DbgPrint("MiProcessLoaderEntryAddress = %p \n", (ULONG64)MiProcessLoaderEntryAddress);

	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

運行驅動程序,即可得到MiProcessLoaderEntryAddress的內存地址。

得到內存地址之後,直接破壞掉自身驅動的入口地址等,即可實現隱藏自身。

// PowerBy: LyShark
// Email: [email protected]
#include <ntddk.h>
#include <ntstrsafe.h>

typedef NTSTATUS(*NTQUERYSYSTEMINFORMATION)(
  IN ULONG SystemInformationClass,
  OUT PVOID   SystemInformation,
  IN ULONG_PTR    SystemInformationLength,
  OUT PULONG_PTR  ReturnLength OPTIONAL);

NTSYSAPI NTSTATUS NTAPI ObReferenceObjectByName(
  __in PUNICODE_STRING ObjectName,
  __in ULONG Attributes,
  __in_opt PACCESS_STATE AccessState,
  __in_opt ACCESS_MASK DesiredAccess,
  __in POBJECT_TYPE ObjectType,
  __in KPROCESSOR_MODE AccessMode,
  __inout_opt PVOID ParseContext,
  __out PVOID* Object
  );

typedef struct _SYSTEM_MODULE_INFORMATION
{
  HANDLE Section;
  PVOID MappedBase;
  PVOID Base;
  ULONG Size;
  ULONG Flags;
  USHORT LoadOrderIndex;
  USHORT InitOrderIndex;
  USHORT LoadCount;
  USHORT PathLength;
  CHAR ImageName[256];
} SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION;

typedef struct _LDR_DATA_TABLE_ENTRY
{
  LIST_ENTRY InLoadOrderLinks;
  LIST_ENTRY InMemoryOrderLinks;
  LIST_ENTRY InInitializationOrderLinks;
  PVOID      DllBase;
  PVOID      EntryPoint;
}LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;

extern POBJECT_TYPE *IoDriverObjectType;
typedef NTSTATUS(__fastcall *MiProcessLoaderEntry)(PVOID pDriverSection, BOOLEAN bLoad);
ULONG64 MiUnloadSystemImageAddress = 0;

// 取出指定函數地址
PVOID GetProcAddress(WCHAR *FuncName)
{
	UNICODE_STRING u_FuncName = { 0 };
	PVOID ref = NULL;

	RtlInitUnicodeString(&u_FuncName, FuncName);
	ref = MmGetSystemRoutineAddress(&u_FuncName);

	if (ref != NULL)
	{
	return ref;
	}

	return ref;
}

// 特徵定位 MiUnloadSystemImage
ULONG64 GetMiUnloadSystemImageAddress()
{
	CHAR MmUnloadSystemImage_Code[] = "\x83\xCA\xFF\x48\x8B\xCF\x48\x8B\xD8\xE8";

	ULONG_PTR MmUnloadSystemImageAddress = 0;
	ULONG_PTR MiUnloadSystemImageAddress = 0;
	ULONG_PTR StartAddress = 0;

	MmUnloadSystemImageAddress = (ULONG_PTR)GetProcAddress(L"MmUnloadSystemImage");
	if (MmUnloadSystemImageAddress == 0)
	{
	return 0;
	}

	// 在MmUnloadSystemImage中搜索特徵碼尋找MiUnloadSystemImage
	StartAddress = MmUnloadSystemImageAddress;
	while (StartAddress < MmUnloadSystemImageAddress + 0x500)
	{
	if (memcmp((VOID*)StartAddress, MmUnloadSystemImage_Code, strlen(MmUnloadSystemImage_Code)) == 0)
	{
		StartAddress += strlen(MmUnloadSystemImage_Code);
		MiUnloadSystemImageAddress = *(LONG*)StartAddress + StartAddress + 4;
		break;
	}
	++StartAddress;
	}

	if (MiUnloadSystemImageAddress != 0)
	{
	return MiUnloadSystemImageAddress;
	}
	return 0;
}

// 特徵定位 MiProcessLoaderEntry
MiProcessLoaderEntry GetMiProcessLoaderEntry(ULONG64 StartAddress)
{
	if (StartAddress == 0)
	{
	return NULL;
	}

	while (StartAddress < StartAddress + 0x600)
	{
	if (*(UCHAR*)StartAddress == 0xE8 && *(UCHAR *)(StartAddress + 5) == 0x8B && *(UCHAR *)(StartAddress + 6) == 0x05)
	{
		StartAddress++;
		return (MiProcessLoaderEntry)(*(LONG*)StartAddress + StartAddress + 4);
	}
	++StartAddress;
	}
	return NULL;
}

// 根據驅動名獲取驅動對象
BOOLEAN GetDriverObjectByName(PDRIVER_OBJECT *DriverObject, WCHAR *DriverName)
{
	PDRIVER_OBJECT TempObject = NULL;
	UNICODE_STRING u_DriverName = { 0 };
	NTSTATUS Status = STATUS_UNSUCCESSFUL;

	RtlInitUnicodeString(&u_DriverName, DriverName);
	Status = ObReferenceObjectByName(&u_DriverName, OBJ_CASE_INSENSITIVE, NULL, 0, *IoDriverObjectType, KernelMode, NULL, &TempObject);
	if (!NT_SUCCESS(Status))
	{
	*DriverObject = NULL;
	return FALSE;
	}

	*DriverObject = TempObject;
	return TRUE;
}

BOOLEAN SupportSEH(PDRIVER_OBJECT DriverObject)
{
	PDRIVER_OBJECT Object = NULL;;
	PLDR_DATA_TABLE_ENTRY LdrEntry = NULL;

	GetDriverObjectByName(&Object, L"\\Driver\\tdx");
	if (Object == NULL)
	{
		return FALSE;
	}

	// 將獲取到的驅動對象節點賦值給自身LDR
	LdrEntry = (PLDR_DATA_TABLE_ENTRY)DriverObject->DriverSection;
	LdrEntry->DllBase = Object->DriverStart;
	ObDereferenceObject(Object);
	return TRUE;
}

VOID InitInLoadOrderLinks(PLDR_DATA_TABLE_ENTRY LdrEntry)
{
	InitializeListHead(&LdrEntry->InLoadOrderLinks);
	InitializeListHead(&LdrEntry->InMemoryOrderLinks);
}

VOID Reinitialize(PDRIVER_OBJECT DriverObject, PVOID Context, ULONG Count)
{
	MiProcessLoaderEntry m_MiProcessLoaderEntry = NULL;
	ULONG *p = NULL;

	m_MiProcessLoaderEntry = GetMiProcessLoaderEntry(MiUnloadSystemImageAddress);
	if (m_MiProcessLoaderEntry == NULL)
	{
		return;
	}

	SupportSEH(DriverObject);

	m_MiProcessLoaderEntry(DriverObject->DriverSection, 0);
	InitInLoadOrderLinks((PLDR_DATA_TABLE_ENTRY)DriverObject->DriverSection);

	// 破壞驅動對象特徵
	DriverObject->DriverSection = NULL;
	DriverObject->DriverStart = NULL;
	DriverObject->DriverSize = 0;
	DriverObject->DriverUnload = NULL;
	DriverObject->DriverInit = NULL;
	DriverObject->DeviceObject = NULL;

	DbgPrint("驅動隱藏 \n");
}

VOID UnDriver(PDRIVER_OBJECT driver)
{
  DbgPrint("卸載完成... \n");
}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
	DbgPrint("hello lyshark.com \n");

	MiUnloadSystemImageAddress = GetMiUnloadSystemImageAddress();
	MiProcessLoaderEntry MiProcessLoaderEntryAddress = GetMiProcessLoaderEntry(MiUnloadSystemImageAddress);

	// 無痕隱藏
	IoRegisterDriverReinitialization(Driver, Reinitialize, NULL);

	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

運行驅動程序,讓後看到如下輸出信息;

參考文獻

https://blog.csdn.net/zhuhuibeishadiao/article/details/75658816
https://github.com/ZhuHuiBeiShaDiao/NewHideDriverEx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章