-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathAssemblyFactory.cs
More file actions
501 lines (462 loc) · 23.3 KB
/
Copy pathAssemblyFactory.cs
File metadata and controls
501 lines (462 loc) · 23.3 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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System;
using System.Linq;
using System.Threading.Tasks;
using Process.NET.Assembly.Assemblers;
using Process.NET.Assembly.CallingConventions;
using Process.NET.Marshaling;
using Process.NET.Memory;
using Process.NET.Native.Types;
using Process.NET.Threads;
using Process.NET.Utilities;
namespace Process.NET.Assembly
{
public class AssemblyFactory : IAssemblyFactory
{
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyFactory" /> class.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="assembler">The assembler.</param>
public AssemblyFactory(IProcess process, IAssembler assembler)
{
Process = process;
Assembler = assembler;
}
/// <summary>
/// Gets or sets the assembler.
/// </summary>
/// <value>
/// The assembler.
/// </value>
public IAssembler Assembler { get; set; }
/// <summary>
/// Gets the process.
/// </summary>
/// <value>
/// The process.
/// </value>
public IProcess Process { get; }
/// <summary>
/// Begins a new transaction to inject and execute assembly code into the process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is injected.</param>
/// <param name="autoExecute">Indicates whether the assembly code is executed once the object is disposed.</param>
/// <returns>The return value is a new transaction.</returns>
public AssemblyTransaction BeginTransaction(IntPtr address, bool autoExecute = true)
{
return new AssemblyTransaction(this, address, autoExecute);
}
/// <summary>
/// Begins a new transaction to inject and execute assembly code into the process.
/// </summary>
/// <param name="autoExecute">Indicates whether the assembly code is executed once the object is disposed.</param>
/// <returns>The return value is a new transaction.</returns>
public AssemblyTransaction BeginTransaction(bool autoExecute = true)
{
return new AssemblyTransaction(this, autoExecute);
}
/// <summary>
/// Releases all resources used by the <see cref="AssemblyFactory" /> object.
/// </summary>
public void Dispose()
{
// Nothing to dispose... yet
}
/// <summary>
/// Executes the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public T Execute<T>(IntPtr address)
{
// Execute and join the code in a new thread
var thread = Process.ThreadFactory.CreateAndJoin(address);
// Return the exit code of the thread
return thread.GetExitCode<T>();
}
/// <summary>
/// Executes the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public IntPtr Execute(IntPtr address)
{
return Execute<IntPtr>(address);
}
/// <summary>
/// Executes the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <param name="parameter">The parameter used to execute the assembly code.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public T Execute<T>(IntPtr address, dynamic parameter)
{
// Execute and join the code in a new thread
IRemoteThread thread = Process.ThreadFactory.CreateAndJoin(address, parameter);
// Return the exit code of the thread
return thread.GetExitCode<T>();
}
/// <summary>
/// Executes the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <param name="parameter">The parameter used to execute the assembly code.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public IntPtr Execute(IntPtr address, dynamic parameter)
{
return Execute<IntPtr>(address, parameter);
}
/// <summary>
/// Executes the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <param name="callingConvention">The calling convention used to execute the assembly code with the parameters.</param>
/// <param name="parameters">An array of parameters used to execute the assembly code.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public T Execute<T>(IntPtr address, Native.Types.CallingConventions callingConvention,
params dynamic[] parameters)
{
// Marshal the parameters
var marshalledParameters =
parameters.Select(p => MarshalValue.Marshal(Process, p)).Cast<IMarshalledValue>().ToArray();
// Start a transaction
AssemblyTransaction t;
using (t = BeginTransaction())
{
// Get the object dedicated to create mnemonics for the given calling convention
var calling = CallingConventionSelector.Get(callingConvention);
// Push the parameters
t.AddLine(calling.FormatParameters(marshalledParameters.Select(p => p.Reference).ToArray()));
// Call the function
t.AddLine(calling.FormatCalling(address));
// Clean the parameters
if (calling.Cleanup == CleanupTypes.Caller)
t.AddLine(calling.FormatCleaning(marshalledParameters.Length));
// Add the return mnemonic
t.AddLine("retn");
}
// Clean the marshalled parameters
foreach (var parameter in marshalledParameters)
parameter.Dispose();
// Return the exit code
return t.GetExitCode<T>();
}
/// <summary>
/// Executes the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <param name="callingConvention">The calling convention used to execute the assembly code with the parameters.</param>
/// <param name="parameters">An array of parameters used to execute the assembly code.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public IntPtr Execute(IntPtr address, Native.Types.CallingConventions callingConvention,
params dynamic[] parameters)
{
return Execute<IntPtr>(address, callingConvention, parameters);
}
/// <summary>
/// Executes asynchronously the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<T> ExecuteAsync<T>(IntPtr address)
{
return Task.Run(() => Execute<T>(address));
}
/// <summary>
/// Executes asynchronously the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<IntPtr> ExecuteAsync(IntPtr address)
{
return ExecuteAsync<IntPtr>(address);
}
/// <summary>
/// Executes asynchronously the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <param name="parameter">The parameter used to execute the assembly code.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<T> ExecuteAsync<T>(IntPtr address, dynamic parameter)
{
return Task.Run(() => (Task<T>) Execute<T>(address, parameter));
}
/// <summary>
/// Executes asynchronously the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <param name="parameter">The parameter used to execute the assembly code.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<IntPtr> ExecuteAsync(IntPtr address, dynamic parameter)
{
return ExecuteAsync<IntPtr>(address, parameter);
}
/// <summary>
/// Executes asynchronously the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <param name="callingConvention">The calling convention used to execute the assembly code with the parameters.</param>
/// <param name="parameters">An array of parameters used to execute the assembly code.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<T> ExecuteAsync<T>(IntPtr address, Native.Types.CallingConventions callingConvention,
params dynamic[] parameters)
{
return Task.Run(() => Execute<T>(address, callingConvention, parameters));
}
/// <summary>
/// Executes asynchronously the assembly code located in the remote process at the specified address.
/// </summary>
/// <param name="address">The address where the assembly code is located.</param>
/// <param name="callingConvention">The calling convention used to execute the assembly code with the parameters.</param>
/// <param name="parameters">An array of parameters used to execute the assembly code.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<IntPtr> ExecuteAsync(IntPtr address, Native.Types.CallingConventions callingConvention,
params dynamic[] parameters)
{
return ExecuteAsync<IntPtr>(address, callingConvention, parameters);
}
/// <summary>
/// Assembles mnemonics and injects the corresponding assembly code into the remote process at the specified address.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
public void Inject(string asm, IntPtr address)
{
Process.Memory.Write(address, Assembler.Assemble(asm, address));
}
/// <summary>
/// Assembles mnemonics and injects the corresponding assembly code into the remote process at the specified address.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
public void Inject(string[] asm, IntPtr address)
{
Inject(string.Join("\n", asm), address);
}
/// <summary>
/// Assembles mnemonics and injects the corresponding assembly code into the remote process.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <returns>The address where the assembly code is injected.</returns>
public IAllocatedMemory Inject(string asm)
{
// Assemble the assembly code
var code = Assembler.Assemble(asm);
// Allocate a chunk of memory to store the assembly code
var memory = Process.MemoryFactory.Allocate(Randomizer.GenerateString(), code.Length);
// Inject the code
Inject(asm, memory.BaseAddress);
// Return the memory allocated
return memory;
}
/// <summary>
/// Assembles mnemonics and injects the corresponding assembly code into the remote process.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <returns>The address where the assembly code is injected.</returns>
public IAllocatedMemory Inject(string[] asm)
{
return Inject(string.Join("\n", asm));
}
/// <summary>
/// Assembles, injects and executes the mnemonics into the remote process at the specified address.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public T InjectAndExecute<T>(string asm, IntPtr address)
{
// Inject the assembly code
Inject(asm, address);
// Execute the code
return Execute<T>(address);
}
/// <summary>
/// Assembles, injects and executes the mnemonics into the remote process at the specified address.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public IntPtr InjectAndExecute(string asm, IntPtr address)
{
return InjectAndExecute<IntPtr>(asm, address);
}
/// <summary>
/// Assembles, injects and executes the mnemonics into the remote process at the specified address.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public T InjectAndExecute<T>(string[] asm, IntPtr address)
{
return InjectAndExecute<T>(string.Join("\n", asm), address);
}
/// <summary>
/// Assembles, injects and executes the mnemonics into the remote process at the specified address.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public IntPtr InjectAndExecute(string[] asm, IntPtr address)
{
return InjectAndExecute<IntPtr>(asm, address);
}
/// <summary>
/// Assembles, injects and executes the mnemonics into the remote process.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public T InjectAndExecute<T>(string asm)
{
// Inject the assembly code
using (var memory = Inject(asm))
// Execute the code
return Execute<T>(memory.BaseAddress);
}
/// <summary>
/// Assembles, injects and executes the mnemonics into the remote process.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public IntPtr InjectAndExecute(string asm)
{
return InjectAndExecute<IntPtr>(asm);
}
/// <summary>
/// Assembles, injects and executes the mnemonics into the remote process.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public T InjectAndExecute<T>(string[] asm)
{
return InjectAndExecute<T>(string.Join("\n", asm));
}
/// <summary>
/// Assembles, injects and executes the mnemonics into the remote process.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <returns>The return value is the exit code of the thread created to execute the assembly code.</returns>
public IntPtr InjectAndExecute(string[] asm)
{
return InjectAndExecute<IntPtr>(asm);
}
/// <summary>
/// Assembles, injects and executes asynchronously the mnemonics into the remote process at the specified address.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<T> InjectAndExecuteAsync<T>(string asm, IntPtr address)
{
return Task.Run(() => InjectAndExecute<T>(asm, address));
}
/// <summary>
/// Assembles, injects and executes asynchronously the mnemonics into the remote process at the specified address.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<IntPtr> InjectAndExecuteAsync(string asm, IntPtr address)
{
return InjectAndExecuteAsync<IntPtr>(asm, address);
}
/// <summary>
/// Assembles, injects and executes asynchronously the mnemonics into the remote process at the specified address.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<T> InjectAndExecuteAsync<T>(string[] asm, IntPtr address)
{
return Task.Run(() => InjectAndExecute<T>(asm, address));
}
/// <summary>
/// Assembles, injects and executes asynchronously the mnemonics into the remote process at the specified address.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <param name="address">The address where the assembly code is injected.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<IntPtr> InjectAndExecuteAsync(string[] asm, IntPtr address)
{
return InjectAndExecuteAsync<IntPtr>(asm, address);
}
/// <summary>
/// Assembles, injects and executes asynchronously the mnemonics into the remote process.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<T> InjectAndExecuteAsync<T>(string asm)
{
return Task.Run(() => InjectAndExecute<T>(asm));
}
/// <summary>
/// Assembles, injects and executes asynchronously the mnemonics into the remote process.
/// </summary>
/// <param name="asm">The mnemonics to inject.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<IntPtr> InjectAndExecuteAsync(string asm)
{
return InjectAndExecuteAsync<IntPtr>(asm);
}
/// <summary>
/// Assembles, injects and executes asynchronously the mnemonics into the remote process.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<T> InjectAndExecuteAsync<T>(string[] asm)
{
return Task.Run(() => InjectAndExecute<T>(asm));
}
/// <summary>
/// Assembles, injects and executes asynchronously the mnemonics into the remote process.
/// </summary>
/// <param name="asm">An array containing the mnemonics to inject.</param>
/// <returns>
/// The return value is an asynchronous operation that return the exit code of the thread created to execute the
/// assembly code.
/// </returns>
public Task<IntPtr> InjectAndExecuteAsync(string[] asm)
{
return InjectAndExecuteAsync<IntPtr>(asm);
}
}
}