1
- Get-ChildItem (Join-Path $PSScriptRoot * .ps1) | % { . $_.FullName }
1
+ # Requires -Modules Capstone
2
+
3
+ function Get-CSDisassembly
4
+ {
5
+ <#
6
+ . SYNOPSIS
7
+
8
+ Disassembles a byte array using the Capstone Engine disassembly framework.
9
+
10
+ PowerSploit Function: Get-CSDisassembly
11
+ Author: Matthew Graeber (@mattifestation)
12
+ License: See LICENSE.TXT
13
+ Required Dependencies: lib\capstone.dll, lib\[x86|x64]\libcapstone.dll
14
+ Optional Dependencies: None
15
+
16
+ . DESCRIPTION
17
+
18
+ Get-CSDisassembly is compatible on 32 and 64-bit.
19
+
20
+ . PARAMETER Architecture
21
+
22
+ Specifies the architecture of the code to be disassembled.
23
+
24
+ . PARAMETER Mode
25
+
26
+ 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'.
27
+
28
+ . PARAMETER Code
29
+
30
+ A byte array consisting of the code to be disassembled.
31
+
32
+ . PARAMETER Offset
33
+
34
+ Specifies the starting address of the disassembly listing.
35
+
36
+ . PARAMETER Count
37
+
38
+ Specifies the maximum number of instructions to disassemble.
39
+
40
+ . PARAMETER Syntax
41
+
42
+ Specifies the syntax flavor to be used (INTEL vs. ATT).
43
+
44
+ . PARAMETER DetailOn
45
+
46
+ Specifies that detailed parsing should be performed - i.e. provide detailed information for each disassembled instruction.
47
+
48
+ . PARAMETER Verstion
49
+
50
+ Prints the running Capstone Framework version.
51
+
52
+ . EXAMPLE
53
+
54
+ $Bytes = [Byte[]] @( 0x8D, 0x4C, 0x32, 0x08, 0x01, 0xD8, 0x81, 0xC6, 0x34, 0x12, 0x00, 0x00 )
55
+ Get-CSDisassembly -Architecture X86 -Mode Mode16 -Code $Bytes -Offset 0x1000
56
+
57
+ $Bytes = [Byte[]] @( 0x8D, 0x4C, 0x32, 0x08, 0x01, 0xD8, 0x81, 0xC6, 0x34, 0x12, 0x00, 0x00 )
58
+ Get-CSDisassembly -Architecture X86 -Mode Mode32 -Code $Bytes
59
+
60
+ $Bytes = [Byte[]] @( 0x8D, 0x4C, 0x32, 0x08, 0x01, 0xD8, 0x81, 0xC6, 0x34, 0x12, 0x00, 0x00 )
61
+ Get-CSDisassembly -Architecture X86 -Mode Mode32 -Code $Bytes -Syntax ATT
62
+
63
+ $Bytes = [Byte[]] @( 0x55, 0x48, 0x8b, 0x05, 0xb8, 0x13, 0x00, 0x00 )
64
+ Get-CSDisassembly -Architecture X86 -Mode Mode64 -Code $Bytes -DetailOn
65
+
66
+ $Bytes = [Byte[]] @( 0xED, 0xFF, 0xFF, 0xEB, 0x04, 0xe0, 0x2d, 0xe5, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x83, 0x22, 0xe5, 0xf1, 0x02, 0x03, 0x0e, 0x00, 0x00, 0xa0, 0xe3, 0x02, 0x30, 0xc1, 0xe7, 0x00, 0x00, 0x53, 0xe3 )
67
+ Get-CSDisassembly -Architecture Arm -Mode Arm -Code $Bytes
68
+
69
+ $Bytes = [Byte[]] @( 0x4f, 0xf0, 0x00, 0x01, 0xbd, 0xe8, 0x00, 0x88, 0xd1, 0xe8, 0x00, 0xf0 )
70
+ Get-CSDisassembly -Architecture Arm -Mode Thumb -Code $Bytes
71
+
72
+ $Bytes = [Byte[]] @( 0x10, 0xf1, 0x10, 0xe7, 0x11, 0xf2, 0x31, 0xe7, 0xdc, 0xa1, 0x2e, 0xf3, 0xe8, 0x4e, 0x62, 0xf3 )
73
+ Get-CSDisassembly -Architecture Arm -Mode Arm -Code $Bytes
74
+
75
+ $Bytes = [Byte[]] @( 0x70, 0x47, 0xeb, 0x46, 0x83, 0xb0, 0xc9, 0x68 )
76
+ Get-CSDisassembly -Architecture Arm -Mode Thumb -Code $Bytes -DetailOn
77
+
78
+ $Bytes = [Byte[]] @( 0x21, 0x7c, 0x02, 0x9b, 0x21, 0x7c, 0x00, 0x53, 0x00, 0x40, 0x21, 0x4b, 0xe1, 0x0b, 0x40, 0xb9 )
79
+ Get-CSDisassembly -Architecture Arm64 -Mode Arm -Code $Bytes
80
+
81
+ $Bytes = [Byte[]] @( 0x0C, 0x10, 0x00, 0x97, 0x00, 0x00, 0x00, 0x00, 0x24, 0x02, 0x00, 0x0c, 0x8f, 0xa2, 0x00, 0x00, 0x34, 0x21, 0x34, 0x56 )
82
+ Get-CSDisassembly -Architecture Mips -Mode 'Mode32, BigEndian' -Code $Bytes
83
+
84
+ $Bytes = [Byte[]] @( 0x56, 0x34, 0x21, 0x34, 0xc2, 0x17, 0x01, 0x00 )
85
+ Get-CSDisassembly -Architecture Mips -Mode 'Mode64, LittleEndian' -Code $Bytes
86
+
87
+ $Bytes = [Byte[]] @( 0x80, 0x20, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x10, 0x43, 0x23, 0x0e, 0xd0, 0x44, 0x00, 0x80, 0x4c, 0x43, 0x22, 0x02, 0x2d, 0x03, 0x00, 0x80, 0x7c, 0x43, 0x20, 0x14, 0x7c, 0x43, 0x20, 0x93, 0x4f, 0x20, 0x00, 0x21, 0x4c, 0xc8, 0x00, 0x21 )
88
+ Get-CSDisassembly -Architecture PPC -Mode BigEndian -Code $Bytes
89
+
90
+ . INPUTS
91
+
92
+ None
93
+
94
+ You cannot pipe objects to Get-CSDisassembly.
95
+
96
+ . OUTPUTS
97
+
98
+ Capstone.Instruction[]
99
+
100
+ Get-CSDisassembly returns an array of Instruction objects.
101
+ #>
102
+
103
+ [OutputType ([Capstone.Instruction ])]
104
+ [CmdletBinding (DefaultParameterSetName = ' Disassemble' )]
105
+ Param (
106
+ [Parameter (Mandatory , ParameterSetName = ' Disassemble' )]
107
+ [Capstone.Architecture ]
108
+ $Architecture ,
109
+
110
+ [Parameter (Mandatory , ParameterSetName = ' Disassemble' )]
111
+ [Capstone.Mode ]
112
+ $Mode ,
113
+
114
+ [Parameter (Mandatory , ParameterSetName = ' Disassemble' )]
115
+ [ValidateNotNullOrEmpty ()]
116
+ [Byte []]
117
+ $Code ,
118
+
119
+ [Parameter ( ParameterSetName = ' Disassemble' )]
120
+ [UInt64 ]
121
+ $Offset = 0 ,
122
+
123
+ [Parameter ( ParameterSetName = ' Disassemble' )]
124
+ [UInt32 ]
125
+ $Count = 0 ,
126
+
127
+ [Parameter ( ParameterSetName = ' Disassemble' )]
128
+ [ValidateSet (' Intel' , ' ATT' )]
129
+ [String ]
130
+ $Syntax ,
131
+
132
+ [Parameter ( ParameterSetName = ' Disassemble' )]
133
+ [Switch ]
134
+ $DetailOn ,
135
+
136
+ [Parameter ( ParameterSetName = ' Version' )]
137
+ [Switch ]
138
+ $Version
139
+ )
140
+
141
+ if ($PsCmdlet.ParameterSetName -eq ' Version' )
142
+ {
143
+ $Disassembly = New-Object Capstone.Capstone([Capstone.Architecture ]::X86, [Capstone.Mode ]::Mode16)
144
+ $Disassembly.Version
145
+
146
+ return
147
+ }
148
+
149
+ $Disassembly = New-Object Capstone.Capstone($Architecture , $Mode )
150
+
151
+ if ($Syntax )
152
+ {
153
+ switch ($Syntax )
154
+ {
155
+ ' Intel' { $SyntaxMode = [Capstone.OptionValue ]::SyntaxIntel }
156
+ ' ATT' { $SyntaxMode = [Capstone.OptionValue ]::SyntaxATT }
157
+ }
158
+
159
+ $Disassembly.SetSyntax ($SyntaxMode )
160
+ }
161
+
162
+ if ($DetailOn )
163
+ {
164
+ $Disassembly.SetDetail ($True )
165
+ }
166
+
167
+ $Disassembly.Disassemble ($Code , $Offset , $Count )
168
+ }
0 commit comments