因此,我的目标是获取所有当前正在运行的进程的完整命令行。为此,我要做的是使用CreateToolhelp32Snapshot
API拍摄流程快照,然后执行流程遍历以将PROCESSENTRY32
类型流程存储在称为数组的数组中process_list
:
BOOL GetProcessList( FILE *f, PROCESSENTRY32* process_list, int process_count)
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
return( FALSE );
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) )
{
CloseHandle( hProcessSnap ); // clean the snapshot object
return( FALSE );
}
// Now walk the snapshot of processes, and
// display information about each process in turn
int i = 0;
do
{
// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, pe32.th32ProcessID );
if( hProcess == NULL ) { }
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
CloseHandle( hProcess );
}
process_list[i] = pe32;
i++;
} while( Process32Next( hProcessSnap, &pe32 ) && i <= process_count);
CloseHandle( hProcessSnap );
return( TRUE );
}
现在,是否可以遍历一系列进程并收集每个进程的完整命令行?我怎样才能做到这一点?
如果重要的话,我会将代码编译为64位进程,并且需要主机上运行的所有进程的完整命令行。
对于每个进程ID:
用于OpenProcess()
获取HANDLE
流程。
然后使用QueryFullProcessImageName()
,GetProcessImageFileName()
或GetModuleFileNameEx
获取进程的路径和文件名。
然后使用NtQueryInformationProcess()
来检索流程PEB
结构的地址,该地址包含一个ProcessParameters
成员,该成员包含该流程的命令行参数(您也可以从那里获取图像路径PEB
)。用ReadProcessMemory()
阅读的内容PEB
。
请查看以下文章以了解更多详细信息:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句