Hey there, I'm trying to hook ZwTerminateProcess with the following code :
prior to the above code I didn't use MDL for defining a writable memory section, and when defining Memory descriptor list I got BSoD .
with much of effort and trying to edit the code and fix the problem I still have the BSoD issue .
Any help would be appreciated .
thanks .
Code: Select all
don't know where's the problem but everytime I load it into kernel I get BSoD (test on VirtualBox) ... .#include "ntddk.h"
#include "stdafx.h"
#define SYSTEMSERVICE(_function) KeServiceDescriptorTable.CounterTableBase[ *(PULONG)((PUCHAR)_function+1)]
extern NTSTATUS ZwTerminateProcess(IN HANDLE ProcessHandle,IN NTSTATUS ExitStatus);
#define DEBUG
typedef unsigned long DWORD, *PDWORD;
typedef unsigned char BYTE, *PBYTE;
void hook(void);
void Unhook();
NTSTATUS DriverEntry(PDRIVER_OBJECT, PUNICODE_STRING);
void Unload_driver(IN PDRIVER_OBJECT);
#pragma pack(1)
typedef struct ServiceDescriptorEntry {
PDWORD ServiceTable;
PDWORD CounterTableBase;
DWORD ServiceLimit;
PBYTE ArgumentTable;
} ServiceDescriptorTableEntry_t;
#pragma pack()
__declspec(dllimport) ServiceDescriptorTableEntry_t KeServiceDescriptorTable;
typedef NTSYSAPI NTSTATUS (*ZWTERMINATEPROCESS)
(
IN HANDLE ProcessHandle,
IN NTSTATUS ExitStatus
);
ZWTERMINATEPROCESS OrigZwTerminateProcess;
extern ZWTERMINATEPROCESS OrigZwTerminateProcess;
NTSTATUS FakeZwTerminateProcess(
IN HANDLE ProcessHandle,
IN NTSTATUS ExitStatus)
{
return STATUS_PROCESS_IS_TERMINATING;
}
PMDL g_pmdlSystemCall;
PVOID *MappedSystemCallTable;
#define SYSCALL_INDEX(_Function) *(PULONG)((PUCHAR)_Function+1)
void Hook(void)
{
_asm{cli}
OrigZwTerminateProcess = (ZWTERMINATEPROCESS) (SYSTEMSERVICE(ZwTerminateProcess));
(ZWTERMINATEPROCESS) (SYSTEMSERVICE(ZwTerminateProcess)) = FakeZwTerminateProcess;
_asm{sti}
}
void Unhook()
{
_asm{cli}
(ZWTERMINATEPROCESS) (SYSTEMSERVICE(ZwTerminateProcess)) = OrigZwTerminateProcess; // restore
_asm{sti}
}
void OnUnload(IN PDRIVER_OBJECT DriverObject)
{
Unhook();
if(g_pmdlSystemCall)
{
MmUnmapLockedPages(MappedSystemCallTable,g_pmdlSystemCall) ;
IoFreeMdl(g_pmdlSystemCall) ;
}
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT theDriverObject,
IN PUNICODE_STRING theRegistryPath)
{
theDriverObject->DriverUnload = OnUnload;
g_pmdlSystemCall = MmCreateMdl(NULL, KeServiceDescriptorTable.CounterTableBase, KeServiceDescriptorTable.ServiceLimit*4);
if(!g_pmdlSystemCall)
return STATUS_UNSUCCESSFUL;
MmBuildMdlForNonPagedPool(g_pmdlSystemCall);
g_pmdlSystemCall->MdlFlags = g_pmdlSystemCall->MdlFlags | MDL_MAPPED_TO_SYSTEM_VA;
MappedSystemCallTable = MmMapLockedPages(g_pmdlSystemCall, KernelMode);
Hook();
return STATUS_SUCCESS;
}
prior to the above code I didn't use MDL for defining a writable memory section, and when defining Memory descriptor list I got BSoD .
with much of effort and trying to edit the code and fix the problem I still have the BSoD issue .
Any help would be appreciated .
thanks .
- Individuality