-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNative.cs
More file actions
261 lines (214 loc) · 6.82 KB
/
Native.cs
File metadata and controls
261 lines (214 loc) · 6.82 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using CG.SDK.Dotnet.Attributes;
using CG.SDK.Dotnet.Helper;
using CG.SDK.Dotnet.Helper.Platform;
using CG.SDK.Dotnet.Plugin.Target;
namespace CG.Memory;
[PluginInfo(
Name = nameof(Native),
Version = "5.0.0",
Author = "CorrM",
Description = "Use current system API to read/write memory process",
WebsiteLink = "https://github.com/CheatGear",
SourceCodeLink = "https://github.com/CheatGear/Memory.Native"
)]
public sealed class Native : TargetHandlerPlugin<Win32MemoryHandler>
{
private readonly int _memoryBasicInformationSize;
private readonly Win32.SystemInfo _sysInfo;
private nuint? _maxValidAddress;
private nuint? _minValidAddress;
private nint _processHandle;
public Native()
{
_memoryBasicInformationSize = Marshal.SizeOf<Win32.MemoryBasicInformation>();
Win32.GetSystemInfo(out _sysInfo);
MemoryHandler = new Win32MemoryHandler(this);
}
public override Win32MemoryHandler MemoryHandler { get; }
private static bool IsValidHandle(nint handle)
{
return handle != nint.Zero && handle != Win32.InvalidHandleValue;
}
private void AssertTarget()
{
if (IsValidHandle(_processHandle))
{
return;
}
throw new Exception("No target");
}
protected override void OnTargetFree()
{
_minValidAddress = null;
_maxValidAddress = null;
MemoryHandler.OnTargetFree();
try
{
// Suspend games case an Exception
if (IsValidHandle(_processHandle))
{
Win32.CloseHandle(_processHandle);
}
}
catch (Exception)
{
// ignored
}
}
protected override bool OnTargetLock(int processId)
{
_processHandle = Win32.OpenProcess(Win32.ProcessAccessFlags.All, false, processId);
return IsValidHandle(_processHandle);
}
protected override void OnTargetReady()
{
_minValidAddress = GetMinValidAddress();
_maxValidAddress = GetMaxValidAddress();
MemoryHandler.OnTargetReady(_processHandle);
}
protected override IReadOnlyList<MemModuleInfo> GetModules()
{
AssertTarget();
var ret = new List<MemModuleInfo>();
int capacity = 1024;
var sb = new StringBuilder(capacity);
Win32.QueryFullProcessImageName(_processHandle, 0, sb, ref capacity);
string fullPath = sb.ToString(0, capacity);
// To Avoid Some Games not share it's modules, or could be emulator/protected game
nint hSnap = Win32.CreateToolhelp32Snapshot(
Win32.SnapshotFlags.Module | Win32.SnapshotFlags.Module32,
ProcessId
);
if (!IsValidHandle(hSnap))
{
return ret;
}
try
{
var modEntry = new Win32.ModuleEntry32 { DwSize = (uint)Marshal.SizeOf(typeof(Win32.ModuleEntry32)) };
if (Win32.Module32First(hSnap, ref modEntry))
{
do
{
var mod = new MemModuleInfo
{
Handle = modEntry.HModule,
Address = modEntry.ModBaseAddr.ToUIntPtr(),
Size = modEntry.ModBaseSize,
Name = modEntry.SzModule,
Path = modEntry.SzExePath,
MainModule = modEntry.SzExePath == fullPath,
};
ret.Add(mod);
} while (Win32.Module32Next(hSnap, ref modEntry));
}
}
catch
{
// Ignore
}
Win32.CloseHandle(hSnap);
return ret;
}
protected override bool GetIs64Bit()
{
AssertTarget();
return UtilsExtensions.Is64BitProcess(_processHandle);
}
protected override int GetSystemPageSize()
{
return (int)_sysInfo.PageSize;
}
public override nuint GetMinValidAddress()
{
AssertTarget();
return _minValidAddress ?? _sysInfo.MinimumApplicationAddress;
}
public override nuint GetMaxValidAddress()
{
AssertTarget();
if (_maxValidAddress is not null)
{
return _maxValidAddress.Value;
}
return (nuint)(_sysInfo.ProcessorArchitecture == Win32.ProcessorArchitecture.X64 && Process64Bit
? 0x800000000000
: 0x100000000);
}
public override MemRegionInfo? GetMemoryRegion(nuint address)
{
AssertTarget();
// Get Region information
bool valid = Win32.VirtualQueryEx(
_processHandle,
address,
out Win32.MemoryBasicInformation info,
(uint)_memoryBasicInformationSize
) ==
_memoryBasicInformationSize;
if (!valid)
{
return null;
}
var region = new MemRegionInfo
{
AllocationBase = info.AllocationBase,
BaseAddress = info.BaseAddress,
Size = info.RegionSize.ToNum(),
State = (uint)info.State,
Protect = (uint)info.Protect,
Type = (uint)info.Type,
};
return region;
}
public override bool IsValidRegion(MemRegionInfo memRegion)
{
AssertTarget();
bool check = ((Win32.MemoryState)memRegion.State & Win32.MemoryState.MemCommit) != 0;
if (!check)
{
return false;
}
check = ((Win32.MemoryProtection)memRegion.Protect & Win32.MemoryProtection.PageNoAccess) == 0 &&
((Win32.MemoryProtection)memRegion.Protect & Win32.MemoryProtection.PageTargetsInvalid) == 0 &&
((Win32.MemoryProtection)memRegion.Protect & Win32.MemoryProtection.PageGuard) == 0 &&
((Win32.MemoryProtection)memRegion.Protect & Win32.MemoryProtection.PageNocache) == 0;
return check;
}
public override bool IsValidProcess(int processId)
{
try
{
Process.GetProcessById(processId).Dispose();
}
catch
{
return false;
}
return true;
}
public override bool IsValidTarget()
{
return IsValidProcess(ProcessId) && IsValidHandle(_processHandle);
}
public override bool Suspend()
{
AssertTarget();
return Win32.NtSuspendProcess(_processHandle) >= 0;
}
public override bool Resume()
{
AssertTarget();
return Win32.NtResumeProcess(_processHandle) >= 0;
}
public override bool Terminate()
{
AssertTarget();
return Win32.NtTerminateProcess(_processHandle, 0) >= 0;
}
}