1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Text; |
4 |
using System.Diagnostics; |
5 |
using System.Threading; |
6 |
using System.Runtime.InteropServices; |
7 |
using RomCheater.Logging; |
8 |
using System.IO; |
9 |
using Sojaner.MemoryScanner.MemoryProviers; |
10 |
using Microsoft.Win32.SafeHandles; |
11 |
using Microsoft.Win32.Interop; |
12 |
using System.ComponentModel; |
13 |
using ManagedWinapi; |
14 |
using RomCheater.PluginFramework.Events; |
15 |
|
16 |
namespace Sojaner.MemoryScanner |
17 |
{ |
18 |
// code borrowed from: http://www.codeproject.com/KB/cs/sojaner_memory_scanner.aspx |
19 |
#region ProcessMemoryReader class |
20 |
//Thanks goes to Arik Poznanski for P/Invokes and methods needed to read and write the Memory |
21 |
//For more information refer to "Minesweeper, Behind the scenes" article by Arik Poznanski at Codeproject.com |
22 |
internal class ProcessMemoryReader : IMemoryReader, IMemoryWriter, IFileWriter, IPatchMemory,IReadMemory, IAcceptsBytesReadEvent |
23 |
{ |
24 |
// constants information can be found in <winnt.h> |
25 |
[Flags] |
26 |
enum ProcessAccessFlags : uint |
27 |
{ |
28 |
All = 0x001F0FFF, |
29 |
Terminate = 0x00000001, |
30 |
CreateThread = 0x00000002, |
31 |
VMOperation = 0x00000008, |
32 |
VMRead = 0x00000010, |
33 |
VMWrite = 0x00000020, |
34 |
DupHandle = 0x00000040, |
35 |
SetInformation = 0x00000200, |
36 |
QueryInformation = 0x00000400, |
37 |
Synchronize = 0x00100000 |
38 |
} |
39 |
[Flags] |
40 |
public enum AllocationProtect : uint |
41 |
{ |
42 |
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa366786(v=vs.85).aspx |
43 |
NONE = 0, |
44 |
PAGE_NOACCESS = 0x00000001, |
45 |
PAGE_READONLY = 0x00000002, |
46 |
PAGE_READWRITE = 0x00000004, |
47 |
PAGE_WRITECOPY = 0x00000008, |
48 |
PAGE_EXECUTE = 0x00000010, |
49 |
PAGE_EXECUTE_READ = 0x00000020, |
50 |
PAGE_EXECUTE_READWRITE = 0x00000040, |
51 |
PAGE_EXECUTE_WRITECOPY = 0x00000080, |
52 |
PAGE_GUARD = 0x00000100, |
53 |
PAGE_NOCACHE = 0x00000200, |
54 |
PAGE_WRITECOMBINE = 0x00000400 |
55 |
} |
56 |
[StructLayout(LayoutKind.Sequential)] |
57 |
public struct MEMORY_BASIC_INFORMATION |
58 |
{ |
59 |
public IntPtr BaseAddress; |
60 |
public IntPtr AllocationBase; |
61 |
public AllocationProtect AllocationProtect; |
62 |
public IntPtr RegionSize; |
63 |
public uint State; |
64 |
public uint Protect; |
65 |
public uint Type; |
66 |
} |
67 |
|
68 |
public ProcessMemoryReader() |
69 |
{ |
70 |
} |
71 |
public event BaseEventHandler<OnBytesReadEventArgs> OnBytesRead; |
72 |
/// <summary> |
73 |
/// Process from which to read |
74 |
/// </summary> |
75 |
public Process ReadProcess |
76 |
{ |
77 |
get |
78 |
{ |
79 |
return m_ReadProcess; |
80 |
} |
81 |
set |
82 |
{ |
83 |
m_ReadProcess = value; |
84 |
} |
85 |
} |
86 |
|
87 |
private Process m_ReadProcess = null; |
88 |
|
89 |
//SafeWaitHandle handle; |
90 |
private static IntPtr handle = IntPtr.Zero; |
91 |
SafeWaitHandle m_hProcess; |
92 |
public void OpenProcess() |
93 |
{ |
94 |
try |
95 |
{ |
96 |
// m_hProcess = ProcessMemoryReaderApi.OpenProcess(ProcessMemoryReaderApi.PROCESS_VM_READ, 1, (uint)m_ReadProcess.Id); |
97 |
//if (!TokenAdjuster.AdjustProcessTokenPrivileges("SeDebugPrivilege")) |
98 |
//{ |
99 |
// logger.Warn.WriteLine("Failed to set SeDebugPrivilege on current process"); |
100 |
//} |
101 |
ProcessAccessFlags access; |
102 |
access = ProcessAccessFlags.VMRead |
103 |
| ProcessAccessFlags.VMWrite |
104 |
| ProcessAccessFlags.VMOperation; |
105 |
//m_hProcess = ProcessMemoryReaderApi.OpenProcess((uint)access, 1, (uint)m_ReadProcess.Id); |
106 |
handle = ProcessMemoryReaderApi.OpenProcess(access, true, m_ReadProcess.Id); |
107 |
//m_hProcess = new SafeWaitHandle(handle, false); |
108 |
} |
109 |
catch (SEHException ex) |
110 |
{ |
111 |
logger.Error.WriteLine("WriteProcessMemoryToFile() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
112 |
logger.Error.WriteLine(ex.ToString()); |
113 |
throw ex; |
114 |
} |
115 |
catch (Exception ex) |
116 |
{ |
117 |
logger.Error.WriteLine(ex.ToString()); |
118 |
throw ex; |
119 |
} |
120 |
} |
121 |
|
122 |
public void CloseHandle() |
123 |
{ |
124 |
try |
125 |
{ |
126 |
////if (handle.IsInvalid) { return; } |
127 |
////if (handle.IsClosed) { return; } |
128 |
//m_hProcess.Close(); |
129 |
//m_hProcess.Dispose(); |
130 |
//m_hProcess = null; |
131 |
//handle = IntPtr.Zero; |
132 |
//m_ReadProcess = null; |
133 |
//string stack_trace = System.Environment.StackTrace; |
134 |
int iRetValue; |
135 |
iRetValue = ProcessMemoryReaderApi.CloseHandle(handle); |
136 |
handle = IntPtr.Zero; |
137 |
ReadProcess = null; |
138 |
if (iRetValue == 0) |
139 |
{ |
140 |
throw new Exception("CloseHandle failed"); |
141 |
} |
142 |
} |
143 |
catch (SEHException ex) |
144 |
{ |
145 |
logger.Error.WriteLine("WriteProcessMemoryToFile() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
146 |
logger.Error.WriteLine(ex.ToString()); |
147 |
throw ex; |
148 |
} |
149 |
catch (Exception ex) |
150 |
{ |
151 |
logger.Error.WriteLine(ex.ToString()); |
152 |
throw ex; |
153 |
} |
154 |
} |
155 |
/// <summary> |
156 |
/// ProcessMemoryReader is a class that enables direct reading a process memory |
157 |
/// </summary> |
158 |
private class ProcessMemoryReaderApi |
159 |
{ |
160 |
[DllImport("kernel32.dll")] |
161 |
public static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength); |
162 |
|
163 |
// function declarations are found in the MSDN and in <winbase.h> |
164 |
|
165 |
// HANDLE OpenProcess( |
166 |
// DWORD dwDesiredAccess, // access flag |
167 |
// BOOL bInheritHandle, // handle inheritance option |
168 |
// DWORD dwProcessId // process identifier |
169 |
// ); |
170 |
[DllImport("kernel32.dll")] |
171 |
public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId); |
172 |
|
173 |
// BOOL CloseHandle( |
174 |
// HANDLE hObject // handle to object |
175 |
// ); |
176 |
[DllImport("kernel32.dll")] |
177 |
public static extern Int32 CloseHandle(IntPtr hObject); |
178 |
|
179 |
// BOOL ReadProcessMemory( |
180 |
// HANDLE hProcess, // handle to the process |
181 |
// LPCVOID lpBaseAddress, // base of memory area |
182 |
// LPVOID lpBuffer, // data buffer |
183 |
// SIZE_T nSize, // number of bytes to read |
184 |
// SIZE_T * lpNumberOfBytesRead // number of bytes read |
185 |
// ); |
186 |
[DllImport("kernel32.dll", SetLastError = true)] |
187 |
public static extern bool ReadProcessMemory( |
188 |
IntPtr hProcess, |
189 |
IntPtr lpBaseAddress, |
190 |
[Out] byte[] lpBuffer, |
191 |
uint dwSize, |
192 |
out int lpNumberOfBytesRead |
193 |
); |
194 |
|
195 |
// BOOL WriteProcessMemory( |
196 |
// HANDLE hProcess, // handle to process |
197 |
// LPVOID lpBaseAddress, // base of memory area |
198 |
// LPCVOID lpBuffer, // data buffer |
199 |
// SIZE_T nSize, // count of bytes to write |
200 |
// SIZE_T * lpNumberOfBytesWritten // count of bytes written |
201 |
// ); |
202 |
[DllImport("kernel32.dll", SetLastError = true)] |
203 |
public static extern bool WriteProcessMemory( |
204 |
IntPtr hProcess, |
205 |
IntPtr lpBaseAddress, |
206 |
byte[] lpBuffer, |
207 |
uint nSize, |
208 |
out UIntPtr lpNumberOfBytesWritten); |
209 |
|
210 |
|
211 |
} |
212 |
|
213 |
#region IMemoryReader Members |
214 |
#region public bool ReadFirstNonZeroByte(uint MemoryAddress, uint bytesToRead, out int address) |
215 |
public bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
216 |
{ |
217 |
//logger.Info.WriteLine("Dumping memory (0x{0:x8}-0x{1:x8}) from pid=({2})", MemoryAddress, MemoryAddress + bytesToRead, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName)); |
218 |
address = 0; |
219 |
uint byte_alignment = 1; |
220 |
// get common init parameters |
221 |
//InitMemoryDump(out byte_alignment); |
222 |
int mem_address = MemoryAddress; |
223 |
uint _bytesToRead = bytesToRead; |
224 |
byte[] buffer = new byte[] { }; |
225 |
try |
226 |
{ |
227 |
//using (MemoryStream ms = new MemoryStream()) |
228 |
//{ |
229 |
// //BinaryWriter bw = new BinaryWriter(ms); |
230 |
// //foreach (byte b in data) { bw.Write(b); } |
231 |
for (uint i = 0; i <= bytesToRead; ) |
232 |
{ |
233 |
if (_bytesToRead < byte_alignment) |
234 |
{ |
235 |
_bytesToRead = bytesToRead; |
236 |
buffer = new byte[_bytesToRead]; |
237 |
} |
238 |
else |
239 |
{ |
240 |
_bytesToRead = byte_alignment; |
241 |
buffer = new byte[byte_alignment]; |
242 |
} |
243 |
int bytesRead = 0; |
244 |
ReadProcessMemory(mem_address, _bytesToRead, out bytesRead, out buffer); |
245 |
if (buffer.Length == 0 && bytesRead == 0) |
246 |
{ |
247 |
throw new Exception(string.Format("Failed to read memory from process: {0}", ReadProcess.ToString())); |
248 |
} |
249 |
//bw.Write(buffer); |
250 |
//bw.Flush(); |
251 |
if (_bytesToRead < byte_alignment) |
252 |
{ |
253 |
i += _bytesToRead; |
254 |
mem_address += (int)_bytesToRead; |
255 |
} |
256 |
else |
257 |
{ |
258 |
i += byte_alignment; |
259 |
mem_address += (int)byte_alignment; |
260 |
} |
261 |
for (uint j = 0; j < buffer.Length; j++) |
262 |
{ |
263 |
if (buffer[j] != 0) |
264 |
{ |
265 |
address = mem_address; |
266 |
break; |
267 |
} |
268 |
} |
269 |
if (address != 0) |
270 |
break; |
271 |
} |
272 |
// bw.Close(); |
273 |
//} |
274 |
//logger.Info.WriteLine("Succefully dumped memory (0x{0:x8}-0x{1:x8}) from pid=({2})", MemoryAddress, MemoryAddress + bytesToRead, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName)); |
275 |
return true; |
276 |
} |
277 |
catch (SEHException ex) |
278 |
{ |
279 |
logger.Error.WriteLine("ReadFirstNonZeroByte() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
280 |
logger.Error.WriteLine(ex.ToString()); |
281 |
throw ex; |
282 |
} |
283 |
catch (OutOfMemoryException ex) |
284 |
{ |
285 |
logger.Error.WriteLine("ReadFirstNonZeroByte(): OutOfMemoryException"); |
286 |
logger.Error.WriteLine(ex.ToString()); |
287 |
} |
288 |
catch (Exception ex) |
289 |
{ |
290 |
logger.Error.WriteLine("ReadFirstNonZeroByte(): Exception"); |
291 |
logger.Error.WriteLine(ex.ToString()); |
292 |
throw ex; |
293 |
} |
294 |
return false; |
295 |
} |
296 |
#endregion |
297 |
|
298 |
public void ReadProcessMemoryAtOnce(uint MemoryAddress, uint bytesToRead,object UserState) |
299 |
{ |
300 |
uint size = 1024 * 128; |
301 |
for (uint j = MemoryAddress; j < bytesToRead; j += size) |
302 |
{ |
303 |
using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size)) |
304 |
{ |
305 |
byte[] bigMem = mem.Read(); |
306 |
if (this.OnBytesRead != null) |
307 |
this.OnBytesRead.Invoke(new OnBytesReadEventArgs(this, UserState, bigMem, j, bytesToRead)); |
308 |
bigMem = null; |
309 |
} |
310 |
} |
311 |
} |
312 |
public void ReadProcessMemoryAtOnce(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
313 |
{ |
314 |
Stopwatch st = new Stopwatch(); |
315 |
st.Start(); |
316 |
uint size = 1024 * 64; |
317 |
List<byte> buffer_list = new List<byte>(); |
318 |
for (uint j = MemoryAddress; j < bytesToRead; j += size) |
319 |
{ |
320 |
using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size)) |
321 |
{ |
322 |
byte[] bigMem = mem.Read(); |
323 |
foreach (byte b in bigMem) { buffer_list.Add(b); } |
324 |
bigMem = null; |
325 |
} |
326 |
} |
327 |
bytesRead = buffer_list.Count; |
328 |
data = new byte[bytesToRead]; |
329 |
data.Initialize(); |
330 |
Buffer.BlockCopy(buffer_list.ToArray(), 0, data, 0, (int)bytesToRead); |
331 |
st.Stop(); |
332 |
logger.Profiler.WriteLine("ReadProcessMemoryAtOnce(): start=0x{0:x8} end=0x{1:x8} took {2:0.0000} seconds", MemoryAddress, MemoryAddress + bytesToRead, st.Elapsed.TotalSeconds); |
333 |
} |
334 |
|
335 |
#region public void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
336 |
public void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
337 |
{ |
338 |
ReadProcessMemory((uint)MemoryAddress, bytesToRead, out bytesRead, out data); |
339 |
} |
340 |
public void ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
341 |
{ |
342 |
try |
343 |
{ |
344 |
bytesRead = 0; |
345 |
data = new byte[bytesToRead]; |
346 |
const int alignment = 1; |
347 |
List<byte> buffer_list = new List<byte>(); |
348 |
uint _MemoryAddress = MemoryAddress; |
349 |
for (int i = 0; i < bytesToRead; i++) |
350 |
{ |
351 |
byte[] buffer = new byte[alignment]; |
352 |
int _bytesRead = 0; |
353 |
MEMORY_BASIC_INFORMATION m; |
354 |
ProcessMemoryReaderApi.VirtualQueryEx(m_ReadProcess.Handle, (IntPtr)_MemoryAddress, out m, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION))); |
355 |
|
356 |
if (m.AllocationProtect.HasFlag(AllocationProtect.PAGE_NOACCESS) || |
357 |
m.AllocationProtect == 0) |
358 |
{ |
359 |
uint OrignalAddress = _MemoryAddress; |
360 |
while (m.AllocationProtect.HasFlag(AllocationProtect.PAGE_NOACCESS) || m.AllocationProtect == 0) |
361 |
{ |
362 |
ProcessMemoryReaderApi.VirtualQueryEx(m_ReadProcess.Handle, (IntPtr)_MemoryAddress, out m, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION))); |
363 |
_MemoryAddress++; |
364 |
buffer_list.Add(0); |
365 |
} |
366 |
_MemoryAddress--; |
367 |
uint diff = _MemoryAddress - OrignalAddress; |
368 |
i += (int)diff; |
369 |
buffer_list.RemoveAt(buffer_list.Count-1); |
370 |
} |
371 |
|
372 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, alignment, out _bytesRead); |
373 |
int LastError = Marshal.GetLastWin32Error(); |
374 |
if (LastError != ResultWin32.ERROR_SUCCESS) |
375 |
{ |
376 |
string ErrorName = ResultWin32.GetErrorName(LastError); |
377 |
} |
378 |
if (_bytesRead > 0) |
379 |
{ |
380 |
foreach (byte b in buffer) |
381 |
{ |
382 |
buffer_list.Add(b); |
383 |
} |
384 |
_MemoryAddress += (uint)buffer.Length; |
385 |
} |
386 |
} |
387 |
bytesRead = buffer_list.Count; |
388 |
Buffer.BlockCopy(buffer_list.ToArray(), 0, data, 0, (int)bytesToRead); |
389 |
} |
390 |
catch (SEHException ex) |
391 |
{ |
392 |
logger.Error.WriteLine("ReadProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
393 |
logger.Error.WriteLine(ex.ToString()); |
394 |
throw ex; |
395 |
} |
396 |
catch (Exception ex) |
397 |
{ |
398 |
logger.Error.WriteLine("ReadProcessMemory(): Exception"); |
399 |
logger.Error.WriteLine(ex.ToString()); |
400 |
throw ex; |
401 |
} |
402 |
} |
403 |
#endregion |
404 |
#endregion |
405 |
|
406 |
#region IMemoryWriter Members |
407 |
#region public void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
408 |
public void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
409 |
{ |
410 |
uint _bytesWritten = 0; |
411 |
WriteProcessMemory((uint)MemoryAddress, bytesToWrite, out _bytesWritten); |
412 |
bytesWritten = (int)_bytesWritten; |
413 |
} |
414 |
public void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out uint bytesWritten) |
415 |
{ |
416 |
try |
417 |
{ |
418 |
uint _bytesWritten = 0; |
419 |
uint _MemoryAddress = MemoryAddress; |
420 |
for (int i = 0; i < bytesToWrite.Length; i++) |
421 |
{ |
422 |
UIntPtr _ptrBytesWritten = UIntPtr.Zero; |
423 |
byte[] buffer = new byte[] { bytesToWrite[i] }; |
424 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, (uint)buffer.Length, out _ptrBytesWritten); |
425 |
_MemoryAddress++; |
426 |
_bytesWritten++; |
427 |
} |
428 |
bytesWritten = _bytesWritten; |
429 |
} |
430 |
catch (SEHException ex) |
431 |
{ |
432 |
logger.Error.WriteLine("WriteProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
433 |
logger.Error.WriteLine(ex.ToString()); |
434 |
throw ex; |
435 |
} |
436 |
catch (Exception ex) |
437 |
{ |
438 |
|
439 |
logger.Error.WriteLine("WriteProcessMemory() Exception"); |
440 |
logger.Error.WriteLine(ex.ToString()); |
441 |
bytesWritten = 0; |
442 |
throw ex; |
443 |
} |
444 |
} |
445 |
#endregion |
446 |
#endregion |
447 |
|
448 |
#region IFileWriter Members |
449 |
#region public bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
450 |
public bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
451 |
{ |
452 |
//logger.Info.WriteLine("Dumping memory (0x{0:x8}-0x{1:x8}) from pid=({3}) to file {2}", MemoryAddress, MemoryAddress + bytesToRead, filename, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName)); |
453 |
bytesRead = 0; |
454 |
uint byte_alignment = 102400; |
455 |
int address = MemoryAddress; |
456 |
uint _bytesToRead = bytesToRead; |
457 |
byte[] buffer = new byte[] { }; |
458 |
try |
459 |
{ |
460 |
FileInfo fi = new FileInfo(filename); |
461 |
if (fi.Exists) |
462 |
fi.Delete(); |
463 |
using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) |
464 |
{ |
465 |
BinaryWriter bw = new BinaryWriter(fs); |
466 |
//foreach (byte b in data) { bw.Write(b); } |
467 |
|
468 |
for (uint i = 0; i <= bytesToRead; ) |
469 |
{ |
470 |
if (_bytesToRead < byte_alignment) |
471 |
{ |
472 |
_bytesToRead = bytesToRead; |
473 |
buffer = new byte[_bytesToRead]; |
474 |
} |
475 |
else |
476 |
{ |
477 |
_bytesToRead = byte_alignment; |
478 |
buffer = new byte[byte_alignment]; |
479 |
} |
480 |
ReadProcessMemory(address, _bytesToRead, out bytesRead, out buffer); |
481 |
bw.Write(buffer); |
482 |
bw.Flush(); |
483 |
|
484 |
if (_bytesToRead < byte_alignment) |
485 |
{ |
486 |
i += _bytesToRead; |
487 |
address += (int)_bytesToRead; |
488 |
} |
489 |
else |
490 |
{ |
491 |
i += byte_alignment; |
492 |
address += (int)byte_alignment; |
493 |
} |
494 |
|
495 |
|
496 |
} |
497 |
bw.Close(); |
498 |
} |
499 |
logger.Info.WriteLine("Succefully dumped memory (0x{0:x8}-0x{1:x8}) from pid=({3}) to file {2}", MemoryAddress, MemoryAddress + bytesToRead, filename, string.Format("0x{0:x4} {1}.exe", ReadProcess.Id, ReadProcess.ProcessName)); |
500 |
return true; |
501 |
} |
502 |
catch (SEHException ex) |
503 |
{ |
504 |
logger.Error.WriteLine("WriteProcessMemoryToFile() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
505 |
logger.Error.WriteLine(ex.ToString()); |
506 |
throw ex; |
507 |
} |
508 |
catch (OutOfMemoryException ex) |
509 |
{ |
510 |
logger.Error.WriteLine("WriteProcessMemoryToFile(): Exception"); |
511 |
logger.Error.WriteLine(ex.ToString()); |
512 |
} |
513 |
catch (Exception ex) |
514 |
{ |
515 |
logger.Error.WriteLine("WriteProcessMemoryToFile(): Exception"); |
516 |
logger.Error.WriteLine(ex.ToString()); |
517 |
throw ex; |
518 |
} |
519 |
return false; |
520 |
} |
521 |
#endregion |
522 |
#endregion |
523 |
|
524 |
#region IPatchMemory members |
525 |
#region public virtual bool PatchMemory(uint address, byte value) |
526 |
public virtual bool PatchMemory(uint address, byte value) |
527 |
{ |
528 |
byte[] bitData = BitConverter.GetBytes(value); |
529 |
UIntPtr ptrBytesWritten; |
530 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
531 |
if (ptrBytesWritten.ToUInt32() == 0) |
532 |
return false; |
533 |
byte check = 0; |
534 |
ReadMemory(address, out check); |
535 |
if (check == value) return true; |
536 |
return false; |
537 |
} |
538 |
#endregion |
539 |
#region public virtual bool PatchMemory(uint address, sbyte value) |
540 |
public virtual bool PatchMemory(uint address, sbyte value) |
541 |
{ |
542 |
byte[] bitData = BitConverter.GetBytes(value); |
543 |
UIntPtr ptrBytesWritten; |
544 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
545 |
if (ptrBytesWritten.ToUInt32() == 0) |
546 |
return false; |
547 |
sbyte check = 0; |
548 |
ReadMemory(address, out check); |
549 |
if (check == value) return true; |
550 |
return false; |
551 |
} |
552 |
#endregion |
553 |
#region public virtual bool PatchMemory(uint address, ushort value) |
554 |
public virtual bool PatchMemory(uint address, ushort value) |
555 |
{ |
556 |
byte[] bitData = BitConverter.GetBytes(value); |
557 |
UIntPtr ptrBytesWritten; |
558 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
559 |
if (ptrBytesWritten.ToUInt32() == 0) |
560 |
return false; |
561 |
ushort check = 0; |
562 |
ReadMemory(address, out check); |
563 |
if (check == value) return true; |
564 |
return false; |
565 |
} |
566 |
#endregion |
567 |
#region public virtual bool PatchMemory(uint address, short value) |
568 |
public virtual bool PatchMemory(uint address, short value) |
569 |
{ |
570 |
byte[] bitData = BitConverter.GetBytes(value); |
571 |
UIntPtr ptrBytesWritten; |
572 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
573 |
if (ptrBytesWritten.ToUInt32() == 0) |
574 |
return false; |
575 |
short check = 0; |
576 |
ReadMemory(address, out check); |
577 |
if (check == value) return true; |
578 |
return false; |
579 |
} |
580 |
#endregion |
581 |
#region public virtual bool PatchMemory(uint address, uint value) |
582 |
public virtual bool PatchMemory(uint address, uint value) |
583 |
{ |
584 |
byte[] bitData = BitConverter.GetBytes(value); |
585 |
UIntPtr ptrBytesWritten; |
586 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
587 |
if (ptrBytesWritten.ToUInt32() == 0) |
588 |
return false; |
589 |
uint check = 0; |
590 |
ReadMemory(address, out check); |
591 |
if (check == value) return true; |
592 |
return false; |
593 |
} |
594 |
#endregion |
595 |
#region public virtual bool PatchMemory(uint address, int value) |
596 |
public virtual bool PatchMemory(uint address, int value) |
597 |
{ |
598 |
byte[] bitData = BitConverter.GetBytes(value); |
599 |
UIntPtr ptrBytesWritten; |
600 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
601 |
if (ptrBytesWritten.ToUInt32() == 0) |
602 |
return false; |
603 |
int check = 0; |
604 |
ReadMemory(address, out check); |
605 |
if (check == value) return true; |
606 |
return false; |
607 |
} |
608 |
#endregion |
609 |
#region public virtual bool PatchMemory(uint address, ulong value) |
610 |
public virtual bool PatchMemory(uint address, ulong value) |
611 |
{ |
612 |
byte[] bitData = BitConverter.GetBytes(value); |
613 |
UIntPtr ptrBytesWritten; |
614 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
615 |
if (ptrBytesWritten.ToUInt32() == 0) |
616 |
return false; |
617 |
ulong check = 0; |
618 |
ReadMemory(address, out check); |
619 |
if (check == value) return true; |
620 |
return false; |
621 |
} |
622 |
#endregion |
623 |
#region public virtual bool PatchMemory(uint address, long value) |
624 |
public virtual bool PatchMemory(uint address, long value) |
625 |
{ |
626 |
byte[] bitData = BitConverter.GetBytes(value); |
627 |
UIntPtr ptrBytesWritten; |
628 |
ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
629 |
if (ptrBytesWritten.ToUInt32() == 0) |
630 |
return false; |
631 |
long check = 0; |
632 |
ReadMemory(address, out check); |
633 |
if (check == value) return true; |
634 |
return false; |
635 |
} |
636 |
#endregion |
637 |
#endregion |
638 |
|
639 |
#region IReadMemory members |
640 |
#region public virtual bool ReadMemory(uint address, out byte value) |
641 |
public virtual bool ReadMemory(uint address, out byte value) |
642 |
{ |
643 |
value = 0; |
644 |
try |
645 |
{ |
646 |
int bytesRead; |
647 |
uint size = sizeof(byte); |
648 |
byte[] bitData = new byte[size]; |
649 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead); |
650 |
if (bytesRead == 0) |
651 |
return false; |
652 |
value = bitData[0]; |
653 |
return true; |
654 |
} |
655 |
catch |
656 |
{ |
657 |
value = 0x00; |
658 |
return false; |
659 |
} |
660 |
} |
661 |
#endregion |
662 |
#region public virtual bool ReadMemory(uint address, out sbyte value) |
663 |
public virtual bool ReadMemory(uint address, out sbyte value) |
664 |
{ |
665 |
value = 0; |
666 |
try |
667 |
{ |
668 |
int bytesRead; |
669 |
uint size = sizeof(sbyte); |
670 |
byte[] bitData = new byte[size]; |
671 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead); |
672 |
if (bytesRead == 0) |
673 |
return false; |
674 |
value = Convert.ToSByte(bitData[0]); |
675 |
return true; |
676 |
} |
677 |
catch |
678 |
{ |
679 |
value = 0x00; |
680 |
return false; |
681 |
} |
682 |
} |
683 |
#endregion |
684 |
#region public virtual bool ReadMemory(uint address, out ushort value) |
685 |
public virtual bool ReadMemory(uint address, out ushort value) |
686 |
{ |
687 |
value = 0; |
688 |
try |
689 |
{ |
690 |
int bytesRead; |
691 |
uint size = sizeof(ushort); |
692 |
byte[] bitData = new byte[size]; |
693 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead); |
694 |
if (bytesRead == 0) |
695 |
return false; |
696 |
value = BitConverter.ToUInt16(bitData, 0); |
697 |
return true; |
698 |
} |
699 |
catch |
700 |
{ |
701 |
value = 0x00; |
702 |
return false; |
703 |
} |
704 |
} |
705 |
#endregion |
706 |
#region public virtual bool ReadMemory(uint address, out short value) |
707 |
public virtual bool ReadMemory(uint address, out short value) |
708 |
{ |
709 |
value = 0; |
710 |
try |
711 |
{ |
712 |
int bytesRead; |
713 |
uint size = sizeof(short); |
714 |
byte[] bitData = new byte[size]; |
715 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead); |
716 |
if (bytesRead == 0) |
717 |
return false; |
718 |
value = BitConverter.ToInt16(bitData, 0); |
719 |
return true; |
720 |
} |
721 |
catch |
722 |
{ |
723 |
value = 0x00; |
724 |
return false; |
725 |
} |
726 |
} |
727 |
#endregion |
728 |
#region public virtual bool ReadMemory(uint address, out uint value) |
729 |
public virtual bool ReadMemory(uint address, out uint value) |
730 |
{ |
731 |
value = 0; |
732 |
try |
733 |
{ |
734 |
int bytesRead; |
735 |
uint size = sizeof(uint); |
736 |
byte[] bitData = new byte[size]; |
737 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead); |
738 |
if (bytesRead == 0) |
739 |
return false; |
740 |
value = BitConverter.ToUInt32(bitData, 0); |
741 |
return true; |
742 |
} |
743 |
catch |
744 |
{ |
745 |
value = 0x00; |
746 |
return false; |
747 |
} |
748 |
} |
749 |
#endregion |
750 |
#region public virtual bool ReadMemory(uint address, out int value) |
751 |
public virtual bool ReadMemory(uint address, out int value) |
752 |
{ |
753 |
value = 0; |
754 |
try |
755 |
{ |
756 |
int bytesRead; |
757 |
uint size = sizeof(int); |
758 |
byte[] bitData = new byte[size]; |
759 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead); |
760 |
if (bytesRead == 0) |
761 |
return false; |
762 |
value = BitConverter.ToInt32(bitData, 0); |
763 |
return true; |
764 |
} |
765 |
catch |
766 |
{ |
767 |
value = 0x00; |
768 |
return false; |
769 |
} |
770 |
} |
771 |
#endregion |
772 |
#region public virtual bool ReadMemory(uint address, out ulong value) |
773 |
public virtual bool ReadMemory(uint address, out ulong value) |
774 |
{ |
775 |
value = 0; |
776 |
try |
777 |
{ |
778 |
int bytesRead; |
779 |
uint size = sizeof(ulong); |
780 |
byte[] bitData = new byte[size]; |
781 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead); |
782 |
if (bytesRead == 0) |
783 |
return false; |
784 |
value = BitConverter.ToUInt64(bitData, 0); |
785 |
return true; |
786 |
} |
787 |
catch |
788 |
{ |
789 |
value = 0x00; |
790 |
return false; |
791 |
} |
792 |
} |
793 |
#endregion |
794 |
#region public virtual bool ReadMemory(uint address, out long value) |
795 |
public virtual bool ReadMemory(uint address, out long value) |
796 |
{ |
797 |
value = 0; |
798 |
try |
799 |
{ |
800 |
int bytesRead; |
801 |
uint size = sizeof(long); |
802 |
byte[] bitData = new byte[size]; |
803 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead); |
804 |
if (bytesRead == 0) |
805 |
return false; |
806 |
value = BitConverter.ToInt64(bitData, 0); |
807 |
return true; |
808 |
} |
809 |
catch |
810 |
{ |
811 |
value = 0x00; |
812 |
return false; |
813 |
} |
814 |
} |
815 |
#endregion |
816 |
#endregion |
817 |
} |
818 |
#endregion |
819 |
} |