-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathNodeDissector.cs
More file actions
178 lines (147 loc) · 4.36 KB
/
NodeDissector.cs
File metadata and controls
178 lines (147 loc) · 4.36 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
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using ReClassNET.Extensions;
using ReClassNET.Nodes;
namespace ReClassNET.Memory
{
public class NodeDissector
{
public static void DissectNodes(IEnumerable<BaseHexNode> nodes, IProcessReader reader, MemoryBuffer memory)
{
Contract.Requires(nodes != null);
Contract.Requires(Contract.ForAll(nodes, n => n != null));
Contract.Requires(memory != null);
foreach (var node in nodes)
{
if (GuessNode(node, reader, memory, out var guessedNode))
{
node.GetParentContainer()?.ReplaceChildNode(node, guessedNode);
}
}
}
public static bool GuessNode(BaseHexNode node, IProcessReader reader, MemoryBuffer memory, out BaseNode guessedNode)
{
Contract.Requires(node != null);
Contract.Requires(memory != null);
guessedNode = null;
var offset = node.Offset;
var is4ByteAligned = offset % 4 == 0;
var is8ByteAligned = offset % 8 == 0;
// The node is not aligned, skip it.
if (!is4ByteAligned)
{
return false;
}
var data64 = new UInt64FloatDoubleData
{
Raw1 = memory.ReadInt32(offset),
Raw2 = memory.ReadInt32(offset + sizeof(int))
}; ;
var data32 = new UInt32FloatData
{
Raw = memory.ReadInt32(offset)
};
var raw = memory.ReadBytes(offset, node.MemorySize);
if (raw.InterpretAsSingleByteCharacter().IsLikelyPrintableData())
{
guessedNode = new Utf8TextNode();
return true;
}
if (raw.InterpretAsDoubleByteCharacter().IsLikelyPrintableData())
{
guessedNode = new Utf16TextNode();
return true;
}
#if RECLASSNET64
if (is8ByteAligned)
{
if (GuessPointerNode(data64.IntPtr, reader, out guessedNode))
{
return true;
}
}
#else
if (GuessPointerNode(data32.IntPtr, reader, out guessedNode))
{
return true;
}
#endif
// 0 could be anything.
if (data32.IntValue != 0)
{
// If the data represents a reasonable range, it could be a float.
if (-999999.0f <= data32.FloatValue && data32.FloatValue <= 999999.0f && !data32.FloatValue.IsNearlyEqual(0.0f, 0.001f))
{
guessedNode = new FloatNode();
return true;
}
if (-999999 <= data32.IntValue && data32.IntValue <= 999999)
{
guessedNode = new Int32Node();
return true;
}
}
if (is8ByteAligned)
{
if (data64.LongValue != 0)
{
// If the data represents a reasonable range, it could be a double.
if (-999999.0 <= data64.DoubleValue && data64.DoubleValue <= 999999.0 && !data64.DoubleValue.IsNearlyEqual(0.0, 0.001))
{
guessedNode = new DoubleNode();
return true;
}
}
}
return false;
}
private static bool GuessPointerNode(IntPtr address, IProcessReader process, out BaseNode node)
{
Contract.Requires(process != null);
node = null;
if (address.IsNull())
{
return false;
}
var section = process.GetSectionToPointer(address);
if (section == null)
{
return false;
}
if (section.Category == SectionCategory.CODE) // If the section contains code, it should be a function pointer.
{
node = new FunctionPtrNode();
return true;
}
if (section.Category == SectionCategory.DATA || section.Category == SectionCategory.HEAP) // If the section contains data, it is at least a pointer to a class or something.
{
// Check if it is a vtable. Check if the first 3 values are pointers to a code section.
if (process.GetSectionToPointer(process.ReadRemoteIntPtr(address))?.Category == SectionCategory.CODE
&& process.GetSectionToPointer(process.ReadRemoteIntPtr(address + IntPtr.Size))?.Category == SectionCategory.CODE
&& process.GetSectionToPointer(process.ReadRemoteIntPtr(address + 2 * IntPtr.Size))?.Category == SectionCategory.CODE)
{
node = new VirtualMethodTableNode();
return true;
}
// Check if it is a string.
var data = process.ReadRemoteMemory(address, IntPtr.Size * 2);
if (data.Take(IntPtr.Size).InterpretAsSingleByteCharacter().IsLikelyPrintableData())
{
node = new Utf8TextPtrNode();
return true;
}
if (data.InterpretAsDoubleByteCharacter().IsLikelyPrintableData())
{
node = new Utf16TextPtrNode();
return true;
}
// Now it could be a pointer to something else but we can't tell. :(
node = new PointerNode();
return true;
}
return false;
}
}
}