/
MemoryPointer.cs
362 lines (327 loc) · 14.2 KB
/
MemoryPointer.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
using System;
using System.Text;
using Process.NET.Native.Types;
namespace Process.NET.Memory
{
/// <summary>
/// Class representing a pointer in the memory of the remote process.
/// </summary>
public class MemoryPointer : IEquatable<MemoryPointer>, IPointer
{
/// <summary>
/// Initializes a new instance of the <see cref="MemoryPointer" /> class.
/// </summary>
/// <param name="process">The reference of the <see cref="IProcess"></see></param>
/// <param name="address">The location where the pointer points in the remote process.</param>
public MemoryPointer(IProcess process, IntPtr address)
{
// Save the parameters
Process = process;
BaseAddress = address;
}
/// <summary>
/// The reference of the <see cref="IMemory" /> object.
/// </summary>
public IProcess Process { get; protected set; }
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
/// </summary>
public bool Equals(MemoryPointer other)
{
if (ReferenceEquals(null, other)) return false;
return ReferenceEquals(this, other) ||
(BaseAddress.Equals(other.BaseAddress) && Process.Equals(other.Process));
}
/// <summary>
/// The address of the pointer in the remote process.
/// </summary>
public IntPtr BaseAddress { get; protected set; }
/// <summary>
/// Gets if the <see cref="MemoryPointer" /> is valid.
/// </summary>
public virtual bool IsValid => BaseAddress != IntPtr.Zero;
public byte[] Read(int offset, int length)
{
return Process.Memory.Read(BaseAddress + offset, length);
}
/// <summary>
/// Reads the value of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="offset">The offset where the value is read from the pointer.</param>
/// <returns>A value.</returns>
public T Read<T>(int offset)
{
return Process.Memory.Read<T>(BaseAddress + offset);
}
/// <summary>
/// Reads an array of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <param name="offset">The offset where the values is read from the pointer.</param>
/// <param name="count">The number of cells in the array.</param>
/// <returns>An array.</returns>
public T[] Read<T>(int offset, int count)
{
return Process.Memory.Read<T>(BaseAddress + offset, count);
}
public int Write(int offset, byte[] toWrite)
{
return Process.Memory.Write(BaseAddress + offset, toWrite);
}
/// <summary>
/// Reads a string with a specified encoding in the remote process.
/// </summary>
/// <param name="offset">The offset where the string is read from the pointer.</param>
/// <param name="encoding">The encoding used.</param>
/// <param name="maxLength">
/// [Optional] The number of maximum bytes to read. The string is automatically cropped at this end
/// ('\0' char).
/// </param>
/// <returns>The string.</returns>
public string Read(int offset, Encoding encoding, int maxLength = 512)
{
return Process.Memory.Read(BaseAddress + offset, encoding, maxLength);
}
/// <summary>
/// Writes the values of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="offset">The offset where the value is written from the pointer.</param>
/// <param name="value">The value to write.</param>
public void Write<T>(int offset, T value)
{
Process.Memory.Write(BaseAddress + offset, value);
}
/// <summary>
/// Writes an array of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <param name="offset">The offset where the values is written from the pointer.</param>
/// <param name="array">The array to write.</param>
public void Write<T>(int offset, T[] array)
{
Process.Memory.Write(BaseAddress + offset, array);
}
/// <summary>
/// Writes a string with a specified encoding in the remote process.
/// </summary>
/// <param name="offset">The offset where the string is written from the pointer.</param>
/// <param name="text">The text to write.</param>
/// <param name="encoding">The encoding used.</param>
public void Write(int offset, string text, Encoding encoding)
{
Process.Memory.Write(BaseAddress + offset, text, encoding);
}
/// <summary>
/// Changes the protection of the n next bytes in remote process.
/// </summary>
/// <param name="size">The size of the memory to change.</param>
/// <param name="protection">The new protection to apply.</param>
/// <param name="mustBeDisposed">The resource will be automatically disposed when the finalizer collects the object.</param>
/// <returns>A new instance of the <see cref="MemoryProtection" /> class.</returns>
public MemoryProtection ChangeProtection(SafeMemoryHandle handle, int size,
MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite, bool mustBeDisposed = true)
{
return new MemoryProtection(handle, BaseAddress, size, protection, mustBeDisposed);
}
/// <summary>
/// Reads the value of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="offset">The offset where the value is read from the pointer.</param>
/// <returns>A value.</returns>
public T Read<T>(Enum offset)
{
return Read<T>(Convert.ToInt32(offset));
}
/// <summary>
/// Reads the value of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <returns>A value.</returns>
public T Read<T>()
{
return Read<T>(0);
}
/// <summary>
/// Reads an array of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <param name="offset">The offset where the values is read from the pointer.</param>
/// <param name="count">The number of cells in the array.</param>
/// <returns>An array.</returns>
public T[] Read<T>(Enum offset, int count)
{
return Read<T>(Convert.ToInt32(offset), count);
}
/// <summary>
/// Reads a string with a specified encoding in the remote process.
/// </summary>
/// <param name="offset">The offset where the string is read from the pointer.</param>
/// <param name="encoding">The encoding used.</param>
/// <param name="maxLength">
/// [Optional] The number of maximum bytes to read. The string is automatically cropped at this end
/// ('\0' char).
/// </param>
/// <returns>The string.</returns>
public string Read(Enum offset, Encoding encoding, int maxLength = 512)
{
return Read(Convert.ToInt32(offset), encoding, maxLength);
}
/// <summary>
/// Reads a string with a specified encoding in the remote process.
/// </summary>
/// <param name="encoding">The encoding used.</param>
/// <param name="maxLength">
/// [Optional] The number of maximum bytes to read. The string is automatically cropped at this end
/// ('\0' char).
/// </param>
/// <returns>The string.</returns>
public string Read(Encoding encoding, int maxLength = 512)
{
return Read(0, encoding, maxLength);
}
/// <summary>
/// Reads a string using the encoding UTF8 in the remote process.
/// </summary>
/// <param name="offset">The offset where the string is read from the pointer.</param>
/// <param name="maxLength">
/// [Optional] The number of maximum bytes to read. The string is automatically cropped at this end
/// ('\0' char).
/// </param>
/// <param name="encoding"></param>
/// <returns>The string.</returns>
public string Read(int offset, int maxLength, Encoding encoding)
{
return Process.Memory.Read(BaseAddress + offset, encoding, maxLength);
}
/// <summary>
/// Reads a string using the encoding UTF8 in the remote process.
/// </summary>
/// <param name="offset">The offset where the string is read from the pointer.</param>
/// <param name="maxLength">
/// [Optional] The number of maximum bytes to read. The string is automatically cropped at this end
/// ('\0' char).
/// </param>
/// <returns>The string.</returns>
public string Read(Enum offset, int maxLength = 512)
{
return Read(Convert.ToInt32(offset), maxLength, Encoding.UTF8);
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
public override string ToString()
{
return $"BaseAddress = 0x{BaseAddress.ToInt64():X}";
}
/// <summary>
/// Writes the values of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="offset">The offset where the value is written from the pointer.</param>
/// <param name="value">The value to write.</param>
public void Write<T>(Enum offset, T value)
{
Write(Convert.ToInt32(offset), value);
}
/// <summary>
/// Writes the values of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="value">The value to write.</param>
public void Write<T>(T value)
{
Write(0, value);
}
/// <summary>
/// Writes an array of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <param name="offset">The offset where the values is written from the pointer.</param>
/// <param name="array">The array to write.</param>
public void Write<T>(Enum offset, T[] array)
{
Write(Convert.ToInt32(offset), array);
}
/// <summary>
/// Writes an array of a specified type in the remote process.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <param name="array">The array to write.</param>
public void Write<T>(T[] array)
{
Write(0, array);
}
/// <summary>
/// Writes a string with a specified encoding in the remote process.
/// </summary>
/// <param name="offset">The offset where the string is written from the pointer.</param>
/// <param name="text">The text to write.</param>
/// <param name="encoding">The encoding used.</param>
public void Write(Enum offset, string text, Encoding encoding)
{
Write(Convert.ToInt32(offset), text, encoding);
}
/// <summary>
/// Writes a string with a specified encoding in the remote process.
/// </summary>
/// <param name="text">The text to write.</param>
/// <param name="encoding">The encoding used.</param>
public void Write(string text, Encoding encoding)
{
Write(0, text, encoding);
}
/// <summary>
/// Writes a string using the encoding UTF8 in the remote process.
/// </summary>
/// <param name="offset">The offset where the string is written from the pointer.</param>
/// <param name="text">The text to write.</param>
public void Write(int offset, string text)
{
Process.Memory.Write(BaseAddress + offset, text);
}
/// <summary>
/// Writes a string using the encoding UTF8 in the remote process.
/// </summary>
/// <param name="offset">The offset where the string is written from the pointer.</param>
/// <param name="text">The text to write.</param>
public void Write(Enum offset, string text)
{
Write(Convert.ToInt32(offset), text);
}
/// <summary>
/// Writes a string using the encoding UTF8 in the remote process.
/// </summary>
/// <param name="text">The text to write.</param>
public void Write(string text)
{
Write(0, text);
}
/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((MemoryPointer) obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
public override int GetHashCode()
{
return BaseAddress.GetHashCode() ^ Process.GetHashCode();
}
public static bool operator ==(MemoryPointer left, MemoryPointer right)
{
return Equals(left, right);
}
public static bool operator !=(MemoryPointer left, MemoryPointer right)
{
return !Equals(left, right);
}
}
}