19 |
|
IFileWriter |
20 |
|
{ |
21 |
|
private ProcessMemoryReader provider; |
22 |
< |
public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; } |
22 |
> |
public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; } |
23 |
|
public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; } |
24 |
|
public BaseMemoryProvider(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; } |
25 |
|
public BaseMemoryProvider(IAcceptsProcessAndConfig config) : this() { this.AcceptedPlugin = config.AcceptedPlugin; this.AcceptedProcess = config.AcceptedProcess; } |
26 |
|
|
27 |
< |
protected virtual void OpenProvider() |
27 |
> |
|
28 |
> |
private bool isOpen { get; set; } |
29 |
> |
private bool isClosed { get; set; } |
30 |
> |
|
31 |
> |
#region Open/Close Provider |
32 |
> |
#region public virtual void OpenProvider() |
33 |
> |
public virtual void OpenProvider() |
34 |
|
{ |
35 |
< |
provider = new ProcessMemoryReader(); |
36 |
< |
provider.ReadProcess = this.AcceptedProcess; |
37 |
< |
if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "",this.GetType().Name, this.AcceptedProcess.ToString()); return; } |
38 |
< |
provider.OpenProcess(); |
39 |
< |
} |
40 |
< |
protected virtual void CloseProvider() |
41 |
< |
{ |
42 |
< |
if (provider == null) return; |
43 |
< |
provider.CloseHandle(); |
44 |
< |
provider = null; // free any memory associated with the provider |
35 |
> |
if (isOpen) |
36 |
> |
{ |
37 |
> |
logger.Warn.WriteLine("Provider has already been opened."); |
38 |
> |
return; |
39 |
> |
} |
40 |
> |
try |
41 |
> |
{ |
42 |
> |
provider = new ProcessMemoryReader(); |
43 |
> |
provider.ReadProcess = this.AcceptedProcess; |
44 |
> |
if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "", this.GetType().Name, this.AcceptedProcess.ToString()); return; } |
45 |
> |
provider.OpenProcess(); |
46 |
> |
isOpen = true; |
47 |
> |
isClosed = false; |
48 |
> |
} |
49 |
> |
catch (Exception ex) |
50 |
> |
{ |
51 |
> |
logger.Error.WriteLine("Failed to open provider: {0}{1}",System.Environment.NewLine, ex.ToString()); |
52 |
> |
isOpen = false; |
53 |
> |
isClosed = true; |
54 |
> |
} |
55 |
> |
} |
56 |
> |
#endregion |
57 |
> |
#region public virtual void CloseProvider() |
58 |
> |
public virtual void CloseProvider() |
59 |
> |
{ |
60 |
> |
if (isClosed) |
61 |
> |
{ |
62 |
> |
logger.Warn.WriteLine("Provider has already been closed."); |
63 |
> |
return; |
64 |
> |
} |
65 |
> |
if (!isOpen) |
66 |
> |
{ |
67 |
> |
logger.Warn.WriteLine("Provider cannot be closed, it was never opened...attempting to open provider."); |
68 |
> |
OpenProvider(); |
69 |
> |
if (!isOpen) |
70 |
> |
{ |
71 |
> |
logger.Warn.WriteLine("Could not open provider"); |
72 |
> |
return; |
73 |
> |
} |
74 |
> |
} |
75 |
> |
try |
76 |
> |
{ |
77 |
> |
logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace); |
78 |
> |
if (provider == null) return; |
79 |
> |
provider.CloseHandle(); |
80 |
> |
provider = null; // free any memory associated with the provider |
81 |
> |
isClosed = true; |
82 |
> |
isOpen = false; |
83 |
> |
} |
84 |
> |
catch (Exception ex) |
85 |
> |
{ |
86 |
> |
logger.Error.WriteLine("Failed to close provider: {0}{1}", System.Environment.NewLine, ex.ToString()); |
87 |
> |
isClosed = false; |
88 |
> |
if (isOpen) |
89 |
> |
{ |
90 |
> |
throw new Exception("Provider is failed to close and still open."); |
91 |
> |
} |
92 |
> |
} |
93 |
|
} |
94 |
+ |
#endregion |
95 |
+ |
#endregion |
96 |
+ |
|
97 |
|
#region IAcceptsProcess<Process> Members |
98 |
|
public Process AcceptedProcess { get; set; } |
99 |
|
#endregion |
100 |
|
#region IAcceptsPlugin<IConfigPlugin> Members |
101 |
|
public IConfigPlugin AcceptedPlugin { get; set; } |
102 |
|
#endregion |
103 |
+ |
#region EnsureProviderIsOpen methods : log and/or throw errors |
104 |
+ |
private bool EnsureProviderIsOpen() { return EnsureProviderIsOpenOrThrowError(); } |
105 |
+ |
private bool EnsureProviderIsOpenOrLogError() { return EnsureProviderIsOpenOrThrowOrLogError(false, true); } |
106 |
+ |
private bool EnsureProviderIsOpenOrThrowError() { return EnsureProviderIsOpenOrThrowOrLogError(true, true); } |
107 |
+ |
private bool EnsureProviderIsOpenOrThrowOrLogError(bool ThrowError, bool LogError) |
108 |
+ |
{ |
109 |
+ |
if (!isOpen) |
110 |
+ |
{ |
111 |
+ |
try { throw new Exception("Memory operation could not be completed because the provider is not open"); } |
112 |
+ |
catch (Exception ex) |
113 |
+ |
{ |
114 |
+ |
if (LogError) |
115 |
+ |
logger.Error.WriteLine(ex.ToString()); |
116 |
+ |
if (ThrowError) |
117 |
+ |
throw ex; |
118 |
+ |
return false; |
119 |
+ |
} |
120 |
+ |
} |
121 |
+ |
return true; |
122 |
+ |
} |
123 |
+ |
#endregion |
124 |
+ |
|
125 |
|
#region IPatchMemory members |
126 |
|
#region public virtual bool PatchMemory(uint address, byte value) |
127 |
|
public virtual bool PatchMemory(uint address, byte value) |
128 |
|
{ |
129 |
< |
OpenProvider(); |
129 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
130 |
|
int bytesWritten; |
131 |
|
byte[] bitData = BitConverter.GetBytes(value); |
132 |
|
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
54 |
– |
CloseProvider(); |
133 |
|
byte check = 0; |
134 |
|
ReadMemory(address, out check); |
135 |
|
if (check == value) return true; |
139 |
|
#region public virtual bool PatchMemory(uint address, sbyte value) |
140 |
|
public virtual bool PatchMemory(uint address, sbyte value) |
141 |
|
{ |
142 |
< |
OpenProvider(); |
142 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
143 |
|
int bytesWritten; |
144 |
|
byte[] bitData = BitConverter.GetBytes(value); |
145 |
|
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
68 |
– |
CloseProvider(); |
146 |
|
sbyte check = 0; |
147 |
|
ReadMemory(address, out check); |
148 |
|
if (check == value) return true; |
152 |
|
#region public virtual bool PatchMemory(uint address, ushort value) |
153 |
|
public virtual bool PatchMemory(uint address, ushort value) |
154 |
|
{ |
155 |
< |
OpenProvider(); |
155 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
156 |
|
int bytesWritten; |
157 |
|
byte[] bitData = BitConverter.GetBytes(value); |
158 |
|
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
159 |
< |
CloseProvider(); |
159 |
> |
|
160 |
|
ushort check = 0; |
161 |
|
ReadMemory(address, out check); |
162 |
|
if (check == value) return true; |
166 |
|
#region public virtual bool PatchMemory(uint address, short value) |
167 |
|
public virtual bool PatchMemory(uint address, short value) |
168 |
|
{ |
169 |
< |
OpenProvider(); |
169 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
170 |
|
int bytesWritten; |
171 |
|
byte[] bitData = BitConverter.GetBytes(value); |
172 |
|
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
173 |
< |
CloseProvider(); |
173 |
> |
|
174 |
|
short check = 0; |
175 |
|
ReadMemory(address, out check); |
176 |
|
if (check == value) return true; |
180 |
|
#region public virtual bool PatchMemory(uint address, uint value) |
181 |
|
public virtual bool PatchMemory(uint address, uint value) |
182 |
|
{ |
183 |
< |
OpenProvider(); |
183 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
184 |
|
int bytesWritten; |
185 |
|
byte[] bitData = BitConverter.GetBytes(value); |
186 |
|
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
187 |
< |
CloseProvider(); |
187 |
> |
|
188 |
|
uint check = 0; |
189 |
|
ReadMemory(address, out check); |
190 |
|
if (check == value) return true; |
194 |
|
#region public virtual bool PatchMemory(uint address, int value) |
195 |
|
public virtual bool PatchMemory(uint address, int value) |
196 |
|
{ |
197 |
< |
OpenProvider(); |
197 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
198 |
|
int bytesWritten; |
199 |
|
byte[] bitData = BitConverter.GetBytes(value); |
200 |
|
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
201 |
< |
CloseProvider(); |
201 |
> |
|
202 |
|
int check = 0; |
203 |
|
ReadMemory(address, out check); |
204 |
|
if (check == value) return true; |
208 |
|
#region public virtual bool PatchMemory(uint address, ulong value) |
209 |
|
public virtual bool PatchMemory(uint address, ulong value) |
210 |
|
{ |
211 |
< |
OpenProvider(); |
211 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
212 |
|
int bytesWritten; |
213 |
|
byte[] bitData = BitConverter.GetBytes(value); |
214 |
|
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
215 |
< |
CloseProvider(); |
215 |
> |
|
216 |
|
ulong check = 0; |
217 |
|
ReadMemory(address, out check); |
218 |
|
if (check == value) return true; |
222 |
|
#region public virtual bool PatchMemory(uint address, long value) |
223 |
|
public virtual bool PatchMemory(uint address, long value) |
224 |
|
{ |
225 |
< |
OpenProvider(); |
225 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
226 |
|
int bytesWritten; |
227 |
|
byte[] bitData = BitConverter.GetBytes(value); |
228 |
|
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
229 |
< |
CloseProvider(); |
229 |
> |
|
230 |
|
long check = 0; |
231 |
|
ReadMemory(address, out check); |
232 |
|
if (check == value) return true; |
239 |
|
#region public virtual bool ReadMemory(uint address, out byte value) |
240 |
|
public virtual bool ReadMemory(uint address, out byte value) |
241 |
|
{ |
242 |
+ |
value = 0; |
243 |
+ |
if (!EnsureProviderIsOpen()) { return false; } |
244 |
|
try |
245 |
< |
{ |
167 |
< |
value = 0; |
168 |
< |
OpenProvider(); |
169 |
< |
int bytesReadSize; |
245 |
> |
{ int bytesReadSize; |
246 |
|
byte[] bitData; |
247 |
|
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
248 |
|
value = bitData[0]; |
249 |
< |
CloseProvider(); |
249 |
> |
|
250 |
|
return true; |
251 |
|
} |
252 |
|
catch |
259 |
|
#region public virtual bool ReadMemory(uint address, out sbyte value) |
260 |
|
public virtual bool ReadMemory(uint address, out sbyte value) |
261 |
|
{ |
262 |
+ |
value = 0; |
263 |
+ |
if (!EnsureProviderIsOpen()) { return false; } |
264 |
|
try |
265 |
< |
{ |
188 |
< |
value = 0; |
189 |
< |
OpenProvider(); |
265 |
> |
{ |
266 |
|
int bytesReadSize; |
267 |
|
byte[] bitData; |
268 |
|
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
269 |
|
value = Convert.ToSByte(bitData[0]); |
270 |
< |
CloseProvider(); |
270 |
> |
|
271 |
|
return true; |
272 |
|
} |
273 |
|
catch |
280 |
|
#region public virtual bool ReadMemory(uint address, out ushort value) |
281 |
|
public virtual bool ReadMemory(uint address, out ushort value) |
282 |
|
{ |
283 |
+ |
value = 0; |
284 |
+ |
if (!EnsureProviderIsOpen()) { return false; } |
285 |
|
try |
286 |
|
{ |
209 |
– |
value = 0; |
210 |
– |
OpenProvider(); |
287 |
|
int bytesReadSize; |
288 |
|
byte[] bitData; |
289 |
|
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
290 |
|
value = BitConverter.ToUInt16(bitData, 0); |
291 |
< |
CloseProvider(); |
291 |
> |
|
292 |
|
return true; |
293 |
|
} |
294 |
|
catch |
301 |
|
#region public virtual bool ReadMemory(uint address, out short value) |
302 |
|
public virtual bool ReadMemory(uint address, out short value) |
303 |
|
{ |
304 |
+ |
value = 0; |
305 |
+ |
if (!EnsureProviderIsOpen()) { return false; } |
306 |
|
try |
307 |
|
{ |
230 |
– |
value = 0; |
231 |
– |
OpenProvider(); |
308 |
|
int bytesReadSize; |
309 |
|
byte[] bitData; |
310 |
|
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
311 |
|
value = BitConverter.ToInt16(bitData, 0); |
312 |
< |
CloseProvider(); |
312 |
> |
|
313 |
|
return true; |
314 |
|
} |
315 |
|
catch |
322 |
|
#region public virtual bool ReadMemory(uint address, out uint value) |
323 |
|
public virtual bool ReadMemory(uint address, out uint value) |
324 |
|
{ |
325 |
+ |
value = 0; |
326 |
+ |
if (!EnsureProviderIsOpen()) { return false; } |
327 |
|
try |
328 |
|
{ |
251 |
– |
value = 0; |
252 |
– |
OpenProvider(); |
329 |
|
int bytesReadSize; |
330 |
|
byte[] bitData; |
331 |
|
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
332 |
|
value = BitConverter.ToUInt32(bitData, 0); |
333 |
< |
CloseProvider(); |
333 |
> |
|
334 |
|
return true; |
335 |
|
} |
336 |
|
catch |
343 |
|
#region public virtual bool ReadMemory(uint address, out int value) |
344 |
|
public virtual bool ReadMemory(uint address, out int value) |
345 |
|
{ |
346 |
+ |
value = 0; |
347 |
+ |
if (!EnsureProviderIsOpen()) { return false; } |
348 |
|
try |
349 |
|
{ |
272 |
– |
value = 0; |
273 |
– |
OpenProvider(); |
350 |
|
int bytesReadSize; |
351 |
|
byte[] bitData; |
352 |
|
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
353 |
|
value = BitConverter.ToInt32(bitData, 0); |
354 |
< |
CloseProvider(); |
354 |
> |
|
355 |
|
return true; |
356 |
|
} |
357 |
|
catch |
364 |
|
#region public virtual bool ReadMemory(uint address, out ulong value) |
365 |
|
public virtual bool ReadMemory(uint address, out ulong value) |
366 |
|
{ |
367 |
+ |
value = 0; |
368 |
+ |
if (!EnsureProviderIsOpen()) { return false; } |
369 |
|
try |
370 |
|
{ |
293 |
– |
value = 0; |
294 |
– |
OpenProvider(); |
371 |
|
int bytesReadSize; |
372 |
|
byte[] bitData; |
373 |
|
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
374 |
|
value = BitConverter.ToUInt64(bitData, 0); |
375 |
< |
CloseProvider(); |
375 |
> |
|
376 |
|
return true; |
377 |
|
} |
378 |
|
catch |
385 |
|
#region public virtual bool ReadMemory(uint address, out long value) |
386 |
|
public virtual bool ReadMemory(uint address, out long value) |
387 |
|
{ |
388 |
+ |
value = 0; |
389 |
+ |
if (!EnsureProviderIsOpen()) { return false; } |
390 |
|
try |
391 |
|
{ |
314 |
– |
value = 0; |
315 |
– |
OpenProvider(); |
392 |
|
int bytesReadSize; |
393 |
|
byte[] bitData; |
394 |
|
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
395 |
|
value = BitConverter.ToInt64(bitData, 0); |
396 |
< |
CloseProvider(); |
396 |
> |
|
397 |
|
return true; |
398 |
|
} |
399 |
|
catch |
409 |
|
#region public virtual bool ReadFirstNonZeroByte(uint MemoryAddress, uint bytesToRead, out uint address) |
410 |
|
public virtual bool ReadFirstNonZeroByte(uint MemoryAddress, uint bytesToRead, out uint address) |
411 |
|
{ |
412 |
< |
try |
413 |
< |
{ |
414 |
< |
OpenProvider(); |
415 |
< |
provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); |
340 |
< |
CloseProvider(); |
341 |
< |
return true; |
342 |
< |
} |
343 |
< |
catch |
344 |
< |
{ |
345 |
< |
address = 0x00; |
346 |
< |
return false; |
347 |
< |
} |
412 |
> |
address = 0; |
413 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
414 |
> |
try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; } |
415 |
> |
catch { address = 0x00; return false; } |
416 |
|
} |
417 |
|
#endregion |
418 |
|
#region public virtual void ReadProcessMemory(uint MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data) |
419 |
|
public virtual void ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
420 |
|
{ |
421 |
< |
try |
422 |
< |
{ |
423 |
< |
OpenProvider(); |
424 |
< |
provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); |
425 |
< |
CloseProvider(); |
358 |
< |
} |
359 |
< |
catch |
360 |
< |
{ |
361 |
< |
bytesRead = 0x00; |
362 |
< |
data = new byte[] { }; |
363 |
< |
} |
421 |
> |
bytesRead = 0x00; |
422 |
> |
data = new byte[] { }; |
423 |
> |
if (!EnsureProviderIsOpen()) { return; } |
424 |
> |
try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); } |
425 |
> |
catch { bytesRead = 0x00; data = new byte[] { }; } |
426 |
|
} |
427 |
|
#endregion |
428 |
|
#region IMemoryWriter members |
429 |
|
#region public virtual void WriteProcessMemory(uint MemoryAddress, byte byteToWrite, out int bytesWritten) |
430 |
|
public virtual void WriteProcessMemory(uint MemoryAddress, byte byteToWrite, out int bytesWritten) |
431 |
|
{ |
432 |
< |
try |
433 |
< |
{ |
434 |
< |
OpenProvider(); |
435 |
< |
provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); |
374 |
< |
CloseProvider(); |
375 |
< |
} |
376 |
< |
catch |
377 |
< |
{ |
378 |
< |
bytesWritten = 0x00; |
379 |
< |
} |
432 |
> |
bytesWritten = 0; |
433 |
> |
if (!EnsureProviderIsOpen()) { return; } |
434 |
> |
try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); } |
435 |
> |
catch { bytesWritten = 0x00; } |
436 |
|
} |
437 |
|
#endregion |
438 |
|
#region public virtual void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
439 |
|
public virtual void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
440 |
|
{ |
441 |
< |
try |
442 |
< |
{ |
443 |
< |
OpenProvider(); |
444 |
< |
provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); |
389 |
< |
CloseProvider(); |
390 |
< |
} |
391 |
< |
catch |
392 |
< |
{ |
393 |
< |
bytesWritten = 0x00; |
394 |
< |
} |
441 |
> |
bytesWritten = 0; |
442 |
> |
if (!EnsureProviderIsOpen()) { return; } |
443 |
> |
try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); } |
444 |
> |
catch { bytesWritten = 0x00; } |
445 |
|
} |
446 |
|
#endregion |
447 |
|
#endregion |
450 |
|
#region public virtual bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
451 |
|
public virtual bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
452 |
|
{ |
453 |
< |
try |
454 |
< |
{ |
455 |
< |
OpenProvider(); |
406 |
< |
provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); |
407 |
< |
CloseProvider(); |
408 |
< |
return true; |
409 |
< |
} |
453 |
> |
bytesRead = 0; |
454 |
> |
if (!EnsureProviderIsOpen()) { return false; } |
455 |
> |
try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; } |
456 |
|
catch |
457 |
< |
{ |
412 |
< |
bytesRead = 0x00; |
413 |
< |
return false; |
414 |
< |
} |
457 |
> |
{ bytesRead = 0x00; return false; } |
458 |
|
} |
459 |
|
#endregion |
460 |
|
#endregion |