-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryScanner.cs
More file actions
202 lines (185 loc) · 7.16 KB
/
MemoryScanner.cs
File metadata and controls
202 lines (185 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
namespace ProcessMemoryScanner
{
public class MemoryScanner : IDisposable
{
private IntPtr handle;
private Process process;
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_BASIC_INFORMATION
{
public IntPtr BaseAddress;
public IntPtr AllocationBase;
public uint AllocationProtect;
public IntPtr RegionSize;
public uint State;
public uint Protect;
public uint Type;
}
public enum State : uint
{
MEM_COMMIT = 0x00001000,
MEM_RESERVE = 0x00002000,
MEM_RESET = 0x00080000,
MEM_RESET_UNDO = 0x1000000
}
public enum AllocationProtect : uint
{
PAGE_EXECUTE = 0x00000010,
PAGE_EXECUTE_READ = 0x00000020,
PAGE_EXECUTE_READWRITE = 0x00000040,
PAGE_EXECUTE_WRITECOPY = 0x00000080,
PAGE_NOACCESS = 0x00000001,
PAGE_READONLY = 0x00000002,
PAGE_READWRITE = 0x00000004,
PAGE_WRITECOPY = 0x00000008,
PAGE_GUARD = 0x00000100,
PAGE_NOCACHE = 0x00000200,
PAGE_WRITECOMBINE = 0x00000400
}
public enum Type : uint
{
MEM_IMAGE = 0x1000000,
MEM_MAPPED = 0x40000,
MEM_PRIVATE = 0x20000
}
public MemoryScanner(Func<Process, bool> filter)
{
this.process = Process.GetProcesses().SingleOrDefault(filter);
if(this.process == null)
{
throw new InvalidOperationException($"Cannot find process!");
}
var access = Kernel32.ProcessAccessType.PROCESS_QUERY_INFORMATION |
Kernel32.ProcessAccessType.PROCESS_VM_READ |
Kernel32.ProcessAccessType.PROCESS_VM_WRITE |
Kernel32.ProcessAccessType.PROCESS_VM_OPERATION;
this.handle = Kernel32.OpenProcess((uint)access, 1, (uint)this.process.Id);
if (this.handle == null)
{
throw new InvalidOperationException("Cannot open process");
}
}
public T ReadMemory<T>(IntPtr address)
{
var size = Marshal.SizeOf(default(T));
var bytes = this.ReadMemory(address, (uint) size);
var ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(bytes, 0, ptr, size);
var toReturn = (T)Marshal.PtrToStructure(ptr, typeof(T));
Marshal.FreeHGlobal(ptr);
return toReturn;
}
public byte[] ReadMemory(IntPtr address, uint byteArrayLength)
{
var lpNumberOfBytesRead = IntPtr.Zero;
var buffer = new byte[byteArrayLength];
Kernel32.ReadProcessMemory(this.handle, address, buffer, byteArrayLength, out lpNumberOfBytesRead);
return buffer;
}
public void WriteMemory(IntPtr address, byte[] data)
{
var length = data.Length;
var lpNumberOfBytesWritten = IntPtr.Zero;
Kernel32.WriteProcessMemory(this.handle, address, data, (uint)data.Length, out lpNumberOfBytesWritten);
}
public void WriteMemory<T>(IntPtr address, T data)
{
var size = Marshal.SizeOf(data);
var bytes = new byte[size];
var ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(data, ptr, false);
Marshal.Copy(ptr, bytes, 0, size);
Marshal.FreeHGlobal(ptr);
this.WriteMemory(address, bytes);
}
public List<MEMORY_BASIC_INFORMATION> FindMemoryRegion(Func<MEMORY_BASIC_INFORMATION, bool> filter)
{
var result = new List<MEMORY_BASIC_INFORMATION>();
Kernel32.GetSystemInfo(out var systemInformation);
var minAddress = (ulong)systemInformation.minimumApplicationAddress;
var maxAddress = (ulong)systemInformation.maximumApplicationAddress;
var memoryDump = new MEMORY_BASIC_INFORMATION();
var currentAddress = minAddress;
while (currentAddress < maxAddress)
{
var readResult = Kernel32.VirtualQueryEx(this.handle, new IntPtr((long)currentAddress), out memoryDump, (uint)Marshal.SizeOf(memoryDump));
if (readResult != 0)
{
if (filter(memoryDump))
{
result.Add(memoryDump);
}
}
else
{
break;
}
currentAddress += (ulong) memoryDump.RegionSize;
}
return result;
}
public IntPtr FindByAoB(byte[] AoBSignature, MEMORY_BASIC_INFORMATION memoryRegion)
{
var memory = this.ReadMemory(memoryRegion.BaseAddress, (uint)memoryRegion.RegionSize);
var memoryOffset = memory.IndexOf(AoBSignature);
if (memoryOffset == -1)
{
return IntPtr.Zero;
}
return IntPtr.Add(memoryRegion.BaseAddress, memoryOffset);
}
public IntPtr FindByAoBWithWildCard(byte?[] AoBSignature, MEMORY_BASIC_INFORMATION memoryRegion)
{
var memory = (this.ReadMemory(memoryRegion.BaseAddress, (uint)memoryRegion.RegionSize)).ToWildCardByteArray();
var memoryOffset = memory.IndexOfWithWildCard(AoBSignature);
if (memoryOffset == -1)
{
return IntPtr.Zero;
}
return IntPtr.Add(memoryRegion.BaseAddress, memoryOffset);
}
public void ReplaceByAoB(byte[] AoBSignature, byte[] AoBToReplace, MEMORY_BASIC_INFORMATION memoryRegion)
{
this.WriteMemory(this.FindByAoB(AoBSignature, memoryRegion), AoBToReplace);
}
public void SuspendProcess()
{
foreach (ProcessThread pT in this.process.Threads)
{
var pOpenThread = Kernel32.OpenThread(Kernel32.ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
if (pOpenThread == IntPtr.Zero)
{
continue;
}
Kernel32.SuspendThread(pOpenThread);
Kernel32.CloseHandle(pOpenThread);
}
}
public void ResumeProcess()
{
foreach (ProcessThread pT in this.process.Threads)
{
var pOpenThread = Kernel32.OpenThread(Kernel32.ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
if (pOpenThread == IntPtr.Zero)
{
continue;
}
var suspendCount = 0;
do
{
suspendCount = Kernel32.ResumeThread(pOpenThread);
} while (suspendCount > 0);
Kernel32.CloseHandle(pOpenThread);
}
}
public void Dispose()
{
Kernel32.CloseHandle(this.handle);
}
}
}