如何查询正在运行的进程的参数列表?(windows,C++)

beu*_*chs 5 c++ winapi

对于给定的Windows进程,我想知道它启动的命令行参数.Windows任务管理器能够显示例如.

先感谢您!

Ste*_*end 7

假设您知道进程ID,请使用OpenProcess获取它的句柄(这需要提升权限,如文档中所述).然后使用NtQueryInformationProcess获取详细的流程信息.使用该ProcessBasicInformation选项获取进程的PEB - 这包含另一个结构指针,您可以通过该指针获取命令行.


Ank*_*kur 5

远程线程注入:

您使用远程线程注入,调用GetCommandLine(),然后IPC返回结果。在大多数情况下,这可能在Windows XP上有效,但在Windows Vista或更高版本上,它不适用于系统和服务进程。这是因为CreateRemoteThread仅适用于与调用方具有相同会话ID的进程–在Windows Vista中,服务和其他系统进程在会话0中运行,而用户程序在更高的会话中运行。最好和最安全的方法是读取每个Windows进程中存在的结构。

PEB结构:

进程环境块(PEB)通常被存储在处理存储器的高区域,上面0x7ff00000。这些区域还包含线程环境块(TEB)。几乎每个进程的PEB地址都不同,因此您不能简单地使用硬编码常量。

#include <windows.h>
#include <stdio.h>
#include "Winternl.h"

typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)(
    HANDLE ProcessHandle,
    DWORD ProcessInformationClass,
    PVOID ProcessInformation,
    DWORD ProcessInformationLength,
    PDWORD ReturnLength
    );

PVOID GetPebAddress(HANDLE ProcessHandle)
{
    _NtQueryInformationProcess NtQueryInformationProcess =
        (_NtQueryInformationProcess)GetProcAddress(
        GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
    PROCESS_BASIC_INFORMATION pbi;

    NtQueryInformationProcess(ProcessHandle, 0, &pbi, sizeof(pbi), NULL);

    return pbi.PebBaseAddress;
}

int wmain(int argc, WCHAR *argv[])
{
    int pid;
    HANDLE processHandle;
    PVOID pebAddress;
    PVOID rtlUserProcParamsAddress;
    UNICODE_STRING commandLine;
    WCHAR *commandLineContents;

    if (argc < 2)
    {
        printf("Usage: getprocesscommandline [pid]\n");
        return 1;
    }

    pid = _wtoi(argv[1]);

    if ((processHandle = OpenProcess(
        PROCESS_QUERY_INFORMATION | /* required for NtQueryInformationProcess */
        PROCESS_VM_READ, /* required for ReadProcessMemory */
        FALSE, pid)) == 0)
    {
        printf("Could not open process!\n");
        return GetLastError();
    }

    pebAddress = GetPebAddress(processHandle);

    /* get the address of ProcessParameters */
    if (!ReadProcessMemory(processHandle,
            &(((_PEB*) pebAddress)->ProcessParameters),
            &rtlUserProcParamsAddress,
            sizeof(PVOID), NULL))
    {
        printf("Could not read the address of ProcessParameters!\n");
        return GetLastError();
    }

    /* read the CommandLine UNICODE_STRING structure */
    if (!ReadProcessMemory(processHandle,
        &(((_RTL_USER_PROCESS_PARAMETERS*) rtlUserProcParamsAddress)->CommandLine),
        &commandLine, sizeof(commandLine), NULL))
    {
        printf("Could not read CommandLine!\n");
        return GetLastError();
    }

    /* allocate memory to hold the command line */
    commandLineContents = (WCHAR *)malloc(commandLine.Length);

    /* read the command line */
    if (!ReadProcessMemory(processHandle, commandLine.Buffer,
        commandLineContents, commandLine.Length, NULL))
    {
        printf("Could not read the command line string!\n");
        return GetLastError();
    }

    /* print it */
    /* the length specifier is in characters, but commandLine.Length is in bytes */
    /* a WCHAR is 2 bytes */
    printf("%.*S\n", commandLine.Length / 2, commandLineContents);
    CloseHandle(processHandle);
    free(commandLineContents);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请查看“ 获取进程的命令行”

编辑(其他信息):

第一作者说:

CreateRemoteThread仅适用于与调用方具有相同会话ID的进程–在Windows Vista中,服务和其他系统进程在会话0中运行,而用户程序在更高的会话中运行。最好和最安全的方法是读取每个Windows进程中存在的结构。

OpenProcess相同,如果您正在由用户(甚至管理员)运行程序,则无法打开作为服务的进程或由SYSTEMLOCAL SERVICENETWORK SERVICE打开的进程。

如果您的程序是服务,则它可能已经由本地系统帐户运行,因此没有问题。但是,如果没有,一种解决方案是使用psexec启动它:

  1. 下载PSEXEC并解压缩到某些文件夹。
  2. 以管理员身份打开提升的CMD提示。
  3. 导航到解压缩PSEXEC.EXE的文件夹
  4. 跑: PSEXEC -i -s -d CMD
  5. 您将打开一个新的CMD提示。
  6. 在新的CMD提示中键入以下内容,以证明您是谁: WHOAMI

您应该看到自己是SYSTEM,现在可以启动程序并查看所有进程的命令行。