-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSSharpPatcher.cs
More file actions
222 lines (188 loc) · 6.21 KB
/
CSSharpPatcher.cs
File metadata and controls
222 lines (188 loc) · 6.21 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
using System.Data;
using System.Runtime.InteropServices;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Extensions;
using CounterStrikeSharp.API.Modules.Memory;
using Microsoft.Extensions.Logging;
namespace CSSharpPatcher;
public class PatchEntryConfig
{
public string signature { get; set; } = "";
public string patch { get; set; } = "";
}
public class PatchConfig
{
public string module { get; set; } = "server";
public PatchEntryConfig windows { get; set; } = new();
public PatchEntryConfig linux { get; set; } = new();
}
public class PatcherConfig : BasePluginConfig
{
public Dictionary<string, PatchConfig> Patches { get; set; } = new();
public List<string> EnabledPatches { get; set; } = new();
public bool RestoreWhenUnload { get; set; } = true;
}
public record PatchInfo(
string Name,
nint Addr,
List<byte> OriginalBytes
);
public class CSSharpPatcher : BasePlugin, IPluginConfig<PatcherConfig>
{
public override string ModuleName => "CSSharpPatcher";
public override string ModuleVersion => "1.0.1";
public override string ModuleAuthor => "samyyc";
public PatcherConfig Config { get; set; } = new();
private List<PatchInfo> _PatchedPatchs = new();
public void OnConfigParsed(PatcherConfig config)
{
Config = config;
}
private string GetModulePath(string module)
{
var dir = new string[] { "server", "host", "matchmaking", "client" }.Contains(module) ? Constants.GameBinaryPath : Constants.RootBinaryPath;
return Path.Join(dir, Constants.ModulePrefix + module + Constants.ModuleSuffix);
}
public override void Load(bool hotReload)
{
if (hotReload) return;
foreach (var name in Config.EnabledPatches)
{
ApplyPatch(name);
}
}
public override void Unload(bool hotReload)
{
if (hotReload) return;
if (!Config.RestoreWhenUnload) return;
foreach (var patch in _PatchedPatchs)
{
RestorePatch(patch);
}
_PatchedPatchs.Clear();
}
public void ApplyPatch(string name)
{
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var patch = Config.Patches[name];
var entry = isWindows ? patch.windows : patch.linux;
var addr = NativeAPI.FindSignature(GetModulePath(patch.module), entry.signature);
if (addr == 0)
{
Logger.LogError($"Patch '{name}' has invalid signature, skipping...");
return;
}
var payload = entry.patch.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Select(hex => Convert.ToByte(hex, 16))
.ToList();
List<byte> originalBytes = new();
MemoryPatch.SetMemAccess(addr, payload.Count());
for (int j = 0; j < payload.Count(); j++)
{
originalBytes.Add(Marshal.ReadByte(addr, j));
Marshal.WriteByte(addr, j, payload[j]);
}
_PatchedPatchs.Add(new(name, addr, originalBytes));
Logger.LogInformation($"Patch '{name}' successfully patched at 0x{addr:X}.");
}
public void RestorePatch(PatchInfo patch)
{
MemoryPatch.SetMemAccess(patch.Addr, patch.OriginalBytes.Count());
for (int i = 0; i < patch.OriginalBytes.Count(); i++)
{
Marshal.WriteByte(patch.Addr, i, patch.OriginalBytes[i]);
}
Logger.LogInformation($"Patch '{patch.Name}' successfully restored.");
}
[ConsoleCommand("css_patcher")]
[CommandHelper(minArgs: 1, usage: "patch/restore/status [patch name]", CommandUsage.SERVER_ONLY)]
public void PatcherCommand(CCSPlayerController? player, CommandInfo info)
{
void HandleStatusCommand()
{
Console.WriteLine("====================[ Patcher ]====================");
foreach (var (name, p) in Config.Patches)
{
if (!_PatchedPatchs.Any(patch => patch.Name == name))
{
Console.WriteLine($"× {name}\t (Unpatched)");
}
else
{
var patch = _PatchedPatchs.First(patch => patch.Name == name);
Console.WriteLine($"√ {name}\t (0x{patch.Addr:X})");
}
}
Console.WriteLine("===================================================");
}
void HandlePatchCommand()
{
var name = info.GetArg(2);
if (_PatchedPatchs.Any(patch => patch.Name == name))
{
info.ReplyToCommand($"Patch '{name}' has already patched.");
return;
}
if (!Config.Patches.ContainsKey(name))
{
info.ReplyToCommand($"Cannot find patch '{name}'.");
return;
}
ApplyPatch(name);
}
void HandleRestoreCommand()
{
var name = info.GetArg(2);
if (!_PatchedPatchs.Any(patch => patch.Name == name))
{
info.ReplyToCommand($"Patch '{name}' is not patched.");
return;
}
if (!Config.Patches.ContainsKey(name))
{
info.ReplyToCommand($"Cannot find patch '{name}'.");
return;
}
var patch = _PatchedPatchs.First(patch => patch.Name == name);
RestorePatch(patch);
_PatchedPatchs.Remove(patch);
}
switch (info.GetArg(1))
{
case "status":
HandleStatusCommand();
break;
case "patch":
HandlePatchCommand();
break;
case "restore":
HandleRestoreCommand();
break;
default:
info.ReplyToCommand("Unknown command");
break;
}
}
}
// credits to @xstage
// https://discord.com/channels/1160907911501991946/1297699678556524555
static class MemoryPatch {
[DllImport("libc", EntryPoint = "mprotect")]
public static extern int MProtect(nint address, int len, int protect);
[DllImport("kernel32.dll")]
public unsafe static extern bool VirtualProtect(nint address, int dwSize, int newProtect, int* oldProtect);
public unsafe static bool SetMemAccess(nint addr, int size)
{
if (addr == nint.Zero)
throw new ArgumentNullException(nameof(addr));
const int PAGESIZE = 4096;
nint LALIGN(nint addr) => addr & ~(PAGESIZE - 1);
int LALDIF(nint addr) => (int)(addr % PAGESIZE);
int* oldProtect = stackalloc int[1];
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ?
MProtect(LALIGN(addr), size + LALDIF(addr), 7) == 0 : VirtualProtect(addr, size, 0x40, oldProtect);
}
}