#include "ntddk.h"
//#include <ntifs.h>
#include <windef.h>
ULONG g_KiInsertQueueApc;
ULONG g_uCr0;
BYTE g_HookCode[5] = { 0xe9, 0, 0, 0, 0 };
BYTE g_OrigCode[5] = { 0 }; // 原函数的前字节内容
BYTE jmp_orig_code[7] = { 0xEA, 0, 0, 0, 0, 0x08, 0x00 };
BOOL g_bHooked = FALSE;
PEPROCESS eProcess;
NTKERNELAPI
NTSTATUS
PsLookupProcessByProcessId (
IN ULONG ProcessId,
OUT PEPROCESS *Process
);
typedef ULONG (*KIINSERTQUEUEAPC) (
PKAPC Apc,
KPRIORITY Increment
);
KIINSERTQUEUEAPC KiInsertQueueApc;
VOID
fake_KiInsertQueueApc (
PKAPC Apc,
KPRIORITY Increment
);
VOID
Proxy_KiInsertQueueApc (
PKAPC Apc,
KPRIORITY Increment
);
void WPOFF()
{
ULONG uAttr;
_asm
{
push eax;
mov eax, cr0;
mov uAttr, eax;
and eax, 0FFFEFFFFh; // CR0 16 BIT = 0
mov cr0, eax;
pop eax;
cli
};
g_uCr0 = uAttr; //保存原有的 CRO 屬性
}
VOID WPON()
{
_asm
{
sti
push eax;
mov eax, g_uCr0; //恢復原有 CR0 屬性
mov cr0, eax;
pop eax;
};
}
//
// 停止inline hook
//
VOID UnHookKiInsertQueueApc ()
{
KIRQL oldIrql;
WPOFF();
oldIrql = KeRaiseIrqlToDpcLevel();
RtlCopyMemory ( (BYTE*)g_KiInsertQueueApc, g_OrigCode, 5 );
KeLowerIrql(oldIrql);
WPON();
g_bHooked = FALSE;
}
//
// 开始inline hook -- KiInsertQueueApc
//
VOID HookKiInsertQueueApc ()
{
KIRQL oldIrql;
if (g_KiInsertQueueApc == 0) {
DbgPrint("KiInsertQueueApc == NULL\n");
return;
}
//DbgPrint("开始inline hook -- KiInsertQueueApc\n");
DbgPrint( "KiInsertQueueApc的地址t0x%08x\n", (ULONG)g_KiInsertQueueApc );
// 保存原函数的前字节内容
RtlCopyMemory (g_OrigCode, (BYTE*)g_KiInsertQueueApc, 5);
*( (ULONG*)(g_HookCode + 1) ) = (ULONG)fake_KiInsertQueueApc - (ULONG)g_KiInsertQueueApc - 5;
DbgPrint("Start Hook\n");
// 禁止系统写保护,提升IRQL到DPC
WPOFF();
oldIrql = KeRaiseIrqlToDpcLevel();
RtlCopyMemory ( (BYTE*)g_KiInsertQueueApc, g_HookCode, 5 );
*( (ULONG*)(jmp_orig_code + 1) ) = (ULONG) ( (BYTE*)g_KiInsertQueueApc + 5 );
RtlCopyMemory ( (BYTE*)Proxy_KiInsertQueueApc, g_OrigCode, 5);
RtlCopyMemory ( (BYTE*)Proxy_KiInsertQueueApc + 5, jmp_orig_code, 7);
// 恢复写保护,降低IRQL
KeLowerIrql(oldIrql);
WPON();
g_bHooked = TRUE;
}
//
// 跳转到我们的函数里面进行预处理
//
__declspec (naked)
VOID
fake_KiInsertQueueApc (
PKAPC Apc,
KPRIORITY Increment
)
{
KAPC *pApc;
ULONG pTargetThread;
ULONG pTargetProcess;
UCHAR *pTargetProcessName;
DWORD dwTargetProcessId;
PEPROCESS pCurrentEprocess;
UCHAR *pCurrentProcessName;
DWORD dwCurrentProcessId;
KPRIORITY MyIncrement;
_asm{
nop
nop
nop
nop
nop
push ebp
mov ebp,esp
sub esp,__LOCAL_SIZE
pushad
mov edi,ecx
mov pApc,edi
mov MyIncrement,edx
}
pTargetThread=*((PULONG)((ULONG)pApc+0x008));
pTargetProcess =*((PULONG)((ULONG)pTargetThread + 0x044 ));
pTargetProcessName=(UCHAR *)(pTargetProcess+0x174);
dwTargetProcessId = *((DWORD*)((ULONG)pTargetProcess+0x084));
pCurrentEprocess=PsGetCurrentProcess();
pCurrentProcessName=(UCHAR *)((ULONG)pCurrentEprocess+0x174);
dwCurrentProcessId=(DWORD)((ULONG)pCurrentEprocess+0x084);
// _strlwr(pCurrentProcessName);
// _strlwr(pTargetProcessName);
if((_stricmp(pTargetProcessName,"notepad.exe")==0)&&(MyIncrement==2))
{
if(strcmp(pCurrentProcessName,"notepad.exe")!=0 && eProcess!=PsGetCurrentProcess())
{
_asm{
popad
mov esp,ebp
pop ebp
ret
}
}
}
_asm{
popad
mov esp,ebp
pop ebp
}
_asm{
mov eax,Proxy_KiInsertQueueApc
jmp eax
}
}
__declspec (naked)
VOID
Proxy_KiInsertQueueApc (
PKAPC Apc,
KPRIORITY Increment
)
{
__asm { // 共字节
_emit 0x90
_emit 0x90
_emit 0x90
_emit 0x90
_emit 0x90 // 前字节实现原函数的头字节功能
_emit 0x90 // 这个填充jmp
_emit 0x90
_emit 0x90
_emit 0x90
_emit 0x90 // 这字节保存原函数+5处的地址
_emit 0x90
_emit 0x90 // 因为是长转移,所以必须是0x0080
}
}
ULONG GetFunctionAddr( IN PCWSTR FunctionName)
{
UNICODE_STRING UniCodeFunctionName;
RtlInitUnicodeString( &UniCodeFunctionName, FunctionName );
return (ULONG)MmGetSystemRoutineAddress( &UniCodeFunctionName );
}
ULONG FindKiInsertQueueApcAddress()
{
char * Addr_KeInsertQueueApc = 0;
int i = 0;
char Findcode[] = { 0xE8, 0xcc, 0x29, 0x00, 0x00 };
ULONG Addr_KiInsertQueueApc = 0;
Addr_KeInsertQueueApc = (char *) GetFunctionAddr(L"KeInsertQueueApc");
for(i = 0; i < 100; i ++)
{
if( Addr_KeInsertQueueApc[i] == Findcode[0] &&
Addr_KeInsertQueueApc[i + 1] == Findcode[1] &&
Addr_KeInsertQueueApc[i + 2] == Findcode[2] &&
Addr_KeInsertQueueApc[i + 3] == Findcode[3] &&
Addr_KeInsertQueueApc[i + 4] == Findcode[4]
)
{
Addr_KiInsertQueueApc = (ULONG)&Addr_KeInsertQueueApc[i] + 0x29cc + 5;
break;
}
}
DbgPrint( "KiInsertQueueApc的地址t0x%08x\n", (ULONG)Addr_KiInsertQueueApc );
return Addr_KiInsertQueueApc;
}
ULONG FindKiInsertQueueApcAddress_other()
{
PUCHAR cPtr;
PUCHAR addr;
addr = (PUCHAR) GetFunctionAddr( L"KeInsertQueueApc" );
for (cPtr = (PUCHAR)addr;
cPtr < (PUCHAR)addr + PAGE_SIZE;
cPtr++)
{
if (*cPtr == 0xE8 && *(PUSHORT)(cPtr + 5) == 0xD88A) {
KiInsertQueueApc = (KIINSERTQUEUEAPC)(*(PULONG)(cPtr + 1) + (ULONG)cPtr + 5);
DbgPrint( "KiInsertQueueApc:\t0x%08x\n", (ULONG)KiInsertQueueApc );
break;
}
}
return (ULONG)KiInsertQueueApc;
}
VOID OnUnload( IN PDRIVER_OBJECT DriverObject )
{
DbgPrint("My Driver Unloaded!");
UnHookKiInsertQueueApc();
}
NTSTATUS DriverEntry( IN PDRIVER_OBJECT theDriverObject, IN PUNICODE_STRING theRegistryPath )
{
NTSTATUS ntStatus;
DbgPrint("My Driver Loaded!");
theDriverObject->DriverUnload = OnUnload;
g_KiInsertQueueApc = FindKiInsertQueueApcAddress_other();
ntStatus=PsLookupProcessByProcessId(1748,&eProcess);
if(NT_SUCCESS(ntStatus))
{
ObDereferenceObject(eProcess);
}
DbgPrint("Find KeInsertQueueApc at %x\n",g_KiInsertQueueApc);
HookKiInsertQueueApc();
return STATUS_SUCCESS;
}
程序运行时一切正常,可当卸载驱动后,保护的进程依然无法关闭.郁闷.前些日子没研究透,就放下了,现在回头研究,还是没弄懂到底那里出问题了.
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课