38 |
|
|
39 |
private Process m_ReadProcess = null; |
private Process m_ReadProcess = null; |
40 |
|
|
41 |
private IntPtr m_hProcess = IntPtr.Zero; |
private static IntPtr m_hProcess = IntPtr.Zero; |
42 |
|
|
43 |
public void OpenProcess() |
public void OpenProcess() |
44 |
{ |
{ |
68 |
} |
} |
69 |
} |
} |
70 |
|
|
71 |
|
#region WriteProcessMemoryToFile |
72 |
|
public bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
73 |
|
{ |
74 |
|
RamDumper dumper = new RamDumper(); |
75 |
|
return dumper.DumpMemoryToFile(ReadProcess, filename, MemoryAddress, bytesToRead, out bytesRead); |
76 |
|
} |
77 |
|
#endregion |
78 |
|
|
79 |
public bool DumpMemory(Process ppid, string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
#region ReadProcessMemory |
80 |
|
public byte[] ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead) |
81 |
{ |
{ |
82 |
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)); |
RamDumper dumper = new RamDumper(); |
83 |
bytesRead = 0; |
return dumper.DumpMemoryToByteArray(ReadProcess, MemoryAddress, bytesToRead, out bytesRead); |
84 |
uint byte_alignment = 102400; // write to file in 100mb chunks |
} |
85 |
uint address = MemoryAddress; |
#endregion |
86 |
uint _bytesToRead = bytesToRead; |
|
87 |
byte[] buffer = new byte[] { }; |
#region WriteProcessMemory |
88 |
try |
public void WriteProcessMemory(UIntPtr MemoryAddress, byte byteToWrite, out int bytesWritten) |
89 |
|
{ |
90 |
|
WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); |
91 |
|
} |
92 |
|
public void WriteProcessMemory(UIntPtr MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
93 |
|
{ |
94 |
|
IntPtr ptrBytesWritten; |
95 |
|
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, MemoryAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten); |
96 |
|
bytesWritten = ptrBytesWritten.ToInt32(); |
97 |
|
} |
98 |
|
#endregion |
99 |
|
|
100 |
|
#region RamDumper |
101 |
|
private interface IRamDumper |
102 |
|
{ |
103 |
|
bool DumpMemoryToFile(Process ppid, string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead); |
104 |
|
byte[] DumpMemoryToByteArray(Process ppid, uint MemoryAddress, uint bytesToRead, out int bytesRead); |
105 |
|
} |
106 |
|
private class RamDumper : IRamDumper |
107 |
|
{ |
108 |
|
public RamDumper() { } |
109 |
|
private void InitMemoryDump(out uint byte_alignment) |
110 |
{ |
{ |
111 |
FileInfo fi = new FileInfo(filename); |
byte_alignment = 102400; // get memory in 100mb chunks |
112 |
if (fi.Exists) |
} |
113 |
fi.Delete(); |
#region IRamDumper members |
114 |
using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) |
#region DumpMemoryToFile |
115 |
|
public bool DumpMemoryToFile(Process ppid, string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
116 |
|
{ |
117 |
|
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)); |
118 |
|
bytesRead = 0; |
119 |
|
uint byte_alignment = 0; |
120 |
|
// get common init parameters |
121 |
|
InitMemoryDump(out byte_alignment); |
122 |
|
uint address = MemoryAddress; |
123 |
|
uint _bytesToRead = bytesToRead; |
124 |
|
byte[] buffer = new byte[] { }; |
125 |
|
try |
126 |
{ |
{ |
127 |
BinaryWriter bw = new BinaryWriter(fs); |
FileInfo fi = new FileInfo(filename); |
128 |
//foreach (byte b in data) { bw.Write(b); } |
if (fi.Exists) |
129 |
|
fi.Delete(); |
130 |
for (uint i = 0; i <= bytesToRead;) |
using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) |
131 |
{ |
{ |
132 |
if (_bytesToRead < byte_alignment) |
BinaryWriter bw = new BinaryWriter(fs); |
133 |
{ |
//foreach (byte b in data) { bw.Write(b); } |
134 |
_bytesToRead = bytesToRead; |
|
135 |
buffer = new byte[_bytesToRead]; |
for (uint i = 0; i <= bytesToRead; ) |
|
} |
|
|
else |
|
|
{ |
|
|
_bytesToRead = byte_alignment; |
|
|
buffer = new byte[byte_alignment]; |
|
|
} |
|
|
IntPtr ptrBytesRead; |
|
|
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, _bytesToRead, out ptrBytesRead); |
|
|
bytesRead = ptrBytesRead.ToInt32(); |
|
|
bw.Write(buffer); |
|
|
bw.Flush(); |
|
|
|
|
|
if (_bytesToRead < byte_alignment) |
|
|
{ |
|
|
i += _bytesToRead; |
|
|
address += _bytesToRead; |
|
|
} |
|
|
else |
|
136 |
{ |
{ |
137 |
i += byte_alignment; |
if (_bytesToRead < byte_alignment) |
138 |
address += byte_alignment; |
{ |
139 |
} |
_bytesToRead = bytesToRead; |
140 |
|
buffer = new byte[_bytesToRead]; |
141 |
|
} |
142 |
|
else |
143 |
|
{ |
144 |
|
_bytesToRead = byte_alignment; |
145 |
|
buffer = new byte[byte_alignment]; |
146 |
|
} |
147 |
|
IntPtr ptrBytesRead; |
148 |
|
|
149 |
|
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, _bytesToRead, out ptrBytesRead); |
150 |
|
bytesRead = ptrBytesRead.ToInt32(); |
151 |
|
bw.Write(buffer); |
152 |
|
bw.Flush(); |
153 |
|
|
154 |
|
if (_bytesToRead < byte_alignment) |
155 |
|
{ |
156 |
|
i += _bytesToRead; |
157 |
|
address += _bytesToRead; |
158 |
|
} |
159 |
|
else |
160 |
|
{ |
161 |
|
i += byte_alignment; |
162 |
|
address += byte_alignment; |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
} |
167 |
|
bw.Close(); |
168 |
} |
} |
169 |
bw.Close(); |
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", ppid.Id, ppid.ProcessName)); |
170 |
|
return true; |
171 |
} |
} |
172 |
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", ppid.Id, ppid.ProcessName)); |
catch (OutOfMemoryException ex) |
173 |
return true; |
{ |
174 |
} |
logger.Error.WriteLine("Failed to dump 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)); |
175 |
catch (OutOfMemoryException ex) |
logger.Error.WriteLine("DumpMemory(): OutOfMemoryException"); |
176 |
{ |
logger.Error.WriteLine(ex.ToString()); |
177 |
logger.Error.WriteLine("Failed to dump 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)); |
} |
178 |
logger.Error.WriteLine("DumpMemory(): OutOfMemoryException"); |
catch (Exception ex) |
179 |
logger.Error.WriteLine(ex.ToString()); |
{ |
180 |
} |
logger.Error.WriteLine("Failed to dump 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)); |
181 |
catch (Exception ex) |
logger.Error.WriteLine("DumpMemory(): Exception"); |
182 |
{ |
logger.Error.WriteLine(ex.ToString()); |
183 |
logger.Error.WriteLine("Failed to dump 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)); |
} |
184 |
logger.Error.WriteLine("DumpMemory(): Exception"); |
return false; |
|
logger.Error.WriteLine(ex.ToString()); |
|
185 |
} |
} |
186 |
return false; |
#endregion |
187 |
} |
#region DumpMemoryToByteArray |
188 |
|
public byte[] DumpMemoryToByteArray(Process ppid, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
189 |
|
{ |
190 |
|
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)); |
191 |
|
bytesRead = 0; |
192 |
|
uint byte_alignment = 0; |
193 |
|
// get common init parameters |
194 |
|
InitMemoryDump(out byte_alignment); |
195 |
|
uint address = MemoryAddress; |
196 |
|
uint _bytesToRead = bytesToRead; |
197 |
|
byte[] buffer = new byte[] { }; |
198 |
|
try |
199 |
|
{ |
200 |
|
using (MemoryStream ms = new MemoryStream()) |
201 |
|
{ |
202 |
|
BinaryWriter bw = new BinaryWriter(ms); |
203 |
|
//foreach (byte b in data) { bw.Write(b); } |
204 |
|
|
205 |
public byte[] ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead) |
for (uint i = 0; i <= bytesToRead; ) |
206 |
{ |
{ |
207 |
bytesRead = 0; |
if (_bytesToRead < byte_alignment) |
208 |
uint address = MemoryAddress; |
{ |
209 |
List<byte[]> aligned_array_list = new List<byte[]>(); |
_bytesToRead = bytesToRead; |
210 |
try |
buffer = new byte[_bytesToRead]; |
211 |
{ |
} |
212 |
uint byte_alignment = 512; // 4mb alignment |
else |
213 |
|
{ |
214 |
|
_bytesToRead = byte_alignment; |
215 |
|
buffer = new byte[byte_alignment]; |
216 |
|
} |
217 |
|
IntPtr ptrBytesRead; |
218 |
|
|
219 |
|
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, _bytesToRead, out ptrBytesRead); |
220 |
|
bytesRead = ptrBytesRead.ToInt32(); |
221 |
|
bw.Write(buffer); |
222 |
|
bw.Flush(); |
223 |
|
|
224 |
|
if (_bytesToRead < byte_alignment) |
225 |
|
{ |
226 |
|
i += _bytesToRead; |
227 |
|
address += _bytesToRead; |
228 |
|
} |
229 |
|
else |
230 |
|
{ |
231 |
|
i += byte_alignment; |
232 |
|
address += byte_alignment; |
233 |
|
} |
234 |
|
|
235 |
for (uint i = 0; i <= bytesToRead; i += byte_alignment) |
|
236 |
|
} |
237 |
|
bw.Close(); |
238 |
|
return ms.ToArray(); |
239 |
|
} |
240 |
|
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)); |
241 |
|
} |
242 |
|
catch (OutOfMemoryException ex) |
243 |
{ |
{ |
244 |
byte[] buffer = new byte[byte_alignment]; |
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)); |
245 |
uint bytes_to_read = byte_alignment; |
logger.Error.WriteLine("DumpMemory(): OutOfMemoryException"); |
246 |
IntPtr ptrBytesRead; |
logger.Error.WriteLine(ex.ToString()); |
|
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, bytes_to_read, out ptrBytesRead); |
|
|
bytesRead = ptrBytesRead.ToInt32(); |
|
|
aligned_array_list.Add(buffer); |
|
|
address += byte_alignment; |
|
247 |
} |
} |
248 |
|
catch (Exception ex) |
249 |
//List<byte> big_array = new List<byte>(); |
{ |
250 |
//foreach (byte[] aligned_array in aligned_array_list) { foreach (byte b in aligned_array) { big_array.Add(b); } } |
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)); |
251 |
|
logger.Error.WriteLine("DumpMemory(): Exception"); |
252 |
return new byte[] { }; |
logger.Error.WriteLine(ex.ToString()); |
253 |
} |
} |
254 |
catch (OutOfMemoryException ex) |
return new byte[]{}; |
|
{ |
|
|
logger.Error.WriteLine("ReadProcessMemory(): OutOfMemoryException"); |
|
|
logger.Error.WriteLine(ex.ToString()); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
logger.Error.WriteLine("ReadProcessMemory(): Exception"); |
|
|
logger.Error.WriteLine(ex.ToString()); |
|
255 |
} |
} |
256 |
return new byte[] { }; |
#endregion |
257 |
|
#endregion |
258 |
} |
} |
259 |
|
#endregion |
|
public void WriteProcessMemory(UIntPtr MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
|
|
{ |
|
|
IntPtr ptrBytesWritten; |
|
|
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, MemoryAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten); |
|
|
|
|
|
bytesWritten = ptrBytesWritten.ToInt32(); |
|
|
} |
|
|
|
|
|
|
|
260 |
/// <summary> |
/// <summary> |
261 |
/// ProcessMemoryReader is a class that enables direct reading a process memory |
/// ProcessMemoryReader is a class that enables direct reading a process memory |
262 |
/// </summary> |
/// </summary> |
263 |
class ProcessMemoryReaderApi |
public class ProcessMemoryReaderApi |
264 |
{ |
{ |
265 |
// constants information can be found in <winnt.h> |
// constants information can be found in <winnt.h> |
266 |
[Flags] |
[Flags] |