首页
社区
课程
招聘
[分享]关于PsSetLoadImageNotifyRoutine 调用ZwProtectVirtualMemory 会死锁的解决办法
发表于: 2021-11-20 14:46 6103

[分享]关于PsSetLoadImageNotifyRoutine 调用ZwProtectVirtualMemory 会死锁的解决办法

2021-11-20 14:46
6103

用MDL的方式修改内存就可以了!

 

NTSTATUS SafeCopyMemory(PVOID ulAddrDst, PVOID ulAddrSrc, ULONG ulLenToCopy)
{
NTSTATUS status = STATUS_UNSUCCESSFUL;
ULONG ulLen = 0;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
KIRQL       irqlCur;
PMDL        pMdlSrc = NULL;
PMDL        pMdlDst = NULL;
 
PVOID       pMdlSafeSrc = NULL;
PVOID       pMdlSafeDst = NULL;
 
/// 校验所有要操作的字节地址 
ulLen = (ulLenToCopy > 0) ? (ulLenToCopy - 1) : 0;
do
{
    if (/*!MmIsAddressValid(((UCHAR *)ulAddrDst + ulLen))  || */
        !MmIsAddressValid(((UCHAR *)ulAddrSrc + ulLen)))
    {
        goto _SafeCopy_END;
    }
} while (0 != ulLen--);
 
pMdlDst = IoAllocateMdl(ulAddrDst, ulLenToCopy, FALSE, FALSE, NULL);
pMdlSrc = IoAllocateMdl(ulAddrSrc, ulLenToCopy, FALSE, FALSE, NULL);
if ((NULL != pMdlSrc) && (NULL != pMdlDst))
{
    __try
    {
        MmProbeAndLockPages(pMdlDst, UserMode, (LOCK_OPERATION)IoReadAccess);
        MmProbeAndLockPages(pMdlSrc, KernelMode, (LOCK_OPERATION)(IoWriteAccess | IoReadAccess));
    }
    __except (EXCEPTION_EXECUTE_HANDLER)
    {
        status = GetExceptionCode();
        goto  _SafeCopy_END;
    }
}
 
pMdlSafeDst = MmGetSystemAddressForMdlSafe(pMdlDst, NormalPagePriority);
pMdlSafeSrc = MmGetSystemAddressForMdlSafe(pMdlSrc, NormalPagePriority);
if ((NULL != pMdlSafeDst) && (NULL != pMdlSafeSrc))
{
    irqlCur = KeRaiseIrqlToDpcLevel();
    memcpy(pMdlSafeDst, pMdlSafeSrc, ulLenToCopy);
    KeLowerIrql(irqlCur);
 
    status = STATUS_SUCCESS;
}

_SafeCopy_END:
if (NULL != pMdlDst)
{
MmUnlockPages(pMdlDst);
IoFreeMdl(pMdlDst);
}

1
2
3
4
5
6
if (NULL != pMdlSrc)
{
    MmUnlockPages(pMdlSrc);
    IoFreeMdl(pMdlSrc);
}
return status;

}


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//