MSR_HOOK
mov eax,0xeb //调用号
mov edx,KUSER_SHARED_SYSCALL
// cpu初始化时,根据架构不同,KUSER_SHARED_SYSCALL里面的实现的内核函数指针接口也不同,可能是 KiIntSystemCall中断式系统调用 也可能是 KiFastSystemCall快速系统调用
call [edx]
ret 0x10mov edx,esp
sysenter
retnLast updated
mov eax,0xeb //调用号
mov edx,KUSER_SHARED_SYSCALL
// cpu初始化时,根据架构不同,KUSER_SHARED_SYSCALL里面的实现的内核函数指针接口也不同,可能是 KiIntSystemCall中断式系统调用 也可能是 KiFastSystemCall快速系统调用
call [edx]
ret 0x10mov edx,esp
sysenter
retnLast updated
#include <ntddk.h>
ULONG OldAddr;
VOID DriverUnload(PDRIVER_OBJECT pDriver_Object);
VOID OnHook();
UINT32 g_Pid = 2652;
void _declspec(naked) MyKiFastCallEntry() //过滤参数
{
__asm
{
cmp eax, 0xbe;//对比是否是NtOpenProcess的调用号
jne _End; //不是则不处理
push eax; //保存寄存器
mov eax, [edx + 0x14];//获取第4个参数PCLIENT_ID
mov eax, [eax];//获取PCLIENT_ID第一个字段PID
//PCLIENT_ID->UniqueProcess的值
cmp eax, g_Pid;//判断是否是要保护的进程
pop eax;
jne _End;
cmp[edx + 0xc], 1;//判断是否是关闭操作
jne _End;
mov[edx + 0xc], 0;//是就把访问权限设为无
_End:
jmp OldAddr;//调用原来的_KiFastCallEntry函数
}
}
VOID OnHook(){
KAFFINITY ActiveProcessors, CurrentAffinity;
ActiveProcessors = KeQueryActiveProcessors();
for (CurrentAffinity = 1; ActiveProcessors; CurrentAffinity <<= 1) //考虑多核同步下,msr逻辑分离,所以修改每个内核的msr
{
if (ActiveProcessors & CurrentAffinity)
{
ActiveProcessors &= ~CurrentAffinity;
KeSetSystemAffinityThread(CurrentAffinity);
_asm
{
cli // 锁,防止中断
push ecx
push eax
mov ecx, 0x176
rdmsr
mov OldAddr, eax //保存原来的 SYSENTER_EIP_MSR中的_KiFastCallEntry
xor eax, eax
mov eax, MyKiFastCallEntry // 将 SYSENTER_EIP_MSR寄存器的值设置为我们的过滤函数
wrmsr
xor eax, eax
xor ecx, ecx
pop eax
pop ecx
sti
}
}
}
DbgPrint("NewKiFastCallEntry Addr:%08x\n", MyKiFastCallEntry);
}
VOID DriverUnload(PDRIVER_OBJECT pDriver_Object) //恢复HOOK
{
KAFFINITY ActiveProcessors, CurrentAffinity;
ActiveProcessors = KeQueryActiveProcessors();
for (CurrentAffinity = 1; ActiveProcessors; CurrentAffinity <<= 1)
{
if (ActiveProcessors & CurrentAffinity)
{
ActiveProcessors &= ~CurrentAffinity;
KeSetSystemAffinityThread(CurrentAffinity);
_asm
{
cli
push ecx
push eax
mov ecx, 0x176
mov eax, OldAddr
wrmsr
xor ecx, ecx
xor eax, eax
pop eax
pop ecx
sti
}
}
}
DbgPrint("驱动卸载成功\n");
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriver_Object, PUNICODE_STRING pRegstryString)
{
OnHook();
pDriver_Object->DriverUnload = DriverUnload;
return STATUS_SUCCESS;
}