metasm 1.0.0 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +3 -0
- data/.gitignore +3 -0
- data/.hgtags +3 -0
- data/Gemfile +3 -0
- data/INSTALL +61 -0
- data/LICENCE +458 -0
- data/README +29 -21
- data/Rakefile +10 -0
- data/TODO +10 -12
- data/doc/code_organisation.txt +3 -1
- data/doc/core/DynLdr.txt +247 -0
- data/doc/core/ExeFormat.txt +43 -0
- data/doc/core/Expression.txt +220 -0
- data/doc/core/GNUExports.txt +27 -0
- data/doc/core/Ia32.txt +236 -0
- data/doc/core/SerialStruct.txt +108 -0
- data/doc/core/VirtualString.txt +145 -0
- data/doc/core/WindowsExports.txt +61 -0
- data/doc/core/index.txt +1 -0
- data/doc/style.css +6 -3
- data/doc/usage/debugger.txt +327 -0
- data/doc/usage/index.txt +1 -0
- data/doc/use_cases.txt +2 -2
- data/metasm.gemspec +23 -0
- data/{lib/metasm.rb → metasm.rb} +15 -3
- data/{lib/metasm → metasm}/compile_c.rb +15 -9
- data/metasm/cpu/arc.rb +8 -0
- data/metasm/cpu/arc/decode.rb +404 -0
- data/metasm/cpu/arc/main.rb +191 -0
- data/metasm/cpu/arc/opcodes.rb +588 -0
- data/metasm/cpu/arm.rb +14 -0
- data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
- data/{lib/metasm → metasm/cpu}/arm/decode.rb +15 -18
- data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
- data/{lib/metasm → metasm/cpu}/arm/main.rb +3 -6
- data/metasm/cpu/arm/opcodes.rb +324 -0
- data/{lib/metasm → metasm/cpu}/arm/parse.rb +25 -13
- data/{lib/metasm → metasm/cpu}/arm/render.rb +2 -2
- data/metasm/cpu/arm64.rb +15 -0
- data/metasm/cpu/arm64/debug.rb +38 -0
- data/metasm/cpu/arm64/decode.rb +285 -0
- data/metasm/cpu/arm64/encode.rb +41 -0
- data/metasm/cpu/arm64/main.rb +105 -0
- data/metasm/cpu/arm64/opcodes.rb +232 -0
- data/metasm/cpu/arm64/parse.rb +20 -0
- data/metasm/cpu/arm64/render.rb +95 -0
- data/{lib/metasm/mips/compile_c.rb → metasm/cpu/bpf.rb} +4 -2
- data/metasm/cpu/bpf/decode.rb +110 -0
- data/metasm/cpu/bpf/main.rb +60 -0
- data/metasm/cpu/bpf/opcodes.rb +81 -0
- data/metasm/cpu/bpf/render.rb +30 -0
- data/{lib/metasm/ppc.rb → metasm/cpu/cy16.rb} +2 -4
- data/metasm/cpu/cy16/decode.rb +247 -0
- data/metasm/cpu/cy16/main.rb +63 -0
- data/metasm/cpu/cy16/opcodes.rb +78 -0
- data/metasm/cpu/cy16/render.rb +30 -0
- data/metasm/cpu/dalvik.rb +11 -0
- data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +34 -34
- data/{lib/metasm → metasm/cpu}/dalvik/main.rb +71 -4
- data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +21 -12
- data/{lib/metasm/mips.rb → metasm/cpu/ebpf.rb} +3 -4
- data/metasm/cpu/ebpf/debug.rb +61 -0
- data/metasm/cpu/ebpf/decode.rb +142 -0
- data/metasm/cpu/ebpf/main.rb +58 -0
- data/metasm/cpu/ebpf/opcodes.rb +97 -0
- data/metasm/cpu/ebpf/render.rb +36 -0
- data/metasm/cpu/ia32.rb +17 -0
- data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +23 -9
- data/{lib/metasm → metasm/cpu}/ia32/debug.rb +44 -6
- data/{lib/metasm → metasm/cpu}/ia32/decode.rb +342 -128
- data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +75 -53
- data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
- data/{lib/metasm → metasm/cpu}/ia32/main.rb +66 -8
- data/metasm/cpu/ia32/opcodes.rb +1424 -0
- data/{lib/metasm → metasm/cpu}/ia32/parse.rb +55 -17
- data/{lib/metasm → metasm/cpu}/ia32/render.rb +32 -5
- data/metasm/cpu/mcs51.rb +8 -0
- data/metasm/cpu/mcs51/decode.rb +99 -0
- data/metasm/cpu/mcs51/main.rb +87 -0
- data/metasm/cpu/mcs51/opcodes.rb +120 -0
- data/metasm/cpu/mips.rb +14 -0
- data/metasm/cpu/mips/debug.rb +42 -0
- data/{lib/metasm → metasm/cpu}/mips/decode.rb +59 -38
- data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
- data/{lib/metasm → metasm/cpu}/mips/main.rb +13 -6
- data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +87 -18
- data/{lib/metasm → metasm/cpu}/mips/parse.rb +1 -1
- data/{lib/metasm → metasm/cpu}/mips/render.rb +1 -1
- data/{lib/metasm/dalvik.rb → metasm/cpu/msp430.rb} +1 -1
- data/metasm/cpu/msp430/decode.rb +243 -0
- data/metasm/cpu/msp430/main.rb +62 -0
- data/metasm/cpu/msp430/opcodes.rb +101 -0
- data/metasm/cpu/openrisc.rb +11 -0
- data/metasm/cpu/openrisc/debug.rb +106 -0
- data/metasm/cpu/openrisc/decode.rb +182 -0
- data/metasm/cpu/openrisc/decompile.rb +350 -0
- data/metasm/cpu/openrisc/main.rb +70 -0
- data/metasm/cpu/openrisc/opcodes.rb +109 -0
- data/metasm/cpu/openrisc/render.rb +37 -0
- data/{lib/metasm → metasm/cpu}/pic16c/decode.rb +6 -7
- data/{lib/metasm → metasm/cpu}/pic16c/main.rb +0 -0
- data/{lib/metasm → metasm/cpu}/pic16c/opcodes.rb +1 -1
- data/metasm/cpu/ppc.rb +11 -0
- data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -37
- data/{lib/metasm → metasm/cpu}/ppc/decompile.rb +3 -3
- data/{lib/metasm → metasm/cpu}/ppc/encode.rb +2 -2
- data/{lib/metasm → metasm/cpu}/ppc/main.rb +23 -18
- data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -6
- data/metasm/cpu/ppc/parse.rb +55 -0
- data/metasm/cpu/python.rb +8 -0
- data/metasm/cpu/python/decode.rb +116 -0
- data/metasm/cpu/python/main.rb +36 -0
- data/metasm/cpu/python/opcodes.rb +180 -0
- data/{lib/metasm → metasm/cpu}/sh4.rb +1 -1
- data/{lib/metasm → metasm/cpu}/sh4/decode.rb +50 -23
- data/{lib/metasm → metasm/cpu}/sh4/main.rb +38 -27
- data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
- data/metasm/cpu/st20.rb +9 -0
- data/metasm/cpu/st20/decode.rb +173 -0
- data/metasm/cpu/st20/decompile.rb +283 -0
- data/metasm/cpu/st20/main.rb +37 -0
- data/metasm/cpu/st20/opcodes.rb +140 -0
- data/{lib/metasm/arm.rb → metasm/cpu/webasm.rb} +4 -5
- data/metasm/cpu/webasm/debug.rb +31 -0
- data/metasm/cpu/webasm/decode.rb +321 -0
- data/metasm/cpu/webasm/decompile.rb +386 -0
- data/metasm/cpu/webasm/encode.rb +104 -0
- data/metasm/cpu/webasm/main.rb +81 -0
- data/metasm/cpu/webasm/opcodes.rb +214 -0
- data/metasm/cpu/x86_64.rb +15 -0
- data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +40 -25
- data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
- data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +58 -15
- data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +59 -28
- data/{lib/metasm → metasm/cpu}/x86_64/main.rb +18 -6
- data/metasm/cpu/x86_64/opcodes.rb +138 -0
- data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +12 -4
- data/metasm/cpu/x86_64/render.rb +35 -0
- data/metasm/cpu/z80.rb +9 -0
- data/metasm/cpu/z80/decode.rb +286 -0
- data/metasm/cpu/z80/main.rb +67 -0
- data/metasm/cpu/z80/opcodes.rb +224 -0
- data/metasm/cpu/z80/render.rb +48 -0
- data/{lib/metasm/os/main.rb → metasm/debug.rb} +201 -407
- data/{lib/metasm → metasm}/decode.rb +104 -24
- data/{lib/metasm → metasm}/decompile.rb +804 -478
- data/{lib/metasm → metasm}/disassemble.rb +385 -170
- data/{lib/metasm → metasm}/disassemble_api.rb +684 -105
- data/{lib/metasm → metasm}/dynldr.rb +231 -138
- data/{lib/metasm → metasm}/encode.rb +20 -5
- data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
- data/{lib/metasm → metasm}/exe_format/autoexe.rb +3 -0
- data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
- data/{lib/metasm → metasm}/exe_format/coff.rb +35 -7
- data/{lib/metasm → metasm}/exe_format/coff_decode.rb +70 -23
- data/{lib/metasm → metasm}/exe_format/coff_encode.rb +24 -22
- data/{lib/metasm → metasm}/exe_format/dex.rb +26 -8
- data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
- data/{lib/metasm → metasm}/exe_format/elf.rb +108 -58
- data/{lib/metasm → metasm}/exe_format/elf_decode.rb +202 -36
- data/{lib/metasm → metasm}/exe_format/elf_encode.rb +126 -32
- data/metasm/exe_format/gb.rb +65 -0
- data/metasm/exe_format/javaclass.rb +424 -0
- data/{lib/metasm → metasm}/exe_format/macho.rb +218 -16
- data/{lib/metasm → metasm}/exe_format/main.rb +28 -3
- data/{lib/metasm → metasm}/exe_format/mz.rb +2 -0
- data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
- data/{lib/metasm → metasm}/exe_format/pe.rb +96 -11
- data/metasm/exe_format/pyc.rb +167 -0
- data/{lib/metasm → metasm}/exe_format/serialstruct.rb +67 -14
- data/{lib/metasm → metasm}/exe_format/shellcode.rb +7 -3
- data/metasm/exe_format/shellcode_rwx.rb +114 -0
- data/metasm/exe_format/swf.rb +205 -0
- data/metasm/exe_format/wasm.rb +402 -0
- data/{lib/metasm → metasm}/exe_format/xcoff.rb +7 -7
- data/metasm/exe_format/zip.rb +335 -0
- data/metasm/gui.rb +13 -0
- data/{lib/metasm → metasm}/gui/cstruct.rb +35 -41
- data/{lib/metasm → metasm}/gui/dasm_coverage.rb +11 -11
- data/{lib/metasm → metasm}/gui/dasm_decomp.rb +177 -114
- data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
- data/metasm/gui/dasm_graph.rb +1754 -0
- data/{lib/metasm → metasm}/gui/dasm_hex.rb +16 -12
- data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
- data/{lib/metasm → metasm}/gui/dasm_main.rb +360 -77
- data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
- data/{lib/metasm → metasm}/gui/debug.rb +109 -34
- data/{lib/metasm → metasm}/gui/gtk.rb +174 -44
- data/{lib/metasm → metasm}/gui/qt.rb +14 -4
- data/{lib/metasm → metasm}/gui/win32.rb +180 -43
- data/{lib/metasm → metasm}/gui/x11.rb +59 -59
- data/{lib/metasm → metasm}/main.rb +421 -286
- data/metasm/os/emulator.rb +175 -0
- data/{lib/metasm/os/remote.rb → metasm/os/gdbremote.rb} +146 -54
- data/{lib/metasm → metasm}/os/gnu_exports.rb +1 -1
- data/{lib/metasm → metasm}/os/linux.rb +628 -151
- data/metasm/os/main.rb +335 -0
- data/{lib/metasm → metasm}/os/windows.rb +151 -58
- data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
- data/{lib/metasm → metasm}/parse.rb +49 -36
- data/{lib/metasm → metasm}/parse_c.rb +405 -246
- data/{lib/metasm → metasm}/preprocessor.rb +71 -41
- data/{lib/metasm → metasm}/render.rb +14 -38
- data/misc/hexdump.rb +4 -3
- data/misc/lint.rb +58 -0
- data/misc/objdiff.rb +4 -1
- data/misc/objscan.rb +1 -1
- data/misc/openrisc-parser.rb +79 -0
- data/misc/txt2html.rb +9 -7
- data/samples/bindiff.rb +3 -4
- data/samples/dasm-plugins/bindiff.rb +15 -0
- data/samples/dasm-plugins/bookmark.rb +133 -0
- data/samples/dasm-plugins/c_constants.rb +57 -0
- data/samples/dasm-plugins/colortheme_solarized.rb +125 -0
- data/samples/dasm-plugins/cppobj_funcall.rb +60 -0
- data/samples/dasm-plugins/dasm_all.rb +70 -0
- data/samples/dasm-plugins/demangle_cpp.rb +31 -0
- data/samples/dasm-plugins/deobfuscate.rb +251 -0
- data/samples/dasm-plugins/dump_text.rb +35 -0
- data/samples/dasm-plugins/export_graph_svg.rb +86 -0
- data/samples/dasm-plugins/findgadget.rb +75 -0
- data/samples/dasm-plugins/hl_opcode.rb +32 -0
- data/samples/dasm-plugins/hotfix_gtk_dbg.rb +19 -0
- data/samples/dasm-plugins/imm2off.rb +34 -0
- data/samples/dasm-plugins/match_libsigs.rb +93 -0
- data/samples/dasm-plugins/patch_file.rb +95 -0
- data/samples/dasm-plugins/scanfuncstart.rb +36 -0
- data/samples/dasm-plugins/scanxrefs.rb +29 -0
- data/samples/dasm-plugins/selfmodify.rb +197 -0
- data/samples/dasm-plugins/stringsxrefs.rb +28 -0
- data/samples/dasmnavig.rb +1 -1
- data/samples/dbg-apihook.rb +24 -9
- data/samples/dbg-plugins/heapscan.rb +283 -0
- data/samples/dbg-plugins/heapscan/compiled_heapscan_lin.c +155 -0
- data/samples/dbg-plugins/heapscan/compiled_heapscan_win.c +128 -0
- data/samples/dbg-plugins/heapscan/graphheap.rb +616 -0
- data/samples/dbg-plugins/heapscan/heapscan.rb +709 -0
- data/samples/dbg-plugins/heapscan/winheap.h +174 -0
- data/samples/dbg-plugins/heapscan/winheap7.h +307 -0
- data/samples/dbg-plugins/trace_func.rb +214 -0
- data/samples/disassemble-gui.rb +48 -7
- data/samples/disassemble.rb +31 -6
- data/samples/dump_upx.rb +24 -12
- data/samples/dynamic_ruby.rb +35 -27
- data/samples/elfencode.rb +15 -0
- data/samples/emubios.rb +251 -0
- data/samples/emudbg.rb +127 -0
- data/samples/exeencode.rb +6 -5
- data/samples/factorize-headers-peimports.rb +1 -1
- data/samples/lindebug.rb +186 -391
- data/samples/metasm-shell.rb +68 -57
- data/samples/peldr.rb +2 -2
- data/tests/all.rb +1 -1
- data/tests/arc.rb +26 -0
- data/tests/dynldr.rb +22 -4
- data/tests/expression.rb +57 -0
- data/tests/graph_layout.rb +285 -0
- data/tests/ia32.rb +80 -26
- data/tests/mcs51.rb +27 -0
- data/tests/mips.rb +10 -3
- data/tests/preprocessor.rb +18 -0
- data/tests/x86_64.rb +66 -18
- metadata +465 -219
- metadata.gz.sig +2 -0
- data/lib/metasm/arm/opcodes.rb +0 -177
- data/lib/metasm/gui.rb +0 -23
- data/lib/metasm/gui/dasm_graph.rb +0 -1354
- data/lib/metasm/ia32.rb +0 -14
- data/lib/metasm/ia32/opcodes.rb +0 -872
- data/lib/metasm/ppc/parse.rb +0 -52
- data/lib/metasm/x86_64.rb +0 -12
- data/lib/metasm/x86_64/opcodes.rb +0 -118
- data/samples/gdbclient.rb +0 -583
- data/samples/rubstop.rb +0 -399
@@ -0,0 +1,61 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/cpu/ebpf/opcodes'
|
8
|
+
|
9
|
+
module Metasm
|
10
|
+
class EBPF
|
11
|
+
def dbg_register_pc
|
12
|
+
@dbg_register_pc ||= :pc
|
13
|
+
end
|
14
|
+
def dbg_register_sp
|
15
|
+
@dbg_register_sp ||= :r10
|
16
|
+
end
|
17
|
+
|
18
|
+
def dbg_register_list
|
19
|
+
@dbg_register_list ||= [:r0, :r1, :r2, :r3, :r4, :r5, :r6, :r7, :r8, :r9, :r10, :pc]
|
20
|
+
end
|
21
|
+
|
22
|
+
def dbg_flag_list
|
23
|
+
@dbg_flag_list ||= []
|
24
|
+
end
|
25
|
+
|
26
|
+
def dbg_register_size
|
27
|
+
@dbg_register_size ||= Hash.new(64)
|
28
|
+
end
|
29
|
+
|
30
|
+
def dbg_need_stepover(dbg, addr, di)
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
def dbg_resolve_pc(di, fbd, pc_reg, dbg_ctx)
|
35
|
+
a = di.instruction.args.map { |aa| symbolic(aa) }
|
36
|
+
|
37
|
+
cond = case di.opcode.name
|
38
|
+
when 'jeq'; dbg_ctx.resolve(a[0]) == dbg_ctx.resolve(a[1])
|
39
|
+
when 'jgt'; dbg_ctx.resolve(a[0]) > dbg_ctx.resolve(a[1])
|
40
|
+
when 'jge'; dbg_ctx.resolve(a[0]) >= dbg_ctx.resolve(a[1])
|
41
|
+
when 'jset'; dbg_ctx.resolve(a[0]) & dbg_ctx.resolve(a[1]) > 0
|
42
|
+
when 'jne'; dbg_ctx.resolve(a[0]) != dbg_ctx.resolve(a[1])
|
43
|
+
when 'jsgt'; Expression.make_signed(dbg_ctx.resolve(a[0]), 64) > Expression.make_signed(dbg_ctx.resolve(a[1]), 64)
|
44
|
+
when 'jsge'; Expression.make_signed(dbg_ctx.resolve(a[0]), 64) >= Expression.make_signed(dbg_ctx.resolve(a[1]), 64)
|
45
|
+
else return super(di, fbd, pc_reg, dbg_ctx)
|
46
|
+
end
|
47
|
+
|
48
|
+
if cond
|
49
|
+
fbd[pc_reg] = a.last
|
50
|
+
else
|
51
|
+
fbd[pc_reg] = di.next_addr
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def dbg_enable_bp(dbg, bp)
|
56
|
+
end
|
57
|
+
|
58
|
+
def dbg_disable_bp(dbg, bp)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/cpu/ebpf/opcodes'
|
8
|
+
require 'metasm/decode'
|
9
|
+
|
10
|
+
module Metasm
|
11
|
+
class EBPF
|
12
|
+
def build_bin_lookaside
|
13
|
+
opcode_list.inject({}) { |h, op| h.update op.bin => op }
|
14
|
+
end
|
15
|
+
|
16
|
+
# tries to find the opcode encoded at edata.ptr
|
17
|
+
def decode_findopcode(edata)
|
18
|
+
return if edata.ptr > edata.data.length-8
|
19
|
+
di = DecodedInstruction.new self
|
20
|
+
code_off = (@endianness == :little ? 0 : 7)
|
21
|
+
code = edata.data[edata.ptr+code_off, 1].unpack('C')[0]
|
22
|
+
return di if di.opcode = @bin_lookaside[code]
|
23
|
+
end
|
24
|
+
|
25
|
+
def decode_instr_op(edata, di)
|
26
|
+
op = di.opcode
|
27
|
+
di.instruction.opname = op.name
|
28
|
+
di.bin_length = 8
|
29
|
+
blob = edata.decode_imm(:u64, @endianness)
|
30
|
+
imm = (blob >> 32) & 0xffff_ffff
|
31
|
+
imm = Expression.make_signed(imm, 32)
|
32
|
+
off = (blob >> 16) & 0xffff
|
33
|
+
off = Expression.make_signed(off, 16)
|
34
|
+
src = (blob >> 12) & 0xf
|
35
|
+
dst = (blob >> 8) & 0xf
|
36
|
+
#code = blob & 0xff
|
37
|
+
|
38
|
+
if di.opcode.props[:imm64]
|
39
|
+
imm = (imm & 0xffff_ffff) | (edata.decode_imm(:u64, @endianness) & 0xffff_ffff_0000_0000) # next_imm << 32
|
40
|
+
di.bin_length += 8
|
41
|
+
end
|
42
|
+
|
43
|
+
op.args.each { |a|
|
44
|
+
di.instruction.args << case a
|
45
|
+
when :i; Expression[imm]
|
46
|
+
when :r0; Reg.new(0)
|
47
|
+
when :rs; Reg.new(src)
|
48
|
+
when :rd; Reg.new(dst)
|
49
|
+
when :off; Expression[off]
|
50
|
+
when :p_rs_o; Memref.new(Reg.new(src), Expression[off], op.props[:msz])
|
51
|
+
when :p_rd_o; Memref.new(Reg.new(dst), Expression[off], op.props[:msz])
|
52
|
+
when :p_pkt_i; Pktref.new(nil, Expression[imm], op.props[:msz])
|
53
|
+
when :p_pkt_rs_i; Pktref.new(Reg.new(src), Expression[imm], op.props[:msz])
|
54
|
+
else raise "unhandled arg #{a}"
|
55
|
+
end
|
56
|
+
}
|
57
|
+
|
58
|
+
di
|
59
|
+
end
|
60
|
+
|
61
|
+
def decode_instr_interpret(di, addr)
|
62
|
+
if di.opcode.props[:setip]
|
63
|
+
delta = di.instruction.args[-1].reduce + 1
|
64
|
+
arg = Expression[addr, :+, 8*delta].reduce
|
65
|
+
di.instruction.args[-1] = Expression[arg]
|
66
|
+
end
|
67
|
+
|
68
|
+
di
|
69
|
+
end
|
70
|
+
|
71
|
+
# populate the @backtrace_binding hash with default values
|
72
|
+
def init_backtrace_binding
|
73
|
+
@backtrace_binding ||= {}
|
74
|
+
|
75
|
+
bswap = lambda { |val, nbytes|
|
76
|
+
case nbytes
|
77
|
+
when 1; val
|
78
|
+
when 2; Expression[[[val, :&, 0xff], :<<, 8], :|, [[val, :&, 0xff00], :>>, 8]]
|
79
|
+
when 4; Expression[[bswap[Expression[val, :&, 0xffff], 2], :<<, 16], :|, bswap[Expression[[val, :>>, 16], :&, 0xffff], 2]]
|
80
|
+
when 8; Expression[[bswap[Expression[val, :&, 0xffffffff], 4], :<<, 32], :|, bswap[Expression[[val, :>>, 32], :&, 0xffffffff], 4]]
|
81
|
+
end
|
82
|
+
}
|
83
|
+
|
84
|
+
opcode_list.map { |ol| ol.basename }.uniq.sort.each { |op|
|
85
|
+
binding = case op
|
86
|
+
|
87
|
+
when 'add'; lambda { |di, a0, a1| { a0 => Expression[a0, :+, a1] } }
|
88
|
+
when 'sub'; lambda { |di, a0, a1| { a0 => Expression[a0, :-, a1] } }
|
89
|
+
when 'mul'; lambda { |di, a0, a1| { a0 => Expression[[a0, :*, a1], :&, 0xffff_ffff_ffff_ffff] } }
|
90
|
+
when 'div'; lambda { |di, a0, a1| { a0 => Expression[a0, :/, a1] } }
|
91
|
+
when 'or'; lambda { |di, a0, a1| { a0 => Expression[a0, :|, a1] } }
|
92
|
+
when 'and'; lambda { |di, a0, a1| { a0 => Expression[a0, :&, a1] } }
|
93
|
+
when 'shl'; lambda { |di, a0, a1| { a0 => Expression[[a0, :<<, a1], :&, 0xffff_ffff_ffff_ffff] } }
|
94
|
+
when 'shr'; lambda { |di, a0, a1| { a0 => Expression[a0, :>>, a1] } } # XXX sign
|
95
|
+
when 'neg'; lambda { |di, a0| { a0 => Expression[:-, a0] } }
|
96
|
+
when 'mod'; lambda { |di, a0, a1| { a0 => Expression[a0, :%, a1] } }
|
97
|
+
when 'xor'; lambda { |di, a0, a1| { a0 => Expression[a0, :^, a1] } }
|
98
|
+
when 'mov'; lambda { |di, a0, a1| { a0 => Expression[a1] } }
|
99
|
+
when 'sar'; lambda { |di, a0, a1| { a0 => Expression[a0, :>>, a1] } }
|
100
|
+
|
101
|
+
when 'add32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :+, a1], :&, 0xffff_ffff] } }
|
102
|
+
when 'sub32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :-, a1], :&, 0xffff_ffff] } }
|
103
|
+
when 'mul32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :*, a1], :&, 0xffff_ffff] } }
|
104
|
+
when 'div32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :/, a1], :&, 0xffff_ffff] } }
|
105
|
+
when 'or32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :|, a1], :&, 0xffff_ffff] } }
|
106
|
+
when 'and32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :&, a1], :&, 0xffff_ffff] } }
|
107
|
+
when 'shl32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :<<, a1], :&, 0xffff_ffff] } }
|
108
|
+
when 'shr32'; lambda { |di, a0, a1| { a0 => Expression[[[a0, :&, 0xffff_ffff], :>>, a1], :&, 0xffff_ffff] } } # XXX sign
|
109
|
+
when 'neg32'; lambda { |di, a0| { a0 => Expression[:-, [a0, :&, 0xffff_ffff]] } }
|
110
|
+
when 'mod32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :%, a1], :&, 0xffff_ffff] } }
|
111
|
+
when 'xor32'; lambda { |di, a0, a1| { a0 => Expression[[a0, :^, a1], :&, 0xffff_ffff] } }
|
112
|
+
when 'mov32'; lambda { |di, a0, a1| { a0 => Expression[a1, :&, 0xffff_ffff] } }
|
113
|
+
when 'sar32'; lambda { |di, a0, a1| { a0 => Expression[[[a0, :&, 0xffff_ffff], :>>, a1], :&, 0xffff_ffff] } }
|
114
|
+
|
115
|
+
when 'be', 'le'; lambda { |di, a0, a1|
|
116
|
+
if @endianness.to_s[0] == di.opcode.name[0]
|
117
|
+
{}
|
118
|
+
else
|
119
|
+
{ a1 => bswap[a1, Expression[a0].reduce] }
|
120
|
+
end
|
121
|
+
}
|
122
|
+
when /^ldind|^ldabs|^stind|^stabs/; lambda { |di, a0, a1|
|
123
|
+
if @endianness == :big
|
124
|
+
{ a0 => Expression[a1] }
|
125
|
+
else
|
126
|
+
{ a0 => bswap[a1, di.opcode.props[:msz]] }
|
127
|
+
end
|
128
|
+
}
|
129
|
+
when /^ld|^st/; lambda { |di, a0, a1| { a0 => Expression[a1] } }
|
130
|
+
when /^xadd/; lambda { |di, a0, a1| { a0 => Expression[a0, :+, a1] } } # XXX bswap ?
|
131
|
+
|
132
|
+
when 'call'; lambda { |di, *a| { :r0 => Expression::Unknown } }
|
133
|
+
|
134
|
+
when 'jmp', 'jeq', 'jgt', 'jge', 'jset', 'jne', 'jsgt', 'jsge'; lambda { |di, *a| { } }
|
135
|
+
end
|
136
|
+
@backtrace_binding[op] ||= binding if binding
|
137
|
+
}
|
138
|
+
|
139
|
+
@backtrace_binding
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/main'
|
8
|
+
|
9
|
+
module Metasm
|
10
|
+
class EBPF < CPU
|
11
|
+
class Reg
|
12
|
+
attr_accessor :v
|
13
|
+
def initialize(v)
|
14
|
+
@v = v
|
15
|
+
end
|
16
|
+
|
17
|
+
def symbolic(di=nil) ; "r#@v".to_sym ; end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Memref
|
21
|
+
attr_accessor :base, :offset, :msz
|
22
|
+
|
23
|
+
def initialize(base, offset, msz)
|
24
|
+
@base = base
|
25
|
+
@offset = offset
|
26
|
+
@msz = msz
|
27
|
+
end
|
28
|
+
|
29
|
+
def symbolic(di=nil)
|
30
|
+
p = Expression[@base.symbolic] if base
|
31
|
+
p = Expression[p, :+, @offset] if offset
|
32
|
+
Indirection[p, @msz, (di.address if di)]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Pktref < Memref
|
37
|
+
def symbolic(di=nil)
|
38
|
+
p = Expression[:packet]
|
39
|
+
p = Expression[p, :+, @base.symbolic] if base
|
40
|
+
p = Expression[p, :+, @offset] if offset
|
41
|
+
Indirection[p, @msz, (di.address if di)]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def initialize(family = :latest, endianness = :big)
|
46
|
+
super()
|
47
|
+
@endianness = endianness
|
48
|
+
@size = 64
|
49
|
+
@family = family
|
50
|
+
end
|
51
|
+
|
52
|
+
def init_opcode_list
|
53
|
+
send("init_#@family")
|
54
|
+
@opcode_list
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
require 'metasm/cpu/ebpf/main'
|
7
|
+
|
8
|
+
module Metasm
|
9
|
+
|
10
|
+
# https://www.kernel.org/doc/Documentation/networking/filter.txt
|
11
|
+
class EBPF
|
12
|
+
def addop(name, bin, *args)
|
13
|
+
o = Opcode.new name, bin
|
14
|
+
args.each { |a|
|
15
|
+
o.args << a if @valid_args[a]
|
16
|
+
o.props.update a if a.kind_of?(::Hash)
|
17
|
+
}
|
18
|
+
@opcode_list << o
|
19
|
+
end
|
20
|
+
|
21
|
+
def addop_alu(name, bin)
|
22
|
+
addop name, bin | 0x07, :rd, :i
|
23
|
+
addop name, bin | 0x0F, :rd, :rs
|
24
|
+
addop name+'32', bin | 0x04, :rd, :i
|
25
|
+
addop name+'32', bin | 0x0C, :rd, :rs
|
26
|
+
end
|
27
|
+
|
28
|
+
def addop_sz32(name, bin, dst, src)
|
29
|
+
addop name + 'w', bin | 0x00, dst, src, :msz => 4
|
30
|
+
addop name + 'h', bin | 0x08, dst, src, :msz => 2
|
31
|
+
addop name + 'b', bin | 0x10, dst, src, :msz => 1
|
32
|
+
end
|
33
|
+
|
34
|
+
def addop_sz64(name, bin, dst, src)
|
35
|
+
addop name + 'w', bin | 0x00, dst, src, :msz => 4
|
36
|
+
addop name + 'dw', bin | 0x18, dst, src, :msz => 8
|
37
|
+
end
|
38
|
+
|
39
|
+
def addop_sz(name, bin, dst, src)
|
40
|
+
addop_sz32(name, bin, dst, src)
|
41
|
+
addop name + 'dw', bin | 0x18, dst, src, :msz => 8
|
42
|
+
end
|
43
|
+
|
44
|
+
def addop_j(name, bin)
|
45
|
+
addop name, bin | 0x00, :rd, :i, :off, :setip => true
|
46
|
+
addop name, bin | 0x08, :rd, :rs, :off, :setip => true
|
47
|
+
end
|
48
|
+
|
49
|
+
def init_ebpf
|
50
|
+
@opcode_list = []
|
51
|
+
[:i, :rs, :rd, :off, :p_rs_o, :p_rd_o, :r0, :p_pkt_i, :p_pkt_rs_i].each { |a| @valid_args[a] = true }
|
52
|
+
|
53
|
+
# ALU
|
54
|
+
addop_alu 'add', 0x00
|
55
|
+
addop_alu 'sub', 0x10
|
56
|
+
addop_alu 'mul', 0x20
|
57
|
+
addop_alu 'div', 0x30
|
58
|
+
addop_alu 'or', 0x40
|
59
|
+
addop_alu 'and', 0x50
|
60
|
+
addop_alu 'shl', 0x60
|
61
|
+
addop_alu 'shr', 0x70
|
62
|
+
addop 'neg', 0x87, :rd
|
63
|
+
addop 'neg32', 0x84, :rd
|
64
|
+
addop_alu 'mod', 0x90
|
65
|
+
addop_alu 'xor', 0xa0
|
66
|
+
addop_alu 'mov', 0xb0
|
67
|
+
addop_alu 'sar', 0xc0
|
68
|
+
|
69
|
+
addop 'le', 0xd4, :i, :rd # native to little endian (short if i==16, word if i==32, quad if imm==64)
|
70
|
+
addop 'be', 0xdC, :i, :rd # native to big endian
|
71
|
+
|
72
|
+
# LD/ST
|
73
|
+
addop 'lddw', 0x18, :rd, :i, :imm64 => true # next insns serves only to store high 32bits of imm64
|
74
|
+
addop_sz32 'ldabs', 0x20, :r0, :p_pkt_i
|
75
|
+
addop_sz32 'ldind', 0x40, :r0, :p_pkt_rs_i
|
76
|
+
addop_sz 'ldx', 0x61, :rd, :p_rs_o
|
77
|
+
addop_sz 'st', 0x62, :p_rd_o, :i
|
78
|
+
addop_sz 'stx', 0x63, :p_rd_o, :rs
|
79
|
+
addop_sz64 'xadd', 0xC3, :p_rd_o, :rs
|
80
|
+
|
81
|
+
# BRANCH
|
82
|
+
addop 'jmp', 0x05, :off, :setip => true, :stopexec => true # 'ja'
|
83
|
+
addop_j 'jeq', 0x15
|
84
|
+
addop_j 'jgt', 0x25
|
85
|
+
addop_j 'jge', 0x35
|
86
|
+
addop_j 'jset', 0x45
|
87
|
+
addop_j 'jne', 0x55
|
88
|
+
addop_j 'jsgt', 0x65
|
89
|
+
addop_j 'jsge', 0x75
|
90
|
+
addop 'call', 0x85, :i # native call, doesn't interfere with bpf code flow
|
91
|
+
addop 'tailcall', 0x8D, :i, :stopexec => true # tail call: r2 is a map of bpf programs, r3 is an index, pass control to r2[r3] (no return)
|
92
|
+
addop 'exit', 0x95, :stopexec => true
|
93
|
+
end
|
94
|
+
|
95
|
+
alias init_latest init_ebpf
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/cpu/ebpf/opcodes'
|
8
|
+
require 'metasm/render'
|
9
|
+
|
10
|
+
module Metasm
|
11
|
+
class EBPF
|
12
|
+
class Reg
|
13
|
+
include Renderable
|
14
|
+
def render ; ["r#@v"] end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Memref
|
18
|
+
include Renderable
|
19
|
+
def render
|
20
|
+
r = []
|
21
|
+
r << { 1 => 'byte ', 2 => 'word ', 4 => 'dword ', 8 => 'qword ' }[@msz]
|
22
|
+
r << '['
|
23
|
+
r << @base if @base
|
24
|
+
r << '+' if @base and @offset
|
25
|
+
r << @offset if @offset
|
26
|
+
r << ']'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Pktref
|
31
|
+
def render
|
32
|
+
['pkt '] + super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/metasm/cpu/ia32.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
# fix autorequire warning
|
7
|
+
class Metasm::Ia32 < Metasm::CPU
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'metasm/main'
|
11
|
+
require 'metasm/cpu/ia32/parse'
|
12
|
+
require 'metasm/cpu/ia32/encode'
|
13
|
+
require 'metasm/cpu/ia32/decode'
|
14
|
+
require 'metasm/cpu/ia32/render'
|
15
|
+
require 'metasm/cpu/ia32/compile_c'
|
16
|
+
require 'metasm/cpu/ia32/decompile'
|
17
|
+
require 'metasm/cpu/ia32/debug'
|
@@ -4,7 +4,7 @@
|
|
4
4
|
# Licence is LGPL, see LICENCE in the top-level directory
|
5
5
|
|
6
6
|
|
7
|
-
require 'metasm/ia32/parse'
|
7
|
+
require 'metasm/cpu/ia32/parse'
|
8
8
|
require 'metasm/compile_c'
|
9
9
|
|
10
10
|
module Metasm
|
@@ -267,6 +267,11 @@ class CCompiler < C::Compiler
|
|
267
267
|
e2 = inuse findreg(rsz)
|
268
268
|
op = ((type.specifier == :unsigned) ? 'movzx' : 'movsx')
|
269
269
|
op = 'mov' if e.sz == e2.sz
|
270
|
+
elsif e.sz > sz
|
271
|
+
e2 = inuse findreg(sz)
|
272
|
+
el, eh = get_composite_parts(e)
|
273
|
+
e = el
|
274
|
+
op = 'mov'
|
270
275
|
else
|
271
276
|
e2 = inuse findreg(sz)
|
272
277
|
op = 'mov'
|
@@ -527,7 +532,7 @@ class CCompiler < C::Compiler
|
|
527
532
|
if expr.rexpr.type.specifier == :unsigned and r.sz == 64
|
528
533
|
label = new_label('unsign_float')
|
529
534
|
if m.sz == 64 and @cpusz < 64
|
530
|
-
|
535
|
+
m = get_composite_parts(m)[1]
|
531
536
|
end
|
532
537
|
m2 = m
|
533
538
|
m2 = make_volatile(m, expr.rexpr.type) if m.kind_of? ModRM
|
@@ -637,7 +642,7 @@ class CCompiler < C::Compiler
|
|
637
642
|
# both sides are already cast to the same type by the precompiler
|
638
643
|
# XXX expr.type.pointer?
|
639
644
|
if expr.type.integral? and expr.type.name == :ptr and expr.lexpr.type.kind_of? C::BaseType and
|
640
|
-
|
645
|
+
typesize[expr.lexpr.type.name] == typesize[:ptr]
|
641
646
|
expr.lexpr.type.name = :ptr
|
642
647
|
end
|
643
648
|
l = c_cexpr_inner(expr.lexpr)
|
@@ -711,6 +716,7 @@ class CCompiler < C::Compiler
|
|
711
716
|
end
|
712
717
|
when :-
|
713
718
|
r = c_cexpr_inner(expr.rexpr)
|
719
|
+
r = resolve_address r if r.kind_of? Address
|
714
720
|
if r.kind_of? Expression
|
715
721
|
unuse l, r
|
716
722
|
l = Address.new(l.modrm.dup)
|
@@ -778,6 +784,9 @@ class CCompiler < C::Compiler
|
|
778
784
|
else
|
779
785
|
instr 'mov', l, Reg.new(r.val, l.sz)
|
780
786
|
end
|
787
|
+
elsif r.kind_of? Composite
|
788
|
+
rl, rh = get_composite_parts r
|
789
|
+
instr 'mov', l, rl
|
781
790
|
else
|
782
791
|
instr 'mov', l, r
|
783
792
|
end
|
@@ -1077,6 +1086,7 @@ class CCompiler < C::Compiler
|
|
1077
1086
|
when 'add', 'sub', 'and', 'or', 'xor'
|
1078
1087
|
r = make_volatile(r, type) if l.kind_of? ModRM and r.kind_of? ModRM
|
1079
1088
|
unuse r
|
1089
|
+
r = Reg.new(r.val, l.sz) if r.kind_of?(Reg) and l.kind_of?(ModRM) and l.sz and l.sz != r.sz # add byte ptr [eax], bl
|
1080
1090
|
instr op, l, r
|
1081
1091
|
when 'shr', 'sar', 'shl'
|
1082
1092
|
if r.kind_of? Expression
|
@@ -1327,11 +1337,11 @@ class CCompiler < C::Compiler
|
|
1327
1337
|
unuse l, r
|
1328
1338
|
if expr.lexpr.type.integral?
|
1329
1339
|
if expr.lexpr.type.name == :__int64 and @cpusz != 64
|
1330
|
-
raise # TODO
|
1340
|
+
raise "unsupported 64bit comparison #{expr}" # TODO
|
1331
1341
|
end
|
1332
1342
|
instr 'cmp', l, r
|
1333
1343
|
elsif expr.lexpr.type.float?
|
1334
|
-
raise # TODO
|
1344
|
+
raise "unsupported float comparison #{expr}" # TODO
|
1335
1345
|
instr 'fcmpp', l, r
|
1336
1346
|
else raise 'bad comparison ' + expr.to_s
|
1337
1347
|
end
|
@@ -1341,12 +1351,20 @@ class CCompiler < C::Compiler
|
|
1341
1351
|
r = c_cexpr_inner(expr.rexpr)
|
1342
1352
|
r = make_volatile(r, expr.rexpr.type)
|
1343
1353
|
unuse r
|
1354
|
+
if r.kind_of? Composite
|
1355
|
+
r, rh = get_composite_parts(r)
|
1356
|
+
instr 'or', r, rh
|
1357
|
+
end
|
1344
1358
|
instr 'test', r, r
|
1345
1359
|
instr 'jz', Expression[target]
|
1346
1360
|
else
|
1347
1361
|
r = c_cexpr_inner(expr)
|
1348
1362
|
r = make_volatile(r, expr.type)
|
1349
1363
|
unuse r
|
1364
|
+
if r.kind_of? Composite
|
1365
|
+
r, rh = get_composite_parts(r)
|
1366
|
+
instr 'or', r, rh
|
1367
|
+
end
|
1350
1368
|
instr 'test', r, r
|
1351
1369
|
instr 'jnz', Expression[target]
|
1352
1370
|
end
|
@@ -1509,10 +1527,6 @@ class CCompiler < C::Compiler
|
|
1509
1527
|
#File.open('m-dbg-precomp.c', 'w') { |fd| fd.puts @parser }
|
1510
1528
|
#File.open('m-dbg-src.asm', 'w') { |fd| fd.puts @source }
|
1511
1529
|
end
|
1512
|
-
|
1513
|
-
def check_reserved_name(var)
|
1514
|
-
Reg.s_to_i[var.name] or super(var)
|
1515
|
-
end
|
1516
1530
|
end
|
1517
1531
|
|
1518
1532
|
def new_ccompiler(parser, exe=ExeFormat.new)
|