-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathVTablePatcher.cs
More file actions
201 lines (176 loc) · 9.85 KB
/
VTablePatcher.cs
File metadata and controls
201 lines (176 loc) · 9.85 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
namespace Microsoft.ClearScript.Util
{
[SuppressMessage("ReSharper", "CommentTypo", Justification = "This class uses comments to show machine code disassembly.")]
internal abstract class VTablePatcher
{
private static readonly HashSet<IntPtr> patchedVTables = new();
private static IntPtr hHeap;
public static VTablePatcher GetInstance()
{
return Environment.Is64BitProcess ? VTablePatcher64.Instance : VTablePatcher32.Instance;
}
public static object PatchLock => patchedVTables;
public abstract void PatchDispatchEx(IntPtr pDispatchEx);
private static void ApplyVTablePatches(IntPtr pInterface, params VTablePatch[] patches)
{
lock (PatchLock)
{
var pVTable = Marshal.ReadIntPtr(pInterface);
if (patchedVTables.Add(pVTable))
{
EnsureHeap();
foreach (var patch in patches)
{
var pSlot = pVTable + patch.SlotIndex * IntPtr.Size;
var pTarget = VTableHelpers.ReadMethodPtr(pSlot);
var thunkSize = patch.ThunkBytes.Length;
var pThunk = NativeMethods.HeapAlloc(hHeap, 0, (UIntPtr)thunkSize);
if (pThunk != IntPtr.Zero)
{
for (var index = 0; index < thunkSize; index++)
{
Marshal.WriteByte(pThunk + index, patch.ThunkBytes[index]);
}
Marshal.WriteIntPtr(pThunk + patch.TargetOffset, pTarget);
if (!VTableHelpers.WriteMethodPtr(pSlot, pThunk))
{
NativeMethods.HeapFree(hHeap, 0, pThunk);
}
}
}
}
}
}
private static void EnsureHeap()
{
if (hHeap == IntPtr.Zero)
{
hHeap = NativeMethods.HeapCreate(0x00040001 /*HEAP_CREATE_ENABLE_EXECUTE|HEAP_NO_SERIALIZE*/, UIntPtr.Zero, UIntPtr.Zero);
if (hHeap == IntPtr.Zero)
{
throw new Win32Exception();
}
}
}
#region Nested type: VTablePatcher32
private sealed class VTablePatcher32 : VTablePatcher
{
public static readonly VTablePatcher Instance = new VTablePatcher32();
private VTablePatcher32()
{
}
public override void PatchDispatchEx(IntPtr pDispatchEx)
{
// JScript in Standards Mode extends the IDispatchEx contract slightly in order to
// pass extra data to the host. This confuses the CLR's IDispatchEx implementation.
// The vtable patches below sanitize the arguments before passing them through.
ApplyVTablePatches(
pDispatchEx,
new VTablePatch
{
SlotIndex = 7, // IDispatchEx::GetDispID()
ThunkBytes = new byte[] { //-------------------------
0x55, // push ebp
0x8B, 0xEC, // mov ebp,esp
0x81, 0x65, 0x10, 0xFF, 0xFF, 0xFF, 0x0F, // and dword ptr [grfdex],0FFFFFFFh
0xB8, 0x0D, 0xF0, 0xAD, 0xBA, // mov eax,0BAADF00Dh <- Target
0x5D, // pop ebp
0xFF, 0xE0 // jmp eax
},
TargetOffset = 11
},
new VTablePatch
{
SlotIndex = 9, // IDispatchEx::DeleteMemberByName()
ThunkBytes = new byte[] { // ---------------------------------
0x55, // push ebp
0x8B, 0xEC, // mov ebp,esp
0x8B, 0x45, 0x10, // mov eax,dword ptr [grfdex]
0x25, 0xFF, 0xFF, 0xFF, 0x0F, // and eax,0FFFFFFFh
0x89, 0x45, 0x10, // mov dword ptr [grfdex],eax
0xB8, 0x0D, 0xF0, 0xAD, 0xBA, // mov eax,0BAADF00Dh <- Target
0x5D, // pop ebp
0xFF, 0xE0 // jmp eax
},
TargetOffset = 15
},
new VTablePatch
{
SlotIndex = 13, // IDispatchEx::GetNextDispID()
ThunkBytes = new byte[] { // ----------------------------
0x55, // push ebp
0x8B, 0xEC, // mov ebp,esp
0x81, 0x65, 0x0C, 0xFF, 0xFF, 0xFF, 0x0F, // and dword ptr [grfdex],0FFFFFFFh
0xB8, 0x0D, 0xF0, 0xAD, 0xBA, // mov eax,0BAADF00Dh <- Target
0x5D, // pop ebp
0xFF, 0xE0 // jmp eax
},
TargetOffset = 11
}
);
}
}
#endregion
#region Nested type: VTablePatcher64
private sealed class VTablePatcher64 : VTablePatcher
{
public static readonly VTablePatcher Instance = new VTablePatcher64();
private VTablePatcher64()
{
}
public override void PatchDispatchEx(IntPtr pDispatchEx)
{
// JScript in Standards Mode extends the IDispatchEx contract slightly in order to
// pass extra data to the host. This confuses the CLR's IDispatchEx implementation.
// The vtable patches below sanitize the arguments before passing them through.
ApplyVTablePatches(
pDispatchEx,
new VTablePatch
{
SlotIndex = 7, // IDispatchEx::GetDispID()
ThunkBytes = new byte[] { //-------------------------
0x41, 0x81, 0xE0, 0xFF, 0xFF, 0xFF, 0x0F, // and r8d,0FFFFFFFh
0x48, 0xB8, 0x0D, 0xF0, 0xAD, 0xBA, 0x0D, 0xF0, 0xAD, 0xBA, // mov rax,0BAADF00DBAADF00Dh <- Target
0x48, 0xFF, 0xE0 // jmp rax
},
TargetOffset = 9
},
new VTablePatch {
SlotIndex = 9, // IDispatchEx::DeleteMemberByName()
ThunkBytes = new byte[] { // ---------------------------------
0x41, 0x81, 0xE0, 0xFF, 0xFF, 0xFF, 0x0F, // and r8d,0FFFFFFFh
0x48, 0xB8, 0x0D, 0xF0, 0xAD, 0xBA, 0x0D, 0xF0, 0xAD, 0xBA, // mov rax,0BAADF00DBAADF00Dh <- Target
0x48, 0xFF, 0xE0 // jmp rax
},
TargetOffset = 9
},
new VTablePatch {
SlotIndex = 13, // IDispatchEx::GetNextDispID()
ThunkBytes = new byte[] { // ----------------------------
0x81, 0xE2, 0xFF, 0xFF, 0xFF, 0x0F, // and edx,0FFFFFFFh
0x48, 0xB8, 0x0D, 0xF0, 0xAD, 0xBA, 0x0D, 0xF0, 0xAD, 0xBA, // mov rax,0BAADF00DBAADF00Dh <- Target
0x48, 0xFF, 0xE0 // jmp rax
},
TargetOffset = 8
}
);
}
}
#endregion
#region Nested type: VTablePatch
private sealed class VTablePatch
{
public int SlotIndex;
public byte[] ThunkBytes;
public int TargetOffset;
}
#endregion
}
}