|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +using Windows.Win32; |
| 6 | +using Windows.Win32.Foundation; |
| 7 | +using Windows.Win32.System.Threading; |
| 8 | +using Windows.Win32.System.Memory; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Threading; |
| 12 | +using System.Runtime.InteropServices; |
| 13 | + |
| 14 | +namespace TheLeftExit.Trickster.Memory { |
| 15 | + public class TricksterException : Exception { } |
| 16 | + |
| 17 | + public record struct TypeInfo(string Name, nuint Address, nuint Offset) { |
| 18 | + public override string ToString() { |
| 19 | + return $"{Name} - {Offset:X}"; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public unsafe struct MemoryRegionInfo { |
| 24 | + public void* BaseAddress; |
| 25 | + public nuint Size; |
| 26 | + public MemoryRegionInfo(void* baseAddress, nuint size) { BaseAddress = baseAddress; Size = size; } |
| 27 | + } |
| 28 | + |
| 29 | + public unsafe struct MemoryRegion { |
| 30 | + public void* Pointer; |
| 31 | + public void* BaseAddress; |
| 32 | + public nuint Size; |
| 33 | + public MemoryRegion(void* pointer, void* baseAddress, nuint size) { Pointer = pointer; BaseAddress = baseAddress; Size = size; } |
| 34 | + } |
| 35 | + |
| 36 | + public unsafe class Trickster : IDisposable { |
| 37 | + private HANDLE _processHandle; |
| 38 | + |
| 39 | + private nuint _mainModuleBaseAddress; |
| 40 | + private nuint _mainModuleSize; |
| 41 | + private bool _is32Bit; |
| 42 | + |
| 43 | + public TypeInfo[] ScannedTypes; |
| 44 | + public MemoryRegion[] Regions; |
| 45 | + |
| 46 | + public Trickster(Process process) { |
| 47 | + _processHandle = Kernel32.OpenProcess(PROCESS_ACCESS_RIGHTS.PROCESS_ALL_ACCESS, true, (uint)process.Id); |
| 48 | + if (_processHandle.IsNull) throw new TricksterException(); |
| 49 | + |
| 50 | + _mainModuleBaseAddress = (nuint)process.MainModule.BaseAddress.ToPointer(); |
| 51 | + _mainModuleSize = (nuint)process.MainModule.ModuleMemorySize; |
| 52 | + |
| 53 | + BOOL is32Bit; Kernel32.IsWow64Process(_processHandle, &is32Bit); _is32Bit = is32Bit; |
| 54 | + } |
| 55 | + |
| 56 | + private TypeInfo[] ScanTypesCore() { |
| 57 | + List<TypeInfo> list = new(); |
| 58 | + |
| 59 | + using (RttiScanner processMemory = new(_processHandle, _mainModuleBaseAddress, _mainModuleSize)) { |
| 60 | + nuint inc = (nuint)(_is32Bit ? 4 : 8); |
| 61 | + Func<ulong, string> getClassName = _is32Bit ? processMemory.GetClassName32 : processMemory.GetClassName64; |
| 62 | + for (nuint offset = inc; offset < _mainModuleSize; offset += inc) { |
| 63 | + nuint address = _mainModuleBaseAddress + offset; |
| 64 | + if (getClassName(address) is string className) { |
| 65 | + list.Add(new TypeInfo(className, address, offset)); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return list.ToArray(); |
| 71 | + } |
| 72 | + |
| 73 | + private MemoryRegionInfo[] ScanRegionInfoCore() { |
| 74 | + ulong stop = _is32Bit ? uint.MaxValue : 0x7ffffffffffffffful; |
| 75 | + nuint size = (nuint)sizeof(MEMORY_BASIC_INFORMATION); |
| 76 | + |
| 77 | + List<MemoryRegionInfo> list = new(); |
| 78 | + |
| 79 | + MEMORY_BASIC_INFORMATION mbi; |
| 80 | + nuint address = 0; |
| 81 | + |
| 82 | + |
| 83 | + while (address < stop && Kernel32.VirtualQueryEx(_processHandle, (void*)address, &mbi, size) > 0 && address + mbi.RegionSize > address) { |
| 84 | + if (mbi.State == VIRTUAL_ALLOCATION_TYPE.MEM_COMMIT && |
| 85 | + !mbi.Protect.HasFlag(PAGE_PROTECTION_FLAGS.PAGE_NOACCESS) && |
| 86 | + !mbi.Protect.HasFlag(PAGE_PROTECTION_FLAGS.PAGE_GUARD) && |
| 87 | + !mbi.Protect.HasFlag(PAGE_PROTECTION_FLAGS.PAGE_NOCACHE)) |
| 88 | + list.Add(new MemoryRegionInfo(mbi.BaseAddress, mbi.RegionSize)); |
| 89 | + address += mbi.RegionSize; |
| 90 | + } |
| 91 | + |
| 92 | + return list.ToArray(); |
| 93 | + } |
| 94 | + |
| 95 | + private MemoryRegion[] ReadRegionsCore(MemoryRegionInfo[] infoArray) { |
| 96 | + MemoryRegion[] regionArray = new MemoryRegion[infoArray.Length]; |
| 97 | + for(int i = 0; i < regionArray.Length; i++) { |
| 98 | + void* baseAddress = infoArray[i].BaseAddress; |
| 99 | + nuint size = infoArray[i].Size; |
| 100 | + void* pointer = NativeMemory.Alloc(size); |
| 101 | + Kernel32.ReadProcessMemory(_processHandle, baseAddress, pointer, size); |
| 102 | + regionArray[i] = new(pointer, baseAddress, size); |
| 103 | + } |
| 104 | + return regionArray; |
| 105 | + } |
| 106 | + |
| 107 | + private void FreeRegionsCore(MemoryRegion[] regionArray) { |
| 108 | + for(int i = 0; i < regionArray.Length; i++) { |
| 109 | + NativeMemory.Free(regionArray[i].Pointer); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private ulong[] ScanRegionsCore(MemoryRegion[] regionArray, ulong value) { |
| 114 | + List<ulong> list = new(); |
| 115 | + |
| 116 | + Parallel.For(0, regionArray.Length, i => { |
| 117 | + MemoryRegion region = regionArray[i]; |
| 118 | + byte* start = (byte*)region.Pointer; |
| 119 | + byte* end = start + region.Size; |
| 120 | + if (_is32Bit) { |
| 121 | + for (byte* a = start; a < end; a += 4) |
| 122 | + if (*(uint*)a == value) |
| 123 | + lock (list) { |
| 124 | + ulong result = (ulong)region.BaseAddress + (ulong)(a - start); |
| 125 | + list.Add(result); |
| 126 | + } |
| 127 | + } else { |
| 128 | + for (byte* a = start; a < end; a += 8) |
| 129 | + if (*(ulong*)a == value) |
| 130 | + lock (list) { |
| 131 | + ulong result = (ulong)region.BaseAddress + (ulong)(a - start); |
| 132 | + list.Add(result); |
| 133 | + } |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + return list.ToArray(); |
| 138 | + } |
| 139 | + |
| 140 | + public void ScanTypes() { |
| 141 | + ScannedTypes = ScanTypesCore(); |
| 142 | + } |
| 143 | + |
| 144 | + public void ReadRegions() { |
| 145 | + if(Regions is not null) { |
| 146 | + FreeRegionsCore(Regions); |
| 147 | + } |
| 148 | + |
| 149 | + MemoryRegionInfo[] scannedRegions = ScanRegionInfoCore(); |
| 150 | + Regions = ReadRegionsCore(scannedRegions); |
| 151 | + } |
| 152 | + |
| 153 | + public ulong[] ScanRegions(ulong value) { |
| 154 | + return ScanRegionsCore(Regions, value); |
| 155 | + } |
| 156 | + |
| 157 | + public void Dispose() { |
| 158 | + if (Regions is not null) { |
| 159 | + FreeRegionsCore(Regions); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments