-
-
[原创]HOOK SSDT AND HOOK Shadow SSDT FOR DELPHI
-
发表于:
2008-11-26 14:33
12316
-
[原创]HOOK SSDT AND HOOK Shadow SSDT FOR DELPHI
最近看见论坛很多DELPHI写驱动的教程,小弟学习之余,把自己的学习成果展示出来 与大家一起分享
unit Driver;
interface
uses
nt_status,ntoskrnl,ntutils;
const
DeviceName = '\Device\360safeBoxA';
DosDeviceName = '\DosDevices\360safeBoxA';
IOCTL_HOOK_START = $0022E000;
IOCTL_HOOK_STOP = $0022E004;
IOCTL_PROTECT_PROCESS = $0022E200;
IOCTL_PROTECT_HWND = $0022E201;
IOCTL_PROTECT_OTHER = $0022E204;
var
DosDevName: TUnicodeString;
TempSafeId:Handle=0;
TempSafehandle:Handle=0;
function _DriverEntry(pDriverObject: PDriverObject; RegistryPath: PUnicodeString) : NTSTATUS; stdcall;
implementation
uses hooking;
function hookCreate(ADeviceObject: PDeviceObject; AIrp: PIrp): NTSTATUS; stdcall;
begin
Result := STATUS_SUCCESS;
AIrp^.IoStatus.Status := Result;
IoCompleteRequest(AIrp, IO_NO_INCREMENT);
end;
function hookClose(ADeviceObject: PDeviceObject; AIrp: PIrp): NTSTATUS; stdcall;
begin
Result := STATUS_SUCCESS;
AIrp^.IoStatus.Status := Result;
IoCompleteRequest(AIrp, IO_NO_INCREMENT);
end;
function hookDeviceControl(ADeviceObject: PDeviceObject; AIrp:PIrp): NTSTATUS; stdcall;
var
LStack: PIO_STACK_LOCATION;
pIOBuffer: Pointer;
LBufInLen, LBufOutLen,
LCode, LRet,OutByteCount: ULONG;
begin
LStack := IoGetCurrentIrpStackLocation(AIrp);
Result := STATUS_SUCCESS;
AIrp^.IoStatus.Information := 0;
LCode := LStack^.Parameters.DeviceIoControl.IoControlCode;
pIOBuffer := AIrp^.AssociatedIrp.SystemBuffer;
LBufInLen := LStack^.Parameters.DeviceIoControl.InputBufferLength;
LBufOutLen := LStack^.Parameters.DeviceIoControl.OutputBufferLength;
OutByteCount:=0;
case LCode of
IOCTL_HOOK_START: begin
LRet := HookingHook;
OutByteCount:=4;
LONG(pIOBuffer^):=LRet;
end;
IOCTL_HOOK_STOP: begin
LRet := HookingUnhook;
OutByteCount:=4;
LONG(pIOBuffer^):=LRet;
end;
IOCTL_PROTECT_PROCESS: begin
TempSafeId:=Handle(pIOBuffer^);
SetSafeId(TempSafeId);
OutByteCount:=4;
LONG(pIOBuffer^):=Integer(True);
end;
IOCTL_PROTECT_HWND:begin
TempSafehandle:=Handle(pIOBuffer^);
SetSafehandle(TempSafehandle);
OutByteCount:=4;
LONG(pIOBuffer^):=Integer(True);
end;
IOCTL_PROTECT_OTHER: begin
DoPub;
OutByteCount:=4;
LONG(pIOBuffer^):=Integer(True);
end;
else
Result := STATUS_INVALID_DEVICE_REQUEST;
AIrp^.IoStatus.Information := 0;
end;
AIrp^.IoStatus.Status := Result;
AIrp^.IoStatus.Information := OutByteCount;
IoCompleteRequest(AIrp, IO_NO_INCREMENT);
end;
procedure DriverUnload(pDriverObject: PDriverObject); stdcall;
begin
HookingUnhook;
IoDeleteSymbolicLink(@DosDevName);
IoDeleteDevice(pDriverObject^.DeviceObject);
end;
function _DriverEntry(pDriverObject: PDriverObject; RegistryPath: PUnicodeString) : NTSTATUS; stdcall;
var
LDevName: TUnicodeString;
LDevObj: PDeviceObject;
begin
RtlInitUnicodeString(LDevName, DeviceName);
RtlInitUnicodeString(DosDevName, DosDeviceName);
Result := IoCreateDevice(pDriverObject,0, @LDevName,
FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, LDevObj);
if NT_SUCCESS(Result) then
begin
pDriverObject^.MajorFunction[IRP_MJ_CREATE] := @hookCreate;
pDriverObject^.MajorFunction[IRP_MJ_CLOSE] := @hookClose;
pDriverObject^.MajorFunction[IRP_MJ_DEVICE_CONTROL] := @hookDeviceControl;
pDriverObject^.DriverUnload := @DriverUnload;
Result := IoCreateSymbolicLink(@DosDevName, @LDevName);
if not NT_SUCCESS(Result) then
begin
IoDeleteDevice(pDriverObject^.DeviceObject);
end;
end;
end;
end.
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)