-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadWriteMemory.cs
More file actions
468 lines (396 loc) · 17.2 KB
/
ReadWriteMemory.cs
File metadata and controls
468 lines (396 loc) · 17.2 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
using System.Diagnostics;
using System.Runtime.InteropServices;
using System;
using System.Windows.Forms;
using System.Media;
using System.Text;
using System.Threading;
namespace ReadWriteMemory
{
internal class ProcessMemory
{
// Fields
protected int BaseAddress;
protected Process[] MyProcess;
protected ProcessModule myProcessModule;
private const uint PAGE_EXECUTE = 16;
private const uint PAGE_EXECUTE_READ = 32;
private const uint PAGE_EXECUTE_READWRITE = 64;
private const uint PAGE_EXECUTE_WRITECOPY = 128;
private const uint PAGE_GUARD = 256;
private const uint PAGE_NOACCESS = 1;
private const uint PAGE_NOCACHE = 512;
private const uint PAGE_READONLY = 2;
private const uint PAGE_READWRITE = 4;
private const uint PAGE_WRITECOPY = 8;
private const uint PROCESS_ALL_ACCESS = 2035711;
protected int processHandle;
protected string ProcessName;
// Methods
public ProcessMemory(string pProcessName)
{
this.ProcessName = pProcessName;
}
public bool CheckProcess()
{
return (Process.GetProcessesByName(this.ProcessName).Length > 0);
}
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(int hObject);
public string CutString(string mystring)
{
char[] chArray = mystring.ToCharArray();
string str = "";
for (int i = 0; i < mystring.Length; i++)
{
if ((chArray[i] == ' ') && (chArray[i + 1] == ' '))
{
return str;
}
if (chArray[i] == '\0')
{
return str;
}
str = str + chArray[i].ToString();
}
return mystring.TrimEnd(new char[] { '0' });
}
public int DllImageAddress(string dllname)
{
ProcessModuleCollection modules = this.MyProcess[0].Modules;
foreach (ProcessModule procmodule in modules)
{
if (dllname == procmodule.ModuleName)
{
return (int)procmodule.BaseAddress;
}
}
return -1;
}
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern int FindWindowByCaption(int ZeroOnly, string lpWindowName);
public int ImageAddress()
{
this.BaseAddress = 0;
this.myProcessModule = this.MyProcess[0].MainModule;
this.BaseAddress = (int)this.myProcessModule.BaseAddress;
return this.BaseAddress;
}
public int ImageAddress(int pOffset)
{
this.BaseAddress = 0;
this.myProcessModule = this.MyProcess[0].MainModule;
this.BaseAddress = (int)this.myProcessModule.BaseAddress;
return (pOffset + this.BaseAddress);
}
public string MyProcessName()
{
return this.ProcessName;
}
[DllImport("kernel32.dll")]
public static extern int OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
public int Pointer(bool AddToImageAddress, int pOffset)
{
return this.ReadInt(this.ImageAddress(pOffset));
}
public int Pointer(string Module, int pOffset)
{
return this.ReadInt(this.DllImageAddress(Module) + pOffset);
}
public int Pointer(bool AddToImageAddress, int pOffset, int pOffset2)
{
//look at this shit, it doesnt even have a if statement
if (AddToImageAddress)
return (this.ReadInt(this.ImageAddress() + pOffset) + pOffset2);
else
return (this.ReadInt(pOffset) + pOffset2);
}
public int Pointer(string Module, int pOffset, int pOffset2)
{
return (this.ReadInt(this.DllImageAddress(Module) + pOffset) + pOffset2);
}
public int Pointer(bool AddToImageAddress, int pOffset, int pOffset2, int pOffset3)
{
return (this.ReadInt(this.ReadInt(this.ImageAddress(pOffset)) + pOffset2) + pOffset3);
}
public int Pointer(string Module, int pOffset, int pOffset2, int pOffset3)
{
return (this.ReadInt(this.ReadInt(this.DllImageAddress(Module) + pOffset) + pOffset2) + pOffset3);
}
public int Pointer(bool AddToImageAddress, int pOffset, int pOffset2, int pOffset3, int pOffset4)
{
return (this.ReadInt(this.ReadInt(this.ReadInt(this.ImageAddress(pOffset)) + pOffset2) + pOffset3) + pOffset4);
}
public int Pointer(string Module, int pOffset, int pOffset2, int pOffset3, int pOffset4)
{
return (this.ReadInt(this.ReadInt(this.ReadInt(this.DllImageAddress(Module) + pOffset) + pOffset2) + pOffset3) + pOffset4);
}
public int Pointer(bool AddToImageAddress, int pOffset, int pOffset2, int pOffset3, int pOffset4, int pOffset5)
{
return (this.ReadInt(this.ReadInt(this.ReadInt(this.ReadInt(this.ImageAddress(pOffset)) + pOffset2) + pOffset3) + pOffset4) + pOffset5);
}
public int Pointer(string Module, int pOffset, int pOffset2, int pOffset3, int pOffset4, int pOffset5)
{
return (this.ReadInt(this.ReadInt(this.ReadInt(this.ReadInt(this.DllImageAddress(Module) + pOffset) + pOffset2) + pOffset3) + pOffset4) + pOffset5);
}
public int Pointer(bool AddToImageAddress, int pOffset, int pOffset2, int pOffset3, int pOffset4, int pOffset5, int pOffset6)
{
return (this.ReadInt(this.ReadInt(this.ReadInt(this.ReadInt(this.ReadInt(this.ImageAddress(pOffset)) + pOffset2) + pOffset3) + pOffset4) + pOffset5) + pOffset6);
}
public int Pointer(string Module, int pOffset, int pOffset2, int pOffset3, int pOffset4, int pOffset5, int pOffset6)
{
return (this.ReadInt(this.ReadInt(this.ReadInt(this.ReadInt(this.ReadInt(this.DllImageAddress(Module) + pOffset) + pOffset2) + pOffset3) + pOffset4) + pOffset5) + pOffset6);
}
public byte ReadByte(int pOffset)
{
byte[] buffer = new byte[1];
ReadProcessMemory(this.processHandle, pOffset, buffer, 1, 0);
return buffer[0];
}
public byte ReadByte(bool AddToImageAddress, int pOffset)
{
byte[] buffer = new byte[1];
int lpBaseAddress = AddToImageAddress ? this.ImageAddress(pOffset) : pOffset;
ReadProcessMemory(this.processHandle, lpBaseAddress, buffer, 1, 0);
return buffer[0];
}
public byte ReadByte(string Module, int pOffset)
{
byte[] buffer = new byte[1];
ReadProcessMemory(this.processHandle, this.DllImageAddress(Module) + pOffset, buffer, 1, 0);
return buffer[0];
}
public float ReadFloat(int pOffset)
{
return BitConverter.ToSingle(this.ReadMem(pOffset, 4), 0);
}
public float ReadFloat(bool AddToImageAddress, int pOffset)
{
return BitConverter.ToSingle(this.ReadMem(pOffset, 4, AddToImageAddress), 0);
}
public float ReadFloat(string Module, int pOffset)
{
return BitConverter.ToSingle(this.ReadMem(this.DllImageAddress(Module) + pOffset, 4), 0);
}
public int ReadInt(int pOffset)
{
return BitConverter.ToInt32(this.ReadMem(pOffset, 4), 0);
}
public int ReadInt(bool AddToImageAddress, int pOffset)
{
return BitConverter.ToInt32(this.ReadMem(pOffset, 4, AddToImageAddress), 0);
}
public int ReadInt(string Module, int pOffset)
{
return BitConverter.ToInt32(this.ReadMem(this.DllImageAddress(Module) + pOffset, 4), 0);
}
public byte[] ReadMem(int pOffset, int pSize)
{
byte[] buffer = new byte[pSize];
ReadProcessMemory(this.processHandle, pOffset, buffer, pSize, 0);
return buffer;
}
public byte[] ReadMem(int pOffset, int pSize, bool AddToImageAddress)
{
byte[] buffer = new byte[pSize];
int lpBaseAddress = AddToImageAddress ? this.ImageAddress(pOffset) : pOffset;
ReadProcessMemory(this.processHandle, lpBaseAddress, buffer, pSize, 0);
return buffer;
}
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesRead);
public short ReadShort(int pOffset)
{
return BitConverter.ToInt16(this.ReadMem(pOffset, 2), 0);
}
public short ReadShort(bool AddToImageAddress, int pOffset)
{
return BitConverter.ToInt16(this.ReadMem(pOffset, 2, AddToImageAddress), 0);
}
public short ReadShort(string Module, int pOffset)
{
return BitConverter.ToInt16(this.ReadMem(this.DllImageAddress(Module) + pOffset, 2), 0);
}
public string ReadStringAscii(int pOffset, int pSize)
{
return this.CutString(Encoding.ASCII.GetString(this.ReadMem(pOffset, pSize)));
}
public string ReadStringAscii(bool AddToImageAddress, int pOffset, int pSize)
{
return this.CutString(Encoding.ASCII.GetString(this.ReadMem(pOffset, pSize, AddToImageAddress)));
}
public string ReadStringAscii(string Module, int pOffset, int pSize)
{
return this.CutString(Encoding.ASCII.GetString(this.ReadMem(this.DllImageAddress(Module) + pOffset, pSize)));
}
public string ReadStringUnicode(int pOffset, int pSize)
{
return this.CutString(Encoding.Unicode.GetString(this.ReadMem(pOffset, pSize)));
}
public string ReadStringUnicode(bool AddToImageAddress, int pOffset, int pSize)
{
return this.CutString(Encoding.Unicode.GetString(this.ReadMem(pOffset, pSize, AddToImageAddress)));
}
public string ReadStringUnicode(string Module, int pOffset, int pSize)
{
return this.CutString(Encoding.Unicode.GetString(this.ReadMem(this.DllImageAddress(Module) + pOffset, pSize)));
}
public uint ReadUInt(int pOffset)
{
return BitConverter.ToUInt32(this.ReadMem(pOffset, 4), 0);
}
public uint ReadUInt(bool AddToImageAddress, int pOffset)
{
return BitConverter.ToUInt32(this.ReadMem(pOffset, 4, AddToImageAddress), 0);
}
public uint ReadUInt(string Module, int pOffset)
{
return BitConverter.ToUInt32(this.ReadMem(this.DllImageAddress(Module) + pOffset, 4), 0);
}
public bool OpenProcess()
{
if (this.ProcessName != "")
{
this.MyProcess = Process.GetProcessesByName(this.ProcessName);
if (this.MyProcess.Length == 0)
{
MessageBox.Show(this.ProcessName + " is not running or has not been found. Please check and try again", "Process Not Found", MessageBoxButtons.OK, MessageBoxIcon.Hand);
return false;
}
this.processHandle = OpenProcess(2035711, false, this.MyProcess[0].Id);
if (this.processHandle == 0)
{
MessageBox.Show(this.ProcessName + " is not running or has not been found. Please check and try again", "Process Not Found", MessageBoxButtons.OK, MessageBoxIcon.Hand);
return false;
}
return true;
}
MessageBox.Show("Define process name first!");
return false;
}
[DllImport("kernel32.dll")]
public static extern bool VirtualProtectEx(int hProcess, int lpAddress, int dwSize, uint flNewProtect, out uint lpflOldProtect);
public void WriteByte(int pOffset, byte pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes((short)pBytes));
}
public void WriteByte(bool AddToImageAddress, int pOffset, byte pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes((short)pBytes), AddToImageAddress);
}
public void WriteByte(string Module, int pOffset, byte pBytes)
{
this.WriteMem(this.DllImageAddress(Module) + pOffset, BitConverter.GetBytes((short)pBytes));
}
public void WriteDouble(int pOffset, double pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteDouble(bool AddToImageAddress, int pOffset, double pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes), AddToImageAddress);
}
public void WriteDouble(string Module, int pOffset, double pBytes)
{
this.WriteMem(this.DllImageAddress(Module) + pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteFloat(int pOffset, float pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteFloat(bool AddToImageAddress, int pOffset, float pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes), AddToImageAddress);
}
public void WriteFloat(string Module, int pOffset, float pBytes)
{
this.WriteMem(this.DllImageAddress(Module) + pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteInt(int pOffset, int pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteInt(bool AddToImageAddress, int pOffset, int pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes), AddToImageAddress);
}
public void WriteInt(string Module, int pOffset, int pBytes)
{
this.WriteMem(this.DllImageAddress(Module) + pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteMem(int pOffset, byte[] pBytes)
{
WriteProcessMemory(this.processHandle, pOffset, pBytes, pBytes.Length, 0);
}
public void WriteMem(int pOffset, byte[] pBytes, bool AddToImageAddress)
{
int lpBaseAddress = AddToImageAddress ? this.ImageAddress(pOffset) : pOffset;
WriteProcessMemory(this.processHandle, lpBaseAddress, pBytes, pBytes.Length, 0);
}
[DllImport("kernel32.dll")]
public static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesWritten);
public void WriteShort(int pOffset, short pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteShort(bool AddToImageAddress, int pOffset, short pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes), AddToImageAddress);
}
public void WriteShort(string Module, int pOffset, short pBytes)
{
this.WriteMem(this.DllImageAddress(Module) + pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteStringAscii(int pOffset, string pBytes)
{
this.WriteMem(pOffset, Encoding.ASCII.GetBytes(pBytes + "\0"));
}
public void WriteStringAscii(bool AddToImageAddress, int pOffset, string pBytes)
{
this.WriteMem(pOffset, Encoding.ASCII.GetBytes(pBytes + "\0"), AddToImageAddress);
}
public void WriteStringAscii(string Module, int pOffset, string pBytes)
{
this.WriteMem(this.DllImageAddress(Module) + pOffset, Encoding.ASCII.GetBytes(pBytes + "\0"));
}
public void WriteStringUnicode(int pOffset, string pBytes)
{
this.WriteMem(pOffset, Encoding.Unicode.GetBytes(pBytes + "\0"));
}
public void WriteStringUnicode(bool AddToImageAddress, int pOffset, string pBytes)
{
this.WriteMem(pOffset, Encoding.Unicode.GetBytes(pBytes + "\0"), AddToImageAddress);
}
public void WriteStringUnicode(string Module, int pOffset, string pBytes)
{
this.WriteMem(this.DllImageAddress(Module) + pOffset, Encoding.Unicode.GetBytes(pBytes + "\0"));
}
public void WriteUInt(int pOffset, uint pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes));
}
public void WriteUInt(bool AddToImageAddress, int pOffset, uint pBytes)
{
this.WriteMem(pOffset, BitConverter.GetBytes(pBytes), AddToImageAddress);
}
public void WriteUInt(string Module, int pOffset, uint pBytes)
{
this.WriteMem(this.DllImageAddress(Module) + pOffset, BitConverter.GetBytes(pBytes));
}
// Nested Types
[Flags]
public enum ProcessAccessFlags : uint
{
All = 2035711,
CreateThread = 2,
DupHandle = 64,
QueryInformation = 1024,
SetInformation = 512,
Synchronize = 1048576,
Terminate = 1,
VMOperation = 8,
VMRead = 16,
VMWrite = 32
}
}
}