[hide]#include <ntddk.h>
#include "Header.h"
//NtOpenKey的前5字节
UCHAR NtCreateFileHeader[5] = {NULL};
//UCHAR Jmp[5] = {233,0,0,0,0};
void PageProtectOn()
{
__asm
{
mov eax, cr0;
or eax, 10000h;
mov cr0, eax;
sti
}
}
void PageProtectOff() //关闭
{
__asm
{
cli
mov eax, cr0;
and eax, not 10000h;
mov cr0, eax;
}
}
VOID Hook_Stop();
VOID Hook_Start();
/* NtCreateFile原形 */
NTSTATUS NtCreateFile(
PHANDLE FileHandle,
ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes,
PIO_STATUS_BLOCK IoStatusBlock,
PLARGE_INTEGER AllocationSize,
ULONG FileAttributes,
ULONG ShareAccess,
ULONG CreateDisposition,
ULONG CreateOptions,
PVOID EaBuffer,
ULONG EaLength
)
{
NTSTATUS status;
/*
__asm
{
mov edi,edi
push ebp
mov ebp,esp
mov eax,KeServiceDescriptorTable.ServiceTableBase[119]
add eax,5
jmp eax
}
*/
/* 停止HOOK */
Hook_Stop();
DbgPrint("目录:%ws",ObjectAttributes->ObjectName->Buffer);
/* 在文件名中寻找字符串 -> fishc 中文可能DbgView 不支持 */
if(wcsstr(ObjectAttributes->ObjectName->Buffer,L"fishc"))
{
/* 修改返回值有不一样的结果 */
status = STATUS_OBJECT_NAME_NOT_FOUND;
}
else
{
/* 把参数传 给zwcreatefile 并保存返回值 */
status = ZwCreateFile(FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,AllocationSize,FileAttributes,ShareAccess,CreateDisposition,CreateOptions,EaBuffer,EaLength);
}
Hook_Start();
return status;
}
VOID Hook_Start()
{
/* 局部变量 */
UINT32 *Address = NULL; //
UINT32 Desaddress = NULL; //偏移地址
//关闭内存保护
PageProtectOff();
//写前保存前5字节
RtlCopyMemory((VOID*)&NtCreateFileHeader,(VOID*)KeServiceDescriptorTable.ServiceTableBase[37],5);
/* 计算偏移地址 公式 = 目标地址 - 原地址 - 5 */
Desaddress = (ULONG)&NtCreateFile - (ULONG)KeServiceDescriptorTable.ServiceTableBase[37] - 5;
*((UCHAR*)(KeServiceDescriptorTable.ServiceTableBase[37])) = 233; //JMP
*((unsigned int*)(KeServiceDescriptorTable.ServiceTableBase[37]+1)) = Desaddress; //地址
//恢复内存保护
PageProtectOn();
}
VOID Hook_Stop()
{
PageProtectOff();
/* 恢复HOOK */
RtlCopyMemory((VOID*)KeServiceDescriptorTable.ServiceTableBase[37],(VOID*)NtCreateFileHeader,5);
PageProtectOn();
}
VOID DriverUnload(PDRIVER_OBJECT pDriverObject)
{
DbgPrint("Driver Unload!");
/* 停止HOOK */
Hook_Stop();
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING RegistryPath)
{
DbgPrint("Hello! Driver! Driver Entry\n");
DbgPrint("NtOpenKey address:0x%x\n",(unsigned int)KeServiceDescriptorTable.ServiceTableBase[37]);
pDriverObject->DriverUnload = DriverUnload;
/* 开始HOOK */
Hook_Start();
return STATUS_SUCCESS;
}[/hide]