Module: PWN::Plugins::Assembly

Defined in:
lib/pwn/plugins/assembly.rb

Overview

This plugin converts images to readable text

Class Method Summary collapse

Class Method Details

.asm_to_opcodes(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Assembly.asm_to_opcodes(

asm: 'required - assembly instruction(s) (e.g. 'nop\nnop\nnop\njmp rsp\n)',
arch: 'optional - architecture returned from objdump --info (defaults to PWN::Plugins::DetectOS.arch)',
endian: 'optional - endianess :big|:little (defaults to current system endianess)'

)



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
# File 'lib/pwn/plugins/assembly.rb', line 124

public_class_method def self.asm_to_opcodes(opts = {})
  asm = opts[:asm]
  arch = opts[:arch] ||= PWN::Plugins::DetectOS.arch
  endian = opts[:endian] ||= PWN::Plugins::DetectOS.endian
  endian = endian.to_sym if opts[:endian]

  asm_tmp = Tempfile.new('pwn_asm')

  raise 'ERROR: asm parameter is required.' if asm.nil?

  case arch.to_s.downcase
  when 'i386', 'i686', 'x86'
    arch_obj = Metasm::Ia32.new(endian)
  when 'amd64', 'x86_64'
    arch_obj = Metasm::X86_64.new(endian)
  when 'arc'
    arch_obj = Metasm::ARC.new(endian)
  when 'armv4l', 'armv4b', 'armv5l', 'armv5b', 'armv6l', 'armv6b', 'armv7b', 'armv7l', 'arm', 'armhf'
    arch_obj = Metasm::ARM.new(endian)
  when 'aarch64', 'arm64'
    arch_obj = Metasm::ARM64.new(endian)
  when 'bpf'
    arch_obj = Metasm::BPF.new(endian)
  when 'cy16'
    arch_obj = Metasm::CY16.new(endian)
  when 'dalvik'
    arch_obj = Metasm::Dalvik.new(endian)
  when 'ebpf'
    arch_obj = Metasm::EBPF.new(endian)
  when 'mcs51'
    arch_obj = Metasm::MCS51.new(endian)
  when 'mips'
    arch_obj = Metasm::MIPS.new(endian)
  when 'mips64'
    arch_obj = Metasm::MIPS64.new(endian)
  when 'msp430'
    arch_obj = Metasm::MSP430.new(endian)
  when 'openrisc'
    arch_obj = Metasm::OpenRisc.new(endian)
  when 'ppc'
    arch_obj = Metasm::PPC.new(endian)
  when 'sh4'
    arch_obj = Metasm::SH4.new(endian)
  when 'st20'
    arch_obj = Metasm::ST20.new(endian)
  when 'webasm'
    arch_obj = Metasm::WebAsm.new(endian)
  when 'z80'
    arch_obj = Metasm::Z80.new(endian)
  else
    raise "Unsupported architecture: #{arch}"
  end

  opcodes = Metasm::Shellcode.assemble(arch_obj, asm).encode_string
  hex_encoded_opcodes = opcodes.bytes.map { |b| format('\x%02x', b) }.join

  "\n#{hex_encoded_opcodes}\n"
rescue Metasm::ParseError
  puts "Invalid assembly instruction(s) provided:\n#{asm}"
  # Should we try to call opcode_to_asm here or just raise the error?
rescue StandardError => e
  raise e
end

.authorsObject

Author(s)

0day Inc. <[email protected]>



232
233
234
235
236
# File 'lib/pwn/plugins/assembly.rb', line 232

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.helpObject

Display Usage for this Module



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/pwn/plugins/assembly.rb', line 240

public_class_method def self.help
  puts "USAGE:
    #{self}.opcodes_to_asm(
      opcodes: 'required - hex escaped opcode(s) (e.g. \"\\x90\\x90\\x90\")',
      opcodes_always_string_obj: 'optional - always interpret opcodes passed in as a string object (defaults to false)',
      arch: 'optional - architecture returned from objdump --info (defaults to PWN::Plugins::DetectOS.arch)',
      endian: 'optional - endianess :big|:little (defaults to system endianess)'
    )

    #{self}.asm_to_opcodes(
      asm: 'required - assembly instruction(s) (e.g. 'nop\nnop\nnop\njmp rsp\n)',
      arch: 'optional - architecture returned from objdump --info (defaults to PWN::Plugins::DetectOS.arch)',
      endian: 'optional - endianess :big|:little (defaults to system endianess)'
    )

    #{self}.list_supported_archs

    #{self}.authors
  "
end

.list_supported_archsObject

Supported Method Parameters

PWN::Plugins::Assembly.list_archs



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
# File 'lib/pwn/plugins/assembly.rb', line 191

public_class_method def self.list_supported_archs
  [
    { name: 'i386', endian: 'little' },
    { name: 'i686', endian: 'little' },
    { name: 'x86', endian: 'little' },
    { name: 'amd64', endian: 'little' },
    { name: 'x86_64', endian: 'little' },
    { name: 'arc', endian: 'little' },
    { name: 'armv4l', endian: 'little' },
    { name: 'armv4b', endian: 'big' },
    { name: 'armv5l', endian: 'little' },
    { name: 'armv5b', endian: 'big' },
    { name: 'armv6l', endian: 'little' },
    { name: 'armv6b', endian: 'big' },
    { name: 'armv7b', endian: 'big' },
    { name: 'armv7l', endian: 'little' },
    { name: 'arm', endian: 'little' },
    { name: 'armhf', endian: 'little' },
    { name: 'aarch64', endian: 'little' },
    { name: 'arm64', endian: 'little' },
    { name: 'bpf', endian: 'little' },
    { name: 'cy16', endian: 'little' },
    { name: 'dalvik', endian: 'little' },
    { name: 'ebpf', endian: 'little' },
    { name: 'mcs51', endian: 'little' },
    { name: 'mips', endian: 'little' },
    { name: 'mips64', endian: 'little' },
    { name: 'msp430', endian: 'little' },
    { name: 'openrisc', endian: 'little' },
    { name: 'ppc', endian: 'little' },
    { name: 'sh4', endian: 'little' },
    { name: 'st20', endian: 'little' },
    { name: 'webasm', endian: 'little' },
    { name: 'z80', endian: 'little' }
  ]
rescue StandardError => e
  raise e
end

.opcodes_to_asm(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Assembly.opcodes_to_asm(

opcodes: 'required - hex escaped opcode(s) (e.g. "\x90\x90\x90")',
opcodes_always_string_obj: 'optional - always interpret opcodes passed in as a string object (defaults to false)',
arch: 'optional - architecture returned from objdump --info (defaults to PWN::Plugins::DetectOS.arch)',
endian: 'optional - endianess :big|:little (defaults to current system endianess)'

)



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
# File 'lib/pwn/plugins/assembly.rb', line 19

public_class_method def self.opcodes_to_asm(opts = {})
  opcodes = opts[:opcodes]
  opcodes_always_string_obj = opts[:opcodes_always_string_obj] ||= false
  arch = opts[:arch] ||= PWN::Plugins::DetectOS.arch
  endian = opts[:endian] ||= PWN::Plugins::DetectOS.endian
  endian = endian.to_sym if opts[:endian]

  raise 'ERROR: opcodes parameter is required.' if opcodes.nil?

  case arch.to_s.downcase
  when 'i386', 'i686', 'x86'
    arch_obj = Metasm::Ia32.new(endian)
  when 'amd64', 'x86_64'
    arch_obj = Metasm::X86_64.new(endian)
  when 'arc'
    arch_obj = Metasm::ARC.new(endian)
  when 'armv4l', 'armv4b', 'armv5l', 'armv5b', 'armv6l', 'armv6b', 'armv7b', 'armv7l', 'arm', 'armhf'
    arch_obj = Metasm::ARM.new(endian)
  when 'aarch64', 'arm64'
    arch_obj = Metasm::ARM64.new(endian)
  when 'bpf'
    arch_obj = Metasm::BPF.new(endian)
  when 'cy16'
    arch_obj = Metasm::CY16.new(endian)
  when 'dalvik'
    arch_obj = Metasm::Dalvik.new(endian)
  when 'ebpf'
    arch_obj = Metasm::EBPF.new(endian)
  when 'mcs51'
    arch_obj = Metasm::MCS51.new(endian)
  when 'mips'
    arch_obj = Metasm::MIPS.new(endian)
  when 'mips64'
    arch_obj = Metasm::MIPS64.new(endian)
  when 'msp430'
    arch_obj = Metasm::MSP430.new(endian)
  when 'openrisc'
    arch_obj = Metasm::OpenRisc.new(endian)
  when 'ppc'
    arch_obj = Metasm::PPC.new(endian)
  when 'sh4'
    arch_obj = Metasm::SH4.new(endian)
  when 'st20'
    arch_obj = Metasm::ST20.new(endian)
  when 'webasm'
    arch_obj = Metasm::WebAsm.new(endian)
  when 'z80'
    arch_obj = Metasm::Z80.new(endian)
  else
    raise "Unsupported architecture: #{arch}"
  end

  # TOOD: Still needs a fix if opcodes are passed in as:
  # '\x90\x90\x90' (not to be confused w/ "\x90\x90\x90")
  # '909090'
  opcodes_orig_len = opcodes.length
  opcodes = opcodes.join(',') if opcodes.is_a?(Array)
  # puts opcodes.inspect
  opcodes = CGI.escape(opcodes)
  # puts opcodes.inspect
  # known to work (when method is called directly) with:
  # 'ffe4'
  # 'ff,e4'
  # 'ff e4'
  # "ff,e4"
  # "ff e4"
  # ['ff', 'e4']
  # ["ff", "e4"]
  # '\xff\xe4'
  # "\xff\xe4"
  # "'ff', 'e4'"
  # '"ff", "e4"'
  # only known to work in pwn REPL driver with:
  # ffe4
  # ff e4
  # puts opcodes.inspect
  #  More stripping if passed in via pwn REPL driver
  # if opcodes_always_string_obj
  # end

  opcodes.delete!('%5B')
  opcodes.delete!('%5D')
  opcodes.delete!('%5Cx')
  opcodes.delete!('%2C')
  opcodes.delete!('%22')
  opcodes.delete!('%27')
  opcodes.delete!('+')
  opcodes.delete!('%')

  # puts opcodes.inspect
  opcodes = [opcodes].pack('H*')
  # puts opcodes.inspect

  Metasm::Shellcode.disassemble(arch_obj, opcodes).to_s.squeeze("\n")
rescue StandardError => e
  raise e
end