文件系统驱动里获取读写当前文件的进程信息

NTSTATUS GetProcessName(IN PEPROCESS pEproc, PWCHAR* outFullPath, PWCHAR* outProcName)
{
    typedef NTSTATUS(*xxQUERY_INFO_PROCESS) (
        __in HANDLE ProcessHandle,
        __in PROCESSINFOCLASS ProcessInformationClass,
        __out_bcount(ProcessInformationLength) PVOID ProcessInformation,
        __in ULONG ProcessInformationLength,
        __out_opt PULONG ReturnLength
        );

    static xxQUERY_INFO_PROCESS ZwQueryInformationProcess = NULL;
    NTSTATUS status = STATUS_UNSUCCESSFUL;
    ULONG returnedLength;
    ULONG bufferLength;
    PVOID buffer;
    PUNICODE_STRING imageName;
    HANDLE handle;

    *outProcName = NULL; *outFullPath = 0;

    if (KeGetCurrentIrql() != PASSIVE_LEVEL)
    {
        DbgPrint("IRQL Must PASSIVE_LEVEL.\n");
        return status;
    }

    if (NULL == ZwQueryInformationProcess)
    {
        UNICODE_STRING routineName;

        RtlInitUnicodeString(&routineName, L"ZwQueryInformationProcess");

        ZwQueryInformationProcess =
            (xxQUERY_INFO_PROCESS)MmGetSystemRoutineAddress(&routineName);

        if (NULL == ZwQueryInformationProcess)
        {
            DbgPrint("Cannot resolve ZwQueryInformationProcess\n");
            return status;
        }
    }

    // 
    // 1. pEproc --> handle 
    // 
    status = ObOpenObjectByPointer(pEproc,
        OBJ_KERNEL_HANDLE,
        NULL,
        0,
        NULL,
        KernelMode,
        &handle
    );

    //
    status = ZwQueryInformationProcess(handle,
        ProcessImageFileName,
        NULL, // buffer
        0, // buffer size
        &returnedLength);
    if (STATUS_INFO_LENGTH_MISMATCH != status)
    {
        ZwClose(handle);
        return status;
    }

    bufferLength = returnedLength - sizeof(UNICODE_STRING);

    buffer = ExAllocatePoolWithTag(NonPagedPool, returnedLength, 'FXSD');
    if (NULL == buffer)
    {
        ZwClose(handle);
        return STATUS_NO_MEMORY;
    }

    __try
    {

        status = ZwQueryInformationProcess(handle,
            ProcessImageFileName,
            buffer,
            returnedLength,
            &returnedLength);

        if (NT_SUCCESS(status))
        {

            imageName = (PUNICODE_STRING)buffer;

            USHORT len = imageName->Length;
            RtlMoveMemory(buffer, imageName->Buffer, imageName->Length);
            RtlZeroMemory(((PUCHAR)buffer) + len, sizeof(WCHAR));

            *outFullPath = (PWCHAR)buffer;

            PWCHAR ptr = wcsrchr((PWCHAR)buffer, L'\\');
            if (ptr)
            {
                *outProcName = ptr + 1;
            }
            else
            {
                *outProcName = (PWCHAR)buffer;
            }

            ZwClose(handle);
            return STATUS_SUCCESS;
        }
    }
    __except (EXCEPTION_EXECUTE_HANDLER)
    {
        status = GetExceptionCode();
        DbgPrint("get_process_name: Exception!!\n");
    }

    ZwClose(handle);
    ExFreePool(buffer);

    return status;
}

//调用示例        
PEPROCESS eProcess;
eProcess = IoThreadToProcess( PsGetCurrentThread());

PWCHAR pwFullPath = NULL, pwProcName = NULL;
NTSTATUS ntStatus = STATUS_SUCCESS;
ntStatus = GetProcessName(eProcess, &pwFullPath, &pwProcName);
if (NT_SUCCESS(ntStatus))
{
      DbgPrint("Process111 %ws !\n", pwProcName);
      if (NULL != pwFullPath)
      {
            ExFreePool(pwFullPath);
      }
}
else
{
       DbgPrint("GetProcessName failed, status = 0x%08X \n", ntStatus);
}

上面代码调试通过,记录下来,以备后续Ctrl+V时使用~ 如果你使用时遇到问题,欢迎交流(WX: zhxunCC)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

danxuezx

如果对你有用是我的快乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值