1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #include <Ntifs.h>
#define FuctionID 0x101
void PageProtectOn() { __asm { mov eax,cr0 or eax,10000h mov cr0,eax sti } }
void PageProtectOff() { __asm { cli mov eax,cr0 and eax,not 10000h mov cr0,eax } }
typedef struct _KSYSTEM_SERVICE_TABLE { PULONG ServiceTableBase; PULONG ServiceCounterTableBase; ULONG NumberOfService; PUCHAR ParamTableBase; }KSYSTEM_SERVICE_TABLE,*PKSYSTEM_SERVICE_TABLE;
typedef struct _KSERVICE_DESCRIPTOR_TABLE { KSYSTEM_SERVICE_TABLE ntoskrnl; KSYSTEM_SERVICE_TABLE win32k; KSYSTEM_SERVICE_TABLE notUsed1; KSYSTEM_SERVICE_TABLE notUsed2; }KSERVICE_DESCRIPTOR_TABLE,*PKSERVICE_DESCRIPTOR_TABLE;
typedef NTSTATUS (*NTTERMINATEPROCESS) (HANDLE ProcessHandle, NTSTATUS ExitStatus);
extern PKSERVICE_DESCRIPTOR_TABLE KeServiceDescriptorTable;
ULONG uOldNtTerminateProcess;
NTSTATUS MyNtTerminateProcess(HANDLE ProcessHandle, NTSTATUS ExitStatus) { PEPROCESS pEprocess; NTSTATUS status; PCHAR ImageFileName;
status = ObReferenceObjectByHandle(ProcessHandle,FILE_ANY_ACCESS,*PsProcessType,KernelMode,&pEprocess,NULL); if (!NT_SUCCESS(status)) { return status; } ImageFileName = (PCHAR)pEprocess + 0x174; if (strcmp(ImageFileName, "notepad.exe") == 0) { if (ProcessHandle == (HANDLE)0xFFFFFFFF) { DbgPrint("点X关闭 成功退出:%s: NtTerminateProcess(%x, %x)\n", ImageFileName, ProcessHandle, ExitStatus); return ((NTTERMINATEPROCESS)uOldNtTerminateProcess)(ProcessHandle, ExitStatus); } else if(ExitStatus == 1) { DbgPrint("通过任务管理器关闭 拒绝关闭:%s: NtTerminateProcess(%x, %x)\n", ImageFileName, ProcessHandle, ExitStatus); return STATUS_ACCESS_DENIED; } else { return STATUS_ACCESS_DENIED; } }
return ((NTTERMINATEPROCESS)uOldNtTerminateProcess)(ProcessHandle, ExitStatus); }
NTSTATUS HookNtTerminateProcess() { NTSTATUS Status; Status = STATUS_SUCCESS; PageProtectOff(); uOldNtTerminateProcess = KeServiceDescriptorTable->ntoskrnl.ServiceTableBase[FuctionID]; KeServiceDescriptorTable->ntoskrnl.ServiceTableBase[FuctionID] = (ULONG)MyNtTerminateProcess; PageProtectOn(); return Status; }
VOID UnHookNtTerminateProcess() { PageProtectOff(); KeServiceDescriptorTable->ntoskrnl.ServiceTableBase[FuctionID] = (ULONG)uOldNtTerminateProcess; PageProtectOn(); }
VOID DriverUnload(PDRIVER_OBJECT driver) { UnHookNtTerminateProcess(); KdPrint(("驱动程序停止运行了.\r\n")); }
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriver, PUNICODE_STRING reg_path) { HookNtTerminateProcess();
pDriver->DriverUnload = DriverUnload; return STATUS_SUCCESS; }
|