|
| 1 | +using System; |
| 2 | +using System.Diagnostics.Contracts; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +namespace ReClassNET.Memory |
| 6 | +{ |
| 7 | + public class Dumper |
| 8 | + { |
| 9 | + private readonly RemoteProcess process; |
| 10 | + |
| 11 | + public Dumper(RemoteProcess process) |
| 12 | + { |
| 13 | + Contract.Requires(process != null); |
| 14 | + |
| 15 | + this.process = process; |
| 16 | + } |
| 17 | + |
| 18 | + /// <summary>Dumps a section to the given stream.</summary> |
| 19 | + /// <param name="address">The begin of the section.</param> |
| 20 | + /// <param name="size">The size of the section.</param> |
| 21 | + /// <param name="stream">The stream to dump to.</param> |
| 22 | + public void DumpSection(IntPtr address, int size, Stream stream) |
| 23 | + { |
| 24 | + Contract.Requires(stream != null); |
| 25 | + |
| 26 | + var data = process.ReadRemoteMemory(address, size); |
| 27 | + |
| 28 | + stream.Write(data, 0, data.Length); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary>Dumps a module to the given stream. The section headers of the pe header get fixed to make a valid pe file.</summary> |
| 32 | + /// <param name="address">The begin of the module.</param> |
| 33 | + /// <param name="size">The size of the module.</param> |
| 34 | + /// <param name="stream">The stream to dump to.</param> |
| 35 | + public void DumpModule(IntPtr address, int size, Stream stream) |
| 36 | + { |
| 37 | + Contract.Requires(stream != null); |
| 38 | + |
| 39 | + var data = process.ReadRemoteMemory(address, size); |
| 40 | + |
| 41 | + var pe = new SimplePeHeader(data); |
| 42 | + |
| 43 | + // Fix the section headers. |
| 44 | + using (var bw = new BinaryWriter(new MemoryStream(data))) |
| 45 | + { |
| 46 | + for (var i = 0; i < pe.NumberOfSections; ++i) |
| 47 | + { |
| 48 | + var offset = pe.SectionOffset(i); |
| 49 | + bw.Seek(offset + 16, SeekOrigin.Begin); |
| 50 | + bw.Write(BitConverter.ToUInt32(data, offset + 8)); // SizeOfRawData = VirtualSize |
| 51 | + bw.Write(BitConverter.ToUInt32(data, offset + 12)); // PointerToRawData = VirtualAddress |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + stream.Write(data, 0, data.Length); |
| 56 | + } |
| 57 | + |
| 58 | + private class SimplePeHeader |
| 59 | + { |
| 60 | + private readonly byte[] data; |
| 61 | + |
| 62 | + private int e_lfanew => BitConverter.ToInt32(data, 60); |
| 63 | + |
| 64 | + private int FileHeader => e_lfanew + 4; |
| 65 | + |
| 66 | + public int NumberOfSections => BitConverter.ToInt16(data, FileHeader + 2); |
| 67 | + |
| 68 | + private int SizeOfOptionalHeader => BitConverter.ToInt16(data, FileHeader + 16); |
| 69 | + |
| 70 | + private int FirstSectionOffset => e_lfanew + 24 + SizeOfOptionalHeader; |
| 71 | + |
| 72 | + public int SectionOffset(int index) => FirstSectionOffset + index * 40; |
| 73 | + |
| 74 | + public SimplePeHeader(byte[] data) |
| 75 | + { |
| 76 | + this.data = data; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments