forked from Xkein/YRDynamicPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookTransferStation.cs
More file actions
301 lines (254 loc) · 7.55 KB
/
HookTransferStation.cs
File metadata and controls
301 lines (254 loc) · 7.55 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DynamicPatcher
{
abstract class HookTransferStation : IDisposable
{
public LinkedList<HookInfo> HookInfos { get; set; } = new LinkedList<HookInfo>();
public HookInfo MaxHookInfo { get => HookInfos.Max(); }
public abstract bool Match(HookType hookType);
protected byte[] code_over;
public HookTransferStation(HookInfo info)
{
SetHook(info);
}
virtual public void SetHook(HookInfo info)
{
UnHook();
HookInfos.AddLast(info);
info.TransferStation = this;
HookAttribute hook = MaxHookInfo.GetHookAttribute();
if (hook.Size > 0)
{
code_over = new byte[hook.Size];
MemoryHelper.Read(hook.Address, code_over, hook.Size);
}
}
// write back origin code
virtual public void UnHook()
{
if (code_over != null)
{
HookAttribute hook = MaxHookInfo.GetHookAttribute();
MemoryHelper.Write(hook.Address, code_over, hook.Size);
ASMWriter.FlushInstructionCache(hook.Address, Math.Max(hook.Size, 5));
code_over = null;
}
}
// unhook a hook
virtual public void UnHook(HookInfo info)
{
UnHook();
HookInfos.Remove(info);
// delegate unavailable
info.CallableDlg = null;
if (HookInfos.Count > 0)
{
HookInfos.Remove(MaxHookInfo);
SetHook(MaxHookInfo);
}
}
private bool disposedValue;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
}
UnHook();
disposedValue = true;
}
}
~HookTransferStation()
{
Dispose(disposing: false);
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
class JumpHookTransferStation : HookTransferStation
{
public JumpHookTransferStation(HookInfo info) : base(info)
{
}
public override bool Match(HookType hookType)
{
return hookType == HookType.SimpleJumpToRet || hookType == HookType.DirectJumpToHook;
}
public override void SetHook(HookInfo info)
{
if (HookInfos.Count > 0)
{
throw new InvalidOperationException("JumpHook can not mix with other hook");
}
base.SetHook(info);
HookAttribute hook = info.GetHookAttribute();
switch (hook.Type)
{
case HookType.SimpleJumpToRet:
int address = (int)info.GetReturnValue();
Logger.Log("jump to address: 0x{0:X}", address);
ASMWriter.WriteJump(new JumpStruct(hook.Address, address));
break;
case HookType.DirectJumpToHook:
int callable = (int)info.GetCallable();
Logger.Log("jump to callable: 0x{0:X}", callable);
ASMWriter.WriteJump(new JumpStruct(hook.Address, callable));
break;
default:
Logger.LogError("found unkwnow jump hook: " + info.Member.Name);
break;
}
}
}
class AresHookTransferStation : HookTransferStation
{
static readonly byte INIT = ASM.INIT;
static readonly byte[] code_call =
{
0x60, 0x9C, //PUSHAD, PUSHFD
0x68, INIT, INIT, INIT, INIT, //PUSH HookAddress
0x83, 0xEC, 0x04,//SUB ESP, 4
0x8D, 0x44, 0x24, 0x04,//LEA EAX,[ESP + 4]
0x50, //PUSH EAX
0xE8, INIT, INIT, INIT, INIT, //CALL ProcAddress
0x83, 0xC4, 0x0C, //ADD ESP, 0Ch
0x89, 0x44, 0x24, 0xF8,//MOV ss:[ESP - 8], EAX
0x9D, 0x61, //POPFD, POPAD
0x83, 0x7C, 0x24, 0xD4, 0x00,//CMP ss:[ESP - 2Ch], 0
0x74, 0x04, //JZ .proceed
0xFF, 0x64, 0x24, 0xD4 //JMP ss:[ESP - 2Ch]
};
MemoryHandle memoryHandle;
public AresHookTransferStation(HookInfo info) : base(info)
{
}
public override bool Match(HookType hookType)
{
return hookType == HookType.AresHook;
}
IntPtr GetMemory(int size)
{
if (memoryHandle == null)
{
// alloc bigger space
memoryHandle = new MemoryHandle(code_call.Length + 0x10 + ASM.Jmp.Length);
}
if (memoryHandle.Size < size)
{
memoryHandle = new MemoryHandle(size);
}
return (IntPtr)memoryHandle.Memory;
}
public override void SetHook(HookInfo info)
{
base.SetHook(info);
HookAttribute hook = MaxHookInfo.GetHookAttribute();
var callable = (int)MaxHookInfo.GetCallable();
Logger.Log("ares hook callable: 0x{0:X}", callable);
int pMemory = (int)GetMemory(code_call.Length + hook.Size + ASM.Jmp.Length);
Logger.Log("AresHookTransferStation alloc: 0x{0:X}", pMemory);
if (pMemory != (int)IntPtr.Zero)
{
MemoryHelper.Write(pMemory, code_call, code_call.Length);
MemoryHelper.Write(pMemory + 3, hook.Address);
ASMWriter.WriteCall(new JumpStruct(pMemory + 0xF, callable));
var origin_code_offset = pMemory + code_call.Length;
if (hook.Size > 0)
{ // write origin code
MemoryHelper.Write(origin_code_offset, code_over, hook.Size);
// protect relative jmp or call
if(code_over[0] == ASM.Jmp[0] || code_over[0] == ASM.Call[0])
{
int destination = 0;
MemoryHelper.Read(hook.Address + 1, ref destination);
destination = hook.Address + 5 + destination;
MemoryHelper.Write(origin_code_offset + 1, new JumpStruct(origin_code_offset, destination).Offset);
}
}
var jmp_back_offset = origin_code_offset + hook.Size;
ASMWriter.WriteJump(new JumpStruct(jmp_back_offset, hook.Address + hook.Size));
ASMWriter.WriteJump(new JumpStruct(hook.Address, pMemory));
ASMWriter.FlushInstructionCache(pMemory, memoryHandle.Size);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
memoryHandle.Dispose();
}
base.Dispose(disposing);
}
}
class WriteBytesHookTransferStation : HookTransferStation
{
public WriteBytesHookTransferStation(HookInfo info) : base(info)
{
}
public override bool Match(HookType hookType)
{
return hookType == HookType.WriteBytesHook;
}
public override void SetHook(HookInfo info)
{
if (HookInfos.Count > 0)
{
throw new InvalidOperationException("WriteBytesHook can not mix with other hook");
}
base.SetHook(info);
HookAttribute hook = info.GetHookAttribute();
var bytes = info.GetReturnValue() as byte[];
Logger.Log("write bytes: [{0}]", string.Join(", ", bytes));
MemoryHelper.Write(hook.Address, bytes, bytes.Length);
}
}
class ExportTableHookTransferStation : HookTransferStation
{
public ExportTableHookTransferStation(HookInfo info) : base(info)
{
}
public override bool Match(HookType hookType)
{
return hookType == HookType.ExportTableHook;
}
public override void SetHook(HookInfo info)
{
base.SetHook(info);
HookAttribute hook = info.GetHookAttribute();
var callable = (int)info.GetCallable();
Logger.Log("export table hook on {0}::{1}", hook.Module, hook.TargetName);
Logger.Log("export table hook callable: 0x{0:X}", callable);
var module = Helpers.GetProcessModule(hook.Module);
int callableOffset = callable - (int)module.BaseAddress;
Logger.Log("export table hook callable offset: 0x{0:X}", callableOffset);
MemoryHelper.Write(hook.Address, callableOffset);
}
}
class ImportTableHookTransferStation : HookTransferStation
{
public ImportTableHookTransferStation(HookInfo info) : base(info)
{
}
public override bool Match(HookType hookType)
{
return hookType == HookType.ImportTableHook;
}
public override void SetHook(HookInfo info)
{
base.SetHook(info);
HookAttribute hook = info.GetHookAttribute();
var callable = (int)info.GetCallable();
Logger.Log("import table hook on {0}::{1}", hook.Module, hook.TargetName);
Logger.Log("import table hook callable: 0x{0:X}", callable);
MemoryHelper.Write(hook.Address, callable);
}
}
}