-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManager.cs
More file actions
541 lines (442 loc) · 18.5 KB
/
MemoryManager.cs
File metadata and controls
541 lines (442 loc) · 18.5 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
namespace ProCheatEngineCLI
{
public class MemoryManager
{
[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead);
[DllImport("kernel32.dll")]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, out int lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
private static extern bool VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
[StructLayout(LayoutKind.Sequential)]
struct MEMORY_BASIC_INFORMATION
{
public IntPtr BaseAddress;
public IntPtr AllocationBase;
public uint AllocationProtect;
public IntPtr RegionSize;
public uint State;
public uint Protect;
public uint Type;
}
Dictionary<IntPtr, int> frozenValues = new();
bool freezeRunning = false;
const int PROCESS_ALL_ACCESS = 0x1F0FFF;
const uint MEM_COMMIT = 0x1000;
private IntPtr hProcess;
private List<IntPtr> foundAddresses = new();
public void CheatProcess(ProcessInfo processInfo)
{
hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, processInfo.Id);
if (hProcess == IntPtr.Zero)
{
Console.WriteLine("Failed to attach to process!");
return;
}
Console.Clear();
Console.WriteLine("=== VoidMemory ===");
Console.WriteLine($"Attached to: {processInfo.ProcessName} (PID: {processInfo.Id})");
Console.WriteLine();
while (true)
{
ShowMenu();
string? cmd = Console.ReadLine()?.Trim().ToLower();
switch (cmd)
{
case "1": FirstScan(); break;
case "2": NextScan(); break;
case "3": ListAddresses(); break;
case "4": PatchValue(); break;
case "5": foundAddresses.Clear(); Console.WriteLine("Results cleared"); break;
case "6": FreezeValue(); break;
case "7": UnfreezeAll(); break;
case "8": AutoHealthScan(); break;
case "q":
case "quit": goto end;
default: Console.WriteLine("Invalid option! (1-5 or q)"); break;
}
Console.WriteLine();
}
end:
CloseHandle(hProcess);
}
void ShowMenu()
{
Console.WriteLine("=== Pro Cheat Engine CLI ===");
Console.WriteLine();
Console.WriteLine("1. First Scan (search for exact value)");
Console.WriteLine("2. Next Scan (narrow results)");
Console.WriteLine("3. List Results (show addresses)");
Console.WriteLine("4. Patch Value (change memory)");
Console.WriteLine("5. Clear Results");
Console.WriteLine();
Console.WriteLine("=== Advanced ===");
Console.WriteLine("6. Freeze Value (lock value)");
Console.WriteLine("7. Unfreeze All");
Console.WriteLine();
Console.WriteLine("=== Auto Features ===");
Console.WriteLine("8. Auto Scan (Health)");
Console.WriteLine();
Console.WriteLine($"q. Quit (Found: {foundAddresses.Count})");
Console.Write("Choose: ");
}
void AutoHealthScan()
{
Console.WriteLine("=== AUTO HEALTH SCAN ===");
Console.WriteLine("Step 1: Make sure your health is full.");
Console.WriteLine("Press ENTER to start initial scan...");
Console.ReadLine();
foundAddresses.Clear();
Dictionary<IntPtr, int> currentValues = new();
Console.WriteLine("[*] Scanning initial values...");
ScanAllMemoryUnknown(currentValues);
Console.WriteLine($"[+] Initial scan: {currentValues.Count} candidates");
for (int round = 1; round <= 3; round++)
{
Console.WriteLine($"\n[ROUND {round}] Take damage now!");
Console.WriteLine("Press ENTER after health decreases...");
Console.ReadLine();
var newValues = new Dictionary<IntPtr, int>();
var filtered = new List<IntPtr>();
foreach (var kvp in currentValues)
{
int newVal = ReadInt32(kvp.Key);
if (newVal < kvp.Value)
{
newValues[kvp.Key] = newVal;
filtered.Add(kvp.Key);
}
}
currentValues = newValues;
Console.WriteLine($"[+] After filter: {currentValues.Count} results");
if (currentValues.Count < 5)
break;
}
foundAddresses = currentValues.Keys.ToList();
Console.WriteLine("\n[+] Auto scan complete!");
Console.WriteLine($"Possible addresses: {foundAddresses.Count}");
ListAddresses();
}
void ScanAllMemoryUnknown(Dictionary<IntPtr, int> results)
{
IntPtr addr = IntPtr.Zero;
const int chunkSize = 4096;
while (addr.ToInt64() < 0x7FFFFFFF)
{
if (!VirtualQueryEx(hProcess, addr, out MEMORY_BASIC_INFORMATION mbi,
(uint)Marshal.SizeOf<MEMORY_BASIC_INFORMATION>()))
break;
if (mbi.State == 0x1000 && mbi.Protect == 0x04)
{
long regionSize = mbi.RegionSize.ToInt64();
byte[] buffer = new byte[chunkSize];
for (long offset = 0; offset < regionSize; offset += chunkSize)
{
int bytesToRead = (int)Math.Min(chunkSize, regionSize - offset);
IntPtr currentAddr = new IntPtr(mbi.BaseAddress.ToInt64() + offset);
if (ReadProcessMemory(hProcess, currentAddr, buffer, bytesToRead, out int bytesRead))
{
for (int i = 0; i < bytesRead - 4; i++)
{
int val = BitConverter.ToInt32(buffer, i);
results[new IntPtr(currentAddr.ToInt64() + i)] = val;
if (results.Count > 200000)
return;
}
}
}
}
addr = new IntPtr(mbi.BaseAddress.ToInt64() + (long)mbi.RegionSize);
}
}
void FirstScan()
{
Console.Write("Value (dec/hex): ");
string? valStr = Console.ReadLine();
Console.Write("Type (4byte/2byte/byte/float): ");
string type = (Console.ReadLine()?.ToLower()) ?? "4byte";
bool isFloat = type == "float";
float floatValue = 0;
int intValue = 0;
if (isFloat)
{
if (!float.TryParse(valStr, out floatValue))
{
Console.WriteLine("Invalid float!");
return;
}
}
else
{
if (!ParseValue(valStr, out intValue))
return;
}
foundAddresses.Clear();
ScanAllMemory(intValue);
Console.WriteLine($"[+] First scan complete: {foundAddresses.Count} results");
}
void NextScan()
{
if (foundAddresses.Count == 0)
{
Console.WriteLine("[-] No previous results! Do first scan first.");
return;
}
Console.Write("New value: ");
string? valStr = Console.ReadLine();
Console.Write("Type: ");
string type = (Console.ReadLine()?.ToLower()) ?? "4byte";
bool isFloat = type == "float";
float floatValue = 0;
int intValue = 0;
if (isFloat)
{
if (!float.TryParse(valStr, out floatValue))
{
Console.WriteLine("Invalid float!");
return;
}
}
else
{
if (!ParseValue(valStr, out intValue))
return;
}
var newList = new List<IntPtr>();
foreach (var addr in foundAddresses)
{
byte[] buffer = new byte[4];
if (!ReadProcessMemory(hProcess, addr, buffer, 4, out _))
continue;
bool match = false;
switch (type)
{
case "byte":
match = buffer[0] == intValue;
break;
case "2byte":
match = BitConverter.ToInt16(buffer, 0) == intValue;
break;
case "4byte":
match = BitConverter.ToInt32(buffer, 0) == intValue;
break;
case "float":
float f = BitConverter.ToSingle(buffer, 0);
match = Math.Abs(f - floatValue) < 0.01f;
break;
}
if (match)
newList.Add(addr);
}
foundAddresses = newList;
Console.WriteLine($"[+] Next scan complete: {foundAddresses.Count} results");
}
void ListAddresses()
{
if (foundAddresses.Count == 0)
{
Console.WriteLine("[-] No results");
return;
}
Console.WriteLine($"\nAddresses ({foundAddresses.Count}):");
int maxShow = Math.Min(20, foundAddresses.Count);
for (int i = 0; i < maxShow; i++)
{
int val = ReadInt32(foundAddresses[i]);
Console.WriteLine($"{i + 1}. 0x{foundAddresses[i]:X8} = {val}");
}
if (foundAddresses.Count > 20)
Console.WriteLine($"... and {foundAddresses.Count - 20} more");
}
void FreezeLoop()
{
while (freezeRunning)
{
foreach (var kvp in frozenValues)
{
byte[] buffer = BitConverter.GetBytes(kvp.Value);
WriteProcessMemory(hProcess, kvp.Key, buffer, 4, out _);
}
Thread.Sleep(10);
}
}
void PatchValue()
{
if (foundAddresses.Count == 0)
{
Console.WriteLine("[-] No results to patch!");
return;
}
Console.Write($"Address # (1-{foundAddresses.Count}): ");
string? indexStr = Console.ReadLine();
if (!int.TryParse(indexStr, out int index) || index < 1 || index > foundAddresses.Count)
{
Console.WriteLine("Invalid index!");
return;
}
IntPtr addr = foundAddresses[index - 1];
Console.Write("New value (dec/hex): ");
string? newValStr = Console.ReadLine();
if (!ParseValue(newValStr, out int newVal))
return;
if (WriteInt32(addr, newVal))
Console.WriteLine($"[+] Patched 0x{addr:X8} = {newVal} ✓");
else
Console.WriteLine("[-] Patch failed!");
}
void FreezeValue()
{
if (foundAddresses.Count == 0)
{
Console.WriteLine("[-] No results to freeze!");
return;
}
Console.Write($"Address # (1-{foundAddresses.Count}): ");
if (!int.TryParse(Console.ReadLine(), out int index) ||
index < 1 || index > foundAddresses.Count)
{
Console.WriteLine("Invalid index!");
return;
}
IntPtr addr = foundAddresses[index - 1];
Console.Write("Value to freeze: ");
if (!int.TryParse(Console.ReadLine(), out int value))
{
Console.WriteLine("Invalid value!");
return;
}
frozenValues[addr] = value;
Console.WriteLine($"[+] Freezing 0x{addr:X} = {value}");
if (!freezeRunning)
{
freezeRunning = true;
new Thread(FreezeLoop) { IsBackground = true }.Start();
}
}
void UnfreezeAll()
{
frozenValues.Clear();
freezeRunning = false;
Console.WriteLine("[+] All addresses unfrozen");
}
static bool ParseValue(string? input, out int value)
{
value = 0;
if (string.IsNullOrEmpty(input)) return false;
if (input.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
return int.TryParse(input.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out value);
return int.TryParse(input, out value);
}
int ReadValue(IntPtr addr, int size)
{
byte[] buffer = new byte[4];
ReadProcessMemory(hProcess, addr, buffer, size, out _);
return size switch
{
1 => buffer[0],
2 => BitConverter.ToInt16(buffer, 0),
4 => BitConverter.ToInt32(buffer, 0),
_ => BitConverter.ToInt32(buffer, 0)
};
}
int ReadInt32(IntPtr addr)
{
byte[] buffer = new byte[4];
ReadProcessMemory(hProcess, addr, buffer, 4, out _);
return BitConverter.ToInt32(buffer, 0);
}
bool WriteInt32(IntPtr addr, int value)
{
byte[] buffer = BitConverter.GetBytes(value);
return WriteProcessMemory(hProcess, addr, buffer, 4, out _);
}
void ScanAllMemory(int targetValue)
{
foundAddresses.Clear();
IntPtr addr = IntPtr.Zero;
const int chunkSize = 4096;
while (addr.ToInt64() < 0x7FFFFFFF0000)
{
if (!VirtualQueryEx(hProcess, addr, out MEMORY_BASIC_INFORMATION mbi,
(uint)Marshal.SizeOf<MEMORY_BASIC_INFORMATION>()))
break;
if (mbi.State == 0x1000 && mbi.Protect == 0x04)
{
long regionSize = mbi.RegionSize.ToInt64();
byte[] buffer = new byte[chunkSize];
for (long offset = 0; offset < regionSize; offset += chunkSize)
{
int bytesToRead = (int)Math.Min(chunkSize, regionSize - offset);
IntPtr currentAddr = new IntPtr(mbi.BaseAddress.ToInt64() + offset);
if (ReadProcessMemory(hProcess, currentAddr, buffer, bytesToRead, out int bytesRead))
{
for (int i = 0; i < bytesRead - 4; i++)
{
int val = BitConverter.ToInt32(buffer, i);
if (val == targetValue)
{
foundAddresses.Add(new IntPtr(currentAddr.ToInt64() + i));
if (foundAddresses.Count > 10000)
return;
}
}
}
}
}
addr = new IntPtr(mbi.BaseAddress.ToInt64() + (long)mbi.RegionSize);
}
Console.WriteLine($"[+] Scan done: {foundAddresses.Count} results");
}
void ScanRegion(IntPtr baseAddr, int regionSize, int intValue, float floatValue, string type)
{
byte[] buffer = new byte[regionSize];
int bytesRead;
if (!ReadProcessMemory(hProcess, baseAddr, buffer, regionSize, out bytesRead))
return;
int dataSize = type switch
{
"byte" => 1,
"2byte" => 2,
"float" => 4,
_ => 4
};
for (int i = 0; i < bytesRead - dataSize; i++)
{
bool match = false;
switch (type)
{
case "byte":
match = buffer[i] == intValue;
break;
case "2byte":
short s = BitConverter.ToInt16(buffer, i);
match = s == intValue;
break;
case "4byte":
int val = BitConverter.ToInt32(buffer, i);
match = val == intValue;
break;
case "float":
float f = BitConverter.ToSingle(buffer, i);
match = Math.Abs(f - floatValue) < 0.01f;
break;
}
if (match)
{
foundAddresses.Add(new IntPtr(baseAddr.ToInt64() + i));
if (foundAddresses.Count > 50000)
return;
}
}
}
}
}