-
Notifications
You must be signed in to change notification settings - Fork 2
/
Magic.cs
561 lines (501 loc) · 19.7 KB
/
Magic.cs
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#region License/Copyright
// WhiteMagic - Injected .NET Helper Library
// Copyright (C) 2009 Apoc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using WhiteMagic.Internals;
using WhiteMagic.Native;
#if X64
using ADDR = System.UInt64;
#else
using ADDR = System.UInt32;
#endif
namespace WhiteMagic
{
/*
Notes on this project:
*
* I am not using Marshal.Read/Write for good reasons. The main one, being the required security
* contract permissions to actually have it work right. This may sometimes fail, regardless of whether
* the end user has administrator permissions or not. Instead, I opted to use ReadProcessMemory and WriteProcessMemory
* (contained in the Native.Win32 class) to do the actual reading/writing. There is no obvious overhead, so it
* shouldn't be an issue either way. This class handles opening a proc handle, and closing it as well.
*
* The Win32 API is much faster than the Marshal API, so there's another reason. (Granted; not a good one, but hey, this is
* written mainly for those hacking games from C#)
*
* This library DOES NOT HANDLE INJECTING INTO PROCESSES! You will need to write that functionality yourself,
* as I'm far too lazy to write the injection library. (It's not that hard anyway...)
*
* Major credit to Shynd, who's name I pretty much stole (from his BlackMagic lib, aimed towards out of proc editors)
*
* Other credits:
*
* kynox; being an awesome ausfag
* Cypher: being a portability nazi kiwi (yes, this lib is mainly for x86, no x64 support is implied)
* jjaa; a good bit of his code is used in this library, however, I've changed/rewritten a good chunk of it.
* Shynd; the BlackMagic library that I used as a basis of.. well... even writing this one...
* Everyone at GD; just plain awesome people.
* MMOwned; 95% retarded dimwits, but some of you are awesome.
*/
/// <summary>
/// The main memory library class. Just instantiate this class, and everything else you need is contained within.
/// Alternatively, you can use the Magic.Instance property, to grab a static instance of this class (singleton)
///
/// I highly suggest tracking your own instances for fairly obvious reasons.
/// </summary>
public sealed class Magic : IDisposable
{
private static Magic _instance;
private readonly DetourManager _detours;
private readonly PatchManager _patches;
private readonly PatternManager _patterns;
private readonly Win32 _win32;
/// <summary>
/// Creates a new instance of the <see cref="Magic"/> class.
/// </summary>
public Magic()
{
Process.EnterDebugMode();
_win32 = new Win32(IntPtr.Zero);
_detours = new DetourManager(_win32);
_patches = new PatchManager(_win32);
_patterns = new PatternManager(_win32);
}
/// <summary>
/// Retrieves a static instance of the <see cref="Magic"/> class. (A singleton)
/// </summary>
public static Magic Instance
{
get
{
if (_instance == null)
{
_instance = new Magic();
}
return _instance;
}
}
/// <summary>
/// Provides access to the DetourManager class, that allows you to create and remove
/// detours and hooks for functions. (Or any other use you may find...)
/// </summary>
public DetourManager Detours { get { return _detours; } }
/// <summary>
/// Provides access to the PatchManager class, which allows you to apply and remove patches.
/// </summary>
public PatchManager Patches { get { return _patches; } }
/// <summary>
/// Provides access to the PatternsManager class, which allows you to load, and search for patterns in memory.
/// </summary>
public PatternManager Patterns { get { return _patterns; } }
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
Process.LeaveDebugMode();
_win32.Dispose();
GC.SuppressFinalize(this);
}
#endregion
#region Read/Write
private T ReadInternal<T>(IntPtr address)
{
object ret;
// Handle types that don't have a real typecode
// and/or can be done without the ReadByte bullshit
if (typeof (T) == typeof (IntPtr))
{
ret = Marshal.ReadIntPtr(address);
return (T) ret;
}
if (typeof (T) == typeof (string))
{
byte[] buffer = new byte[1024];
Marshal.Copy(address, buffer, 0, 1024);
int index = 0;
foreach (var b in buffer)
{
if (b == 0x00)
break;
index++;
}
byte[] convert = new byte[index];
Array.Copy(buffer, convert, index);
ret = Encoding.UTF8.GetString(convert);
return (T) ret;
}
int size = Marshal.SizeOf(typeof (T));
byte[] ba = _win32.ReadBytes(address, size);
switch (Type.GetTypeCode(typeof (T)))
{
case TypeCode.Boolean:
ret = BitConverter.ToBoolean(ba, 0);
break;
case TypeCode.Char:
ret = BitConverter.ToChar(ba, 0);
break;
case TypeCode.Byte:
ret = ba[0];
break;
case TypeCode.Int16:
ret = BitConverter.ToInt16(ba, 0);
break;
case TypeCode.UInt16:
ret = BitConverter.ToUInt16(ba, 0);
break;
case TypeCode.Int32:
ret = BitConverter.ToInt32(ba, 0);
break;
case TypeCode.UInt32:
ret = BitConverter.ToUInt32(ba, 0);
break;
case TypeCode.Int64:
ret = BitConverter.ToInt64(ba, 0);
break;
case TypeCode.UInt64:
ret = BitConverter.ToUInt64(ba, 0);
break;
case TypeCode.Single:
ret = BitConverter.ToSingle(ba, 0);
break;
case TypeCode.Double:
ret = BitConverter.ToDouble(ba, 0);
break;
default:
throw new NotSupportedException(typeof (T).FullName + " is not currently supported by Read<T>");
}
return (T) ret;
}
/// <summary>
/// Reads a specific number of bytes from memory.
/// </summary>
/// <param name="address"></param>
/// <param name="count"></param>
/// <returns></returns>
public byte[] ReadBytes(IntPtr address, int count)
{
return _win32.ReadBytes(address, count);
}
/// <summary>
/// Reads a specific number of bytes from memory.
/// </summary>
/// <param name="address"></param>
/// <param name="count"></param>
/// <returns></returns>
public byte[] ReadBytes(ADDR address, int count)
{
return ReadBytes((IntPtr) address, count);
}
/// <summary>
/// Reads a "T" from memory, using consecutive reading.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="addresses"></param>
/// <returns></returns>
public T Read<T>(params IntPtr[] addresses)
{
var tmp = new List<ADDR>();
foreach (IntPtr ptr in addresses)
{
tmp.Add((ADDR) ptr.ToInt32());
}
return Read<T>(tmp.ToArray());
}
/// <summary>
/// Reads a "T" from memory, using consecutive reading.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="addresses"></param>
/// <returns></returns>
public T Read<T>(params ADDR[] addresses)
{
if (addresses.Length == 0)
{
return default(T);
}
if (addresses.Length == 1)
{
return ReadInternal<T>((IntPtr) addresses[0]);
}
ADDR last = 0;
for (int i = 0; i < addresses.Length; i++)
{
if (i == addresses.Length - 1)
{
return ReadInternal<T>((IntPtr) (addresses[i] + last));
}
last = ReadInternal<ADDR>(new IntPtr(last + addresses[i]));
}
// Should never hit this.
// The compiler just bitches.
return default(T);
}
/// <summary>
/// Reads a struct from memory. The struct must be attributed properly!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
/// <returns></returns>
public T ReadStruct<T>(IntPtr address) where T : struct
{
#if !NOEXCEPTIONS
if (!Utilities.HasAttrib<StructLayoutAttribute>(typeof(T)))
throw new MissingAttributeException("StructLayoutAttribute is missing.");
#endif
return (T) Marshal.PtrToStructure(address, typeof (T));
}
/// <summary>
/// Writes a set of bytes to memory.
/// </summary>
/// <param name="address"></param>
/// <param name="bytes"></param>
/// <returns></returns>
public bool WriteBytes(IntPtr address, byte[] bytes)
{
return _win32.WriteBytes(address, bytes) == bytes.Length;
}
/// <summary>
/// Writes a set of bytes to memory.
/// </summary>
/// <param name="address"></param>
/// <param name="bytes"></param>
/// <returns></returns>
public bool WriteBytes(ADDR address, byte[] bytes)
{
return WriteBytes((IntPtr) address, bytes);
}
/// <summary>
/// Writes a generic datatype to memory. (Note; only base datatypes are supported [int,float,uint,byte,sbyte,double,etc])
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool Write<T>(IntPtr address, T value)
{
// We can't handle strings yet...
// Maybe in the future. Since strings require special treatment
// depending on the context. (Can we just write the string bytes? Do we need to write
// them in a cave somewhere, and write the pointer to the cave somewhere else?
// What type of string should be written? Etc.)
// Just handle writing strings yourselves!
if (typeof (T) == typeof (string))
{
throw new ArgumentException("Writing strings are not currently supported!", "value");
}
try
{
object val = value;
byte[] bytes;
// Handle types that don't have a real typecode
// and/or can be done without the ReadByte bullshit
if (typeof (T) == typeof (IntPtr))
{
// Since we're already here...... we might as well do some stuffs.
Marshal.WriteIntPtr(address, (IntPtr) val);
return true;
}
// Make sure we're handling passing in stuff as a byte array.
if (typeof (T) == typeof (byte[]))
{
bytes = (byte[]) val;
return _win32.WriteBytes(address, bytes) == bytes.Length;
}
switch (Type.GetTypeCode(typeof (T)))
{
case TypeCode.Boolean:
bytes = BitConverter.GetBytes((bool) val);
break;
case TypeCode.Char:
bytes = BitConverter.GetBytes((char) val);
break;
case TypeCode.Byte:
bytes = new[] {(byte) val};
break;
case TypeCode.Int16:
bytes = BitConverter.GetBytes((short) val);
break;
case TypeCode.UInt16:
bytes = BitConverter.GetBytes((ushort) val);
break;
case TypeCode.Int32:
bytes = BitConverter.GetBytes((int) val);
break;
case TypeCode.UInt32:
bytes = BitConverter.GetBytes((uint) val);
break;
case TypeCode.Int64:
bytes = BitConverter.GetBytes((long) val);
break;
case TypeCode.UInt64:
bytes = BitConverter.GetBytes((ulong) val);
break;
case TypeCode.Single:
bytes = BitConverter.GetBytes((float) val);
break;
case TypeCode.Double:
bytes = BitConverter.GetBytes((double) val);
break;
default:
throw new NotSupportedException(typeof (T).FullName + " is not currently supported by Write<T>");
}
return _win32.WriteBytes(address, bytes) == bytes.Length;
}
catch
{
return false;
}
}
/// <summary>
/// Writes a generic datatype to memory. (Note; only base datatypes are supported [int,float,uint,byte,sbyte,double,etc])
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool Write<T>(ADDR address, T value)
{
return Write((IntPtr) address, value);
}
/// <summary>
/// Writes a struct to memory.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool WriteStruct<T>(IntPtr address, T value) where T : struct
{
#if !NOEXCEPTIONS
if (!Utilities.HasAttrib<StructLayoutAttribute>(typeof(T)))
throw new MissingAttributeException("StructLayoutAttribute is missing.");
#endif
try
{
Marshal.StructureToPtr(value, address, true);
}
catch
{
return false;
}
return true;
}
/// <summary>
/// Writes a struct to memory.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool WriteStruct<T>(ADDR address, T value) where T : struct
{
return WriteStruct((IntPtr) address, value);
}
#endregion
#region VTable Stuff
/// <summary>
/// Retrieves a function pointer, to an objects virtual function table.
/// </summary>
/// <param name="objectBase">The pointer to the object.</param>
/// <param name="functionIndex">The nth function to retrieve.</param>
/// <returns></returns>
public IntPtr GetObjectVtableFunction(IntPtr objectBase, uint functionIndex)
{
// [[objBase]+4*vfuncIdx]
return Read<IntPtr>((ADDR) objectBase, functionIndex * 4);
}
#endregion
#region Delegate/Function Registration
/// <summary>
/// Registers a function into a delegate. Note: The delegate must provide a proper function signature!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
/// <returns></returns>
public T RegisterDelegate<T>(ADDR address) where T : class
{
return RegisterDelegate<T>((IntPtr) address);
}
/// <summary>
/// Registers a function into a delegate. Note: The delegate must provide a proper function signature!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
/// <returns></returns>
public T RegisterDelegate<T>(IntPtr address) where T : class
{
#if !NOEXCEPTIONS
// Make sure delegates are attributed properly!
if (!Utilities.HasUFPAttribute(typeof (T)))
{
throw new ArgumentException("The delegate does not have proper attributes! It must be adorned with" +
" the System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute, with proper calling conventions" +
" to work properly!");
}
#endif
return Marshal.GetDelegateForFunctionPointer(address, typeof (T)) as T;
}
#endregion
#region LoadLib/GetProcAddr/FreeLib
/// <summary>
/// Win32 LoadLibrary
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public IntPtr LoadLibrary(string fileName)
{
return Win32.LoadLibrary(fileName);
}
/// <summary>
/// Win32 FreeLibrary
/// </summary>
/// <param name="hLib"></param>
/// <returns></returns>
public bool FreeLibrary(IntPtr hLib)
{
return Win32.FreeLibrary(hLib);
}
/// <summary>
/// Win32 GetProcAddress
/// </summary>
/// <param name="hModule"></param>
/// <param name="procedureName"></param>
/// <returns></returns>
public IntPtr GetProcAddress(IntPtr hModule, string procedureName)
{
return Win32.GetProcAddress(hModule, procedureName);
}
/// <summary>
/// Loads a library, and gets a procedure address.
/// </summary>
/// <param name="fileName"></param>
/// <param name="procedureName"></param>
/// <returns></returns>
public IntPtr GetProcAddress(string fileName, string procedureName)
{
return GetProcAddress(LoadLibrary(fileName), procedureName);
}
#endregion
}
}