-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDMAPluginExt.cs
More file actions
409 lines (325 loc) · 13.5 KB
/
DMAPluginExt.cs
File metadata and controls
409 lines (325 loc) · 13.5 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using ReClassNET.Core;
using ReClassNET.Debugger;
using ReClassNET.Extensions;
using ReClassNET.Memory;
using ReClassNET.Plugins;
using vmmsharp;
using System.Data;
using System.Runtime.InteropServices;
using ReClassNET.MemoryScanner;
using static vmmsharp.Vmm;
using static System.Collections.Specialized.BitVector32;
using ReClassNET.Forms;
using ReClassNET;
namespace DMAPlugin
{
[Flags]
public enum SectionCharacteristics : uint
{
IMAGE_SCN_MEM_EXECUTE = 0x20000000,
IMAGE_SCN_MEM_READ = 0x40000000,
IMAGE_SCN_MEM_WRITE = 0x80000000,
IMAGE_SCN_MEM_DISCARDABLE = 0x02000000,
IMAGE_SCN_MEM_NOT_CACHED = 0x04000000,
IMAGE_SCN_MEM_NOT_PAGED = 0x08000000,
IMAGE_SCN_CNT_CODE = 0x00000020,
IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040,
IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080,
}
public struct ProcessData
{
public uint Id { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public override string ToString()
{
return $"PID: {Id}, Name: {Name}, Path: {Path}";
}
public bool isValid()
{
return Id != 0 && Name != null;
}
}
public class DMAPluginExt : Plugin, ICoreProcessFunctions
{
private readonly object sync = new object();
private IPluginHost host;
private ProcessBrowserForm procBrowser;
private Vmm vmm;
private ProcessData[] processList;
private Vmm.MAP_MODULEENTRY[] mModule;
public override bool Initialize(IPluginHost host)
{
Contract.Requires(host != null);
this.host = host ?? throw new ArgumentNullException(nameof(host));
host.Process.CoreFunctions.RegisterFunctions("DMA", this);
vmm = new Vmm("", "-device","fpga");
procBrowser = new ProcessBrowserForm(Program.Settings.LastProcess);
return true;
}
public override void Terminate()
{
processList = null;
host = null;
vmm.Close();
}
private void FetchProcessListFromDMA()
{
uint[] dwPidList = vmm.PidList();
processList = new ProcessData[dwPidList.Length];
int i = 0;
foreach (uint dwPid in dwPidList)
{
Vmm.PROCESS_INFORMATION procInfo = vmm.ProcessGetInformation(dwPid);
ProcessData pd = new ProcessData();
pd.Id = procInfo.dwPID;
pd.Name = procInfo.szName;
pd.Path = procInfo.szNameLong;
processList[i++] = pd;
}
}
public void EnumerateProcesses(EnumerateProcessCallback callbackProcess)
{
if (callbackProcess == null)
{
return;
}
FetchProcessListFromDMA();
foreach (ProcessData process in processList)
{
if (process.isValid())
{
var data = new EnumerateProcessData
{
Id = (IntPtr)process.Id,
Name = process.Name,
Path = process.Path
};
callbackProcess(ref data);
}
}
}
private bool EnumerateRemoteModules(IntPtr process, EnumerateRemoteModuleCallback callbackModule)
{
if(callbackModule == null)
{
return false;
}
mModule = vmm.Map_GetModule( (uint)process, false);
if(mModule.Length == 0)
{
return false;
}
foreach (Vmm.MAP_MODULEENTRY module in mModule)
{
var data = new EnumerateRemoteModuleData
{
BaseAddress = (IntPtr)module.vaBase,
Size = (IntPtr)module.cbImageSize,
Path = module.wszFullName
};
callbackModule(ref data);
}
return true;
}
private String VadMap_Protection(Vmm.MAP_VADENTRY pVad)
{
char[] protection = new char[6];
byte vh = (byte)(pVad.Protection >> 3);
byte vl = (byte)(pVad.Protection & 7);
protection[0] = pVad.fPrivateMemory ? 'p' : '-'; // PRIVATE MEMORY
protection[1] = (vh & 2) != 0 ? ((vh & 1) != 0 ? 'm' : 'g') : ((vh & 1) != 0 ? 'n' : '-'); // -/NO_CACHE/GUARD/WRITECOMBINE
protection[2] = (vl == 1 || vl == 3 || vl == 4 || vl == 6) ? 'r' : '-'; // READ
protection[3] = (vl & 4) != 0 ? 'w' : '-'; // WRITE
protection[4] = (vl & 2) != 0 ? 'x' : '-'; // EXECUTE
protection[5] = (vl == 5 || vl == 7) ? 'c' : '-'; // COPY ON WRITE
if (protection[1] != '-' && protection[2] == '-' && protection[3] == '-' && protection[4] == '-' && protection[5] == '-')
{
protection[1] = '-';
}
return new string(protection);
}
/// <summary>Reports a single module and section for the loaded file.</summary>
/// <param name="process">The process handle (in our case the process PID).</param>
/// <param name="callbackSection">The callback which gets called for every section.</param>
/// <param name="callbackModule">The callback which gets called for every module.</param>
public void EnumerateRemoteSectionsAndModules(IntPtr process, EnumerateRemoteSectionCallback callbackSection, EnumerateRemoteModuleCallback callbackModule)
{
EnumerateRemoteModules(process, callbackModule);
foreach (Vmm.MAP_MODULEENTRY module in mModule)
{
Vmm.IMAGE_SECTION_HEADER[] SECTIONs = vmm.ProcessGetSections((uint)process, module.wszText);
foreach (IMAGE_SECTION_HEADER SECTION in SECTIONs)
{
var section = new EnumerateRemoteSectionData
{
BaseAddress = (IntPtr)(module.vaBase + SECTION.VirtualAddress),
Size = (IntPtr)SECTION.MiscPhysicalAddressOrVirtualSize,
Protection = SectionProtection.NoAccess,
Type = SectionType.Image,
ModulePath = module.wszText,
Name = SECTION.Name
};
SectionCharacteristics characteristics = (SectionCharacteristics)SECTION.Characteristics;
if (characteristics.HasFlag(SectionCharacteristics.IMAGE_SCN_MEM_READ))
section.Protection |= SectionProtection.Read;
if (characteristics.HasFlag(SectionCharacteristics.IMAGE_SCN_MEM_WRITE))
section.Protection |= SectionProtection.Write;
if (characteristics.HasFlag(SectionCharacteristics.IMAGE_SCN_MEM_EXECUTE))
section.Protection |= SectionProtection.Execute;
if (characteristics.HasFlag(SectionCharacteristics.IMAGE_SCN_CNT_CODE))
section.Category = SectionCategory.CODE;
if (characteristics.HasFlag(SectionCharacteristics.IMAGE_SCN_CNT_INITIALIZED_DATA))
section.Category = SectionCategory.DATA;
if (characteristics.HasFlag(SectionCharacteristics.IMAGE_SCN_CNT_UNINITIALIZED_DATA))
section.Category = SectionCategory.DATA;
callbackSection(ref section);
}
}
Vmm.MAP_VADENTRY[] mVad = vmm.Map_GetVad((uint)process);
foreach (MAP_VADENTRY vad in mVad)
{
if (vad.CommitCharge <= 0)
continue;
var section = new EnumerateRemoteSectionData
{
BaseAddress = (IntPtr)vad.vaStart,
Size = (IntPtr)vad.cbSize,
Protection = SectionProtection.NoAccess,
};
if (vad.fFile)
{
section.Type = SectionType.Mapped;
section.Category = SectionCategory.Unknown;
}
else if (vad.fPrivateMemory)
{
section.Type = SectionType.Private;
section.Category = SectionCategory.HEAP;
}
else if (vad.fStack || vad.fTeb || vad.fPageFile)
{
section.Type = SectionType.Private;
section.Category = SectionCategory.DATA;
}
else
{
section.Type = SectionType.Private;
section.Category = SectionCategory.Unknown;
}
String protectionStr = VadMap_Protection(vad);
if (protectionStr.Contains("p"))
section.Protection |= SectionProtection.NoAccess;
if (protectionStr.Contains("g"))
section.Protection |= SectionProtection.Guard;
if (protectionStr.Contains("r"))
section.Protection |= SectionProtection.Read;
if (protectionStr.Contains("w"))
section.Protection |= SectionProtection.Write;
if (protectionStr.Contains("x"))
section.Protection = SectionProtection.Execute;
if(protectionStr.Contains("c"))
section.Protection = SectionProtection.CopyOnWrite;
callbackSection(ref section);
}
/*Vmm.MAP_HEAP mHeap = vmm.Map_GetHeap( (uint)process);
foreach (MAP_HEAPSEGMENTENTRY segment in mHeap.segments)
{
var section = new EnumerateRemoteSectionData
{
BaseAddress = (IntPtr)segment.va,
Size = (IntPtr)segment.cb,
Protection = SectionProtection.NoAccess,
Type = SectionType.Private,
Category = SectionCategory.HEAP
};
uint tpHeapSegment = segment.tpHeapSegment;
if (tpHeapSegment == 1 || tpHeapSegment == 2 || tpHeapSegment == 3 || tpHeapSegment == 5 || tpHeapSegment == 6 || tpHeapSegment == 7)
section.Protection |= SectionProtection.Read;
callbackSection(ref section);
}*/
}
/// <summary>Request a handle to the remote process (in our case, the handle is just the PID).</summary>
/// <param name="pid">The process ID.</param>
/// <param name="desiredAccess">access type (not needed).</param>
/// <returns>the pid unchanged as the process handle.</returns>
public IntPtr OpenRemoteProcess(IntPtr pid, ProcessAccess desiredAccess)
{
return pid;
}
public bool IsProcessValid(IntPtr process)
{
lock (sync)
{
return vmm.ProcessGetInformation((uint)process).dwState != 0xFFFFFFFF;
}
}
/// <summary>Close the remote process.</summary>
/// <param name="process">The process handle (in our case the process PID).</param>
public void CloseRemoteProcess(IntPtr process)
{
if (process == null)
{
return;
}
process = IntPtr.Zero;
}
public bool ReadRemoteMemory(IntPtr process, IntPtr address, ref byte[] buffer, int offset, int size)
{
byte[] memRead = vmm.MemRead((uint)process, (ulong)address, (uint)size, Vmm.FLAG_NOCACHE);
if (memRead == null)
{
return false;
}
if (memRead.Length != size)
{
return false;
}
Buffer.BlockCopy(memRead, 0, buffer, offset, size);
return true;
}
public bool WriteRemoteMemory(IntPtr process, IntPtr address, ref byte[] buffer, int offset, int size)
{
//Not supported
return false;
}
public void ControlRemoteProcess(IntPtr process, ControlRemoteProcessAction action)
{
//Not supported
return;
}
public bool AttachDebuggerToProcess(IntPtr id)
{
//Not supported
return false;
}
public void DetachDebuggerFromProcess(IntPtr id)
{
//Not supported
return;
}
public bool AwaitDebugEvent(ref DebugEvent evt, int timeoutInMilliseconds)
{
//Not supported
return false;
}
public void HandleDebugEvent(ref DebugEvent evt)
{
//Not supported
return;
}
public bool SetHardwareBreakpoint(IntPtr id, IntPtr address, HardwareBreakpointRegister register, HardwareBreakpointTrigger trigger, HardwareBreakpointSize size, bool set)
{
//Not supported
return false;
}
}
}