使用内核驱动程序C ++编写只读地址

时间:2020-09-30 10:09:10

标签: c++ memory kernel readonly write

我正在尝试使用内核驱动程序将内存写入用户模式进程,

我要为其写入内存的当前地址是只读的,我想向当前地址写入4个字节,

问题是,如果我使用VirtualProtectEx更改了进程的保护(页面),它会工作并写入内存,但这仅在用户模式级别,我的意图是从内核模式更改该进程的保护,我想要使其为READWRITE,然后将其从内核空间改回READ,

现在我想做的就是给我一个蓝屏死机(蓝屏死机),并显示错误:Kmode_exception_not_handld

我无法理解代码中的什么触发了此蓝屏死机,我的PC的规格非常有限,我无法在VM中进行调试。

我将编写在用户模式下有效的代码,以及在内核空间中对我不起作用的代码:

此处有效的代码:

void dispatch::handler(void* info_struct)
{
    PINFO_STRUCT info = (PINFO_STRUCT)info_struct;

    if (info->code == CODE_READ_MEMORY)
    {
        PEPROCESS target_process = NULL;
        if (NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)info->process_id, &target_process)))
        {
            memory::read_memory(target_process, (void*)info->address, &info->buffer, info->size);
        }
        DbgPrintEx(0, 0, "[TEST]: Read Memory\n");
    }
    else if (info->code == CODE_WRITE_MEMORY)
    {
        PEPROCESS target_process = NULL;
        if (NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)info->process_id, &target_process)))
        {


            memory::write_memory(target_process, &info->buffer, (void*)info->address, info->size);

        }
        DbgPrintEx(0, 0, "[TEST]: Write Memory\n");

    }
}


NTSTATUS memory::write_memory(PEPROCESS target_process, void* source, void* target, size_t size)
{
    if (!target_process) { return STATUS_INVALID_PARAMETER; }



    size_t bytes = 0;
    NTSTATUS status = MmCopyVirtualMemory(IoGetCurrentProcess(), source, target_process, target, size, KernelMode, &bytes);
    if (!NT_SUCCESS(status) || !bytes)
    {
        return STATUS_INVALID_ADDRESS;
    }


    return status;
}


int main()

{

DWORD oldprt
ULONG writeTest1 = 3204497152;


VirtualProtectEx(ProcManager::hProcess, (PVOID)(testAddr), 4, PAGE_READWRITE, &oldprt);
driver_control::write_memory(process_id, testAddr, writeTest1);
VirtualProtectEx(ProcManager::hProcess, (PVOID)(testAddr), 4, PAGE_READONLY, &oldprt);

return 0;

}

现在我想做的就是停止使用VirtualProtectEx,并将PAGE保护从内核空间更改为READWRITE,所以我要做的就是在dispatch :: handler函数中添加它:

else if (info->code == CODE_WRITE_MEMORY)
{
    PEPROCESS target_process = NULL;
    if (NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)info->process_id, &target_process)))
    {
        PMDL Mdl = IoAllocateMdl((void*)info->address, info->size, FALSE, FALSE, NULL);

        if (!Mdl)
            return false;

        // Locking and mapping memory with RW-rights:
        MmProbeAndLockPages(Mdl, KernelMode, IoReadAccess);
        PVOID Mapping = MmMapLockedPagesSpecifyCache(Mdl, KernelMode, MmNonCached, NULL, FALSE, NormalPagePriority);
        MmProtectMdlSystemAddress(Mdl, PAGE_READWRITE);

        memory::write_memory(target_process, &info->buffer, (void*)info->address, info->size);


        // Resources freeing:
        MmUnmapLockedPages(Mapping, Mdl);
        MmUnlockPages(Mdl);
        IoFreeMdl(Mdl);

    }
    DbgPrintEx(0, 0, "[TEST]: Write Read Only Memory\n");

}

所以我添加的这引起了BSOD,但是为什么我不明白,我在这里做错了什么?

如果需要了解更多信息,这里是信息结构:

#define CODE_READ_MEMORY 0x1
#define CODE_WRITE_MEMORY 0x2

typedef struct _INFO_STRUCT
{
    ULONG code;
    ULONG process_id;
    ULONG address;
    ULONG buffer;
    ULONG size;
}INFO_STRUCT, * PINFO_STRUCT;

有关解决此问题的任何建议?

0 个答案:

没有答案
相关问题