首页
论坛
课程
招聘
[分享]关于PsSetLoadImageNotifyRoutine 调用ZwProtectVirtualMemory 会死锁的解决办法
2021-11-20 14:46 4820

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

2021-11-20 14:46
4820

用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;

}


[2022冬季班]《安卓高级研修班(网课)》月薪三万班招生中~

收藏
点赞0
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回