//原始函数的正确功能,没有返回状态
__declspec(naked) OldObReferenceObjectByHandle(
IN HANDLE Handle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_TYPE ObjectType OPTIONAL,
IN KPROCESSOR_MODE AccessMode,
OUT PVOID *Object,
OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL
)
{
_asm
{
//功能函数,完成过滤操作
ULONG FilterFunction(
IN HANDLE Handle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_TYPE ObjectType OPTIONAL,
IN KPROCESSOR_MODE AccessMode,
OUT PVOID *Object,
OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL
)
{
PEPROCESS Process;
__try{
if(*PsProcessType==ObjectType)//判断句柄所属对象类型是不是*PsProcessType
{
OldObReferenceObjectByHandle(Handle,DesiredAccess,ObjectType,AccessMode,&Process,NULL);
//截获传递进来的参数,修改参数,调用原来的函数
if (_stricmp((char*)((char*)Process+0x174), ProtectName) == 0 )
return 1;
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
return 0;
}
return 0;
}
//中间函数,根据过滤函数决定原函数是否正常运行
__declspec(naked) T_ObReferenceObjectByHandle(
IN HANDLE Handle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_TYPE ObjectType OPTIONAL,
IN KPROCESSOR_MODE AccessMode,
OUT PVOID *Object,
OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL
) {
不好意思...贴漏了GainExclusivity....安全INLINE 用....不过听说也不是很安全
LONG AllCPURaised; //0表示所有CPU还没提升到DISPATCH_LEVE上,,,否则为1
LONG NumberOfRaisedCPU; //表示有多少个CPU的IRQL提升到了DISPATCH_LEVEL
VOID RaiseCPUIrqlAndWait(IN PKDPC Dpc,
IN PVOID DeferredContext,
IN PVOID SystemArgument1,
IN PVOID SystemArgument2)
{
InterlockedIncrement(&NumberOfRaisedCPU);
while(!InterlockedCompareExchange(&AllCPURaised, 1, 1))
{
__asm nop;
}
InterlockedDecrement(&NumberOfRaisedCPU);
if (KeGetCurrentIrql() != DISPATCH_LEVEL)
return NULL;
// Initialize both globals to zero.
InterlockedAnd(&AllCPURaised, 0);
InterlockedAnd(&NumberOfRaisedCPU, 0);
// Allocate room for our DPCs. This must be in NonPagedPool!
temp_pkdpc = (PKDPC) ExAllocatePool(NonPagedPool,KeNumberProcessors*sizeof(KDPC));
if (temp_pkdpc == NULL)
return NULL; //STATUS_INSUFFICIENT_RESOURCES;
u_currentCPU = KeGetCurrentProcessorNumber();
pkdpc = temp_pkdpc;
for (i = 0; i < KeNumberProcessors; i++, *temp_pkdpc++)
{
// Make sure we don't schedule a DPC on the current
// processor. This would cause a deadlock.
if (i != u_currentCPU)
{
KeInitializeDpc(temp_pkdpc,
RaiseCPUIrqlAndWait,
NULL);
// Set the target processor for the DPC; otherwise,
// it will be queued on the current processor when
// we call KeInsertQueueDpc.
NTSTATUS ReleaseExclusivity(PVOID pkdpc)
{
InterlockedIncrement(&AllCPURaised); // Each DPC will decrement
// the count now and exit.
// We need to free the memory allocated for the DPCs.
while(InterlockedCompareExchange(&NumberOfRaisedCPU, 0, 0))
{
__asm nop;
}
if (pkdpc != NULL)
{
ExFreePool(pkdpc);
pkdpc = NULL;
}
return STATUS_SUCCESS;