远程线程注入:
你使用远程线程注入,调用GetCommandLine(),然后IPC返回结果。这可能在 Windows XP 上大部分时间都有效,但在 Windows Vista 或更高版本上,它不适用于系统和服务进程。这是因为CreateRemoteThread 仅适用于与调用者具有相同会话 ID 的进程——在 Windows Vista 中,服务和其他系统进程在会话 0 中运行,而用户程序在更高的会话中运行。最好和最安全的方法是读取每个 Windows 进程中存在的结构。
PEB结构:
Process Environment Block (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;
}
更多详情请关注Get the command line of a process
编辑(附加信息):
第一作者说:
CreateRemoteThread 仅适用于与会话 ID 相同的进程
调用者——在 Windows Vista 中,服务和其他系统进程运行
在会话 0 中,而用户程序在更高的会话中运行。最好和最安全的方法是读取每个 Windows 进程中存在的结构。
OpenProcess 也一样,你不能打开一个服务进程,或者 SYSTEM 或 LOCAL SERVICE 或 打开的进程>NETWORK SERVICE,如果您是由用户(甚至是管理员)运行程序。
如果您的程序是服务,它可能已经由本地系统帐户运行,所以没问题。
但如果没有,一个解决方案是使用psexec 启动它:
- 下载PSEXEC并解压到某个文件夹。
- 以管理员身份打开提升的 CMD 提示符。
- 导航到解压 PSEXEC.EXE 的文件夹
- 运行:
PSEXEC -i -s -d CMD
- 您将打开一个新的 CMD 提示符。
- 在新的 CMD 提示符中键入以下内容以证明您的身份:
WHOAMI
你应该看到你是SYSTEM,现在你可以启动你的程序并查看所有进程的命令行。