|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.Contracts; |
| 4 | +using ReClassNET.Nodes; |
| 5 | +using ReClassNET.Util; |
| 6 | + |
| 7 | +namespace ReClassNET.Memory |
| 8 | +{ |
| 9 | + public class NodeDissector |
| 10 | + { |
| 11 | + public static void DissectNodes(IEnumerable<BaseHexNode> nodes, MemoryBuffer memory) |
| 12 | + { |
| 13 | + Contract.Requires(nodes != null); |
| 14 | + Contract.Requires(memory != null); |
| 15 | + |
| 16 | + foreach (var node in nodes) |
| 17 | + { |
| 18 | + var type = GuessType(node, memory); |
| 19 | + if (type != null) |
| 20 | + { |
| 21 | + node.ParentNode.ReplaceChildNode(node, type); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public static Type GuessType(BaseHexNode node, MemoryBuffer memory) |
| 27 | + { |
| 28 | + Contract.Requires(node != null); |
| 29 | + Contract.Requires(memory != null); |
| 30 | + |
| 31 | + var offset = node.Offset.ToInt32(); |
| 32 | + var is4ByteAligned = offset % 4 == 0; |
| 33 | + var is8ByteAligned = offset % 8 == 0; |
| 34 | + |
| 35 | + // The node is not aligned, skip it. |
| 36 | + if (!is4ByteAligned) |
| 37 | + { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + var data64 = memory.ReadObject<UInt64FloatDoubleData>(offset); |
| 42 | + var data32 = memory.ReadObject<UInt32FloatData>(offset); |
| 43 | + |
| 44 | + /*var raw = memory.ReadBytes(offset, node.MemorySize); |
| 45 | + if (raw.IsPrintableData()) |
| 46 | + { |
| 47 | + return typeof(UTF8TextNode); |
| 48 | + } |
| 49 | + else if (raw.EveryNth(2).IsPrintableData()) |
| 50 | + { |
| 51 | + return typeof(UTF16TextNode); |
| 52 | + }*/ |
| 53 | + |
| 54 | + if (is8ByteAligned) |
| 55 | + { |
| 56 | +#if WIN64 |
| 57 | + var pointerType = GuessPointerType(data64.IntPtr, memory); |
| 58 | + if (pointerType != null) |
| 59 | + { |
| 60 | + return pointerType; |
| 61 | + } |
| 62 | +#endif |
| 63 | + } |
| 64 | + |
| 65 | + if (is4ByteAligned) |
| 66 | + { |
| 67 | +#if WIN32 |
| 68 | + var pointerType = GuessPointerType(data32.IntPtr, memory); |
| 69 | + if (pointerType != null) |
| 70 | + { |
| 71 | + return pointerType; |
| 72 | + } |
| 73 | +#endif |
| 74 | + |
| 75 | + // 0 could be anything. |
| 76 | + if (data32.IntValue != 0) |
| 77 | + { |
| 78 | + // If the data represents a reasonable range, it could be a float. |
| 79 | + if (-99999.0f <= data32.FloatValue && data32.FloatValue <= 99999.0f && !data32.FloatValue.IsNearlyEqual(0.0f, 0.001f)) |
| 80 | + { |
| 81 | + return typeof(FloatNode); |
| 82 | + } |
| 83 | + |
| 84 | + if (-99999 <= data32.IntValue && data32.IntValue <= 99999) |
| 85 | + { |
| 86 | + return typeof(Int32Node); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (is8ByteAligned) |
| 92 | + { |
| 93 | + if (data64.LongValue != 0) |
| 94 | + { |
| 95 | + // If the data represents a reasonable range, it could be a double. |
| 96 | + if (-99999.0 <= data64.DoubleValue && data64.DoubleValue <= 99999.0 && !data64.DoubleValue.IsNearlyEqual(0.0, 0.001)) |
| 97 | + { |
| 98 | + return typeof(DoubleNode); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + private static Type GuessPointerType(IntPtr address, MemoryBuffer memory) |
| 107 | + { |
| 108 | + Contract.Requires(memory != null); |
| 109 | + |
| 110 | + if (address.IsNull()) |
| 111 | + { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + var section = memory.Process.GetSectionToPointer(address); |
| 116 | + if (section != null) // If the address points to a section it's valid memory. |
| 117 | + { |
| 118 | + if (section.Category == RemoteProcess.SectionCategory.Code) // If the section contains code, it should be a function pointer. |
| 119 | + { |
| 120 | + return typeof(FunctionPtrNode); |
| 121 | + } |
| 122 | + else if (section.Category == RemoteProcess.SectionCategory.Data) // If the section contains data, it is at least a pointer to a class or something. |
| 123 | + { |
| 124 | + // Check if it is a vtable. Check if the first 3 values are pointers to a code section. |
| 125 | + bool valid = true; |
| 126 | + for (var i = 0; i < 3; ++i) |
| 127 | + { |
| 128 | + var pointee = memory.Process.ReadRemoteObject<IntPtr>(address); |
| 129 | + if (memory.Process.GetSectionToPointer(pointee)?.Category != RemoteProcess.SectionCategory.Code) |
| 130 | + { |
| 131 | + valid = false; |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + if (valid) |
| 136 | + { |
| 137 | + return typeof(VTableNode); |
| 138 | + } |
| 139 | + |
| 140 | + // Check if it is a string. |
| 141 | + var data = memory.Process.ReadRemoteMemory(address, IntPtr.Size); |
| 142 | + if (data.IsPrintableData()) |
| 143 | + { |
| 144 | + return typeof(UTF8TextPtrNode); |
| 145 | + } |
| 146 | + else if (data.EveryNth(2).IsPrintableData()) |
| 147 | + { |
| 148 | + return typeof(UTF16TextPtrNode); |
| 149 | + } |
| 150 | + /*else if (!data.EveryNth(4).Where(b => !((char)b).IsPrintable()).Any()) |
| 151 | + { |
| 152 | + return typeof(UTF32TextPtrNode); |
| 153 | + }*/ |
| 154 | + |
| 155 | + // Now it could be a pointer to something else but we can't tell. :( |
| 156 | + //return typeof(ClassPtrNode); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return null; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments