驅動開發:內核監控Register註冊表回調

在筆者前一篇文章《驅動開發:內核枚舉Registry註冊表回調》中實現了對註冊表的枚舉,本章將實現對註冊表的監控,不同於32位系統在64位系統中,微軟爲我們提供了兩個針對註冊表的專用內核監控函數,通過這兩個函數可以在不劫持內核API的前提下實現對註冊表增加,刪除,創建等事件的有效監控,註冊表監視通常會通過CmRegisterCallback創建監控事件並傳入自己的回調函數,與該創建對應的是CmUnRegisterCallback當註冊表監控結束後可用於註銷回調。

  • CmRegisterCallback 設置註冊表回調
  • CmUnRegisterCallback 註銷註冊表回調

默認情況下CmRegisterCallback需傳入三個參數,參數一回調函數地址,參數二空餘,參數三回調句柄,微軟定義如下。

// 署名權
// right to sign one's name on a piece of work
// PowerBy: LyShark
// Email: [email protected]

// 參數1:回調函數地址
// 參數2:無作用
// 參數3:回調句柄
NTSTATUS CmRegisterCallback(
  [in]           PEX_CALLBACK_FUNCTION Function,
  [in, optional] PVOID                 Context,
  [out]          PLARGE_INTEGER        Cookie
);

自定義註冊表回調函數MyLySharkCallback需要保留三個參數,CallbackContext回調上下文,Argument1是操作類型,Argument2定義詳細結構體指針。

NTSTATUS MyLySharkCallback(_In_ PVOID CallbackContext, _In_opt_ PVOID Argument1, _In_opt_ PVOID Argument2)

在自定義回調函數內Argument1則可獲取到操作類型,類型是一個REG_NOTIFY_CLASS枚舉結構,微軟對其的具體定義如下所示。

// 署名權
// right to sign one's name on a piece of work
// PowerBy: LyShark
// Email: [email protected]

typedef enum _REG_NOTIFY_CLASS {
    RegNtDeleteKey,
    RegNtPreDeleteKey = RegNtDeleteKey,
    RegNtSetValueKey,
    RegNtPreSetValueKey = RegNtSetValueKey,
    RegNtDeleteValueKey,
    RegNtPreDeleteValueKey = RegNtDeleteValueKey,
    RegNtSetInformationKey,
    RegNtPreSetInformationKey = RegNtSetInformationKey,
    RegNtRenameKey,
    RegNtPreRenameKey = RegNtRenameKey,
    RegNtEnumerateKey,
    RegNtPreEnumerateKey = RegNtEnumerateKey,
    RegNtEnumerateValueKey,
    RegNtPreEnumerateValueKey = RegNtEnumerateValueKey,
    RegNtQueryKey,
    RegNtPreQueryKey = RegNtQueryKey,
    RegNtQueryValueKey,
    RegNtPreQueryValueKey = RegNtQueryValueKey,
    RegNtQueryMultipleValueKey,
    RegNtPreQueryMultipleValueKey = RegNtQueryMultipleValueKey,
    RegNtPreCreateKey,
    RegNtPostCreateKey,
    RegNtPreOpenKey,
    RegNtPostOpenKey,
    RegNtKeyHandleClose,
    RegNtPreKeyHandleClose = RegNtKeyHandleClose,
    //
    // .Net only
    //    
    RegNtPostDeleteKey,
    RegNtPostSetValueKey,
    RegNtPostDeleteValueKey,
    RegNtPostSetInformationKey,
    RegNtPostRenameKey,
    RegNtPostEnumerateKey,
    RegNtPostEnumerateValueKey,
    RegNtPostQueryKey,
    RegNtPostQueryValueKey,
    RegNtPostQueryMultipleValueKey,
    RegNtPostKeyHandleClose,
    RegNtPreCreateKeyEx,
    RegNtPostCreateKeyEx,
    RegNtPreOpenKeyEx,
    RegNtPostOpenKeyEx,
    //
    // new to Windows Vista
    //
    RegNtPreFlushKey,
    RegNtPostFlushKey,
    RegNtPreLoadKey,
    RegNtPostLoadKey,
    RegNtPreUnLoadKey,
    RegNtPostUnLoadKey,
    RegNtPreQueryKeySecurity,
    RegNtPostQueryKeySecurity,
    RegNtPreSetKeySecurity,
    RegNtPostSetKeySecurity,
    //
    // per-object context cleanup
    //
    RegNtCallbackObjectContextCleanup,
    //
    // new in Vista SP2 
    //
    RegNtPreRestoreKey,
    RegNtPostRestoreKey,
    RegNtPreSaveKey,
    RegNtPostSaveKey,
    RegNtPreReplaceKey,
    RegNtPostReplaceKey,

    MaxRegNtNotifyClass //should always be the last enum
} REG_NOTIFY_CLASS;

其中對於註冊表最常用的監控項爲以下幾種類型,當然爲了實現監控則我們必須要使用之前,如果使用之後則只能起到監視而無法做到監控的目的。

  • RegNtPreCreateKey 創建註冊表之前
  • RegNtPreOpenKey 打開註冊表之前
  • RegNtPreDeleteKey 刪除註冊表之前
  • RegNtPreDeleteValueKey 刪除鍵值之前
  • RegNtPreSetValueKey 修改註冊表之前

如果需要實現監視則,首先CmRegisterCallback註冊一個自定義回調,當有消息時則觸發MyLySharkCallback其內部獲取到lOperateType操作類型,並通過switch選擇不同的處理例程,每個處理例程都通過GetFullPath得到註冊表完整路徑,並打印出來,這段代碼實現如下。

// 署名權
// right to sign one's name on a piece of work
// PowerBy: LyShark
// Email: [email protected]

#include <ntifs.h>
#include <windef.h>

// 未導出函數聲明 pEProcess -> PID
PUCHAR PsGetProcessImageFileName(PEPROCESS pEProcess);

NTSTATUS ObQueryNameString(
	_In_ PVOID Object,
	_Out_writes_bytes_opt_(Length) POBJECT_NAME_INFORMATION ObjectNameInfo,
	_In_ ULONG Length,
	_Out_ PULONG ReturnLength
	);

// 註冊表回調Cookie
LARGE_INTEGER g_liRegCookie;

// 獲取註冊表完整路徑
BOOLEAN GetFullPath(PUNICODE_STRING pRegistryPath, PVOID pRegistryObject)
{
	// 判斷數據地址是否有效
	if ((FALSE == MmIsAddressValid(pRegistryObject)) ||
		(NULL == pRegistryObject))
	{
		return FALSE;
	}
	// 申請內存
	ULONG ulSize = 512;
	PVOID lpObjectNameInfo = ExAllocatePool(NonPagedPool, ulSize);
	if (NULL == lpObjectNameInfo)
	{
		return FALSE;
	}
	// 獲取註冊表路徑
	ULONG ulRetLen = 0;
	NTSTATUS status = ObQueryNameString(pRegistryObject, (POBJECT_NAME_INFORMATION)lpObjectNameInfo, ulSize, &ulRetLen);
	if (!NT_SUCCESS(status))
	{
		ExFreePool(lpObjectNameInfo);
		return FALSE;
	}
	// 複製
	RtlCopyUnicodeString(pRegistryPath, (PUNICODE_STRING)lpObjectNameInfo);
	// 釋放內存
	ExFreePool(lpObjectNameInfo);
	return TRUE;
}

// 註冊表回調函數
NTSTATUS MyLySharkCallback(_In_ PVOID CallbackContext, _In_opt_ PVOID Argument1, _In_opt_ PVOID Argument2)
{
	NTSTATUS status = STATUS_SUCCESS;
	UNICODE_STRING ustrRegPath;

	// 獲取操作類型
	LONG lOperateType = (REG_NOTIFY_CLASS)Argument1;
	
	// 申請內存
	ustrRegPath.Length = 0;
	ustrRegPath.MaximumLength = 1024 * sizeof(WCHAR);
	ustrRegPath.Buffer = ExAllocatePool(NonPagedPool, ustrRegPath.MaximumLength);
	if (NULL == ustrRegPath.Buffer)
	{
		return status;
	}
	RtlZeroMemory(ustrRegPath.Buffer, ustrRegPath.MaximumLength);
	
	// 判斷操作
	switch (lOperateType)
	{
		// 創建註冊表之前
	case RegNtPreCreateKey:
	{
		// 獲取註冊表路徑
		GetFullPath(&ustrRegPath, ((PREG_CREATE_KEY_INFORMATION)Argument2)->RootObject);
		DbgPrint("[創建註冊表][%wZ][%wZ]\n", &ustrRegPath, ((PREG_CREATE_KEY_INFORMATION)Argument2)->CompleteName);
		break;
	}
	// 打開註冊表之前
	case RegNtPreOpenKey:
	{
		// 獲取註冊表路徑
		GetFullPath(&ustrRegPath, ((PREG_CREATE_KEY_INFORMATION)Argument2)->RootObject);
		DbgPrint("[打開註冊表][%wZ][%wZ]\n", &ustrRegPath, ((PREG_CREATE_KEY_INFORMATION)Argument2)->CompleteName);
		break;
	}
	// 刪除鍵之前
	case RegNtPreDeleteKey:
	{
		// 獲取註冊表路徑
		GetFullPath(&ustrRegPath, ((PREG_DELETE_KEY_INFORMATION)Argument2)->Object);
		DbgPrint("[刪除鍵][%wZ] \n", &ustrRegPath);
		break;
	}
	// 刪除鍵值之前
	case RegNtPreDeleteValueKey:
	{
		// 獲取註冊表路徑
		GetFullPath(&ustrRegPath, ((PREG_DELETE_VALUE_KEY_INFORMATION)Argument2)->Object);
		DbgPrint("[刪除鍵值][%wZ][%wZ] \n", &ustrRegPath, ((PREG_DELETE_VALUE_KEY_INFORMATION)Argument2)->ValueName);

		// 獲取當前進程, 即操作註冊表的進程
		PEPROCESS pEProcess = PsGetCurrentProcess();
		if (NULL != pEProcess)
		{
			UCHAR *lpszProcessName = PsGetProcessImageFileName(pEProcess);
			if (NULL != lpszProcessName)
			{
				DbgPrint("進程 [%s] 刪除了鍵值對 \n", lpszProcessName);
			}
		}
		break;
	}
	// 修改鍵值之前
	case RegNtPreSetValueKey:
	{
		// 獲取註冊表路徑
		GetFullPath(&ustrRegPath, ((PREG_SET_VALUE_KEY_INFORMATION)Argument2)->Object);
		DbgPrint("[修改鍵值][%wZ][%wZ] \n", &ustrRegPath, ((PREG_SET_VALUE_KEY_INFORMATION)Argument2)->ValueName);
		break;
	}
	default:
		break;
	}

	// 釋放內存
	if (NULL != ustrRegPath.Buffer)
	{
		ExFreePool(ustrRegPath.Buffer);
		ustrRegPath.Buffer = NULL;
	}

	return status;
}

VOID UnDriver(PDRIVER_OBJECT driver)
{
	DbgPrint(("Uninstall Driver Is OK \n"));

	// 註銷當前註冊表回調
	if (0 < g_liRegCookie.QuadPart)
	{
		CmUnRegisterCallback(g_liRegCookie);
	}
}

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

	// 設置註冊表回調
	NTSTATUS status = CmRegisterCallback(MyLySharkCallback, NULL, &g_liRegCookie);
	if (!NT_SUCCESS(status))
	{
		g_liRegCookie.QuadPart = 0;
		return status;
	}

	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

運行驅動程序,則會輸出當前系統中所有針對註冊表的操作,如下圖所示。

如上的代碼只能實現註冊表項的監視,而如果需要監控則需要在回調函數MyLySharkCallback判斷,如果指定註冊表項是需要保護的則直接返回status = STATUS_ACCESS_DENIED;從而達到保護註冊表的目的,核心代碼如下所示。

// 反註冊表刪除回調
NTSTATUS MyLySharkCallback(_In_ PVOID CallbackContext, _In_opt_ PVOID Argument1, _In_opt_ PVOID Argument2)
{
	NTSTATUS status = STATUS_SUCCESS;
	UNICODE_STRING ustrRegPath;

	// 獲取操作類型
	LONG lOperateType = (REG_NOTIFY_CLASS)Argument1;
	ustrRegPath.Length = 0;
	ustrRegPath.MaximumLength = 1024 * sizeof(WCHAR);
	ustrRegPath.Buffer = ExAllocatePool(NonPagedPool, ustrRegPath.MaximumLength);
	if (NULL == ustrRegPath.Buffer)
	{
		return status;
	}

	RtlZeroMemory(ustrRegPath.Buffer, ustrRegPath.MaximumLength);
	// 判斷操作
	switch (lOperateType)
	{
		// 刪除鍵值之前
	case RegNtPreDeleteValueKey:
	{
		// 獲取註冊表路徑
		GetFullPath(&ustrRegPath, ((PREG_DELETE_VALUE_KEY_INFORMATION)Argument2)->Object);
		DbgPrint("[刪除鍵值][%wZ][%wZ]\n", &ustrRegPath, ((PREG_DELETE_VALUE_KEY_INFORMATION)Argument2)->ValueName);

		// 如果要刪除指定註冊表項則拒絕
		PWCH pszRegister = L"\\REGISTRY\\MACHINE\\SOFTWARE\\lyshark.com";
		if (wcscmp(ustrRegPath.Buffer, pszRegister) == 0)
		{
			DbgPrint("[lyshark] 註冊表項刪除操作已被攔截! \n");
			// 拒絕操作
			status = STATUS_ACCESS_DENIED;
		}
		break;
	}
	default:
		break;
	}

	// 釋放內存
	if (NULL != ustrRegPath.Buffer)
	{
		ExFreePool(ustrRegPath.Buffer);
		ustrRegPath.Buffer = NULL;
	}

	return status;
}

運行驅動程序,然後我們嘗試刪除\\LyShark\HKEY_LOCAL_MACHINE\SOFTWARE\lyshark.com裏面的子項,則會提示如下信息。

當然這裏的RegNtPreDeleteValueKey是指的刪除操作,如果將其替換成RegNtPreSetValueKey,那麼只有當註冊表被創建纔會攔截,此時就會變成攔截創建。

// 攔截創建操作
NTSTATUS MyLySharkCallback(_In_ PVOID CallbackContext, _In_opt_ PVOID Argument1, _In_opt_ PVOID Argument2)
{
	NTSTATUS status = STATUS_SUCCESS;
	UNICODE_STRING ustrRegPath;

	// 獲取操作類型
	LONG lOperateType = (REG_NOTIFY_CLASS)Argument1;

	// 申請內存
	ustrRegPath.Length = 0;
	ustrRegPath.MaximumLength = 1024 * sizeof(WCHAR);
	ustrRegPath.Buffer = ExAllocatePool(NonPagedPool, ustrRegPath.MaximumLength);
	if (NULL == ustrRegPath.Buffer)
	{
		return status;
	}
	RtlZeroMemory(ustrRegPath.Buffer, ustrRegPath.MaximumLength);

	// 判斷操作
	switch (lOperateType)
	{
	// 修改鍵值之前
	case RegNtPreSetValueKey:
	{
		// 獲取註冊表路徑
		GetFullPath(&ustrRegPath, ((PREG_DELETE_VALUE_KEY_INFORMATION)Argument2)->Object);

		// 攔截創建
		PWCH pszRegister = L"\\REGISTRY\\MACHINE\\SOFTWARE\\lyshark.com";
		if (wcscmp(ustrRegPath.Buffer, pszRegister) == 0)
		{
			DbgPrint("[lyshark] 註冊表項創建操作已被攔截! \n");
			status = STATUS_ACCESS_DENIED;
		}
		break;
	}
	default:
		break;
	}

	// 釋放內存
	if (NULL != ustrRegPath.Buffer)
	{
		ExFreePool(ustrRegPath.Buffer);
		ustrRegPath.Buffer = NULL;
	}

	return status;
}

加載驅動保護,然後我們嘗試在\\LyShark\HKEY_LOCAL_MACHINE\SOFTWARE\lyshark.com裏面創建一個子項,則會提示創建失敗。

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