-
Notifications
You must be signed in to change notification settings - Fork 121
/
GetProcessCommandLine.cpp
261 lines (231 loc) · 7.13 KB
/
GetProcessCommandLine.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <windows.h>
#include <Ntsecapi.h>
typedef NTSTATUS(WINAPI* _NtQueryInformationProcess)(
HANDLE ProcessHandle,
DWORD ProcessInformationClass,
PVOID ProcessInformation,
DWORD ProcessInformationLength,
PDWORD ReturnLength
);
struct PROCESS_BASIC_INFORMATION {
PVOID Reserved1;
LPCVOID PebBaseAddress;
PVOID Reserved2[2];
DWORD UniqueProcessId;
PVOID Reserved3;
};
int SpawnTheThing(char *Launch, char *FakeCmdLine, char *RealCmdLineChar)
{
//char to wchar
wchar_t RealCmdLineWchar[100];
swprintf(RealCmdLineWchar, 100, L"%hs", RealCmdLineChar);
_wcslwr_s(RealCmdLineWchar, wcslen(RealCmdLineWchar) + 1);
int err = 0;
// determine if 64 or 32-bit processor
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
printf("[+]System Arch:64-bit\r\n");
else
printf("[+]System Arch:32-bit\r\n");
// determine if this process is running on WOW64
BOOL wow;
IsWow64Process(GetCurrentProcess(), &wow);
if (wow)
{
printf("[!]PE Arch:32-bit\r\n");
printf("[!]You need to use the 64-bit PE\r\n");
return 0;
}
// use WinDbg "dt ntdll!_PEB" command and search for ProcessParameters offset to find the truth out
DWORD ProcessParametersOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x20 : 0x10;
DWORD CommandLineOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x70 : 0x40;
// read basic info to get ProcessParameters address, we only need the beginning of PEB
DWORD pebSize = ProcessParametersOffset + 8;
PBYTE peb = (PBYTE)malloc(pebSize);
ZeroMemory(peb, pebSize);
// read basic info to get CommandLine address, we only need the beginning of ProcessParameters
DWORD ppSize = CommandLineOffset + 16;
PBYTE pp = (PBYTE)malloc(ppSize);
ZeroMemory(pp, ppSize);
PWSTR cmdLine;
LPSTARTUPINFOA pStartupInfo = new STARTUPINFOA();
// hide window
// pStartupInfo->wShowWindow = SW_HIDE;
// pStartupInfo->dwFlags = STARTF_USESHOWWINDOW;
LPPROCESS_INFORMATION pProcessInfo = new PROCESS_INFORMATION();
printf("[*]CreateProcess -> Suspended\r\n");
CreateProcessA
(
Launch,
FakeCmdLine,
0,
0,
0,
CREATE_SUSPENDED,
0,
0,
pStartupInfo,
pProcessInfo
);
if (!pProcessInfo->hProcess)
{
printf("[!]Error creating process\r\n");
return 0;
}
PROCESS_BASIC_INFORMATION pbi;
ZeroMemory(&pbi, sizeof(pbi));
// get process information
_NtQueryInformationProcess query = (_NtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
err = query(pProcessInfo->hProcess, 0, &pbi, sizeof(pbi), NULL);
if (err != 0)
{
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
printf("[!]NtQueryInformationProcess failed\r\n");
return 0;
}
// read PEB
if (!ReadProcessMemory(pProcessInfo->hProcess, pbi.PebBaseAddress, peb, pebSize, NULL))
{
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
printf("[!]ReadProcessMemory PEB failed\r\n");
return 0;
}
// read ProcessParameters
PBYTE* parameters = (PBYTE*)*(LPVOID*)(peb + ProcessParametersOffset); // address in remote process adress space
if (!ReadProcessMemory(pProcessInfo->hProcess, parameters, pp, ppSize, NULL))
{
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
printf("[!]ReadProcessMemory Parameters failed\r\n");
return 0;
}
printf("[*]ReadProcessMemory -> commandline -> ");
// read CommandLine
UNICODE_STRING* pCommandLine = (UNICODE_STRING*)(pp + CommandLineOffset);
cmdLine = (PWSTR)malloc(pCommandLine->MaximumLength);
if (!ReadProcessMemory(pProcessInfo->hProcess, pCommandLine->Buffer, cmdLine, pCommandLine->MaximumLength, NULL))
{
printf("Failed\r\n");
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
free(cmdLine);
printf("[!]ReadProcessMemory Parameters failed\r\n");
return 0;
}
printf("Success\r\n");
printf("[+]Commandline:%ws\n", cmdLine);
return 1;
}
BOOL getProcessCMD(DWORD pid) {
int err = 0;
// determine if 64 or 32-bit processor
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
printf("[+]System Arch:64-bit\r\n");
else
printf("[+]System Arch:32-bit\r\n");
// determine if this process is running on WOW64
BOOL wow;
IsWow64Process(GetCurrentProcess(), &wow);
if (wow)
{
printf("[!]PE Arch:32-bit\r\n");
printf("[!]You need to use the 64-bit PE\r\n");
return 0;
}
// use WinDbg "dt ntdll!_PEB" command and search for ProcessParameters offset to find the truth out
DWORD ProcessParametersOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x20 : 0x10;
DWORD CommandLineOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x70 : 0x40;
// read basic info to get ProcessParameters address, we only need the beginning of PEB
DWORD pebSize = ProcessParametersOffset + 8;
PBYTE peb = (PBYTE)malloc(pebSize);
ZeroMemory(peb, pebSize);
// read basic info to get CommandLine address, we only need the beginning of ProcessParameters
DWORD ppSize = CommandLineOffset + 16;
PBYTE pp = (PBYTE)malloc(ppSize);
ZeroMemory(pp, ppSize);
PWSTR cmdLine;
PROCESS_BASIC_INFORMATION pbi;
ZeroMemory(&pbi, sizeof(pbi));
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (hProc == INVALID_HANDLE_VALUE)
{
printf("[!]OpenProcess error\r\n");
return 0;
}
HANDLE hNewProcess = NULL;
if (!DuplicateHandle(GetCurrentProcess(), hProc, GetCurrentProcess(), &hNewProcess, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
printf("[!]DuplicateHandle error\r\n");
return 0;
}
// get process information
_NtQueryInformationProcess query = (_NtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
err = query(hNewProcess, 0, &pbi, sizeof(pbi), NULL);
if (err != 0)
{
CloseHandle(hNewProcess);
free(peb);
free(pp);
printf("[!]NtQueryInformationProcess failed\r\n");
return 0;
}
// read PEB
if (!ReadProcessMemory(hNewProcess, pbi.PebBaseAddress, peb, pebSize, NULL))
{
CloseHandle(hNewProcess);
free(peb);
free(pp);
printf("[!]ReadProcessMemory PEB failed\r\n");
return 0;
}
// read ProcessParameters
PBYTE* parameters = (PBYTE*)*(LPVOID*)(peb + ProcessParametersOffset); // address in remote process adress space
if (!ReadProcessMemory(hNewProcess, parameters, pp, ppSize, NULL))
{
CloseHandle(hNewProcess);
free(peb);
free(pp);
printf("[!]ReadProcessMemory Parameters failed\r\n");
return 0;
}
printf("[*]ReadProcessMemory -> commandline -> ");
// read CommandLine
UNICODE_STRING* pCommandLine = (UNICODE_STRING*)(pp + CommandLineOffset);
cmdLine = (PWSTR)malloc(pCommandLine->MaximumLength);
if (!ReadProcessMemory(hNewProcess, pCommandLine->Buffer, cmdLine, pCommandLine->MaximumLength, NULL))
{
printf("Failed\r\n");
CloseHandle(hNewProcess);
free(peb);
free(pp);
free(cmdLine);
printf("[!]ReadProcessMemory Parameters failed\r\n");
return 0;
}
printf("Success\r\n");
printf("[+]Commandline:%ws\n", cmdLine);
return 1;
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("\nGet the commandline of the selected process.\n");
printf("Usage:\n");
printf(" %s <pid>\n", argv[0]);
return 0;
}
DWORD pid;
sscanf_s(argv[1], "%d", &pid);
getProcessCMD(pid);
return 1;
}