-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattacker.cpp
More file actions
356 lines (248 loc) · 8.68 KB
/
attacker.cpp
File metadata and controls
356 lines (248 loc) · 8.68 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
#include <windows.h>
// #include <memoryapi.h>
#include <psapi.h>
// #include <processthreadsapi.h>
// #include <handleapi.h>
// #include <errhandlingapi.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef struct _infoNode {
MEMORY_BASIC_INFORMATION* info;
struct _infoNode* next;
} InfoNode;
typedef struct _addressNode {
LPCVOID address;
struct _addressNode* next;
} AddressNode;
char* formatBytes(unsigned long long bytes)
{
char* result = (char*) malloc(sizeof(char) * 16);
if (result == NULL) return NULL;
if (bytes == 0) {
sprintf(result, "0 Bytes");
} else {
char sizes[][6] = { "Bytes", "kB", "MB", "GB", "TB", "PB" };
int i = (int) (log(bytes) / log(1024.0));
sprintf(result, "%.2f %s", bytes / pow(1024, i), sizes[i]);
}
return result;
}
void printProcessName(DWORD processId)
{
int i;
const int processPathSize = 300;
CHAR processPath[processPathSize];
HANDLE processHandle;
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId);
if (GetModuleFileNameExA(processHandle, NULL, processPath, processPathSize) == 0) {
return; // Skip unsuccessfull reads
}
CloseHandle(processHandle);
// Finding the first occurence of `\`
for (i = strlen(processPath) - 1; processPath[i] != '\\' && i >= 0; i--);
printf("------- @%ld\t%s\n", processId, processPath + i + 1);
}
int listAllProcesses()
{
DWORD processList[1024], amountReturned; //!! 1 DWORD = 4 bytes !! 1024 DWORD x 4 bytes = 4.096 kB !!
if (EnumProcesses(processList, sizeof(processList), &amountReturned) == false) {
return false;
}
int found = amountReturned / sizeof(DWORD);
printf("End of scan, %d processes found:\n", found);
for (DWORD i = 0; i < (DWORD) found; i++) {
DWORD processId = processList[i];
printProcessName(processId);
}
return true;
}
unsigned long printProcessModule(MEMORY_BASIC_INFORMATION info)
{
unsigned long memUsage = 0;
char* size = formatBytes(info.RegionSize / 1024.0);
printf("0x%p (%s) \t", info.BaseAddress, size);
free(size);
switch (info.State)
{
case MEM_COMMIT:
printf("Committed"); break;
case MEM_RESERVE:
printf("Reserved "); break;
case MEM_FREE:
printf("Free "); break;
}
printf("\t");
switch (info.Type)
{
case MEM_IMAGE:
printf("Image "); break;
case MEM_MAPPED:
printf("Section "); break;
case MEM_PRIVATE:
printf("Private "); break;
default:
printf("0x%lx", info.Type); break;
}
if (info.State == MEM_COMMIT && (info.AllocationProtect == PAGE_READWRITE || info.AllocationProtect == PAGE_READONLY))
memUsage += info.RegionSize;
printf("\n");
return memUsage;
}
unsigned long printProcessMemory(DWORD targetPid, unsigned long* totalMemUsage)
{
HANDLE processHandle;
MEMORY_BASIC_INFORMATION resultContainer = {};
unsigned char* startingPoint;
unsigned long modulesCount = 0;
*totalMemUsage = 0;
// Opening the process to get the starting memory address
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, false, targetPid);
for (startingPoint = NULL;
VirtualQueryEx(processHandle, startingPoint, &resultContainer, sizeof(resultContainer)) != 0;
startingPoint += resultContainer.RegionSize)
{
*totalMemUsage += printProcessModule(resultContainer);
modulesCount++;
}
CloseHandle(processHandle);
return modulesCount;
}
void printSelectedModules(InfoNode* info)
{
while (info != NULL) {
printProcessModule(*(info->info));
info = info->next;
}
}
InfoNode* createProcessInfoNode(MEMORY_BASIC_INFORMATION info)
{
InfoNode* node = (InfoNode*) malloc(sizeof(InfoNode));
if (node == NULL) return NULL;
node->info = (MEMORY_BASIC_INFORMATION*) malloc(sizeof(MEMORY_BASIC_INFORMATION));
if (node->info == NULL) return NULL;
// Copying the MEM._BASIC_INFO block
node->info->BaseAddress = info.BaseAddress;
node->info->AllocationBase = info.AllocationBase;
node->info->AllocationProtect = info.AllocationProtect;
node->info->PartitionId = info.PartitionId;
node->info->RegionSize = info.RegionSize;
node->info->State = info.State;
node->info->Protect = info.Protect;
node->info->Type = info.Type;
node->next = NULL;
return node;
}
AddressNode* createAddressNode(LPCVOID address)
{
AddressNode* node = (AddressNode*) malloc(sizeof(AddressNode));
if (node != NULL) {
node->next = NULL;
}
return node;
}
void freeInfoNode(InfoNode* node)
{
free(node->info);
free(node);
}
long long readLLUserInput(const char text[])
{
printf("%s\n>>> ", text);
// Reading input and parsing it to a `long long`
char input[16];
fgets(input, 16, stdin);
long long parsed = atoll(input);
// While the user input is not numeric
while (parsed == 0) {
printf("Wrong input.\n>>> ");
fgets(input, 16, stdin);
parsed = atoi(input);
}
return parsed;
}
void printAddresses(AddressNode* addr)
{
printf("Possible addresses:\n");
while (addr != NULL) {
printf("\t- 0x%p\n", addr->address);
addr = addr->next;
}
}
AddressNode* scanMemoryForValue(DWORD targetPid, InfoNode* modules, int value, int readableModulesCount)
{
AddressNode* addresses = NULL;
AddressNode* newAddress;
HANDLE processHandle = OpenProcess(PROCESS_VM_READ, false, targetPid);
int buffer[12];
size_t NumberOfBytesRead;
int count = 0;
// Iterating over all readable modules
while (modules != NULL) {
for (LPVOID address = modules->info->BaseAddress; ReadProcessMemory(processHandle, (LPVOID) address[i], (LPVOID) buffer, sizeof(value), &NumberOfBytesRead) != 0; address = (LPVOID) (address + sizeof(int))) {
// address < ((uint8_t*) modules->info->BaseAddress + modules->info->RegionSize);
// Found the value somewhere!
if (value == *buffer) {
// Append to linked list
newAddress = createAddressNode(address);
newAddress->next = addresses;
addresses = newAddress;
}
}
printf("Scanned module %d/%d\r", count, readableModulesCount);
count++;
modules = modules->next;
}
printf("\n");
CloseHandle(processHandle);
return addresses;
}
InfoNode* getReadableModules(DWORD targetPid, unsigned long* modulesCount, unsigned long* totalMemUsage, unsigned long* readableModulesCount)
{
HANDLE processHandle;
MEMORY_BASIC_INFORMATION resultContainer = {};
unsigned char* startingPoint;
InfoNode* newNode, * node = NULL;
*readableModulesCount = 0;
*modulesCount = 0;
*totalMemUsage = 0;
// Opening the process to get the starting memory address
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, false, targetPid);
int code = 1;
for (startingPoint = NULL; code != 0; startingPoint += resultContainer.RegionSize) {
code = VirtualQueryEx(processHandle, startingPoint, &resultContainer, sizeof(MEMORY_BASIC_INFORMATION));
*totalMemUsage += printProcessModule(resultContainer);
*modulesCount += 1;
if (resultContainer.State == MEM_COMMIT) {
newNode = createProcessInfoNode(resultContainer);
newNode->next = node;
node = newNode;
*readableModulesCount += 1;
}
}
CloseHandle(processHandle);
return node;
}
int main()
{
if (listAllProcesses() == false) {
return 1;
}
// Getting the target PID
long long targetPid = readLLUserInput("Please enter the PID you want to analyze the memory from.");
unsigned long modulesCount, readableModulesCount, totalMemUsage;
InfoNode* readableModules = getReadableModules(targetPid, &modulesCount, &totalMemUsage, &readableModulesCount);
char* humanReadableMemoryUsage = formatBytes(totalMemUsage);
printf("-------------------------------------------------------------");
printf("\nTOTAL MEMORY USAGE FOR PID %llu: %s in %lu modules (%lu readable).\n\n", targetPid, humanReadableMemoryUsage, modulesCount, readableModulesCount);
free(humanReadableMemoryUsage);
printf("Selected memory blocks:\n");
printSelectedModules(readableModules);
long long value = readLLUserInput("Please enter the value to be located in the process memory.");
AddressNode* addresses = scanMemoryForValue(targetPid, readableModules, value, readableModulesCount);
printAddresses(addresses);
return 0;
}