[原创]IRP Function Hook
发表于:
2008-11-19 23:16
8675
大虾们可以飘过~
相信各位都玩过HOOK,就不多说了。
下午看了一大虾写的RootKit专题,提到IRP HOOK ,于是小菜我试了一下其中的法3.
http://bbs.pediy.com/showthread.php?t=60022
具体见代码,我只是HOOK 了 IRP_MJ_DEVICECONTROL 很简单~
只说自己遇到的一个问题,虽不普遍但说出来我爽下
在安装我的驱动的时候,发现老是返回STATUS_ACCESS_DENIED,郁闷,遂翻开WRK查查函数IoGetDeviceObjectPointer,应该是在NtOpenFile处返回了这个拒绝…………
就是我打开的驱动没有设置共享,IoCreateDevice的Exclusive应该是当初被设置成TRUE了(的确就是这样的 呵呵 因为这个驱动就是我写的~~),不过还好,一般都是设置成FALSE的,否则驱动就被独占了。
FROM MSDN:Most drivers set this value to FALSE.
下面提供相关CODE,请斧正(大菜鸟以上飘过):
#include "ntddk.h"
typedef NTSTATUS (_stdcall *fnDevIoControl)(PDEVICE_OBJECT pdev,PIRP pIrp);
fnDevIoContro RealDevIoControl;
NTSTATUS HookDevIoControl(PDEVICE_OBJECT pdev,PIRP pIrp)
{
KdPrint(("被我Hook了 哈哈哈哈哈哈哈哈哈啊哈哈哈 \n"));
return RealDevIoControl(pdev,pIrp);
}
//注意这里我没有恢复HOOK
VOID Unload(IN PDRIVER_OBJECT DriverObject)
{
PDEVICE_OBJECT pDevObj;
UNICODE_STRING sysLinkName;
RtlInitUnicodeString(&sysLinkName,L"\\??\\Fuck");
KdPrint(("==>DriverUnload……\n"));
pDevObj=DriverObject->DeviceObject;
KdPrint(("删除符号链接……\n"));
IoDeleteSymbolicLink(&sysLinkName);
KdPrint(("删除CDO……\n"));
IoDeleteDevice(pDevObj);
KdPrint(("<==DriverUnload……\n"));
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
NTSTATUS ststus;
UNICODE_STRING devName;
UNICODE_STRING linkName;
PDEVICE_OBJECT pdevObj,fuckdevObj;
PFILE_OBJECT fuckfileObj;
UNICODE_STRING fuckName;
ULONG uAddr=0;
ULONG MJEntry=0;
ULONG IRP_Mj_Create=0;
ULONG IRP_Mj_DeviceIoControl=0;
//完成一些工作先,当然就这个程序而言,不需要创建设备及链接
RtlInitUnicodeString(&devName,L"\\Device\\Fuck");
KdPrint(("==>DriverEntry\n"));
DriverObject->DriverUnload=Unload;
ststus=IoCreateDevice(DriverObject,0,&devName,FILE_DEVICE_UNKNOWN,0,TRUE,&pdevObj);
if (!NT_SUCCESS(ststus))
{
return ststus;
}
DriverObject->Flags|= DO_BUFFERED_IO;
RtlInitUnicodeString(&linkName,L"\\??\\Fuck");
ststus=IoCreateSymbolicLink(&linkName,&devName);
if (!NT_SUCCESS(ststus))
{
IoDeleteDevice(pdevObj);
return ststus;
}
KdPrint(("Now Fuck^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"));
RtlInitUnicodeString(&fuckName,L"\\Device\\mySSDT");//这个mySSDT是我们HOOK的对象,具体设备名可以用DDK自带的DeviceTree看看
_try
{
ststus=IoGetDeviceObjectPointer(&fuckName,FILE_ALL_ACCESS,&fuckfileObj,&fuckdevObj);
if (!NT_SUCCESS(ststus))
{
KdPrint(("Fuck Failure!IoGetDeviceObjectPointer status Failure\n"));
KdPrint(("status : %x\n",ststus));
return STATUS_SUCCESS;
}
KdPrint(("Fuck SUCCESS! fuckdevObj: %x\n",fuckdevObj));
//delete the object ~
// KdPrint(("delete the object ~\n"));
//
// IoDeleteDevice(fuckdevObj);
// KdPrint(("delete the object OK ~\n"));
KdPrint(("MajorFunction: %x\n",*(ULONG*)((ULONG)fuckdevObj+0x8)+0x38)); //可以不用硬编码,直接用结构体,是有导出的,见WinDBG
uAddr=(ULONG)fuckdevObj;
uAddr+=0x8;
uAddr=*(ULONG*)uAddr;
uAddr+=0x38;
MJEntry=uAddr;
IRP_Mj_Create=*(ULONG*)MJEntry;
KdPrint(("IRP_MJ_CREATE: %x\n",IRP_Mj_Create));
IRP_Mj_DeviceIoControl=*(ULONG*)(MJEntry+0x0e*4);
RealDevIoControl=(fnDevIoContro)IRP_Mj_DeviceIoControl;
*(ULONG*)(MJEntry+0x0e*4)=(ULONG)HookDevIoControl;
KdPrint(("IRP_Mj_DeviceIoControl %x\n",IRP_Mj_DeviceIoControl));
KdPrint(("IRP_Mj_DeviceIoControl Hook %x\n",HookDevIoControl));
}
_except(EXCEPTION_EXECUTE_HANDLER)
{
KdPrint(("Fuck Failure!IoGetDeviceObjectPointer Failure\n"));
}
KdPrint(("<==DriverEntry\n"));
return ststus;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!