84 |
} |
} |
85 |
#endregion |
#endregion |
86 |
|
|
87 |
|
#region ReadProcessMemory |
88 |
|
public bool ReadFirstNonZeroByte(uint MemoryAddress, uint bytesToRead, out uint address) |
89 |
|
{ |
90 |
|
RamDumper dumper = new RamDumper(); |
91 |
|
return dumper.ReadFirstNonZeroByte(ReadProcess, MemoryAddress, bytesToRead, out address); |
92 |
|
} |
93 |
|
#endregion |
94 |
#region WriteProcessMemory |
#region WriteProcessMemory |
95 |
public void WriteProcessMemory(UIntPtr MemoryAddress, byte byteToWrite, out int bytesWritten) |
public void WriteProcessMemory(UIntPtr MemoryAddress, byte byteToWrite, out int bytesWritten) |
96 |
{ |
{ |
262 |
} |
} |
263 |
#endregion |
#endregion |
264 |
#endregion |
#endregion |
265 |
|
|
266 |
|
#region ReadFirstNonZeroByte |
267 |
|
public bool ReadFirstNonZeroByte(Process ppid, uint MemoryAddress, uint bytesToRead, out uint address) |
268 |
|
{ |
269 |
|
//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)); |
270 |
|
address = 0; |
271 |
|
uint byte_alignment = 1; |
272 |
|
// get common init parameters |
273 |
|
//InitMemoryDump(out byte_alignment); |
274 |
|
uint mem_address = MemoryAddress; |
275 |
|
uint _bytesToRead = bytesToRead; |
276 |
|
byte[] buffer = new byte[] { }; |
277 |
|
try |
278 |
|
{ |
279 |
|
//using (MemoryStream ms = new MemoryStream()) |
280 |
|
//{ |
281 |
|
// //BinaryWriter bw = new BinaryWriter(ms); |
282 |
|
// //foreach (byte b in data) { bw.Write(b); } |
283 |
|
for (uint i = 0; i <= bytesToRead; ) |
284 |
|
{ |
285 |
|
if (_bytesToRead < byte_alignment) |
286 |
|
{ |
287 |
|
_bytesToRead = bytesToRead; |
288 |
|
buffer = new byte[_bytesToRead]; |
289 |
|
} |
290 |
|
else |
291 |
|
{ |
292 |
|
_bytesToRead = byte_alignment; |
293 |
|
buffer = new byte[byte_alignment]; |
294 |
|
} |
295 |
|
IntPtr ptrBytesRead; |
296 |
|
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)mem_address, buffer, _bytesToRead, out ptrBytesRead); |
297 |
|
//bw.Write(buffer); |
298 |
|
//bw.Flush(); |
299 |
|
if (_bytesToRead < byte_alignment) |
300 |
|
{ |
301 |
|
i += _bytesToRead; |
302 |
|
mem_address += _bytesToRead; |
303 |
|
} |
304 |
|
else |
305 |
|
{ |
306 |
|
i += byte_alignment; |
307 |
|
mem_address += byte_alignment; |
308 |
|
} |
309 |
|
for (uint j = 0; j < buffer.Length; j++) |
310 |
|
{ |
311 |
|
if (buffer[j] != 0) |
312 |
|
{ |
313 |
|
address = mem_address; |
314 |
|
break; |
315 |
|
} |
316 |
|
} |
317 |
|
if (address != 0) |
318 |
|
break; |
319 |
|
} |
320 |
|
// bw.Close(); |
321 |
|
//} |
322 |
|
//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)); |
323 |
|
return true; |
324 |
|
} |
325 |
|
catch (OutOfMemoryException ex) |
326 |
|
{ |
327 |
|
logger.Error.WriteLine("Failed to dump memory (0x{0:x8}-0x{1:x8}) from pid=({2})", MemoryAddress, MemoryAddress + bytesToRead, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName)); |
328 |
|
logger.Error.WriteLine("DumpMemory(): OutOfMemoryException"); |
329 |
|
logger.Error.WriteLine(ex.ToString()); |
330 |
|
} |
331 |
|
catch (Exception ex) |
332 |
|
{ |
333 |
|
logger.Error.WriteLine("Failed to dump memory (0x{0:x8}-0x{1:x8}) from pid=({2})", MemoryAddress, MemoryAddress + bytesToRead, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName)); |
334 |
|
logger.Error.WriteLine("DumpMemory(): Exception"); |
335 |
|
logger.Error.WriteLine(ex.ToString()); |
336 |
|
} |
337 |
|
return false; |
338 |
|
} |
339 |
|
#endregion |
340 |
} |
} |
341 |
#endregion |
#endregion |
342 |
/// <summary> |
/// <summary> |