内核驱动程序读取内存未发送整个字符串

Kernel driver read memory is not sending the whole string

本文关键字:字符串 驱动程序 读取 内存 内核      更新时间:2023-10-16

我有这个内核驱动程序用于从进程内存中读取字符串:

KeAttachProcess(GlobalProcessPE);
char* source = *(ULONG*)pBuf;
RtlZeroMemory(pBuf, pIoStackLocation->Parameters.DeviceIoControl.OutputBufferLength);
RtlCopyMemory(pBuf, source, 256);
KeDetachProcess();

以下是C++中的沟通过程:

DWORD ReadBuffer2[180] = { 0 };
DeviceIoControl(hDevice, IOCTL_READPROCMEM_S, &msg, sizeof(msg), ReadBuffer2, sizeof(ReadBuffer2), &dwBytesRead, NULL);
printf("Message: %sn", ReadBuffer2);
printf("Bytes read: %dn", dwBytesRead);

在运行和搜索字符串时,它实际上会从中捕获前四个字母,并显示以下内容:

Message: ABCD
Bytes read: 4

我已经使用替代方法检查了字符串,它应该显示 ABCDEFGHIJKL...

问题就在这里,为什么它只读取(或可能写入)前四个字节?

我已经设法通过读取每个地址的每个 4 个字符 + 4 来读取字符串。

这是通信代码:(我还在驱动程序中添加了一些 {} _except () {} __try,所以它不会蓝屏)

std::string str = "";
bool scanning = true;
for (int i = 0; i < 35; i++) {
    if (!scanning) break;
    msg = 0x095A2A28 + i * 0x4;
    DWORD ReadBuffer2[50] = {0};
    DeviceIoControl(hDevice, IOCTL_READPROCMEM_S, &msg, sizeof(msg), ReadBuffer2, sizeof(ReadBuffer2), &dwBytesRead, NULL);
    char dtostr[4];
    sprintf(dtostr, "%s", ReadBuffer2);
    for (int l = 0; l < 4; l++) {
        str += dtostr[l];
        if (dtostr[l] == '') {
            scanning = false;
            break;
        }
    }
}
std::cout << "~Message: " << str << std::endl;

欢迎来到"内核土地"。这个答案可能有点晚,但总比没有好,对吗?

您发送/读取数据所做的事情既不安全又丑陋。

若要将整个字符串发送到用户模式,下面是一个示例:

PCHAR data = "This String is from Device Driver !!!";
size_t datalen = strlen(data) + 1;//Length of data including null
RtlCopyBytes(Irp->AssociatedIrp.SystemBuffer, data, irpStack->Parameters.DeviceIoControl.OutputBufferLength);

这假设您没有使用 UNICODE,并注意尽管此示例 100% 有效,但它并不完整,需要改进。

享受。