通过C++语言在应用层获取任意进程命令行参数

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <Winternl.h>
 
typedef NTSTATUS (NTAPI *_NtQueryInformationProcess)(
    HANDLE ProcessHandle,
    DWORD ProcessInformationClass,
    PVOID ProcessInformation,
    DWORD ProcessInformationLength,
    PDWORD ReturnLength
    );
 
/*
写成一个函数,来获得所有的PEB结构体信息
*/
TCHAR* GetProcessCommandLine(HANDLE hProcess)
{
	UNICODE_STRING commandLine;
	TCHAR *commandLineContents = NULL;
	_NtQueryInformationProcess NtQuery = (_NtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
	if (NtQuery)
	{
		PROCESS_BASIC_INFORMATION pbi;
		NTSTATUS isok = NtQuery(hProcess, ProcessBasicInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION), NULL);        
		if (NT_SUCCESS(isok))
		{
			PEB peb;
			RTL_USER_PROCESS_PARAMETERS upps;
			PVOID rtlUserProcParamsAddress;
			if (ReadProcessMemory(hProcess, &(((_PEB*) pbi.PebBaseAddress)->ProcessParameters), &rtlUserProcParamsAddress, sizeof(PVOID), NULL))
			{
				if (ReadProcessMemory(hProcess,
								&(((_RTL_USER_PROCESS_PARAMETERS*) rtlUserProcParamsAddress)->CommandLine),
								&commandLine, sizeof(commandLine), NULL))
				{
					commandLineContents = (TCHAR *)malloc(commandLine.Length + sizeof(TCHAR));
					memset(commandLineContents, 0, commandLine.Length + sizeof(TCHAR));
					ReadProcessMemory(hProcess, commandLine.Buffer,
						commandLineContents, commandLine.Length, NULL);
				}
			}
		}
	}
 
	return commandLineContents;
}
 
int _tmain(int argc,TCHAR* argv[])
{
	HANDLE hProcess = OpenProcess(
        PROCESS_QUERY_INFORMATION | /* required for NtQueryInformationProcess */
        PROCESS_VM_READ, /* required for ReadProcessMemory */
        FALSE, 3088/*ProcessID*/);
	if (hProcess == NULL)
		return -1;
	
	TCHAR* pszProcessCmd = GetProcessCommandLine(hProcess);
	if (pszProcessCmd != NULL)
		wprintf(L"%s", pszProcessCmd);
 
	CloseHandle(hProcess);
	return 0;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Linux中的管道(pipe)来获取命令行输出的内容。在C++中,可以通过fork()函数创建一个子进程,并在子进程中执行要获取输出的命令。然后,将子进程的标准输出重定向到管道的写端,父进程从管道的读端读取命令行输出。 下面是一个简单的示例代码: ```cpp #include <iostream> #include <unistd.h> #include <sys/wait.h> int main() { int pipefd[2]; pipe(pipefd); pid_t pid = fork(); if (pid == 0) { // 子进程中执行命令并将输出写入管道 close(pipefd[0]); // 关闭管道的读端 dup2(pipefd[1], STDOUT_FILENO); // 将标准输出重定向到管道的写端 // 执行命令 execlp("your_command", "your_command", nullptr); // 替换为你要执行的命令 // 执行失败则退出 std::cerr << "Failed to execute command" << std::endl; exit(1); } else { // 父进程从管道中读取命令行输出 close(pipefd[1]); // 关闭管道的写端 char buffer[4096]; ssize_t bytesRead; while ((bytesRead = read(pipefd[0], buffer, sizeof(buffer))) > 0) { // 处理读取到的数据,例如打印输出 std::cout.write(buffer, bytesRead); } // 等待子进程结束 int status; waitpid(pid, &status, 0); } return 0; } ``` 在上面的代码中,需要将"your_command"替换为你要执行的命令。执行该程序时,它将创建一个子进程来执行命令,并将命令行输出通过管道传递给父进程,父进程则读取并处理输出。你可以根据需要对读取到的数据进行进一步处理或存储。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值