1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "PEB.h" // http://www.binary-reverser.org/spip/spip.php?article72
typedef enum _PROCESSINFOCLASS {
ProcessBasicInformation = 0
} PROCESSINFOCLASS;
typedef struct _PROCESS_BASIC_INFORMATION
{
DWORD ExitStatus;
PPEB PebBaseAddress;
DWORD AffinityMask;
DWORD BasePriority;
ULONG UniqueProcessId;
ULONG InheritedFromUniqueProcessId;
} PROCESS_BASIC_INFORMATION;
typedef int (NTAPI *lpfNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
BOOL GetProcessCommandLine(DWORD pid, PWSTR CommandLineBuff, SIZE_T BuffSize);
void DoCleanUp(HANDLE hProc, PPEB pPEB, PRTL_USER_PROCESS_PARAMETERS pRUPP);
int main(int argc, char*argv[])
{
DWORD pid;
WCHAR CommandLineBuff[255] = {0};
if(argc < 2)
{
printf("Usage : GetProcessCommandLine.exe ProcessPID");
return 0;
}
pid = strtoul(argv[1], NULL, 10);
if(!GetProcessCommandLine(pid, &CommandLineBuff, sizeof(CommandLineBuff)))
{
printf("Impossible de récupèrer la ligne de commande !\n");
}
else
{
wprintf(L"Ligne de commande du processus numéro %lu :\n%s", pid, CommandLineBuff);
}
return 0;
}
BOOL GetProcessCommandLine(DWORD pid, PWSTR CommandLineBuff, SIZE_T BuffSize)
{
HANDLE hProc = NULL;
HMODULE hNtdll = NULL;
lpfNtQueryInformationProcess pNQIP= NULL;
int Status;
PROCESS_BASIC_INFORMATION pbi = {0};
PPEB pPEB = NULL;
PRTL_USER_PROCESS_PARAMETERS pRUPP = NULL;
SIZE_T NOBR;
//ouvre le processus
hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if(hProc == NULL)
{
printf("Impossible d'ouvrir le processus\n");
return FALSE;
}
// récup le hmod de NTDLL
hNtdll = LoadLibrary("ntdll.dll");
if(hNtdll == NULL)
{
printf("Impossible de charger NTDLL\n");
DoCleanUp(hProc, NULL, NULL);
return FALSE;
}
// récup. pointeur sur NtQueryInformationProcess
pNQIP = (lpfNtQueryInformationProcess) GetProcAddress(hNtdll, "NtQueryInformationProcess");
if(pNQIP == NULL)
{
printf("Impossible d'obtenir l'adresse de NtQueryInformationProcess\n");
DoCleanUp(hProc, NULL, NULL);
return FALSE;
}
// obtient le PEB (Process Env. Block).
Status = pNQIP(hProc, ProcessBasicInformation, (PVOID)&pbi, sizeof(PROCESS_BASIC_INFORMATION), NULL);
if(Status)
{
printf("Problème avec NtQueryInformationProcess.\n");
DoCleanUp(hProc, NULL, NULL);
return FALSE;
}
//décrémente le compteur sur NTDLL
FreeLibrary(hNtdll);
// alloue pour le PEB.
pPEB = (PPEB) VirtualAlloc(NULL, sizeof(PEB), MEM_COMMIT, PAGE_READWRITE);
// alloue pour les param du processus.
pRUPP = (PRTL_USER_PROCESS_PARAMETERS) VirtualAlloc(NULL, sizeof(RTL_USER_PROCESS_PARAMETERS), MEM_COMMIT, PAGE_READWRITE);
// Lit le PEB. du processus.
if(!ReadProcessMemory(hProc, pbi.PebBaseAddress, pPEB, sizeof(PEB),&NOBR))
{
printf("Impossible de lire le PEB.\n");
DoCleanUp(hProc, pPEB, pRUPP);
return FALSE;
}
// lit les param. du processus.
if(!ReadProcessMemory(hProc, pPEB->ProcessParameters, pRUPP, sizeof(RTL_USER_PROCESS_PARAMETERS),&NOBR))
{
printf("Impossible de lire les paramètres du processus.\n");
DoCleanUp(hProc, pPEB, pRUPP);
return FALSE;
}
// nombre d'octets nécessaires au buffer de la command line
if (pRUPP->CommandLine.Length > BuffSize)
{
printf("Le tampon fourni est trop petit !\n");
DoCleanUp(hProc, pPEB, pRUPP);
return FALSE;
}
// lit la ligne de commande du processus
if(!ReadProcessMemory(hProc, pRUPP->CommandLine.Buffer, CommandLineBuff, BuffSize, &NOBR))
{
printf("Impossible de lire le tampon de la ligne de commande du processus.\n");
DoCleanUp(hProc, pPEB, pRUPP);
return FALSE;
}
return TRUE;
}
void DoCleanUp(HANDLE hProc, PPEB pPEB, PRTL_USER_PROCESS_PARAMETERS pRUPP)
{
if(hProc != NULL)
CloseHandle(hProc);
if(pPEB != NULL)
VirtualFree(pPEB, 0, MEM_RELEASE);
if(pRUPP != NULL)
VirtualFree(pRUPP, 0, MEM_RELEASE);
} |
Partager