8 |
using System.IO; |
using System.IO; |
9 |
using Sojaner.MemoryScanner.MemoryProviers; |
using Sojaner.MemoryScanner.MemoryProviers; |
10 |
using Microsoft.Win32.SafeHandles; |
using Microsoft.Win32.SafeHandles; |
11 |
|
using Microsoft.Win32.Interop; |
12 |
|
using System.ComponentModel; |
13 |
|
|
14 |
namespace Sojaner.MemoryScanner |
namespace Sojaner.MemoryScanner |
15 |
{ |
{ |
261 |
{ |
{ |
262 |
try |
try |
263 |
{ |
{ |
264 |
byte[] buffer = new byte[bytesToRead]; |
bytesRead = 0; |
265 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)MemoryAddress, buffer, bytesToRead, out bytesRead); |
|
266 |
data = buffer; |
List<byte> buffer_list = new List<byte>(); |
267 |
|
uint _MemoryAddress = MemoryAddress; |
268 |
|
for (int i = 0; i < bytesToRead; i++) |
269 |
|
{ |
270 |
|
byte[] buffer = new byte[1]; |
271 |
|
int _bytesRead = 0; |
272 |
|
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)_MemoryAddress, buffer, 1, out _bytesRead); |
273 |
|
if (_bytesRead > 0) |
274 |
|
{ |
275 |
|
foreach (byte b in buffer) |
276 |
|
{ |
277 |
|
buffer_list.Add(b); |
278 |
|
} |
279 |
|
_MemoryAddress += (uint)buffer.Length; |
280 |
|
} |
281 |
|
else |
282 |
|
{ |
283 |
|
buffer_list.Add(0); |
284 |
|
_MemoryAddress++; |
285 |
|
} |
286 |
|
|
287 |
|
} |
288 |
|
bytesRead = buffer_list.Count; |
289 |
|
data = buffer_list.ToArray(); |
290 |
} |
} |
291 |
catch (SEHException ex) |
catch (SEHException ex) |
292 |
{ |
{ |
329 |
UIntPtr ptrBytesWritten; |
UIntPtr ptrBytesWritten; |
330 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)MemoryAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten); |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)MemoryAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten); |
331 |
bytesWritten = ptrBytesWritten.ToUInt32(); |
bytesWritten = ptrBytesWritten.ToUInt32(); |
332 |
|
int LastWin32Error = Marshal.GetLastWin32Error(); |
333 |
|
if (LastWin32Error != ResultWin32.ERROR_SUCCESS) |
334 |
|
{ |
335 |
|
string error = ResultWin32.GetErrorName(LastWin32Error); |
336 |
|
throw new Win32Exception(LastWin32Error, "Unable to read process memory"); |
337 |
|
} |
338 |
|
} |
339 |
|
catch (Win32Exception ex) |
340 |
|
{ |
341 |
|
string error_name = ResultWin32.GetErrorName(ex.ErrorCode); |
342 |
|
error_name = (error_name == string.Empty) ? "undefined" : error_name; |
343 |
|
logger.Error.WriteLine("WriteProcessMemory() Win32Exception was thrown: ErrorCode:{0} - {1}", string.Format("0x{0:x8}", ex.ErrorCode, error_name), ex.Message); |
344 |
|
logger.Error.WriteLine(ex.ToString()); |
345 |
|
throw ex; |
346 |
} |
} |
347 |
catch (SEHException ex) |
catch (SEHException ex) |
348 |
{ |
{ |