用来自另一个进程的数据填充分配的内存

问题描述

好吧,这个问题不是常规的,也许是愚蠢的,因为我不熟悉 C++ 中的指针和链接

所以我在另一个进程 (http://prntscr.com/zmfb4p) 的内存中有一些数据,大约 1200-1600 字节。 我有一个驱动程序,它可以对所需的进程进行内核读写。 我有一个用户模式应用程序,它可以像这样与驱动程序通信:

int reading_data = driver.readvirtualmemory<int>(<processId>,<adress to read>,<size to read>);

它适用于小数据类型,但我无法理解,如何获取“大量”字节并存储它:

分配内存来存储数据:

char* test_buf = new char[size_matricies_buffer];    // allocating memory and creating a pointer to it ~1200-1600 depends on situation
*test_buf = driver.ReadVirtualMemory<char>(<process>,<address>,static_cast<uint32_t>(size_matricies_buffer));  // filling allocated memory with data?

它可以编译并运行,但是当我尝试访问 *test 时出现错误

cout << "buf: " << *test_buf << " | " << &test_buf << endl;

Mysoftware.exe 中 0x00007FF6D1DD1671 处未处理的异常:0xC0000005:访问冲突写入位置 0x00000000C21C833C。

知道我在这里遗漏了什么吗?

解决方法

所以基本函数 readVirtualMemory 就是这样做的(我想我上面的解释太简短了,但现在是 nvm):

template <typename type>
type ReadVirtualMemory(ULONG64 ProcessId,ULONG64 ReadAddress,SIZE_T Size)
{
    type Buffer;

    KERNEL_READ_REQUEST ReadRequest;

    ReadRequest.ProcessId = ProcessId;
    ReadRequest.Address = ReadAddress;
    ReadRequest.pBuff = &Buffer;
    ReadRequest.Size = Size;

    if (DeviceIoControl(hDriver,IO_READ_REQUEST,&ReadRequest,sizeof(ReadRequest),0))
    {
        return Buffer;
    }

    return Buffer;
}

而 KERNEL_READ_REQUEST 是:

   typedef struct _KERNEL_READ_REQUEST
{
    ULONG64 ProcessId;
    ULONG64 Address;
    PVOID64 pBuff;
    ULONG64 Size;

} KERNEL_READ_REQUEST,* PKERNEL_READ_REQUEST;

所以问题在于原始函数和函数结构中必要的类型定义。下一个解决方案解决了问题:添加一个新的 kernel_read 函数,没有类型声明,带有指向需要由读取数据填充的缓冲区的指针。 (感谢@SamiKuhmonen 的想法):

    bool ReadBuffer(ULONG64 ProcessId,void* buffer,SIZE_T Size,bool secondary = false)
{
    KERNEL_READ_REQUEST ReadRequest;

    ReadRequest.ProcessId = ProcessId;
    ReadRequest.Address = ReadAddress;
    ReadRequest.pBuff = buffer;
    ReadRequest.Size = Size;

    if (DeviceIoControl(hDriver,0))
    {
        return true;
    }

    return false;
}

在那之后我的缓冲区

char* matricies_buf_buf = new char[size_matricies_buffer];
driver.ReadBuffer(pid,matricies_buf_buf,transform_data[0],size_matricies_buffer);

填充了所需的数据。

请原谅一些对这个问题感到恼火的人,这是我第一次尝试提出问题和 stackoverflow。