This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinapiutils.c
346 lines (302 loc) · 9.31 KB
/
winapiutils.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "winapiutils.h"
static
PVOID
GetPebAddress(HANDLE pHandle) {
_NtQueryInformationProcess NtQueryInformationProcess =
(_NtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
PROCESS_BASIC_INFORMATION pbi;
NtQueryInformationProcess(pHandle, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
return pbi.PebBaseAddress;
}
static
WCHAR *
GetProcessCommandLine(HANDLE pHandle, DWORD *exitTag, DWORD *lastErrorCode) {
PVOID rtlurp;
UNICODE_STRING cmdln;
PVOID peba = GetPebAddress(pHandle);
// Get the address of ProcessParameters.
if (!ReadProcessMemory(pHandle, (PCHAR)peba + FIELD_OFFSET(PEB, ProcessParameters), &rtlurp, sizeof(rtlurp), NULL)) {
// Could not read the address of ProcessParameters.
*exitTag = 1;
*lastErrorCode = GetLastError();
return NULL;
}
// Read the CommandLine UNICODE_STRING structure.
if (!ReadProcessMemory(pHandle, (PCHAR)rtlurp + FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS, CommandLine), &cmdln, sizeof(cmdln), NULL)) {
// Could not read the address of CommandLine.
*exitTag = 2;
*lastErrorCode = GetLastError();
return NULL;
}
// Allocate memory to hold the command line.
WCHAR *cmdlncnts = malloc(cmdln.Length);
if (NULL == cmdlncnts) {
*exitTag = 3;
return NULL;
}
// Read the command line contents.
if (!ReadProcessMemory(pHandle, cmdln.Buffer, cmdlncnts, cmdln.Length, NULL)) {
// Could not read the command line string.
*exitTag = 4;
*lastErrorCode = GetLastError();
free(cmdlncnts);
cmdlncnts = NULL;
return NULL;
}
WCHAR *result = malloc(cmdln.Length + 2);
if(result == NULL) {
*exitTag = 5;
*lastErrorCode = GetLastError();
free(cmdlncnts);
cmdlncnts = NULL;
return NULL;
}
memcpy(result, cmdlncnts, cmdln.Length);
// ... plus two bytes (size of WCHAR) for a nul-terminator.
*(WCHAR*)((char*)result + cmdln.Length) = 0x0000L;
free(cmdlncnts);
cmdlncnts = NULL;
return result;
}
static
BOOL
IsRemote(DWORD pid, DWORD *exitTag, DWORD *lastErrorCode) {
DWORD sessionId;
if (FALSE == ProcessIdToSessionId(pid, &sessionId)) {
*exitTag = 1;
*lastErrorCode = GetLastError();
return FALSE;
}
OSVERSIONINFOW osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOW));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
GetVersionExW(&osvi);
if (osvi.dwMajorVersion > 5) {
// The case of Vista/Server 2008: 0 and 1 means local, greater than 1 - remote.
return (0 != sessionId) && (1 != sessionId);
} else if ((osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1)) {
// The case of XP/Server 2003: 0 is local, another value means remote.
return (0 != sessionId);
} else {
*exitTag = 2;
return FALSE;
}
}
UserProfile *
GetCurrentProcessUserProfile(DWORD *exitTag) {
return GetProcessUserProfile(GetCurrentProcess(), exitTag);
}
UserProfile *
GetProcessUserProfile(HANDLE hProcess, DWORD *exitTag) {
HANDLE hToken = NULL;
DWORD dwDomainSize = 0;
DWORD dwNameSize = 0;
if (FALSE == OpenProcessToken(hProcess, TOKEN_QUERY, &hToken)) {
*exitTag = 1;
goto exit;
}
TOKEN_ELEVATION elevation;
DWORD teSize = sizeof(TOKEN_ELEVATION);
BOOL res = GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &teSize);
if (0 == res) {
*exitTag = 2;
goto exit;
}
DWORD tuSize = 0;
GetTokenInformation(hToken, TokenUser, NULL, 0, &tuSize);
TOKEN_USER *pTu = malloc(tuSize);
if (NULL == pTu) {
*exitTag = 3;
goto exit;
}
res = GetTokenInformation(hToken, TokenUser, pTu, tuSize, &tuSize);
if (0 == res) {
*exitTag = 4;
goto exit;
}
SID_NAME_USE snu;
LookupAccountSidW(NULL, pTu->User.Sid, NULL, &dwNameSize, NULL, &dwDomainSize, &snu);
WCHAR *name = malloc(dwNameSize * sizeof(WCHAR));
if (NULL == name) {
*exitTag = 5;
goto exit;
}
WCHAR *domain = malloc(dwDomainSize * sizeof(WCHAR));
if (NULL == name) {
*exitTag = 6;
goto exit;
}
res = LookupAccountSidW(NULL, pTu->User.Sid, name, &dwNameSize, domain, &dwDomainSize, &snu);
if (0 == res) {
*exitTag = 7;
goto exit;
}
UserProfile *up = malloc(sizeof(*up));
if (NULL == up) {
*exitTag = 8;
goto exit;
}
WCHAR *sidStr = NULL;
res = ConvertSidToStringSidW(pTu->User.Sid, &sidStr);
if (0 == res) {
*exitTag = 9;
goto exit;
}
up->Name = malloc(sizeof(WCHAR) * (wcslen(name) + 1));
if (NULL == up->Name) {
*exitTag = 10;
goto exit;
}
wcscpy_s(up->Name, wcslen(name) + 1, name);
up->Domain = malloc(sizeof(WCHAR) * (wcslen(domain) + 1));
if (NULL == up->Domain) {
*exitTag = 11;
goto exit;
}
wcscpy_s(up->Domain, wcslen(domain) + 1, domain);
up->SID = malloc(sizeof(WCHAR) * (wcslen(sidStr) + 1));
if (NULL == up->SID) {
*exitTag = 12;
goto exit;
}
wcscpy_s(up->SID, wcslen(sidStr) + 1, sidStr);
up->Elevated = elevation.TokenIsElevated;
exit:
if (NULL != hToken) {
CloseHandle(hToken);
}
if (NULL != pTu) {
free(pTu); pTu = NULL;
}
if (NULL != name) {
free(name); name = NULL;
}
if (NULL != domain) {
free(domain); domain = NULL;
}
if (NULL != sidStr) {
LocalFree(sidStr); sidStr = NULL;
}
if (0 != *exitTag) {
if (NULL != up) {
free(up); up = NULL;
}
}
return up;
}
VOID
FreeUserProfile(UserProfile *up) {
free(up->Name);
free(up->Domain);
free(up->SID);
free(up);
}
WCHAR *
GetCurrentExecutableFullName(DWORD *exitTag, DWORD *lastErrorCode) {
WCHAR *fileName = malloc(MAX_NAME_PATH * sizeof(*fileName));
if (NULL == fileName) {
*exitTag = 1;
return NULL;
}
if (0 == GetModuleFileNameW(NULL, fileName, MAX_PATH)) {
*exitTag = 2;
*lastErrorCode = GetLastError();
free(fileName);
return NULL;
}
return fileName;
}
WCHAR *
GetProcessNameInDeviceForm(HANDLE hProcess, DWORD *exitTag, DWORD *lastErrorCode) {
WCHAR *fileName = malloc(sizeof(*fileName) * MAX_NAME_PATH);
if (NULL == fileName) {
*exitTag = 1;
return NULL;
}
if (0 == GetProcessImageFileNameW(hProcess, fileName, MAX_PATH)) {
*exitTag = 2;
*lastErrorCode = GetLastError();
free(fileName);
return NULL;
}
return fileName;
}
OSProcess *
GetOSProcesses(DWORD *n, DWORD *exitTag, DWORD *lastErrorCode) {
// Create toolhelp snapshot.
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(INVALID_HANDLE_VALUE == snapshot) {
*exitTag = 1;
return NULL;
}
PROCESSENTRY32W process;
ZeroMemory(&process, sizeof(process));
process.dwSize = sizeof(process);
DWORD i = 0;
OSProcess *procs = malloc(sizeof(*procs) * 2048);
if (NULL == procs) {
*exitTag = 2;
return NULL;
}
// Walkthrough a snapshot of all OS processes.
if (Process32FirstW(snapshot, &process)) {
do {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process.th32ProcessID);
if (NULL == hProcess) {
// Ignore the process.
continue;
}
procs[i].PID = process.th32ProcessID;
procs[i].PPID = process.th32ParentProcessID;
procs[i].IsRemote = IsRemote(process.th32ProcessID, exitTag, lastErrorCode);
if (0 != *exitTag) {
CloseHandle(hProcess);
*exitTag = 0;
continue;
}
procs[i].ExecName = GetProcessNameInDeviceForm(hProcess, exitTag, lastErrorCode);
if (0 != *exitTag) {
CloseHandle(hProcess);
*exitTag = 0;
continue;
}
procs[i].CommandLine = GetProcessCommandLine(hProcess, exitTag, lastErrorCode);
if (0 != *exitTag) {
free(procs[i].ExecName);
CloseHandle(hProcess);
*exitTag = 0;
continue;
}
procs[i].UProfile = GetProcessUserProfile(hProcess, exitTag);
if (0 != *exitTag) {
free(procs[i].ExecName);
free(procs[i].CommandLine);
FreeUserProfile(procs[i].UProfile);
CloseHandle(hProcess);
*exitTag = 0;
continue;
}
CloseHandle(hProcess);
// Increment index only if OSProccesEx has been filled correctly.
++i;
} while (Process32NextW(snapshot, &process));
} else {
// Could not retrieve information about the first process.
*exitTag = 3;
free(procs);
procs = NULL;
}
CloseHandle(snapshot);
*n = i;
return procs;
}
VOID
FreeOSProcesses(OSProcess *osprocs, DWORD n) {
DWORD i;
for (i = 0; i < n; i++) {
free(osprocs[i].ExecName);
free(osprocs[i].CommandLine);
FreeUserProfile(osprocs[i].UProfile);
}
free(osprocs);
}