/
CapstoneDisassembler.cs
603 lines (545 loc) · 24.1 KB
/
CapstoneDisassembler.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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
using Gee.External.Capstone.Arm;
using Gee.External.Capstone.Arm64;
using Gee.External.Capstone.X86;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace Gee.External.Capstone {
/// <summary>
/// Capstone Disassembler.
/// </summary>
public abstract class CapstoneDisassembler : IDisposable {
/// <summary>
/// Disassembler's Architecture.
/// </summary>
private readonly DisassembleArchitecture _architecture;
/// <summary>
/// Disassembler's Details Flag.
/// </summary>
private bool _detailsFlag;
/// <summary>
/// Disposed Flag.
/// </summary>
private bool _disposed;
/// <summary>
/// Disassembler's Handle.
/// </summary>
private readonly SafeCapstoneHandle _handle;
/// <summary>
/// Disassembler's Mode.
/// </summary>
private DisassembleMode _mode;
/// <summary>
/// Disassembler's Syntax.
/// </summary>
private DisassembleSyntaxOptionValue _syntax;
/// <summary>
/// Get Disassembler's Architecture.
/// </summary>
public DisassembleArchitecture Architecture {
get {
return this._architecture;
}
}
/// <summary>
/// Enable or Disable Disassemble Details.
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the disassemble details could not be set.
/// </exception>
public bool EnableDetails {
get {
return this._detailsFlag;
}
set {
this._detailsFlag = value;
NativeCapstone.SetDisassembleDetails(this._handle, this._detailsFlag);
}
}
/// <summary>
/// Get and Set Disassembler's Mode.
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the disassembler's mode could not be set.
/// </exception>
public DisassembleMode Mode {
get {
return this._mode;
}
set {
this._mode = value;
NativeCapstone.SetDisassembleModeOption(this._handle, this._mode);
}
}
/// <summary>
/// Get and Set Disassembler's Syntax.
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the disassembler's syntax could not be set.
/// </exception>
public DisassembleSyntaxOptionValue Syntax {
get {
return this._syntax;
}
set {
this._syntax = value;
NativeCapstone.SetDisassembleSyntaxOption(this._handle, this._syntax);
}
}
/// <summary>
/// Get Disassembler's Handle.
/// </summary>
protected SafeCapstoneHandle Handle {
get {
return this._handle;
}
}
/// <summary>
/// Create an ARM Disassembler.
/// </summary>
/// <param name="mode">
/// The disassembler's mode.
/// </param>
/// <returns>
/// A capstone disassembler.
/// </returns>
public static CapstoneDisassembler<ArmInstruction, ArmRegister, ArmInstructionGroup, ArmInstructionDetail> CreateArmDisassembler(DisassembleMode mode) {
var @object = new CapstoneArmDisassembler(mode);
return @object;
}
/// <summary>
/// Create a ARM64 Disassembler.
/// </summary>
/// <param name="mode">
/// The disassembler's mode.
/// </param>
/// <returns>
/// A capstone disassembler.
/// </returns>
public static CapstoneDisassembler<Arm64Instruction, Arm64Register, Arm64InstructionGroup, Arm64InstructionDetail> CreateArm64Disassembler(DisassembleMode mode) {
var @object = new CapstoneArm64Disassembler(mode);
return @object;
}
/// <summary>
/// Create an X86 Disassembler.
/// </summary>
/// <param name="mode">
/// The disassembler's mode.
/// </param>
/// <returns>
/// A capstone disassembler.
/// </returns>
public static CapstoneDisassembler<X86Instruction, X86Register, X86InstructionGroup, X86InstructionDetail> CreateX86Disassembler(DisassembleMode mode) {
var @object = new CapstoneX86Disassembler(mode);
return @object;
}
/// <summary>
/// Create a Disassembler.
/// </summary>
/// <param name="architecture">
/// The disassembler's architecture.
/// </param>
/// <param name="mode">
/// The disassembler's mode.
/// </param>
protected CapstoneDisassembler(DisassembleArchitecture architecture, DisassembleMode mode) {
this._architecture = architecture;
this._mode = mode;
this._handle = NativeCapstone.Create(this._architecture, this._mode);
this.EnableDetails = false;
this.Syntax = DisassembleSyntaxOptionValue.Default;
}
/// <summary>
/// Dispose Disassembler.
/// </summary>
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose Disassembler.
/// </summary>
/// <param name="disposing">
/// A boolean true if the disassembler is being disposed from application code. A boolean false otherwise.
/// </param>
protected virtual void Dispose(bool disposing) {
if (!this._disposed) {
this._handle.Close();
}
this._disposed = true;
}
}
/// <summary>
/// Capstone Disassembler.
/// </summary>
public abstract class CapstoneDisassembler<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail> : CapstoneDisassembler {
/// <summary>
/// Create a Disassembler.
/// </summary>
/// <param name="architecture">
/// The disassembler's architecture.
/// </param>
/// <param name="mode">
/// The disassembler's mode.
/// </param>
protected CapstoneDisassembler(DisassembleArchitecture architecture, DisassembleMode mode) : base(architecture, mode) {}
/// <summary>
/// Disassemble Binary Code.
/// </summary>
/// <param name="code">
/// A collection of bytes representing the binary code to disassemble. Should not be a null reference.
/// </param>
/// <param name="count">
/// The number of instructions to disassemble. A 0 indicates all instructions should be disassembled.
/// </param>
/// <param name="startingAddress">
/// The address of the first instruction in the collection of bytes to disassemble.
/// </param>
/// <returns>
/// A collection of dissembled instructions.
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the binary code could not be disassembled.
/// </exception>
public Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail>[] Disassemble(byte[] code, int count, ulong startingAddress) {
var nativeInstructions = NativeCapstone.Disassemble(this.Handle, code, count, startingAddress);
var instructions = nativeInstructions
.Instructions
.Select(this.CreateInstruction)
.ToArray();
return instructions;
}
/// <summary>
/// Disassemble Binary Code.
/// </summary>
/// <param name="code">
/// A collection of bytes representing the binary code to disassemble. Should not be a null reference.
/// </param>
/// <param name="count">
/// The number of instructions to disassemble. A 0 indicates all instructions should be disassembled.
/// </param>
/// <returns>
/// A collection of dissembled instructions.
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the binary code could not be disassembled.
/// </exception>
public Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail>[] Disassemble(byte[] code, int count) {
var instructions = this.Disassemble(code, count, 0x1000);
return instructions;
}
/// <summary>
/// Disassemble Binary Code.
/// </summary>
/// <param name="code">
/// A collection of bytes representing the binary code to disassemble. Should not be a null reference.
/// </param>
/// <param name="startingAddress">
/// The address of the first instruction in the collection of bytes to disassemble.
/// </param>
/// <returns>
/// A collection of dissembled instructions.
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the binary code could not be disassembled.
/// </exception>
public Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail>[] DisassembleAll(byte[] code, ulong startingAddress) {
var instructions = this.Disassemble(code, 0, startingAddress);
return instructions;
}
/// <summary>
/// Defer Disassemble Binary Code.
/// </summary>
/// <param name="code">
/// A collection of bytes representing the binary code to disassemble. Should not be a null reference.
/// </param>
/// <param name="offset">
/// An offset to start disassembling from. A 0 indicates disassembly should start at the first byte.
/// Should be less than the length of the collection of bytes to disassemble.
/// </param>
/// <param name="startingAddress">
/// The address of the first instruction in the collection of bytes to disassemble.
/// </param>
/// <returns>
/// A deferred collection of dissembled instructions.
/// </returns>
public IEnumerable<Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail>> DisassembleStream(byte[] code, int offset, long startingAddress) {
var enumerable = new DeferredInstructionEnumerable(this, code, offset, startingAddress);
return enumerable;
}
/// <summary>
/// Disassemble Binary Code.
/// </summary>
/// <param name="code">
/// A collection of bytes representing the binary code to disassemble. Should not be a null reference.
/// </param>
/// <returns>
/// A collection of dissembled instructions.
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// Thrown if the binary code could not be disassembled.
/// </exception>
public Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail>[] DisassembleAll(byte[] code) {
var instructions = this.DisassembleAll(code, 0x1000);
return instructions;
}
/// <summary>
/// Create a Dissembled Instruction.
/// </summary>
/// <param name="nativeInstruction">
/// A native instruction.
/// </param>
/// <returns>
/// A dissembled instruction.
/// </returns>
protected abstract Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail> CreateInstruction(NativeInstruction nativeInstruction);
/// <summary>
/// Deferred Instruction Enumerable.
/// </summary>
/// <remarks>
/// Represents an enumerable that is used to support deferred enumeration of dissembled binary code. This
/// allows instruction-by-instruction disassembling of small ranges of code conveniently. This mode of
/// use is common with recursive disassemblers, which need to disassembly small chunks of code often.
/// </remarks>
private sealed class DeferredInstructionEnumerable : IEnumerable<Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail>> {
/// <summary>
/// Binary Code to Disassemble.
/// </summary>
private readonly byte[] _code;
/// <summary>
/// Disassembler.
/// </summary>
private readonly CapstoneDisassembler<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail> _disassembler;
/// <summary>
/// Offset.
/// </summary>
private readonly int _offset;
/// <summary>
/// Starting Address.
/// </summary>
private readonly long _startingAddress;
/// <summary>
/// Create a Deferred Instruction Enumerable.
/// </summary>
/// <param name="disassembler">
/// The disassembler to use. Should not be a null reference.
/// </param>
/// <param name="code">
/// A collection of bytes representing the binary code to disassemble. Should not be a null reference.
/// </param>
/// <param name="offset">
/// An offset to start disassembling from. A 0 indicates disassembly should start at the first byte.
/// Should be less than the length of the collection of bytes to disassemble.
/// </param>
/// <param name="startingAddress">
/// The address of the first instruction in the collection of bytes to disassemble.
/// </param>
public DeferredInstructionEnumerable(CapstoneDisassembler<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail> disassembler, byte[] code, int offset, long startingAddress) {
this._disassembler = disassembler;
this._code = code;
this._offset = offset;
this._startingAddress = startingAddress;
}
/// <summary>
/// Get Enumerator.
/// </summary>
/// <returns>
/// An enumerator.
/// </returns>
public IEnumerator<Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail>> GetEnumerator() {
var enumerator = new DeferredInstructionEnumerator(this._disassembler, this._code, this._offset, this._startingAddress);
return enumerator;
}
/// <summary>
/// Get Enumerator.
/// </summary>
/// <returns>
/// An enumerator.
/// </returns>
IEnumerator IEnumerable.GetEnumerator() {
var enumerator = this.GetEnumerator();
return enumerator;
}
}
/// <summary>
/// Deferred Instruction Enumerator.
/// </summary>
/// <remarks>
/// Represents an enumerator that is used to support deferred enumeration of dissembled binary code. This
/// allows instruction-by-instruction disassembling of small ranges of code conveniently. This mode of
/// use is common with recursive disassemblers, which need to disassembly small chunks of code often.
/// </remarks>
private class DeferredInstructionEnumerator : IEnumerator<Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail>> {
/// <summary>
/// Code Size.
/// </summary>
private readonly int _codeSize;
/// <summary>
/// Current Address.
/// </summary>
private ulong _currentAddress;
/// <summary>
/// Current Instruction.
/// </summary>
private Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail> _currentInstruction;
/// <summary>
/// Current Offset.
/// </summary>
private int _currentOffset;
/// <summary>
/// Disassembler.
/// </summary>
private readonly CapstoneDisassembler<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail> _disassembler;
/// <summary>
/// Disposed Flag.
/// </summary>
private bool _disposed;
/// <summary>
/// Pinned Code.
/// </summary>
/// <remarks>
/// Represents a garbage collector handle to the collection of bytes representing the binary code to
/// disassemble. A "pinning" technique is used to pin the collection of bytes while enumerating
/// instead copying the data back and forth between managed and unmanaged memory.
/// </remarks>
private GCHandle _pinnedCode;
/// <summary>
/// Created a Deferred Instruction Enumerator.
/// </summary>
/// <param name="disassembler">
/// The disassembler to use. Should not be a null reference.
/// </param>
/// <param name="code">
/// A collection of bytes representing the binary code to disassemble. Should not be a null reference.
/// </param>
/// <param name="offset">
/// An offset to start disassembling from. A 0 indicates disassembly should start at the first byte.
/// Should be less than the length of the collection of bytes to disassemble.
/// </param>
/// <param name="startingAddress">
/// The address of the first instruction in the collection of bytes to disassemble.
/// </param>
public DeferredInstructionEnumerator(CapstoneDisassembler<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail> disassembler, byte[] code, int offset, long startingAddress) {
this._disassembler = disassembler;
this._pinnedCode = GCHandle.Alloc(code, GCHandleType.Pinned);
this._currentOffset = offset;
this._currentAddress = (ulong) startingAddress;
this._codeSize = code.Length;
this._disposed = false;
}
/// <summary>
/// Destroy Deferred Instruction Enumerator.
/// </summary>
~DeferredInstructionEnumerator() {
this.Dispose(false);
}
/// <summary>
/// Gets the current instruction in the instruction stream.
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// Thrown if an initial call to <c>DeferredInstructionEnumerator.MoveNext()</c> was not made.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the enumerator is disposed.
/// </exception>
public Instruction<TArchitectureInstruction, TArchitectureRegister, TArchitectureGroup, TArchitectureDetail> Current {
get {
this.CheckDisposed();
if (_currentInstruction == null) {
throw new InvalidOperationException();
}
return this._currentInstruction;
}
}
/// <summary>
/// Get Current Instruction.
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// Thrown if an initial call to <c>DeferredInstructionEnumerator.MoveNext()</c> was not made.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the enumerator is disposed.
/// </exception>
object IEnumerator.Current {
get {
return this.Current;
}
}
/// <summary>
/// Destroy Deferred Instruction Enumerator.
/// </summary>
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Get Next Instruction.
/// </summary>
/// <returns>
/// A boolean true if an instruction is returned. A boolean false otherwise.
/// </returns>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the enumerator is disposed.
/// </exception>
public bool MoveNext() {
this.CheckDisposed();
var pCode = this._pinnedCode.AddrOfPinnedObject() + this._currentOffset;
var pCount = (UIntPtr) 1;
var pInstructions = IntPtr.Zero;
var pSize = (UIntPtr) this._codeSize - this._currentOffset;
// Disassemble Binary Code.
//
// ...
var pResultCode = CapstoneImport.Disassemble((UIntPtr)(ulong)(long)_disassembler.Handle.DangerousGetHandle(),
pCode, pSize, this._currentAddress, pCount,
ref pInstructions);
var iResultCode = (int) pResultCode;
var nativeInstructions = MarshalExtension.PtrToStructure<NativeInstruction>(pInstructions, iResultCode);
if (nativeInstructions == null || nativeInstructions.Length == 0) {
return false;
}
var instruction = nativeInstructions[0];
this._currentInstruction = this._disassembler.CreateInstruction(instruction);
this._currentAddress += (ulong) this._currentInstruction.Bytes.Length;
this._currentOffset += this._currentInstruction.Bytes.Length;
return true;
}
/// <summary>
/// Reset Enumerator.
/// </summary>
/// <exception cref="System.NotSupportedException">
/// Thrown always.
/// </exception>
public void Reset() {
throw new NotSupportedException();
}
/// <summary>
/// Destroy Deferred Instruction Enumerator.
/// </summary>
/// <param name="disposing">
/// A boolean true if the enumerator is being disposed from application code. A boolean false otherwise.
/// </param>
private void Dispose(bool disposing) {
if (!this._disposed) {
if (this._pinnedCode.IsAllocated) {
this._pinnedCode.Free();
}
this._disposed = true;
}
}
/// <summary>
/// Check if Enumerator is Disposed.
/// </summary>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the enumerator is disposed.
/// </exception>
private void CheckDisposed() {
if (this._disposed) {
throw new ObjectDisposedException("DeferredInstructionEnumerator");
}
}
}
}
}