//this routine return 0 success run ObReferenceObjectByHandle()
//this routine return 1 failed run ObReferenceObjectByHandle()
int call_failed(
IN HANDLE Handle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_TYPE ObjectType,
IN KPROCESSOR_MODE AccessMode,
OUT PVOID *Object,
OUT POBJECT_HANDLE_INFORMATION HandleInfo
)
{
PEPROCESS process;
KIRQL irql;
//this object is a process
if(ObjectType == *PsProcessType)
{
irql = KeRaiseIrqlToDpcLevel();
close_write_protected();
//Unload inline hook in ObReferenceObjectByHandle()
RtlCopyMemory(ObReferenceObjectByHandle,routine_head,5);
//run the kernel routine
if(ObReferenceObjectByHandle(Handle,DesiredAccess,ObjectType,AccessMode,&process,NULL) == STATUS_SUCCESS)
{
//this request point to notepad.exe
if(_stricmp((char *)((char *)process+0x174),"notepad.exe") == 0)
{
//install inline hook to ObReferenceObjectByHandle()
RtlCopyMemory(ObReferenceObjectByHandle,jmp_code,5);
}
else //this object not a process
{
return 0;
}
}
//jmp this routine
__declspec(naked) my_routine(
IN HANDLE Handle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_TYPE ObjectType,
IN KPROCESSOR_MODE AccessMode,
OUT PVOID *Object,
OUT POBJECT_HANDLE_INFORMATION HandleInfor
)
{
__asm
{
mov edi,edi
//create next stack for sub routine using
push ebp
mov ebp,esp
//parameter enter the stack
push [ebp+0x1c]
push [ebp+0x18]
push [ebp+0x14]
push [ebp+0x10]
push [ebp+0xc]
push [ebp+8]
//enter the routine
call call_failed
cmp eax,1
jz start_new
//in this routine install inline hook
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath)
{
//this is offset of jmp to my routine
int jmp_offset;
KIRQL irql;
//set current driver unload routine
DriverObject->DriverUnload = DriverUnload;
NTKERNELAPI NTSTATUS
ObReferenceObjectByHandle(
IN HANDLE Handle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_TYPE ObjectType OPTIONAL,
IN KPROCESSOR_MODE AccessMode,
OUT PVOID *Object,
OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL
);
//HOOK函数
NTSTATUS DetourMyObReferenceObjectByHandle(
IN HANDLE Handle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_TYPE ObjectType OPTIONAL,
IN KPROCESSOR_MODE AccessMode,
OUT PVOID *Object,
OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL);
//
NTSTATUS DetourMyObReferenceObjectByHandle(
IN HANDLE Handle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_TYPE ObjectType OPTIONAL,
IN KPROCESSOR_MODE AccessMode,
OUT PVOID *Object,
OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
{
NTSTATUS status;
//调用原函数
status=OriginalObReferenceObjectByHandle(Handle,DesiredAccess,ObjectType,AccessMode,Object,HandleInformation);
if((status==STATUS_SUCCESS)&&(DesiredAccess==1))
{