-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypervisor.cs
More file actions
422 lines (335 loc) · 15.6 KB
/
Hypervisor.cs
File metadata and controls
422 lines (335 loc) · 15.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
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
public static class Hypervisor
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint flNewProtect, ref int lpflOldProtect);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
public static IntPtr Handle;
public static Process Process;
public static ulong PureAddress;
public static ulong MemoryOffset;
static byte[]? _patternBuffer = null;
/// <summary>
/// Initialize the Hypervisor on a process.
/// </summary>
/// <param name="Input">The input process.</param>
public static void AttachProcess(Process Input)
{
Process = Input;
Handle = Input.Handle;
PureAddress = (ulong)Input.MainModule.BaseAddress;
MemoryOffset = PureAddress & 0x7FFF00000000;
}
/// <summary>
/// Reads a value with the type of T from an address.
/// Unsafe, must be used with caution.
/// </summary>
/// <typeparam name="T">Type of the value to read.</typeparam>
/// <param name="Address">The address of the value to read.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
/// <returns>The value as it is read from memory.</returns>
public static T Read<T>(ulong Address, bool Absolute = false) where T : struct
{
var _address = (IntPtr)Address;
if (!Absolute)
_address = (IntPtr)(PureAddress + Address);
var _dynoMethod = new DynamicMethod("SizeOfType", typeof(int), []);
ILGenerator _ilGen = _dynoMethod.GetILGenerator();
_ilGen.Emit(OpCodes.Sizeof, typeof(T));
_ilGen.Emit(OpCodes.Ret);
var _outSize = (int)_dynoMethod.Invoke(null, null);
var _outArray = new byte[_outSize];
int _outRead = 0;
ReadProcessMemory(Handle, _address, _outArray, _outSize, ref _outRead);
var _outType = typeof(T);
if (_outType.IsEnum)
{
var _gcHandle = GCHandle.Alloc(_outArray, GCHandleType.Pinned);
var _retData = (T)Marshal.PtrToStructure(_gcHandle.AddrOfPinnedObject(), Enum.GetUnderlyingType(_outType));
_gcHandle.Free();
return _retData;
}
else
{
var _gcHandle = GCHandle.Alloc(_outArray, GCHandleType.Pinned);
var _retData = (T)Marshal.PtrToStructure(_gcHandle.AddrOfPinnedObject(), typeof(T));
_gcHandle.Free();
return _retData;
}
}
/// <summary>
/// Reads an array with the type of T[] from an address.
/// Unsafe, must be used with caution.
/// </summary>
/// <typeparam name="T">Type of the array to read.</typeparam>
/// <param name="Address">The address of the value to read.</param>
/// <param name="Size">The size of the array to read.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
/// <returns>The array as it is read from memory.</returns>
public static T[] Read<T>(ulong Address, int Size, bool Absolute = false) where T : struct
{
var _address = (IntPtr)Address;
if (!Absolute)
_address = (IntPtr)(PureAddress + Address);
var _dynoMethod = new DynamicMethod("SizeOfType", typeof(int), []);
ILGenerator _ilGen = _dynoMethod.GetILGenerator();
_ilGen.Emit(OpCodes.Sizeof, typeof(T));
_ilGen.Emit(OpCodes.Ret);
var _outSize = (int)_dynoMethod.Invoke(null, null);
var _outArray = new byte[Size * _outSize];
int _outRead = 0;
ReadProcessMemory(Handle, _address, _outArray, Size * _outSize, ref _outRead);
var _outType = typeof(T);
if (_outType.IsEnum)
{
var _enumType = Enum.GetUnderlyingType(_outType);
var _retArray = MemoryMarshal.Cast<byte, T>(_outArray);
return _retArray.ToArray();
}
else
{
var _retArray = MemoryMarshal.Cast<byte, T>(_outArray);
return _retArray.ToArray();
}
}
/// <summary>
/// Writes a value with the type of T to an address.
/// Unsafe, must be used with caution.
/// </summary>
/// <typeparam name="T">Type of the value to write. Must have a size.</typeparam>
/// <param name="Address">The address which the value will be written to.</param>
/// <param name="Value">The value to write.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
public static void Write<T>(ulong Address, T Value, bool Absolute = false) where T : struct
{
var _address = (IntPtr)Address;
if (!Absolute)
_address = (IntPtr)(PureAddress + Address);
UnlockBlock(Address, Absolute: Absolute);
var _dynoMethod = new DynamicMethod("SizeOfType", typeof(int), []);
ILGenerator _ilGen = _dynoMethod.GetILGenerator();
_ilGen.Emit(OpCodes.Sizeof, typeof(T));
_ilGen.Emit(OpCodes.Ret);
var _inSize = (int)_dynoMethod.Invoke(null, null);
int _inWrite = 0;
var _inType = typeof(T);
if (_inSize > 1)
{
if (_inType.IsEnum)
_inType = Enum.GetUnderlyingType(_inType);
var _inArray = (byte[])typeof(BitConverter).GetMethod("GetBytes", new[] { _inType }).Invoke(null, new object[] { Value });
WriteProcessMemory(Handle, _address, _inArray, _inArray.Length, ref _inWrite);
}
else
{
var _inArray = new byte[] { (byte)Convert.ChangeType(Value, typeof(byte)) };
WriteProcessMemory(Handle, _address, _inArray, _inArray.Length, ref _inWrite);
}
}
/// <summary>
/// Writes an array with the type of T to an address.
/// Unsafe, must be used with caution.
/// </summary>
/// <typeparam name="T">Type of the array to write. Must have a size.</typeparam>
/// <param name="Address">The address which the Array will be written to.</param>
/// <param name="Value">The array to write.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
public static void Write<T>(ulong Address, T[] Value, bool Absolute = false) where T : struct
{
var _address = (IntPtr)Address;
if (!Absolute)
_address = (IntPtr)(PureAddress + Address);
UnlockBlock(Address, Absolute: Absolute);
var _dynoMethod = new DynamicMethod("SizeOfType", typeof(int), []);
ILGenerator _ilGen = _dynoMethod.GetILGenerator();
_ilGen.Emit(OpCodes.Sizeof, typeof(T));
_ilGen.Emit(OpCodes.Ret);
var _inSize = (int)_dynoMethod.Invoke(null, null);
int _inWrite = 0;
var _writeArray = MemoryMarshal.Cast<T, byte>(Value).ToArray();
WriteProcessMemory(Handle, _address, _writeArray, _writeArray.Length, ref _inWrite);
}
/// <summary>
/// Reads a terminated string from address.
/// </summary>
/// <param name="Address">The address which the value will be read from.</param>
/// <param name="Absolute">Whether the address is an absolute address or not. Defaults to false.</param>
/// <returns></returns>
public static string ReadString(ulong Address, bool Absolute = false)
{
IntPtr _address = (IntPtr)(PureAddress + Address);
if (Absolute)
_address = (IntPtr)(Address);
var _length = 0;
while (Read<byte>((ulong)(_address + _length), true) != 0x00)
_length++;
var _outArray = new byte[_length];
int _outRead = 0;
ReadProcessMemory(Handle, _address, _outArray, _length, ref _outRead);
return Encoding.Default.GetString(_outArray);
}
/// <summary>
/// Writes a string to an address.
/// </summary>
/// <param name="Address">The address which the value will be written to.</param>
/// <param name="Value">The string to write.</param>
/// <param name="Absolute">Whether the address is an absolute address or not. Defaults to false.</param>
public static void Write(ulong Address, string Value, bool Absolute = false, bool Unicode = false)
{
IntPtr _address = (IntPtr)(PureAddress + Address);
if (Absolute)
_address = (IntPtr)(Address);
UnlockBlock(Address, Absolute: Absolute);
int _inWrite = 0;
var _stringArray = Encoding.Default.GetBytes(Value);
if (Unicode)
_stringArray = Encoding.Unicode.GetBytes(Value);
WriteProcessMemory(Handle, _address, _stringArray, _stringArray.Length, ref _inWrite);
}
/// <summary>
/// Calculated a 64-bit pointer with the given offsets.
/// All offsets are added and the resulting address is read.
/// </summary>
/// <param name="Address">The starting point to the pointer.</param>
/// <param name="Offsets">All the offsets of the pointer, null by default.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
/// <returns>The final calculated pointer.</returns>
public static ulong GetPointer64(ulong Address, int[] Offsets = null, bool Absolute = false)
{
var _returnPoint = Read<ulong>(Address, Absolute);
if (Offsets == null)
return _returnPoint;
for (int i = 0; i < Offsets.Length - 1; i++)
_returnPoint = Read<ulong>((ulong)((long)_returnPoint + Offsets[i]), true);
return (ulong)((long)_returnPoint + Offsets.Last());
}
/// <summary>
/// Calculated a 32-bit pointer with the given offsets.
/// All offsets are added and the resulting address is read.
/// </summary>
/// <param name="Address">The starting point to the pointer.</param>
/// <param name="Offsets">All the offsets of the pointer, null by default.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
/// <returns>The final calculated pointer.</returns>
public static uint GetPointer32(ulong Address, uint[] Offsets = null, bool Absolute = false)
{
var _returnPoint = Read<uint>(Address, Absolute);
if (Offsets == null)
return _returnPoint;
for (int i = 0; i < Offsets.Length - 1; i++)
_returnPoint = Read<uint>(_returnPoint + Offsets[i], true);
return _returnPoint + Offsets.Last();
}
/// <summary>
/// Redirects a LEA instruction to another address. Given it expects a relative pointer.
/// </summary>
/// <param name="Address">The instruction address.</param>
/// <param name="Destination">The address in memory it will be reditected to.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
public static void RedirectLEA(ulong Address, uint Destination, bool Absolute = false)
{
var _instEnding = (uint)Address + 0x07;
var _instMath = Destination - _instEnding;
Write(Address + 0x03, BitConverter.GetBytes(_instMath), Absolute);
}
/// <summary>
/// Redirects a MOV instruction to another address. Given it expects a relative pointer.
/// </summary>
/// <param name="Address">The instruction address.</param>
/// <param name="Destination">The address in memory it will be reditected to.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
public static void RedirectMOVZX(ulong Address, uint Destination, bool Absolute = false)
{
var _instEnding = (uint)Address + 0x07;
var _instMath = Destination - _instEnding;
Write(Address + 0x03, BitConverter.GetBytes(_instMath), Absolute);
}
/// <summary>
/// Redirects a MOV instruction to another address. Given it expects a relative pointer.
/// </summary>
/// <param name="Address">The instruction address.</param>
/// <param name="Destination">The address in memory it will be reditected to.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
public static void RedirectMOV(ulong Address, uint Destination, bool Absolute = false)
{
var _instEnding = (uint)Address + 0x06;
var _instMath = Destination - _instEnding;
Write(Address + 0x02, BitConverter.GetBytes(_instMath), Absolute);
}
/// <summary>
/// Redirects a CMP instruction to another address. Given it expects a relative pointer.
/// </summary>
/// <param name="Address">The instruction address.</param>
/// <param name="Destination">The address in memory it will be reditected to.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
public static void RedirectCMP(ulong Address, uint Destination, bool Absolute = false)
{
var _instEnding = (uint)Address + 0x07;
var _instMath = Destination - _instEnding;
Write(Address + 0x02, BitConverter.GetBytes(_instMath), Absolute);
}
/// <summary>
/// NOPs an instruction, given it's length.
/// </summary>
/// <param name="Address">The instruction address.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
public static void DeleteInstruction(ulong Address, int Length, bool Absolute = false) => Write(Address, Enumerable.Repeat<byte>(0x90, Length).ToArray(), Absolute);
/// <summary>
/// Finds a signature in memory. Uses the string signature pattern.
/// Refuses to return more than one result.
/// </summary>
/// <param name="Input">The signature to find.</param>
/// <returns>The address of said signature.</returns>
/// <exception cref="InvalidDataException">The pattern hit more or less than 1 result.</exception>
public static IntPtr FindSignature(string Input)
{
if (_patternBuffer == null)
_patternBuffer = Read<byte>(0x00, Process.MainModule.ModuleMemorySize);
var _sigBytes = Input.Split(' ');
int[] _sigList = new int[_sigBytes.Length];
for (int i = 0; i < _sigList.Length; i++)
{
if (_sigBytes[i] == "??")
_sigList[i] = -1;
else
_sigList[i] = int.Parse(_sigBytes[i], NumberStyles.HexNumber);
}
var results = new List<IntPtr>();
for (int a = 0; a < _patternBuffer.Length; a++)
{
for (int b = 0; b < _sigList.Length; b++)
{
if (_sigList[b] != -1 && _sigList[b] != _patternBuffer[a + b])
break;
if (b + 1 == _sigList.Length)
{
var result = new IntPtr(a);
results.Add(result);
}
}
}
if (results.Count != 1)
throw new InvalidDataException("Signature scan failed! " + results.Count + " result(s) found!");
return results[0];
}
/// <summary>
/// Unlocks a particular block to be written.
/// </summary>
/// <param name="Address">The address of the subject block.</param>
/// <param name="Absolute">If the address is absolute, false by default.</param>
public static void UnlockBlock(ulong Address, int Size = 0x100000, bool Absolute = false)
{
var _address = (IntPtr)Address;
if (!Absolute)
_address = (IntPtr)(PureAddress + Address);
int _oldProtect = 0;
VirtualProtectEx(Handle, _address, Size, 0x40, ref _oldProtect);
}
}