-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRttiTypesFactoryTests.cs
More file actions
358 lines (297 loc) · 14.1 KB
/
RttiTypesFactoryTests.cs
File metadata and controls
358 lines (297 loc) · 14.1 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
using System.Globalization;
using System.Reflection;
using NtApiDotNet.Win32;
using RemoteNET.RttiReflection;
using ScubaDiver;
using ScubaDiver.API;
using ScubaDiver.API.Interactions.Dumps;
using ScubaDiver.Rtti;
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
namespace RemoteNET.Tests
{
[TestFixture]
public class RttiTypesFactoryTests
{
class FakeRemoteApp : RemoteApp
{
class FakeType : Type
{
public override object[] GetCustomAttributes(bool inherit)
{
throw new NotImplementedException();
}
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
throw new NotImplementedException();
}
public override bool IsDefined(Type attributeType, bool inherit)
{
throw new NotImplementedException();
}
public override Module Module { get; }
public override string? Namespace { get; }
public override string Name { get; }
public FakeType(string namespacee, string name)
{
Namespace = namespacee;
Name = name;
}
protected override TypeAttributes GetAttributeFlagsImpl()
{
throw new NotImplementedException();
}
protected override ConstructorInfo? GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention,
Type[] types, ParameterModifier[]? modifiers)
{
throw new NotImplementedException();
}
public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override Type? GetElementType()
{
throw new NotImplementedException();
}
public override EventInfo? GetEvent(string name, BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override EventInfo[] GetEvents(BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override FieldInfo? GetField(string name, BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override FieldInfo[] GetFields(BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention,
Type[]? types, ParameterModifier[]? modifiers)
{
throw new NotImplementedException();
}
public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args,
ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters)
{
throw new NotImplementedException();
}
public override Type UnderlyingSystemType { get; }
protected override bool IsArrayImpl()
{
throw new NotImplementedException();
}
protected override bool IsByRefImpl()
{
throw new NotImplementedException();
}
protected override bool IsCOMObjectImpl()
{
throw new NotImplementedException();
}
protected override bool IsPointerImpl()
{
throw new NotImplementedException();
}
protected override bool IsPrimitiveImpl()
{
throw new NotImplementedException();
}
public override Assembly Assembly { get; }
public override string? AssemblyQualifiedName { get; }
public override Type? BaseType { get; }
public override string? FullName { get; }
public override Guid GUID { get; }
protected override PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types,
ParameterModifier[]? modifiers)
{
throw new NotImplementedException();
}
protected override bool HasElementTypeImpl()
{
throw new NotImplementedException();
}
public override Type? GetNestedType(string name, BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override Type[] GetNestedTypes(BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
public override Type? GetInterface(string name, bool ignoreCase)
{
throw new NotImplementedException();
}
public override Type[] GetInterfaces()
{
throw new NotImplementedException();
}
}
public override DiverCommunicator Communicator { get; }
public override RemoteHookingManager HookingManager { get; }
public override RemoteMarshal Marshal { get; }
public override RemoteActivator Activator => throw new NotImplementedException();
public override IEnumerable<CandidateType> QueryTypes(string typeFullNameFilter)
{
var cands = new CandidateType[]
{
new CandidateType(RuntimeType.Unmanaged, "Peek::Child", "libPeek_lies.dll", 0x00000000_11223344),
new CandidateType(RuntimeType.Unmanaged, "Peek::Parent", "libPeek_lies.dll", 0x00000000_11223355)
};
return cands.Where(cand => cand.TypeFullName.Contains(typeFullNameFilter));
}
public override IEnumerable<CandidateObject> QueryInstances(string typeFullNameFilter, bool dumpHashcodes = true)
{
throw new NotImplementedException();
}
public override Type GetRemoteType(string typeFullName, string assembly = null)
{
var cands = new FakeType[]
{
new FakeType("Peek", "Child"),
new FakeType("Peek", "Parent"),
};
return cands.Single(cand => $"{cand.Namespace}::{cand.Name}".Contains(typeFullName));
}
public override Type GetRemoteType(long methodTableAddress)
{
throw new NotImplementedException();
}
public override RemoteObject GetRemoteObject(ulong remoteAddress, string typeName, int? hashCode = null)
{
throw new NotImplementedException();
}
public override bool InjectAssembly(string path)
{
throw new NotImplementedException();
}
public override bool InjectDll(string path)
{
throw new NotImplementedException();
}
public override void Dispose()
{
throw new NotImplementedException();
}
}
[Test]
public void AddFunctionImpl_DifferentDeclaringClassOnFunc_DifferentDeclaringType()
{
// Arrange
ScubaDiver.Rtti.ModuleInfo module = new ModuleInfo("libPeek_lies.dll", 0xaabbccdd, 0xaabbccdd);
DllExport? export = typeof(DllExport).GetConstructors((BindingFlags)0xffff)
.Single(c => c.GetParameters().Length == 5).Invoke(new object?[]
{
"?ApplyBinary@Parent@Peek@@UEAA_NPEAEIHMHHH@Z", 1, 0xbbccddee, "a", "b"
}) as DllExport;
export.TryUndecorate(module, out var undecFunc);
TypeDump.TypeMethod? func = VftableParser.ConvertToTypeMethod(undecFunc as UndecoratedFunction);
string childTypeLongName = "Peek::Child";
TypeDump? typeDump = new TypeDump()
{
FullTypeName = childTypeLongName,
Assembly = "libPeek_lies.dll"
};
RemoteRttiType? childType = new RemoteRttiType(null, childTypeLongName, "libPeek_lies.dll");
RemoteApp? fakeApp = new FakeRemoteApp();
// Act
RttiTypesFactory.AddFunctionImpl(fakeApp, typeDump.Assembly, func, childType, false);
// Assert
MethodInfo? method = childType.GetMethods().Single();
string decTypeLongName = $"{method.DeclaringType.Namespace}::{method.DeclaringType.Name}";
Assert.That(childTypeLongName, Is.EqualTo(decTypeLongName));
}
[Test]
public void AddFunctionImpl_SameDeclaringClassOnFunc_SameDeclaringType()
{
// Arrange
ScubaDiver.Rtti.ModuleInfo module = new ModuleInfo("libPeek_lies.dll", 0xaabbccdd, 0xaabbccdd);
DllExport? export = typeof(DllExport).GetConstructors((BindingFlags)0xffff)
.Single(c => c.GetParameters().Length == 5).Invoke(new object?[]
{
"?Construct@Child@Peek@@QEAA_NPEBVString@2@@Z", 1, 0xbbccddee, "a", "b"
}) as DllExport;
export.TryUndecorate(module, out var undecFunc);
TypeDump.TypeMethod? func = VftableParser.ConvertToTypeMethod(undecFunc as UndecoratedFunction);
string childTypeLongName = "Peek::Child";
TypeDump? typeDump = new TypeDump()
{
FullTypeName = childTypeLongName,
Assembly = "libPeek_lies.dll"
};
RemoteRttiType? childType = new RemoteRttiType(null, childTypeLongName, "libPeek_lies.dll");
RemoteApp? fakeApp = new FakeRemoteApp();
// Act
RttiTypesFactory.AddFunctionImpl(fakeApp, typeDump.Assembly, func, childType, false);
// Assert
MethodInfo? method = childType.GetMethods().Single();
string decTypeLongName = $"{method.DeclaringType.Namespace}::{method.DeclaringType.Name}";
Assert.That(childTypeLongName, Is.EqualTo(decTypeLongName));
Assert.That(childType, Is.EqualTo(method.DeclaringType));
}
[Test]
public void UndecoratingConstRef()
{
// Arrange
ScubaDiver.Rtti.ModuleInfo module = new ModuleInfo("libPeek_lies.dll", 0xaabbccdd, 0xaabbccdd);
DllExport? export = typeof(DllExport).GetConstructors((BindingFlags)0xffff)
.Single(c => c.GetParameters().Length == 5).Invoke(new object?[]
{
"??0WClass@Peek@@QEAA@AEBV01@@Z", 1, 0xbbccddee, "a", "b"
}) as DllExport;
// Act
export.TryUndecorate(module, out var undecFunc);
var args = (undecFunc as UndecoratedExportedFunc).ArgTypes;
// Assert
var arg = args[1];
Assert.That(arg, Is.EqualTo("Peek::WClass&"));
}
[Test]
public void UndecoratingConstRef_ParseType_NoMethod()
{
// Arrange
ScubaDiver.Rtti.ModuleInfo module = new ModuleInfo("libPeek_lies.dll", 0xaabbccdd, 0xaabbccdd);
DllExport? export = typeof(DllExport).GetConstructors((BindingFlags)0xffff)
.Single(c => c.GetParameters().Length == 5).Invoke(new object?[]
{
"??0WClass@Peek@@QEAA@AEBV01@@Z", 1, 0xbbccddee, "a", "b"
}) as DllExport;
export.TryUndecorate(module, out var undecFunc);
TypeDump.TypeMethod? func = VftableParser.ConvertToTypeMethod(undecFunc as UndecoratedFunction);
string childTypeLongName = "Peek::WClass";
TypeDump? typeDump = new TypeDump()
{
FullTypeName = childTypeLongName,
Assembly = "libPeek_lies.dll"
};
RemoteRttiType? childType = new RemoteRttiType(null, childTypeLongName, "libPeek_lies.dll");
RemoteApp? fakeApp = new FakeRemoteApp();
// Act
RttiTypesFactory.AddFunctionImpl(fakeApp, typeDump.Assembly, func, childType, false);
// Assert
// Expecting `AddFunctionImpl` to NOT add that function (not supported yet)
Assert.That(childType.GetMethods(), Is.Empty);
}
}
}
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
#pragma warning restore CS8602 // Dereference of a possibly null reference.