-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEngine.cpp
More file actions
431 lines (336 loc) · 10.6 KB
/
Engine.cpp
File metadata and controls
431 lines (336 loc) · 10.6 KB
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
/*
A Microsoft Windows memory page delta tool
Released as open source by NCC Group Plc - http://www.nccgroup.com/
Developed by Ollie Whitehouse, ollie dot whitehouse at nccgroup dot com
https://github.com/nccgroup/WindowsMemPageDelta
Released under AGPL see LICENSE for more information
*/
// Includes
#include "stdafx.h"
bool bFirstRun = true;
bool bVerbose = false;
TCHAR strErrMsg[1024];
DWORD dwModuleRelocs = 0;
HANDLE event_log = NULL;
//
#pragma pack(push, 1)
struct procNfoStuct {
DWORD PID;
TCHAR Name[MAX_PATH];
unsigned long long TotalExecMem = 0;
};
#pragma pack(pop)
//
#pragma pack(push, 1)
struct procStuct
{
DWORD PID;
unsigned long long address;
unsigned long allocprotection;
unsigned long long size;
unsigned long protection;
unsigned long state;
unsigned long type;
};
#pragma pack(pop)
procStuct megaStruc[1000000];
procStuct megaStruc2[1000000];
unsigned long long lastEntry = 0;
unsigned long long lastEntry2 = 0;
procNfoStuct Procs[4098];
DWORD NumOfProcs = 0;
// Manual imports
_NtQueryInformationProcess __NtQueryInformationProcess = (_NtQueryInformationProcess)GetProcAddress(GetModuleHandle(_T("ntdll.dll")), "NtQueryInformationProcess");
typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process = fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
//
BOOL putinMegaStruc(DWORD dwPID, TCHAR* cProcess, ULONG64 BaseAddress, DWORD AllocationProtect, SIZE_T RegionSize, DWORD Protect, DWORD State, DWORD Type) {
megaStruc[lastEntry].PID = dwPID;
megaStruc[lastEntry].address = BaseAddress;
megaStruc[lastEntry].allocprotection = AllocationProtect;
megaStruc[lastEntry].size = RegionSize;
megaStruc[lastEntry].protection = Protect;
megaStruc[lastEntry].state = State;
megaStruc[lastEntry].type = Type;
lastEntry++;
return TRUE;
}
//
BOOL putinMegaStrucAlt(DWORD dwPID, TCHAR* cProcess, ULONG64 BaseAddress, DWORD AllocationProtect, SIZE_T RegionSize, DWORD Protect, DWORD State, DWORD Type) {
megaStruc2[lastEntry2].PID = dwPID;
megaStruc2[lastEntry2].address = BaseAddress;
megaStruc2[lastEntry2].allocprotection = AllocationProtect;
megaStruc2[lastEntry2].size = RegionSize;
megaStruc2[lastEntry2].protection = Protect;
megaStruc2[lastEntry2].state = State;
megaStruc2[lastEntry2].type = Type;
lastEntry2++;
return TRUE;
}
CONST TCHAR* Protection(DWORD Protection)
{
switch (Protection) {
case PAGE_EXECUTE:
return L"X.....";
case PAGE_EXECUTE_READ:
return L"XR....";
case PAGE_EXECUTE_READWRITE:
return L"XRW....";
case PAGE_NOACCESS:
return L".......";
case PAGE_READONLY:
return L".R.....";
case PAGE_READWRITE:
return L".RW....";
case PAGE_WRITECOPY:
return L"..WC...";
case PAGE_GUARD:
return L"....G..";
case PAGE_NOCACHE:
return L".....c.";
case PAGE_WRITECOMBINE:
return L".....Cw";
default:
return L"UKNOWN.";
}
}
//
// This calculates the total executable memory for a process
// and then logs it
//
void calcTotals() {
DWORD dwCount = 0;
TCHAR logLine[4098];
while (Procs[dwCount].PID != 0) {
for (DWORD dwCountInner = 0; dwCountInner < lastEntry2; dwCountInner++) {
if (megaStruc2[dwCountInner].PID == Procs[dwCount].PID) {
Procs[dwCount].TotalExecMem += megaStruc2[dwCountInner].size;
}
else if(megaStruc2[dwCountInner].PID == 0)
{
continue;
}
}
_stprintf_s(logLine, TEXT("Total,%u,%s,%llu\n"), Procs[dwCount].PID, Procs[dwCount].Name, Procs[dwCount].TotalExecMem);
if (bConsole == true) {
_ftprintf(stdout, logLine);
}
WriteTotal(logLine);
dwCount++;
}
}
//
// This diffs the new exec pages
// and then logs it
//
BOOL diffMegaStrucAlt() {
bool bBoing = false;
TCHAR logLine[4098];
for (int count = 0; count < lastEntry2; count++) {
bBoing = false;
unsigned int dwPIDMatch = 0;
for (dwPIDMatch = 0; dwPIDMatch < 4098; dwPIDMatch++) {
if (Procs[dwPIDMatch].PID == megaStruc2[count].PID) { // this code assumes likelihood of a process being created and assuming a previous PID within 30 seconds is low
break;
}
}
// noise reduction
if (dwPIDMatch == 0 || dwPIDMatch == 4098) { // New process not seen before so don't report
continue;
}
// optimization
if (megaStruc2[count].protection != PAGE_EXECUTE && megaStruc2[count].protection != PAGE_EXECUTE_READ && megaStruc2[count].protection != PAGE_EXECUTE_READWRITE) {
continue;
}
// then fall back methods
if (count < lastEntry2 && count < lastEntry && megaStruc2[count].PID == megaStruc[count].PID && megaStruc2[count].address == megaStruc[count].address && megaStruc2[count].allocprotection == megaStruc[count].allocprotection && megaStruc2[count].type == megaStruc[count].type && megaStruc2[count].state == megaStruc[count].state && megaStruc2[count].protection == megaStruc[count].protection)
{
continue;
}
else // then slow way
{
for (int count2 = 0; count2 < lastEntry; count2++) {
if (megaStruc2[count].PID == megaStruc[count2].PID && megaStruc2[count].address == megaStruc[count2].address && megaStruc2[count].allocprotection == megaStruc[count2].allocprotection && megaStruc2[count].type == megaStruc[count2].type && megaStruc2[count].state == megaStruc[count2].state && megaStruc2[count].protection == megaStruc[count2].protection) {
bBoing = true;
break; // present
}
else if (megaStruc2[count].PID == megaStruc[count2].PID && megaStruc2[count].address == megaStruc[count2].address && megaStruc2[count].size == megaStruc[count2].size)
{
bBoing = true;
_stprintf_s(logLine, TEXT("Changed,%u,%s,%I64x,%llu,%s,%s\n"), megaStruc2[count].PID, Procs[dwPIDMatch].Name, megaStruc2[count].address, megaStruc2[count].size, Protection(megaStruc2[count].protection), Protection(megaStruc[count2].protection));
if (bConsole == true) {
_ftprintf(stdout, logLine);
}
WriteEvent(logLine);
break;
}
}
if (bBoing == false) {
_stprintf_s(logLine, TEXT("New,%u,%s,%I64x,%llu,%s\n"), megaStruc2[count].PID, Procs[dwPIDMatch].Name, megaStruc2[count].address, megaStruc2[count].size, Protection(megaStruc2[count].protection));
if (bConsole == true) {
_ftprintf(stdout, logLine);
}
WriteEvent(logLine);
}
}
}
// Copy across
memcpy(megaStruc, megaStruc2, sizeof(megaStruc));
lastEntry = lastEntry2;
// Clean up
memset(megaStruc2, 0x00, sizeof(megaStruc2));
lastEntry2 = 0;
return TRUE;
}
//
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684139(v=vs.85).aspx
//
BOOL IsWow64()
{
BOOL bIsWow64 = FALSE;
if (NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
{
return false;
}
}
return bIsWow64;
}
/// <summary>
/// Analyze the process and its memory regions
/// </summary>
/// <param name="dwPID">Process ID</param>
void AnalyzeProc(DWORD dwPID)
{
DWORD dwRet, dwMods;
HANDLE hProcess;
HMODULE hModule[4096];
TCHAR cProcess[MAX_PATH]; // Process name
SYSTEM_INFO sysnfoSysNFO;
BOOL bIsWow64 = FALSE;
BOOL bIsWow64Other = FALSE;
DWORD dwRES = 0;
hProcess = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPID);
if (hProcess == NULL)
{
if (GetLastError() == 5) {
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPID);
if (hProcess == NULL) {
//fprintf(stderr, "Failed to OpenProcess(%d),%d\n", dwPID, GetLastError());
return;
}
}
else {
//fprintf(stderr, "Failed to OpenProcess(%d),%d\n", dwPID, GetLastError());
return;
}
}
if (EnumProcessModules(hProcess, hModule, 4096 * sizeof(HMODULE), &dwRet) == 0)
{
if (GetLastError() == 299) {
//fprintf(stderr, "64bit process and we're 32bit - sad panda! skipping PID %d\n", dwPID);
}
else {
//fprintf(stderr, "Error in EnumProcessModules(%d),%d\n", dwPID, GetLastError());
}
return;
}
dwMods = dwRet / sizeof(HMODULE);
GetModuleBaseName(hProcess, hModule[0], cProcess, MAX_PATH);
Procs[NumOfProcs].PID = dwPID;
_tcscpy_s(Procs[NumOfProcs].Name, MAX_PATH, cProcess);
NumOfProcs++;
//PrintProcessInfo(cProcess, dwPID);
if (IsWow64Process(GetCurrentProcess(), &bIsWow64)) {
GetNativeSystemInfo(&sysnfoSysNFO);
if (bIsWow64)
{
//fwprintf(stdout,L"[i] Running under WOW64 - Page Size %d\n",sysnfoSysNFO.dwPageSize);
}
else
{
//fwprintf(stdout,L"[i] Not running under WOW64 - Page Size %d\n",sysnfoSysNFO.dwPageSize);
}
}
else {
//fwprintf(stderr, L"Error in IsWow64Process(%d),%d\n", dwPID, GetLastError());
return;
}
//
// Walk the processes address space
//
unsigned char* pString = NULL;
ULONG_PTR addrCurrent = 0;
ULONG_PTR lastBase = (-1);
unsigned int iInserted = 0;
for (;;)
{
#ifdef WIN64
MEMORY_BASIC_INFORMATION64 memMeminfo;
#endif
#ifdef WIN32
MEMORY_BASIC_INFORMATION memMeminfo;
#endif
VirtualQueryEx(hProcess, reinterpret_cast<LPVOID>(addrCurrent), reinterpret_cast<PMEMORY_BASIC_INFORMATION>(&memMeminfo), sizeof(memMeminfo));
if (lastBase == (ULONG_PTR)memMeminfo.BaseAddress) {
break;
}
lastBase = (ULONG_PTR)memMeminfo.BaseAddress;
//_ftprintf(stdout, TEXT("%u,%s,%p,%lld,%u,%u\n"), dwPID, cProcess, memMeminfo.BaseAddress, memMeminfo.RegionSize, memMeminfo.Protect, memMeminfo.State);
if (bFirstRun == true && memMeminfo.State == MEM_COMMIT) {
putinMegaStruc(dwPID, cProcess, (ULONG64)memMeminfo.BaseAddress, memMeminfo.AllocationProtect, memMeminfo.RegionSize, memMeminfo.Protect, memMeminfo.State, memMeminfo.Type);
}
else if (memMeminfo.State == MEM_COMMIT)
{
putinMegaStrucAlt(dwPID, cProcess, (ULONG64)memMeminfo.BaseAddress, memMeminfo.AllocationProtect, memMeminfo.RegionSize, memMeminfo.Protect, memMeminfo.State, memMeminfo.Type);
}
else
{
// Skip
}
addrCurrent += memMeminfo.RegionSize;
}
CloseHandle(hProcess);
}
/// <summary>
/// Enumerate all the processes on the system and
/// pass off to the analysis function
/// </summary>
void EnumerateProcesses()
{
DWORD dwPIDArray[4096], dwRet, dwPIDS, intCount;
NumOfProcs = 0;
// Be clean to ensure no stale data
memset(Procs, 0x00, sizeof(Procs));
//
// Enumerate
//
if (EnumProcesses(dwPIDArray, 4096 * sizeof(DWORD), &dwRet) == 0)
{
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, strErrMsg, 1023, NULL);
if (dwRet != 0) {
_ftprintf(stderr, TEXT("[!] EnumProcesses() failed - %s"), strErrMsg);
}
else
{
_ftprintf(stderr, TEXT("[!] EnumProcesses() - Error: %d\n"), GetLastError());
}
return;
}
dwPIDS = dwRet / sizeof(DWORD);
//
// Analyze
//
for (intCount = 0; intCount < dwPIDS; intCount++)
{
AnalyzeProc(dwPIDArray[intCount]);
}
// Do the diff calc the totals
// and log
if (bFirstRun == false)
{
calcTotals();
diffMegaStrucAlt();
}
}