【问题标题】:Kernel driver read memory is not sending the whole string内核驱动程序读取内存未发送整个字符串
【发布时间】:2016-02-11 18:17:49
【问题描述】:

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

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: %s\n", ReadBuffer2);
printf("Bytes read: %d\n", dwBytesRead);

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

Message: ABCD
Bytes read: 4

我用另一种方法检查了字符串,它应该显示 ABCDEFGHIJKL...

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

【问题讨论】:

  • char* source = *(ULONG*)pBuf; wtf?
  • pBuf是Irp->AssociatedIrp.SystemBuffer,应该是我正在读取的内存地址。
  • 那不是我的意思!
  • 请说明您的观点,因为我不明白从 unsigned long 获取 char 指针是否错误...?
  • 这假定ULONG 等价于一个指针,而不是一个安全的赌注。 SystemBuffer 已经是一个指针,所以不需要这样做。

标签: c++ c kernel kmdf


【解决方案1】:

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

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

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] == '\0') {
            scanning = false;
            break;
        }
    }
}
std::cout << "~Message: " << str << std::endl;

【讨论】:

    【解决方案2】:

    欢迎来到“内核之地”。这个答案可能有点晚了,但总比没有好,对吧?

    您发送/读取数据的操作既不安全也不丑陋。

    要将整个字符串发送到用户模式,这里是一个例子:

    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% 有效,但它并不完整,需要改进。

    享受吧。

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2023-02-07
      • 1970-01-01
      相关资源
      最近更新 更多