aboutsummaryrefslogtreecommitdiff
path: root/Capstone/Get-CSDisassembly.ps1
blob: 89c17dba5a49b16f2205d31c9c179fb7da44f44c (plain)
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
#requires -Version 3

function Get-CSDisassembly
{
<#
.SYNOPSIS

    Disassembles a byte array using the Capstone Engine disassembly framework.

    PowerSploit Function: Get-CSDisassembly
    Author: Matthew Graeber (@mattifestation)
    License: See LICENSE.TXT
    Required Dependencies: lib\capstone.dll, lib\libcapstone.dll (64-bit)
    Optional Dependencies: None

.PARAMETER Architecture

    Specifies the architecture of the code to be disassembled.

.PARAMETER Mode

    Specifies the mode in which to disassemble code. For example, to disassemble Amd64 code, architecture is set to 'X86' and Mode is set to 'MODE_64'.

.PARAMETER Code

    A byte array consisting of the code to be disassembled.

.PARAMETER Offset

    Specifies the starting address of the disassembly listing.

.PARAMETER Count

    Specifies the maximum number of instructions to disassemble.

.PARAMETER Syntax

    Specifies the syntax flavor to be used (INTEL vs. ATT).

.PARAMETER DetailOff

    Specifies that detailed parsing should not be performed - i.e. do not perform additional analysis beyond disassembling.
      
.EXAMPLE

    C:\PS>$Bytes = [Byte[]] @( 0x8d, 0x4c, 0x32, 0x08, 0x01, 0xd8, 0x81, 0xc6, 0x34, 0x12, 0x00, 0x00 )
    C:\PS>Get-CSDisassembly -Architecture X86 -Mode MODE_16 -Code $Bytes -Offset 0x1000

.EXAMPLE

    C:\PS>$Bytes = [Byte[]] @( 0x8d, 0x4c, 0x32, 0x08, 0x01, 0xd8, 0x81, 0xc6, 0x34, 0x12, 0x00, 0x00 )
    C:\PS>Get-CSDisassembly -Architecture X86 -Mode MODE_32 -Code $Bytes -Syntax ATT

.INPUTS

    None

    You cannot pipe objects to Get-CSDisassembly.

.OUTPUTS

    Capstone.Instruction[]

    Get-CSDisassembly returns an array of Instruction objects.

.NOTES

    Get-CSDisassembly must be run from 64-bit PowerShell v3.
#>

    [OutputType([Capstone.Instruction])]
    [CmdletBinding()] Param (
        [Parameter(Mandatory)]
        [Capstone.ARCH]
        $Architecture,

        [Parameter(Mandatory)]
        [Capstone.MODE]
        $Mode,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [Byte[]]
        $Code,

        [UInt64]
        $Offset = 0,

        [UInt32]
        $Count = 0,

        [ValidateSet('Intel', 'ATT')]
        [String]
        $Syntax,

        [Switch]
        $DetailOff
    )

    $Disassembly = New-Object Capstone.Capstone($Architecture, $Mode)

    if ($Syntax)
    {
        switch ($Syntax)
        {
            'Intel' { $SyntaxMode = [Capstone.OPT_VALUE]::SYNTAX_INTEL }
            'ATT'   { $SyntaxMode = [Capstone.OPT_VALUE]::SYNTAX_ATT }
        }

        $Disassembly.SetSyntax($SyntaxMode)
    }

    if ($DetailOff)
    {
        $Disassembly.SetDetail($False)
    }

    $Disassembly.Disassemble($Code, $Offset, $Count)
}