forked from DaxxTrias/Process.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessMemory.cs
More file actions
283 lines (252 loc) · 11.7 KB
/
ProcessMemory.cs
File metadata and controls
283 lines (252 loc) · 11.7 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
using System;
using System.Collections.Concurrent;
using System.Text;
using ProcessNET.Extensions;
using ProcessNET.Native;
using ProcessNET.Native.Types;
namespace ProcessNET.Memory
{
/// <summary>
/// Class for memory editing a process.
/// </summary>
/// <seealso cref="IMemory" />
public abstract class ProcessMemory : IMemory
{
/// <summary>
/// The open handle to the process which contains the memory of interest,
/// </summary>
protected readonly SafeMemoryHandle Handle;
/// <summary>
/// Runtime types names based on their addresses.
/// </summary>
protected ConcurrentDictionary<IntPtr, string> rttiCache = null;
protected readonly bool is32Bit = false;
/// <summary>
/// Returns the type of the processor architecture of the process.
/// </summary>
public bool Is32Bit => is32Bit;
/// <summary>
/// Initializes a new instance of the <see cref="ProcessMemory" /> class.
/// </summary>
/// <param name="handle">The open handle to the process which contains the memory of interest.</param>
protected ProcessMemory(SafeMemoryHandle handle)
{
Handle = handle;
is32Bit = Kernel32.Is32BitProcess(handle.DangerousGetHandle());
rttiCache = new ConcurrentDictionary<IntPtr, string>();
}
/// <summary>
/// Writes a set of bytes to memory.
/// </summary>
/// <param name="intPtr">The address where the bytes start in memory.</param>
/// <param name="length">The length of the byte chunk to read from the memory address.</param>
/// <returns>
/// The byte array section read from memory.
/// </returns>
public abstract byte[] Read(IntPtr intPtr, int length);
/// <summary>
/// Writes a set of bytes to memory.
/// </summary>
/// <param name="intPtr">The address where the bytes start in memory.</param>
/// <param name="buffer">The buffer to read data onto, it's size determines the count of bytes to read.</param>
public abstract void Read(IntPtr intPtr, byte[] buffer);
/// <summary>
/// Reads a string with a specified encoding from memory.
/// </summary>
/// <param name="intPtr">The address where the string is read.</param>
/// <param name="encoding">The encoding used.</param>
/// <param name="maxLength">
/// The number of maximum bytes to read. The string is automatically cropped at this end ('\0'
/// char).
/// </param>
/// <returns>The string.</returns>
public string ReadString(IntPtr intPtr, Encoding encoding, int maxLength = 512)
{
var buffer = Read(intPtr, maxLength);
var ret = encoding.GetString(buffer);
if (ret.IndexOf('\0') != -1)
ret = ret.Remove(ret.IndexOf('\0'));
return ret;
}
/// <summary>
/// Reads the value of a specified type from memory.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="intPtr">The address where the value is read.</param>
/// <returns>A value.</returns>
public abstract T Read<T>(IntPtr intPtr);
/// <summary>
/// Reads an array of a specified type from memory.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <param name="intPtr">The address where the values is read.</param>
/// <param name="length">The number of cells in the array.</param>
/// <returns>An array.</returns>
public T[] Read<T>(IntPtr intPtr, int length)
{
var buffer = new T[length];
for (var i = 0; i < buffer.Length; i++)
buffer[i] = Read<T>(intPtr);
return buffer;
}
/// <summary>
/// Write an array of bytes in the remote process.
/// </summary>
/// <param name="intPtr">The address where the array is written.</param>
/// <param name="bytesToWrite">The array of bytes to write.</param>
public abstract int Write(IntPtr intPtr, byte[] bytesToWrite);
/// <summary>
/// Writes a string with a specified encoding to memory.
/// </summary>
/// <param name="intPtr">The address where the string is written.</param>
/// <param name="stringToWrite">The text to write.</param>
/// <param name="encoding">The encoding used.</param>
public virtual void WriteString(IntPtr intPtr, string stringToWrite, Encoding encoding)
{
if (stringToWrite[stringToWrite.Length - 1] != '\0')
stringToWrite += '\0';
var bytes = encoding.GetBytes(stringToWrite);
Write(intPtr, bytes);
}
/// <summary>
/// Writes an array of a specified type to memory,
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <param name="intPtr">The address where the values is written.</param>
/// <param name="values">The array to write.</param>
public void Write<T>(IntPtr intPtr, T[] values)
{
foreach (var value in values)
Write(intPtr, value);
}
/// <summary>
/// Writes the values of a specified type to memory.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="intPtr">The address where the value is written.</param>
/// <param name="value">The value to write.</param>
public abstract void Write<T>(IntPtr intPtr, T value);
/// <summary>
/// Reads the Runtime Type Information (RTTI) at certain address.
/// </summary>
/// <param name="address">Address to read the type from.</param>
/// <returns>Runtime Type Information of the object at the passed address or null if it's not an object.</returns>
public string ReadRemoteRuntimeTypeInformation(IntPtr address)
{
if (address.MayBeValid())
{
if(!rttiCache.TryGetValue(address, out string rtti))
{
var objectLocatorPtr = Read<IntPtr>(address - (Is32Bit ? 4 : 8));
if (objectLocatorPtr.MayBeValid())
{
if (is32Bit)
rtti = ReadRemoteRuntimeTypeInformation32(objectLocatorPtr);
else
rtti = ReadRemoteRuntimeTypeInformation64(objectLocatorPtr);
rttiCache.AddOrUpdate(address, rtti, (oldK, oldV) => rtti);
}
}
return rtti;
}
return null;
}
private string ReadRemoteRuntimeTypeInformation32(IntPtr address)
{
var classHierarchyDescriptorPtr = Read<IntPtr>(address + 0x10);
if (classHierarchyDescriptorPtr.MayBeValid())
{
var baseClassCount = Read<int>(classHierarchyDescriptorPtr + 8);
if (baseClassCount > 0 && baseClassCount < 25)
{
var baseClassArrayPtr = Read<IntPtr>(classHierarchyDescriptorPtr + 0xC);
if (baseClassArrayPtr.MayBeValid())
{
var sb = new StringBuilder();
for (var i = 0; i < baseClassCount; ++i)
{
var baseClassDescriptorPtr = Read<IntPtr>(baseClassArrayPtr + (4 * i));
if (baseClassDescriptorPtr.MayBeValid())
{
var typeDescriptorPtr = Read<IntPtr>(baseClassDescriptorPtr);
if (typeDescriptorPtr.MayBeValid())
{
var name = ReadString(typeDescriptorPtr + 0x0C, Encoding.UTF8, 60);
if (name.EndsWith("@@"))
{
name = DbgHelp.UnDecorateSymbolName($"?{name}", UnDecorateFlags.UNDNAME_NAME_ONLY);
}
sb.Append(name);
sb.Append(" : ");
continue;
}
}
break;
}
if (sb.Length != 0)
{
sb.Length -= 3;
return sb.ToString();
}
}
}
}
return null;
}
private string ReadRemoteRuntimeTypeInformation64(IntPtr address)
{
int baseOffset = Read<int>(address + 0x14);
if (baseOffset != 0)
{
var baseAddress = address - baseOffset;
var classHierarchyDescriptorOffset = Read<int>(address + 0x10);
if (classHierarchyDescriptorOffset != 0)
{
var classHierarchyDescriptorPtr = baseAddress + classHierarchyDescriptorOffset;
var baseClassCount = Read<int>(classHierarchyDescriptorPtr + 0x08);
if (baseClassCount > 0 && baseClassCount < 25)
{
var baseClassArrayOffset = Read<int>(classHierarchyDescriptorPtr + 0x0C);
if (baseClassArrayOffset != 0)
{
var baseClassArrayPtr = baseAddress + baseClassArrayOffset;
var sb = new StringBuilder();
for (var i = 0; i < baseClassCount; ++i)
{
var baseClassDescriptorOffset = Read<int>(baseClassArrayPtr + (4 * i));
if (baseClassDescriptorOffset != 0)
{
var baseClassDescriptorPtr = baseAddress + baseClassDescriptorOffset;
var typeDescriptorOffset = Read<int>(baseClassDescriptorPtr);
if (typeDescriptorOffset != 0)
{
var typeDescriptorPtr = baseAddress + typeDescriptorOffset;
var name = ReadString(typeDescriptorPtr + 0x14, Encoding.UTF8, 60);
if (string.IsNullOrEmpty(name))
{
break;
}
if (name.EndsWith("@@"))
{
name = DbgHelp.UnDecorateSymbolName($"?{name}", UnDecorateFlags.UNDNAME_NAME_ONLY);
}
sb.Append(name);
sb.Append(" : ");
continue;
}
}
break;
}
if (sb.Length != 0)
{
sb.Length -= 3;
return sb.ToString();
}
}
}
}
}
return null;
}
}
}