forked from Micrologist/GhostrunnerTrainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessExtensions.cs
540 lines (446 loc) · 18.5 KB
/
ProcessExtensions.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable 1591
// Note: Please be careful when modifying this because it could break existing components!
// http://stackoverflow.com/questions/1456785/a-definitive-guide-to-api-breaking-changes-in-net
namespace GhostrunnerTrainer
{
using SizeT = UIntPtr;
public class ProcessModuleWow64Safe
{
public IntPtr BaseAddress { get; set; }
public IntPtr EntryPointAddress { get; set; }
public string FileName { get; set; }
public int ModuleMemorySize { get; set; }
public string ModuleName { get; set; }
public FileVersionInfo FileVersionInfo
{
get { return FileVersionInfo.GetVersionInfo(FileName); }
}
public override string ToString()
{
return ModuleName ?? base.ToString();
}
}
public enum ReadStringType
{
AutoDetect,
ASCII,
UTF8,
UTF16
}
public static class ExtensionMethods
{
private static Dictionary<int, ProcessModuleWow64Safe[]> ModuleCache = new Dictionary<int, ProcessModuleWow64Safe[]>();
public static ProcessModuleWow64Safe MainModuleWow64Safe(this Process p)
{
return p.ModulesWow64Safe().First();
}
public static ProcessModuleWow64Safe[] ModulesWow64Safe(this Process p)
{
if (ModuleCache.Count > 100)
ModuleCache.Clear();
const int LIST_MODULES_ALL = 3;
const int MAX_PATH = 260;
var hModules = new IntPtr[1024];
uint cb = (uint)IntPtr.Size * (uint)hModules.Length;
uint cbNeeded;
if (!WinAPI.EnumProcessModulesEx(p.Handle, hModules, cb, out cbNeeded, LIST_MODULES_ALL))
throw new Win32Exception();
uint numMods = cbNeeded / (uint)IntPtr.Size;
int hash = p.StartTime.GetHashCode() + p.Id + (int)numMods;
if (ModuleCache.ContainsKey(hash))
return ModuleCache[hash];
var ret = new List<ProcessModuleWow64Safe>();
// everything below is fairly expensive, which is why we cache!
var sb = new StringBuilder(MAX_PATH);
for (int i = 0; i < numMods; i++)
{
sb.Clear();
if (WinAPI.GetModuleFileNameEx(p.Handle, hModules[i], sb, (uint)sb.Capacity) == 0)
throw new Win32Exception();
string fileName = sb.ToString();
sb.Clear();
if (WinAPI.GetModuleBaseName(p.Handle, hModules[i], sb, (uint)sb.Capacity) == 0)
throw new Win32Exception();
string baseName = sb.ToString();
var moduleInfo = new WinAPI.MODULEINFO();
if (!WinAPI.GetModuleInformation(p.Handle, hModules[i], out moduleInfo, (uint)Marshal.SizeOf(moduleInfo)))
throw new Win32Exception();
ret.Add(new ProcessModuleWow64Safe()
{
FileName = fileName,
BaseAddress = moduleInfo.lpBaseOfDll,
ModuleMemorySize = (int)moduleInfo.SizeOfImage,
EntryPointAddress = moduleInfo.EntryPoint,
ModuleName = baseName
});
}
ModuleCache.Add(hash, ret.ToArray());
return ret.ToArray();
}
public static IEnumerable<MemoryBasicInformation> MemoryPages(this Process process, bool all = false)
{
// hardcoded values because GetSystemInfo / GetNativeSystemInfo can't return info for remote process
var min = 0x10000L;
var max = process.Is64Bit() ? 0x00007FFFFFFEFFFFL : 0x7FFEFFFFL;
var mbiSize = (SizeT)Marshal.SizeOf(typeof(MemoryBasicInformation));
var addr = min;
do
{
MemoryBasicInformation mbi;
if (WinAPI.VirtualQueryEx(process.Handle, (IntPtr)addr, out mbi, mbiSize) == (SizeT)0)
break;
addr += (long)mbi.RegionSize;
// don't care about reserved/free pages
if (mbi.State != MemPageState.MEM_COMMIT)
continue;
// probably don't care about guarded pages
if (!all && (mbi.Protect & MemPageProtect.PAGE_GUARD) != 0)
continue;
// probably don't care about image/file maps
if (!all && mbi.Type != MemPageType.MEM_PRIVATE)
continue;
yield return mbi;
} while (addr < max);
}
public static bool Is64Bit(this Process process)
{
bool procWow64;
WinAPI.IsWow64Process(process.Handle, out procWow64);
if (Environment.Is64BitOperatingSystem && !procWow64)
return true;
return false;
}
public static bool ReadValue<T>(this Process process, IntPtr addr, out T val) where T : struct
{
var type = typeof(T);
type = type.IsEnum ? Enum.GetUnderlyingType(type) : type;
val = default(T);
object val2;
if (!ReadValue(process, addr, type, out val2))
return false;
val = (T)val2;
return true;
}
public static bool ReadValue(Process process, IntPtr addr, Type type, out object val)
{
byte[] bytes;
val = null;
int size = type == typeof(bool) ? 1 : Marshal.SizeOf(type);
if (!ReadBytes(process, addr, size, out bytes))
return false;
val = ResolveToType(bytes, type);
return true;
}
public static bool ReadBytes(this Process process, IntPtr addr, int count, out byte[] val)
{
var bytes = new byte[count];
SizeT read;
val = null;
if (!WinAPI.ReadProcessMemory(process.Handle, addr, bytes, (SizeT)bytes.Length, out read)
|| read != (SizeT)bytes.Length)
return false;
val = bytes;
return true;
}
public static bool ReadPointer(this Process process, IntPtr addr, out IntPtr val)
{
return ReadPointer(process, addr, process.Is64Bit(), out val);
}
public static bool ReadPointer(this Process process, IntPtr addr, bool is64Bit, out IntPtr val)
{
var bytes = new byte[is64Bit ? 8 : 4];
SizeT read;
val = IntPtr.Zero;
if (!WinAPI.ReadProcessMemory(process.Handle, addr, bytes, (SizeT)bytes.Length, out read)
|| read != (SizeT)bytes.Length)
return false;
val = is64Bit ? (IntPtr)BitConverter.ToInt64(bytes, 0) : (IntPtr)BitConverter.ToUInt32(bytes, 0);
return true;
}
public static bool ReadString(this Process process, IntPtr addr, int numBytes, out string str)
{
return ReadString(process, addr, ReadStringType.AutoDetect, numBytes, out str);
}
public static bool ReadString(this Process process, IntPtr addr, ReadStringType type, int numBytes, out string str)
{
var sb = new StringBuilder(numBytes);
if (!ReadString(process, addr, type, sb))
{
str = string.Empty;
return false;
}
str = sb.ToString();
return true;
}
public static bool ReadString(this Process process, IntPtr addr, StringBuilder sb)
{
return ReadString(process, addr, ReadStringType.AutoDetect, sb);
}
public static bool ReadString(this Process process, IntPtr addr, ReadStringType type, StringBuilder sb)
{
var bytes = new byte[sb.Capacity];
SizeT read;
if (!WinAPI.ReadProcessMemory(process.Handle, addr, bytes, (SizeT)bytes.Length, out read)
|| read != (SizeT)bytes.Length)
return false;
if (type == ReadStringType.AutoDetect)
{
if (read.ToUInt64() >= 2 && bytes[1] == '\x0')
sb.Append(Encoding.Unicode.GetString(bytes));
else
sb.Append(Encoding.UTF8.GetString(bytes));
}
else if (type == ReadStringType.UTF8)
sb.Append(Encoding.UTF8.GetString(bytes));
else if (type == ReadStringType.UTF16)
sb.Append(Encoding.Unicode.GetString(bytes));
else
sb.Append(Encoding.ASCII.GetString(bytes));
for (int i = 0; i < sb.Length; i++)
{
if (sb[i] == '\0')
{
sb.Remove(i, sb.Length - i);
break;
}
}
return true;
}
public static T ReadValue<T>(this Process process, IntPtr addr, T default_ = default(T)) where T : struct
{
T val;
if (!process.ReadValue(addr, out val))
val = default_;
return val;
}
public static byte[] ReadBytes(this Process process, IntPtr addr, int count)
{
byte[] bytes;
if (!process.ReadBytes(addr, count, out bytes))
return null;
return bytes;
}
public static IntPtr ReadPointer(this Process process, IntPtr addr, IntPtr default_ = default(IntPtr))
{
IntPtr ptr;
if (!process.ReadPointer(addr, out ptr))
return default_;
return ptr;
}
public static string ReadString(this Process process, IntPtr addr, int numBytes, string default_ = null)
{
string str;
if (!process.ReadString(addr, numBytes, out str))
return default_;
return str;
}
public static string ReadString(this Process process, IntPtr addr, ReadStringType type, int numBytes, string default_ = null)
{
string str;
if (!process.ReadString(addr, type, numBytes, out str))
return default_;
return str;
}
public static bool WriteValue<T>(this Process process, IntPtr addr, T obj) where T : struct
{
int size = Marshal.SizeOf(obj);
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return process.WriteBytes(addr, arr);
}
public static bool WriteBytes(this Process process, IntPtr addr, byte[] bytes)
{
SizeT written;
if (!WinAPI.WriteProcessMemory(process.Handle, addr, bytes, (SizeT)bytes.Length, out written)
|| written != (SizeT)bytes.Length)
return false;
return true;
}
private static bool WriteJumpOrCall(Process process, IntPtr addr, IntPtr dest, bool call)
{
var x64 = process.Is64Bit();
int jmpLen = x64 ? 12 : 5;
var instruction = new List<byte>(jmpLen);
if (x64)
{
instruction.AddRange(new byte[] { 0x48, 0xB8 }); // mov rax immediate
instruction.AddRange(BitConverter.GetBytes((long)dest));
instruction.AddRange(new byte[] { 0xFF, call ? (byte)0xD0 : (byte)0xE0 }); // jmp/call rax
}
else
{
int offset = unchecked((int)dest - (int)(addr + jmpLen));
instruction.AddRange(new byte[] { call ? (byte)0xE8 : (byte)0xE9 }); // jmp/call immediate
instruction.AddRange(BitConverter.GetBytes(offset));
}
MemPageProtect oldProtect;
process.VirtualProtect(addr, jmpLen, MemPageProtect.PAGE_EXECUTE_READWRITE, out oldProtect);
bool success = process.WriteBytes(addr, instruction.ToArray());
process.VirtualProtect(addr, jmpLen, oldProtect);
return success;
}
public static bool WriteJumpInstruction(this Process process, IntPtr addr, IntPtr dest)
{
return WriteJumpOrCall(process, addr, dest, false);
}
public static bool WriteCallInstruction(this Process process, IntPtr addr, IntPtr dest)
{
return WriteJumpOrCall(process, addr, dest, true);
}
public static IntPtr WriteDetour(this Process process, IntPtr src, int overwrittenBytes, IntPtr dest)
{
int jmpLen = process.Is64Bit() ? 12 : 5;
if (overwrittenBytes < jmpLen)
throw new ArgumentOutOfRangeException(nameof(overwrittenBytes),
$"must be >= length of jmp instruction ({jmpLen})");
// allocate memory to store the original src prologue bytes we overwrite with jump to dest
// along with the jump back to src
IntPtr gate;
if ((gate = process.AllocateMemory(jmpLen + overwrittenBytes)) == IntPtr.Zero)
throw new Win32Exception();
try
{
// read the original bytes from the prologue of src
var origSrcBytes = process.ReadBytes(src, overwrittenBytes);
if (origSrcBytes == null)
throw new Win32Exception();
// write the original prologue of src into the start of gate
if (!process.WriteBytes(gate, origSrcBytes))
throw new Win32Exception();
// write the jump from the end of the gate back to src
if (!process.WriteJumpInstruction(gate + overwrittenBytes, src + overwrittenBytes))
throw new Win32Exception();
// finally write the jump from src to dest
if (!process.WriteJumpInstruction(src, dest))
throw new Win32Exception();
// nop the leftover bytes in the src prologue
int extraBytes = overwrittenBytes - jmpLen;
if (extraBytes > 0)
{
var nops = Enumerable.Repeat((byte)0x90, extraBytes).ToArray();
MemPageProtect oldProtect;
if (!process.VirtualProtect(src + jmpLen, nops.Length, MemPageProtect.PAGE_EXECUTE_READWRITE,
out oldProtect))
throw new Win32Exception();
if (!process.WriteBytes(src + jmpLen, nops))
throw new Win32Exception();
process.VirtualProtect(src + jmpLen, nops.Length, oldProtect);
}
}
catch
{
process.FreeMemory(gate);
throw;
}
return gate;
}
static object ResolveToType(byte[] bytes, Type type)
{
object val;
if (type == typeof(int))
{
val = BitConverter.ToInt32(bytes, 0);
}
else if (type == typeof(uint))
{
val = BitConverter.ToUInt32(bytes, 0);
}
else if (type == typeof(float))
{
val = BitConverter.ToSingle(bytes, 0);
}
else if (type == typeof(double))
{
val = BitConverter.ToDouble(bytes, 0);
}
else if (type == typeof(byte))
{
val = bytes[0];
}
else if (type == typeof(bool))
{
if (bytes == null)
val = false;
else
val = (bytes[0] != 0);
}
else if (type == typeof(short))
{
val = BitConverter.ToInt16(bytes, 0);
}
else // probably a struct
{
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
val = Marshal.PtrToStructure(handle.AddrOfPinnedObject(), type);
}
finally
{
handle.Free();
}
}
return val;
}
public static IntPtr AllocateMemory(this Process process, int size)
{
return WinAPI.VirtualAllocEx(process.Handle, IntPtr.Zero, (SizeT)size, (uint)MemPageState.MEM_COMMIT,
MemPageProtect.PAGE_EXECUTE_READWRITE);
}
public static bool FreeMemory(this Process process, IntPtr addr)
{
const uint MEM_RELEASE = 0x8000;
return WinAPI.VirtualFreeEx(process.Handle, addr, SizeT.Zero, MEM_RELEASE);
}
public static bool VirtualProtect(this Process process, IntPtr addr, int size, MemPageProtect protect,
out MemPageProtect oldProtect)
{
return WinAPI.VirtualProtectEx(process.Handle, addr, (SizeT)size, protect, out oldProtect);
}
public static bool VirtualProtect(this Process process, IntPtr addr, int size, MemPageProtect protect)
{
MemPageProtect oldProtect;
return WinAPI.VirtualProtectEx(process.Handle, addr, (SizeT)size, protect, out oldProtect);
}
public static IntPtr CreateThread(this Process process, IntPtr startAddress, IntPtr parameter)
{
IntPtr threadId;
return WinAPI.CreateRemoteThread(process.Handle, IntPtr.Zero, (SizeT)0, startAddress, parameter, 0,
out threadId);
}
public static IntPtr CreateThread(this Process process, IntPtr startAddress)
{
return CreateThread(process, startAddress, IntPtr.Zero);
}
public static void Suspend(this Process process)
{
WinAPI.NtSuspendProcess(process.Handle);
}
public static void Resume(this Process process)
{
WinAPI.NtResumeProcess(process.Handle);
}
public static float ToFloatBits(this uint i)
{
return BitConverter.ToSingle(BitConverter.GetBytes(i), 0);
}
public static uint ToUInt32Bits(this float f)
{
return BitConverter.ToUInt32(BitConverter.GetBytes(f), 0);
}
public static bool BitEquals(this float f, float o)
{
return ToUInt32Bits(f) == ToUInt32Bits(o);
}
}
}