【问题标题】:Windows Driver IOCTL wrong return;Windows Driver IOCTL 错误返回;
【发布时间】:2020-10-10 14:01:04
【问题描述】:

我正在尝试做一些 IOCTL(KM 到 UM 的通信)

这里的重点是获取进程的基地址。

这是我的驱动程序中的功能:

PVOID ioBuffer = NULL;
PUCHAR UserBuffer;
PIO_STACK_LOCATION io;
io = IoGetCurrentIrpStackLocation(irp);
ULONG inputBufferLength = NULL;
ULONG outputBufferLength = NULL;
ULONG ioControlCode = NULL;
PIO_STACK_LOCATION irpStack;
BASE_ADDRESS2 info = { 0 };
BASE_ADDRESS pOutput = { 0 };
    
irpStack = IoGetCurrentIrpStackLocation(irp);
ioBuffer = (*irp).AssociatedIrp.SystemBuffer;
inputBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength;
outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
    
if (inputBufferLength >= sizeof(BASE_ADDRESS2) &&
outputBufferLength >= sizeof(PBASE_ADDRESS) && ioBuffer) {
irp->IoStatus.Status = GetModuleBaseAddress((PBASE_ADDRESS)ioBuffer, &pOutput);
memcpy(ioBuffer, &pOutput, sizeof(pOutput));
irp->IoStatus.Information = sizeof(pOutput);
DbgPrintEx(0, 0, "pOutput.baseaddress :%p\n", pOutput.baseAddress);
DbgPrintEx(0, 0, "IoStatus.Information :%p\n", irp->IoStatus.Information);
IoCompleteRequest(irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}

我的调试打印显示我得到了正确的基地址:

但是,当我将此值返回到我的 UM 应用程序时,我的输出为 0:

    typedef struct _BASEADDRESS
        {
            ULONGLONG baseAddress;
        } BASE_ADDRESS, * PBASE_ADDRESS;
    
        typedef struct _BASEADDRESS2
        {
            LONG pid;
        } BASE_ADDRESS2, * PBASE_ADDRESS2;


        DWORD bytes = 0;
        HANDLE DriverHdl;
        BASE_ADDRESS2 info = { 0 };
        BASE_ADDRESS pResult = { 0 };
        info.pid = pID;
        HANDLE hDevice = INVALID_HANDLE_VALUE;
        hDevice = CreateFileW(DRIVER_NAME, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
        if (!hDevice)
            return 0;

        if (DeviceIoControl(hDevice, IOCTL_BASEADDR, &info, sizeof(info), &pResult, sizeof(pResult), &bytes, nullptr))
            printf("%08p", pResult.baseAddress);
        return pResult.baseAddress;

这里,pResult 始终为 0。

知道我做错了什么吗?老实说,对 C 有点陌生。

【问题讨论】:

    标签: c++ c kernel driver ioctl


    【解决方案1】:

    检查此链接 https://docs.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-deviceiocontrol

    如果您的 DeviceIoControl 函数返回 0 表示操作失败或处于挂起状态,则返回值为零。要获取扩展的错误信息,请调用 GetLastError。

    例如,您也可以参考https://docs.microsoft.com/en-us/windows/win32/DevIO/calling-deviceiocontrol

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 2015-03-13
      • 2014-05-15
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多