-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCallFunction.cs
468 lines (385 loc) · 13.2 KB
/
CallFunction.cs
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace Com.Xenthrax.DllInjector
{
public partial class DllInjector : IDisposable
{
[StructLayout(LayoutKind.Explicit)]
private struct FunctionReturnx86 : FunctionReturn
{
public const int EAXOffset = 0;
public const int EDXOffset = 4;
[FieldOffset(EAXOffset)]
public uint EAX;
[FieldOffset(EDXOffset)]
public uint EDX;
[FieldOffset(EAXOffset)]
private sbyte _Int8;
[FieldOffset(EAXOffset)]
private byte _UInt8;
[FieldOffset(EAXOffset)]
private short _Int16;
[FieldOffset(EAXOffset)]
private ushort _UInt16;
[FieldOffset(EAXOffset)]
private int _Int32;
[FieldOffset(EAXOffset)]
private uint _UInt32;
[FieldOffset(EAXOffset)]
private long _Int64;
[FieldOffset(EAXOffset)]
private ulong _UInt64;
public sbyte Int8 { get { return this._Int8; } }
public byte UInt8 { get { return this._UInt8; } }
public short Int16 { get { return this._Int16; } }
public ushort UInt16 { get { return this._UInt16; } }
public int Int32 { get { return this._Int32; } }
public uint UInt32 { get { return this._UInt32; } }
public long Int64 { get { return this._Int64; } }
public ulong UInt64 { get { return this._UInt64; } }
public IntPtr IntPtr { get { return (IntPtr)this._Int32; } }
public UIntPtr UIntPtr { get { return (UIntPtr)this._UInt32; } }
}
[StructLayout(LayoutKind.Explicit)]
private struct FunctionReturnx64 : FunctionReturn
{
public const int RAXOffset = 0;
[FieldOffset(RAXOffset)]
public ulong RAX;
[FieldOffset(RAXOffset)]
private sbyte _Int8;
[FieldOffset(RAXOffset)]
private byte _UInt8;
[FieldOffset(RAXOffset)]
private short _Int16;
[FieldOffset(RAXOffset)]
private ushort _UInt16;
[FieldOffset(RAXOffset)]
private int _Int32;
[FieldOffset(RAXOffset)]
private uint _UInt32;
[FieldOffset(RAXOffset)]
private long _Int64;
[FieldOffset(RAXOffset)]
private ulong _UInt64;
[FieldOffset(RAXOffset)]
private IntPtr _IntPtr;
[FieldOffset(RAXOffset)]
private UIntPtr _UIntPtr;
public sbyte Int8 { get { return this._Int8; } }
public byte UInt8 { get { return this._UInt8; } }
public short Int16 { get { return this._Int16; } }
public ushort UInt16 { get { return this._UInt16; } }
public int Int32 { get { return this._Int32; } }
public uint UInt32 { get { return this._UInt32; } }
public long Int64 { get { return this._Int64; } }
public ulong UInt64 { get { return this._UInt64; } }
public IntPtr IntPtr { get { return this._IntPtr; } }
public UIntPtr UIntPtr { get { return this._UIntPtr; } }
}
[StructLayout(LayoutKind.Explicit)]
private struct Argumentsx86
{
[FieldOffset(0)]
public uint ECX;
[FieldOffset(8)]
public uint EDX;
[FieldOffset(32)]
public IntPtr Stack;
[FieldOffset(40)]
public int StackLength;
}
[StructLayout(LayoutKind.Explicit)]
private struct Argumentsx64
{
[FieldOffset(0)]
public ulong RCX;
[FieldOffset(8)]
public ulong RDX;
[FieldOffset(16)]
public ulong R8;
[FieldOffset(24)]
public ulong R9;
[FieldOffset(32)]
public IntPtr Stack;
[FieldOffset(40)]
public int StackLength;
}
[StructLayout(LayoutKind.Explicit)]
private struct CallFunctionParam
{
[FieldOffset(0)]
public IntPtr Function;
[FieldOffset(8)]
public Argumentsx86 Argumentsx86;
[FieldOffset(8)]
public Argumentsx64 Argumentsx64;
[FieldOffset(56)]
public FunctionReturnx86 ReturnValuex86;
[FieldOffset(56)]
public FunctionReturnx64 ReturnValuex64;
}
private const int StackPointerAlignment = 8;
public FunctionReturn CallFunction(IntPtr Function, params object[] Paramaters)
{
return this.CallFunction(Function, CallingConvention.Winapi, CharSet.Auto, Paramaters);
}
public FunctionReturn CallFunction(IntPtr Function, CallingConvention callingConvention, params object[] Paramaters)
{
return this.CallFunction(Function, callingConvention, CharSet.Auto, Paramaters);
}
public FunctionReturn CallFunction(IntPtr Function, CharSet charSet, params object[] Paramaters)
{
return this.CallFunction(Function, CallingConvention.Winapi, charSet, Paramaters);
}
public FunctionReturn CallFunction(IntPtr Function, CallingConvention callingConvention, CharSet charSet, params object[] Paramaters)
{
return (FunctionReturn)this.CallFunction(Function, callingConvention, charSet, Paramaters, null, null, null, typeof(FunctionReturn), null);
}
private object CallFunction(IntPtr Function, CallingConvention callingConvention, CharSet charSet, object[] Paramaters, Type[] ParamaterTypes, MarshalAsAttribute[] Marshalling, bool[] IsByRef, Type ReturnType, MarshalAsAttribute ReturnMarshalling)
{
if (Function == IntPtr.Zero)
throw new ArgumentNullException("Function");
if (ReturnType == null)
throw new ArgumentNullException("ReturnType");
this.ThrowIfDisposed();
this.ThrowIfNoHandle();
if (callingConvention == 0)
callingConvention = CallingConvention.Winapi;
if (charSet == 0)
charSet = CharSet.Ansi;
if (Paramaters == null)
Paramaters = new object[0];
if (ParamaterTypes == null)
ParamaterTypes = Type.GetTypeArray(Paramaters);
if (Marshalling == null)
Marshalling = new MarshalAsAttribute[Paramaters.Length];
if (IsByRef == null)
IsByRef = new bool[Paramaters.Length];
if (ReturnMarshalling == null)
ReturnMarshalling = DefaultMarshalling(ReturnType);
object[] OrigParmaters = new object[Paramaters.Length];
Type[] AfterMarshalTypes = new Type[Paramaters.Length];
ICustomMarshaler[] CustomMarshalers = new ICustomMarshaler[Paramaters.Length];
IntPtr[] PtrParamaters = new IntPtr[Paramaters.Length];
for (int i = 0; i < Paramaters.Length; i++)
{
OrigParmaters[i] = Paramaters[i];
if (Marshalling[i] == null)
Marshalling[i] = DefaultMarshalling(ParamaterTypes[i]);
this.MarshalToNative(ref Paramaters[i], Marshalling[i], ParamaterTypes[i], out CustomMarshalers[i], charSet);
if (IsByRef[i])
{
if (Paramaters[i] == null)
{
AfterMarshalTypes[i] = ParamaterTypes[i];
Paramaters[i] = IntPtr.Zero;
}
else
{
AfterMarshalTypes[i] = Paramaters[i].GetType();
Paramaters[i] = this.WriteStruct(Paramaters[i]).Value;
}
}
if (Paramaters[i] is IntPtr)
{
PtrParamaters[i] = (IntPtr)Paramaters[i];
if (this.Is64BitProcess)
Paramaters[i] = PtrParamaters[i].ToInt64();
else
Paramaters[i] = (int)PtrParamaters[i].ToInt64();
}
}
CallFunctionParam Param = new CallFunctionParam()
{
Function = Function
};
if (this.Is64BitProcess)
{
int SkipOnStack = 0;
if (Paramaters.Length >= 1
&& Marshal.SizeOf(Paramaters[0]) <= sizeof(ulong))
{
Param.Argumentsx64.RCX = StructToStruct<ulong>(Paramaters[0]);
SkipOnStack = 1;
if (Paramaters.Length >= 2
&& Marshal.SizeOf(Paramaters[1]) <= sizeof(ulong))
{
Param.Argumentsx64.RDX = StructToStruct<ulong>(Paramaters[1]);
SkipOnStack = 2;
if (Paramaters.Length >= 3
&& Marshal.SizeOf(Paramaters[2]) <= sizeof(ulong))
{
Param.Argumentsx64.R8 = StructToStruct<ulong>(Paramaters[2]);
SkipOnStack = 3;
if (Paramaters.Length >= 4
&& Marshal.SizeOf(Paramaters[3]) <= sizeof(ulong))
{
Param.Argumentsx64.R9 = StructToStruct<ulong>(Paramaters[3]);
SkipOnStack = 4;
}
}
}
}
for (int i = SkipOnStack; i < Paramaters.Length; i++)
Param.Argumentsx64.StackLength += Utilities.Align(Marshal.SizeOf(Paramaters[i]), StackPointerAlignment);
if (Param.Argumentsx64.StackLength != 0)
{
IntPtr args = Marshal.AllocHGlobal(Param.Argumentsx64.StackLength);
try
{
IntPtr _args = args;
for (int i = SkipOnStack; i < Paramaters.Length; i++)
{
Marshal.StructureToPtr(Paramaters[i], _args, false);
_args += Utilities.Align(Marshal.SizeOf(Paramaters[i]), StackPointerAlignment);
}
Param.Argumentsx64.Stack = this.WriteMemory(args, Param.Argumentsx64.StackLength);
}
finally
{
Marshal.FreeHGlobal(args);
}
}
}
else
{
int SkipOnStack = 0;
switch (callingConvention)
{
case CallingConvention.Cdecl:
case CallingConvention.StdCall:
case CallingConvention.Winapi:
for (int i = SkipOnStack; i < Paramaters.Length; i++)
Param.Argumentsx86.StackLength += Utilities.Align(Marshal.SizeOf(Paramaters[i]), StackPointerAlignment);
if (Param.Argumentsx86.StackLength != 0)
{
IntPtr args = Marshal.AllocHGlobal(Param.Argumentsx86.StackLength);
try
{
IntPtr _args = args;
for (int i = SkipOnStack; i < Paramaters.Length; i++)
{
Marshal.StructureToPtr(Paramaters[i], _args, false);
_args += Utilities.Align(Marshal.SizeOf(Paramaters[i]), StackPointerAlignment);
}
Param.Argumentsx86.Stack = this.WriteMemory(args, Param.Argumentsx86.StackLength);
}
finally
{
if (args != IntPtr.Zero)
Marshal.FreeHGlobal(args);
}
}
break;
case CallingConvention.FastCall:
if (Paramaters.Length >= 1
&& Marshal.SizeOf(Paramaters[0]) <= sizeof(uint))
{
Param.Argumentsx86.ECX = StructToStruct<uint>(Paramaters[0]);
SkipOnStack = 1;
if (Paramaters.Length >= 2
&& Marshal.SizeOf(Paramaters[1]) <= sizeof(uint))
{
Param.Argumentsx86.EDX = StructToStruct<uint>(Paramaters[1]);
SkipOnStack = 2;
}
}
goto case CallingConvention.StdCall;
case CallingConvention.ThisCall:
if (Paramaters.Length < 1)
throw new ArgumentException("Calling convention requires at least one argument.");
if (Marshal.SizeOf(Paramaters[0]) > sizeof(uint))
throw new ArgumentException("Paramater 0 must be this paramater and less than or equal to 32-bits.");
Param.Argumentsx86.ECX = StructToStruct<uint>(Paramaters[0]);
SkipOnStack = 1;
goto case CallingConvention.Cdecl;
default:
throw new ArgumentException(null, "callingConvention");
}
}
try
{
uint res = this.CallFunctionThreadProc(this.NativeCallFunction, ref Param);
if (res != Win32.ERROR_SUCCESS)
throw new Exception("CallFunction failed", new Win32Exception((int)res));
}
finally
{
if (Param.Argumentsx86.Stack != IntPtr.Zero)
this.FreeMemory(Param.Argumentsx86.Stack);
for (int i = 0; i < Paramaters.Length; i++)
{
if (IsByRef[i])
{
IntPtr PtrParamater = PtrParamaters[i];
try
{
if (AfterMarshalTypes[i] == typeof(IntPtr))
Paramaters[i] = PtrParamaters[i] = this.ReadIntPtr(PtrParamater);
else if (PtrParamater == IntPtr.Zero)
Paramaters[i] = null;
else
Paramaters[i] = this.ReadStruct(PtrParamater, AfterMarshalTypes[i]);
}
finally
{
if (PtrParamater != IntPtr.Zero)
this.FreeMemory(PtrParamater);
}
}
this.MarshalToManaged(ref Paramaters[i], OrigParmaters[i], PtrParamaters[i], Marshalling[i], ParamaterTypes[i], CustomMarshalers[i], charSet, IsByRef[i]);
}
}
FunctionReturn ReturnValue;
if (this.Is64BitProcess)
ReturnValue = Param.ReturnValuex64;
else
ReturnValue = Param.ReturnValuex86;
if (ReturnMarshalling != null)
{
object _ReturnValue = ReturnValue.Int64;
this.MarshalToManaged(ref _ReturnValue, null, ReturnValue.IntPtr, ReturnMarshalling, ReturnType, null, charSet, true);
return _ReturnValue;
}
else if (ReturnType == typeof(FunctionReturn))
return ReturnValue;
else if (ReturnType == typeof(sbyte))
return ReturnValue.Int8;
else if (ReturnType == typeof(byte))
return ReturnValue.UInt8;
else if (ReturnType == typeof(short))
return ReturnValue.Int16;
else if (ReturnType == typeof(ushort))
return ReturnValue.UInt16;
else if (ReturnType == typeof(int))
return ReturnValue.Int32;
else if (ReturnType == typeof(uint))
return ReturnValue.UInt32;
else if (ReturnType == typeof(long))
return ReturnValue.Int64;
else if (ReturnType == typeof(ulong))
return ReturnValue.UInt64;
else if (ReturnType == typeof(IntPtr))
return ReturnValue.IntPtr;
else if (ReturnType == typeof(UIntPtr))
return ReturnValue.UIntPtr;
else
throw new NotSupportedException(string.Format("Return type `{0}` is not supported", ReturnType));
}
}
public interface FunctionReturn
{
sbyte Int8 { get; }
byte UInt8 { get; }
short Int16 { get; }
ushort UInt16 { get; }
int Int32 { get; }
uint UInt32 { get; }
long Int64 { get; }
ulong UInt64 { get; }
IntPtr IntPtr { get; }
UIntPtr UIntPtr { get; }
}
}