metasm 1.0.0 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +3 -0
  4. data/.gitignore +3 -0
  5. data/.hgtags +3 -0
  6. data/Gemfile +3 -0
  7. data/INSTALL +61 -0
  8. data/LICENCE +458 -0
  9. data/README +29 -21
  10. data/Rakefile +10 -0
  11. data/TODO +10 -12
  12. data/doc/code_organisation.txt +3 -1
  13. data/doc/core/DynLdr.txt +247 -0
  14. data/doc/core/ExeFormat.txt +43 -0
  15. data/doc/core/Expression.txt +220 -0
  16. data/doc/core/GNUExports.txt +27 -0
  17. data/doc/core/Ia32.txt +236 -0
  18. data/doc/core/SerialStruct.txt +108 -0
  19. data/doc/core/VirtualString.txt +145 -0
  20. data/doc/core/WindowsExports.txt +61 -0
  21. data/doc/core/index.txt +1 -0
  22. data/doc/style.css +6 -3
  23. data/doc/usage/debugger.txt +327 -0
  24. data/doc/usage/index.txt +1 -0
  25. data/doc/use_cases.txt +2 -2
  26. data/metasm.gemspec +23 -0
  27. data/{lib/metasm.rb → metasm.rb} +15 -3
  28. data/{lib/metasm → metasm}/compile_c.rb +15 -9
  29. data/metasm/cpu/arc.rb +8 -0
  30. data/metasm/cpu/arc/decode.rb +404 -0
  31. data/metasm/cpu/arc/main.rb +191 -0
  32. data/metasm/cpu/arc/opcodes.rb +588 -0
  33. data/metasm/cpu/arm.rb +14 -0
  34. data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
  35. data/{lib/metasm → metasm/cpu}/arm/decode.rb +15 -18
  36. data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
  37. data/{lib/metasm → metasm/cpu}/arm/main.rb +3 -6
  38. data/metasm/cpu/arm/opcodes.rb +324 -0
  39. data/{lib/metasm → metasm/cpu}/arm/parse.rb +25 -13
  40. data/{lib/metasm → metasm/cpu}/arm/render.rb +2 -2
  41. data/metasm/cpu/arm64.rb +15 -0
  42. data/metasm/cpu/arm64/debug.rb +38 -0
  43. data/metasm/cpu/arm64/decode.rb +285 -0
  44. data/metasm/cpu/arm64/encode.rb +41 -0
  45. data/metasm/cpu/arm64/main.rb +105 -0
  46. data/metasm/cpu/arm64/opcodes.rb +232 -0
  47. data/metasm/cpu/arm64/parse.rb +20 -0
  48. data/metasm/cpu/arm64/render.rb +95 -0
  49. data/{lib/metasm/mips/compile_c.rb → metasm/cpu/bpf.rb} +4 -2
  50. data/metasm/cpu/bpf/decode.rb +110 -0
  51. data/metasm/cpu/bpf/main.rb +60 -0
  52. data/metasm/cpu/bpf/opcodes.rb +81 -0
  53. data/metasm/cpu/bpf/render.rb +30 -0
  54. data/{lib/metasm/ppc.rb → metasm/cpu/cy16.rb} +2 -4
  55. data/metasm/cpu/cy16/decode.rb +247 -0
  56. data/metasm/cpu/cy16/main.rb +63 -0
  57. data/metasm/cpu/cy16/opcodes.rb +78 -0
  58. data/metasm/cpu/cy16/render.rb +30 -0
  59. data/metasm/cpu/dalvik.rb +11 -0
  60. data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +34 -34
  61. data/{lib/metasm → metasm/cpu}/dalvik/main.rb +71 -4
  62. data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +21 -12
  63. data/{lib/metasm/mips.rb → metasm/cpu/ebpf.rb} +3 -4
  64. data/metasm/cpu/ebpf/debug.rb +61 -0
  65. data/metasm/cpu/ebpf/decode.rb +142 -0
  66. data/metasm/cpu/ebpf/main.rb +58 -0
  67. data/metasm/cpu/ebpf/opcodes.rb +97 -0
  68. data/metasm/cpu/ebpf/render.rb +36 -0
  69. data/metasm/cpu/ia32.rb +17 -0
  70. data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +23 -9
  71. data/{lib/metasm → metasm/cpu}/ia32/debug.rb +44 -6
  72. data/{lib/metasm → metasm/cpu}/ia32/decode.rb +342 -128
  73. data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +75 -53
  74. data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
  75. data/{lib/metasm → metasm/cpu}/ia32/main.rb +66 -8
  76. data/metasm/cpu/ia32/opcodes.rb +1424 -0
  77. data/{lib/metasm → metasm/cpu}/ia32/parse.rb +55 -17
  78. data/{lib/metasm → metasm/cpu}/ia32/render.rb +32 -5
  79. data/metasm/cpu/mcs51.rb +8 -0
  80. data/metasm/cpu/mcs51/decode.rb +99 -0
  81. data/metasm/cpu/mcs51/main.rb +87 -0
  82. data/metasm/cpu/mcs51/opcodes.rb +120 -0
  83. data/metasm/cpu/mips.rb +14 -0
  84. data/metasm/cpu/mips/debug.rb +42 -0
  85. data/{lib/metasm → metasm/cpu}/mips/decode.rb +59 -38
  86. data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
  87. data/{lib/metasm → metasm/cpu}/mips/main.rb +13 -6
  88. data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +87 -18
  89. data/{lib/metasm → metasm/cpu}/mips/parse.rb +1 -1
  90. data/{lib/metasm → metasm/cpu}/mips/render.rb +1 -1
  91. data/{lib/metasm/dalvik.rb → metasm/cpu/msp430.rb} +1 -1
  92. data/metasm/cpu/msp430/decode.rb +243 -0
  93. data/metasm/cpu/msp430/main.rb +62 -0
  94. data/metasm/cpu/msp430/opcodes.rb +101 -0
  95. data/metasm/cpu/openrisc.rb +11 -0
  96. data/metasm/cpu/openrisc/debug.rb +106 -0
  97. data/metasm/cpu/openrisc/decode.rb +182 -0
  98. data/metasm/cpu/openrisc/decompile.rb +350 -0
  99. data/metasm/cpu/openrisc/main.rb +70 -0
  100. data/metasm/cpu/openrisc/opcodes.rb +109 -0
  101. data/metasm/cpu/openrisc/render.rb +37 -0
  102. data/{lib/metasm → metasm/cpu}/pic16c/decode.rb +6 -7
  103. data/{lib/metasm → metasm/cpu}/pic16c/main.rb +0 -0
  104. data/{lib/metasm → metasm/cpu}/pic16c/opcodes.rb +1 -1
  105. data/metasm/cpu/ppc.rb +11 -0
  106. data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -37
  107. data/{lib/metasm → metasm/cpu}/ppc/decompile.rb +3 -3
  108. data/{lib/metasm → metasm/cpu}/ppc/encode.rb +2 -2
  109. data/{lib/metasm → metasm/cpu}/ppc/main.rb +23 -18
  110. data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -6
  111. data/metasm/cpu/ppc/parse.rb +55 -0
  112. data/metasm/cpu/python.rb +8 -0
  113. data/metasm/cpu/python/decode.rb +116 -0
  114. data/metasm/cpu/python/main.rb +36 -0
  115. data/metasm/cpu/python/opcodes.rb +180 -0
  116. data/{lib/metasm → metasm/cpu}/sh4.rb +1 -1
  117. data/{lib/metasm → metasm/cpu}/sh4/decode.rb +50 -23
  118. data/{lib/metasm → metasm/cpu}/sh4/main.rb +38 -27
  119. data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
  120. data/metasm/cpu/st20.rb +9 -0
  121. data/metasm/cpu/st20/decode.rb +173 -0
  122. data/metasm/cpu/st20/decompile.rb +283 -0
  123. data/metasm/cpu/st20/main.rb +37 -0
  124. data/metasm/cpu/st20/opcodes.rb +140 -0
  125. data/{lib/metasm/arm.rb → metasm/cpu/webasm.rb} +4 -5
  126. data/metasm/cpu/webasm/debug.rb +31 -0
  127. data/metasm/cpu/webasm/decode.rb +321 -0
  128. data/metasm/cpu/webasm/decompile.rb +386 -0
  129. data/metasm/cpu/webasm/encode.rb +104 -0
  130. data/metasm/cpu/webasm/main.rb +81 -0
  131. data/metasm/cpu/webasm/opcodes.rb +214 -0
  132. data/metasm/cpu/x86_64.rb +15 -0
  133. data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +40 -25
  134. data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
  135. data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +58 -15
  136. data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +59 -28
  137. data/{lib/metasm → metasm/cpu}/x86_64/main.rb +18 -6
  138. data/metasm/cpu/x86_64/opcodes.rb +138 -0
  139. data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +12 -4
  140. data/metasm/cpu/x86_64/render.rb +35 -0
  141. data/metasm/cpu/z80.rb +9 -0
  142. data/metasm/cpu/z80/decode.rb +286 -0
  143. data/metasm/cpu/z80/main.rb +67 -0
  144. data/metasm/cpu/z80/opcodes.rb +224 -0
  145. data/metasm/cpu/z80/render.rb +48 -0
  146. data/{lib/metasm/os/main.rb → metasm/debug.rb} +201 -407
  147. data/{lib/metasm → metasm}/decode.rb +104 -24
  148. data/{lib/metasm → metasm}/decompile.rb +804 -478
  149. data/{lib/metasm → metasm}/disassemble.rb +385 -170
  150. data/{lib/metasm → metasm}/disassemble_api.rb +684 -105
  151. data/{lib/metasm → metasm}/dynldr.rb +231 -138
  152. data/{lib/metasm → metasm}/encode.rb +20 -5
  153. data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
  154. data/{lib/metasm → metasm}/exe_format/autoexe.rb +3 -0
  155. data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
  156. data/{lib/metasm → metasm}/exe_format/coff.rb +35 -7
  157. data/{lib/metasm → metasm}/exe_format/coff_decode.rb +70 -23
  158. data/{lib/metasm → metasm}/exe_format/coff_encode.rb +24 -22
  159. data/{lib/metasm → metasm}/exe_format/dex.rb +26 -8
  160. data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
  161. data/{lib/metasm → metasm}/exe_format/elf.rb +108 -58
  162. data/{lib/metasm → metasm}/exe_format/elf_decode.rb +202 -36
  163. data/{lib/metasm → metasm}/exe_format/elf_encode.rb +126 -32
  164. data/metasm/exe_format/gb.rb +65 -0
  165. data/metasm/exe_format/javaclass.rb +424 -0
  166. data/{lib/metasm → metasm}/exe_format/macho.rb +218 -16
  167. data/{lib/metasm → metasm}/exe_format/main.rb +28 -3
  168. data/{lib/metasm → metasm}/exe_format/mz.rb +2 -0
  169. data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
  170. data/{lib/metasm → metasm}/exe_format/pe.rb +96 -11
  171. data/metasm/exe_format/pyc.rb +167 -0
  172. data/{lib/metasm → metasm}/exe_format/serialstruct.rb +67 -14
  173. data/{lib/metasm → metasm}/exe_format/shellcode.rb +7 -3
  174. data/metasm/exe_format/shellcode_rwx.rb +114 -0
  175. data/metasm/exe_format/swf.rb +205 -0
  176. data/metasm/exe_format/wasm.rb +402 -0
  177. data/{lib/metasm → metasm}/exe_format/xcoff.rb +7 -7
  178. data/metasm/exe_format/zip.rb +335 -0
  179. data/metasm/gui.rb +13 -0
  180. data/{lib/metasm → metasm}/gui/cstruct.rb +35 -41
  181. data/{lib/metasm → metasm}/gui/dasm_coverage.rb +11 -11
  182. data/{lib/metasm → metasm}/gui/dasm_decomp.rb +177 -114
  183. data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
  184. data/metasm/gui/dasm_graph.rb +1754 -0
  185. data/{lib/metasm → metasm}/gui/dasm_hex.rb +16 -12
  186. data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
  187. data/{lib/metasm → metasm}/gui/dasm_main.rb +360 -77
  188. data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
  189. data/{lib/metasm → metasm}/gui/debug.rb +109 -34
  190. data/{lib/metasm → metasm}/gui/gtk.rb +174 -44
  191. data/{lib/metasm → metasm}/gui/qt.rb +14 -4
  192. data/{lib/metasm → metasm}/gui/win32.rb +180 -43
  193. data/{lib/metasm → metasm}/gui/x11.rb +59 -59
  194. data/{lib/metasm → metasm}/main.rb +421 -286
  195. data/metasm/os/emulator.rb +175 -0
  196. data/{lib/metasm/os/remote.rb → metasm/os/gdbremote.rb} +146 -54
  197. data/{lib/metasm → metasm}/os/gnu_exports.rb +1 -1
  198. data/{lib/metasm → metasm}/os/linux.rb +628 -151
  199. data/metasm/os/main.rb +335 -0
  200. data/{lib/metasm → metasm}/os/windows.rb +151 -58
  201. data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
  202. data/{lib/metasm → metasm}/parse.rb +49 -36
  203. data/{lib/metasm → metasm}/parse_c.rb +405 -246
  204. data/{lib/metasm → metasm}/preprocessor.rb +71 -41
  205. data/{lib/metasm → metasm}/render.rb +14 -38
  206. data/misc/hexdump.rb +4 -3
  207. data/misc/lint.rb +58 -0
  208. data/misc/objdiff.rb +4 -1
  209. data/misc/objscan.rb +1 -1
  210. data/misc/openrisc-parser.rb +79 -0
  211. data/misc/txt2html.rb +9 -7
  212. data/samples/bindiff.rb +3 -4
  213. data/samples/dasm-plugins/bindiff.rb +15 -0
  214. data/samples/dasm-plugins/bookmark.rb +133 -0
  215. data/samples/dasm-plugins/c_constants.rb +57 -0
  216. data/samples/dasm-plugins/colortheme_solarized.rb +125 -0
  217. data/samples/dasm-plugins/cppobj_funcall.rb +60 -0
  218. data/samples/dasm-plugins/dasm_all.rb +70 -0
  219. data/samples/dasm-plugins/demangle_cpp.rb +31 -0
  220. data/samples/dasm-plugins/deobfuscate.rb +251 -0
  221. data/samples/dasm-plugins/dump_text.rb +35 -0
  222. data/samples/dasm-plugins/export_graph_svg.rb +86 -0
  223. data/samples/dasm-plugins/findgadget.rb +75 -0
  224. data/samples/dasm-plugins/hl_opcode.rb +32 -0
  225. data/samples/dasm-plugins/hotfix_gtk_dbg.rb +19 -0
  226. data/samples/dasm-plugins/imm2off.rb +34 -0
  227. data/samples/dasm-plugins/match_libsigs.rb +93 -0
  228. data/samples/dasm-plugins/patch_file.rb +95 -0
  229. data/samples/dasm-plugins/scanfuncstart.rb +36 -0
  230. data/samples/dasm-plugins/scanxrefs.rb +29 -0
  231. data/samples/dasm-plugins/selfmodify.rb +197 -0
  232. data/samples/dasm-plugins/stringsxrefs.rb +28 -0
  233. data/samples/dasmnavig.rb +1 -1
  234. data/samples/dbg-apihook.rb +24 -9
  235. data/samples/dbg-plugins/heapscan.rb +283 -0
  236. data/samples/dbg-plugins/heapscan/compiled_heapscan_lin.c +155 -0
  237. data/samples/dbg-plugins/heapscan/compiled_heapscan_win.c +128 -0
  238. data/samples/dbg-plugins/heapscan/graphheap.rb +616 -0
  239. data/samples/dbg-plugins/heapscan/heapscan.rb +709 -0
  240. data/samples/dbg-plugins/heapscan/winheap.h +174 -0
  241. data/samples/dbg-plugins/heapscan/winheap7.h +307 -0
  242. data/samples/dbg-plugins/trace_func.rb +214 -0
  243. data/samples/disassemble-gui.rb +48 -7
  244. data/samples/disassemble.rb +31 -6
  245. data/samples/dump_upx.rb +24 -12
  246. data/samples/dynamic_ruby.rb +35 -27
  247. data/samples/elfencode.rb +15 -0
  248. data/samples/emubios.rb +251 -0
  249. data/samples/emudbg.rb +127 -0
  250. data/samples/exeencode.rb +6 -5
  251. data/samples/factorize-headers-peimports.rb +1 -1
  252. data/samples/lindebug.rb +186 -391
  253. data/samples/metasm-shell.rb +68 -57
  254. data/samples/peldr.rb +2 -2
  255. data/tests/all.rb +1 -1
  256. data/tests/arc.rb +26 -0
  257. data/tests/dynldr.rb +22 -4
  258. data/tests/expression.rb +57 -0
  259. data/tests/graph_layout.rb +285 -0
  260. data/tests/ia32.rb +80 -26
  261. data/tests/mcs51.rb +27 -0
  262. data/tests/mips.rb +10 -3
  263. data/tests/preprocessor.rb +18 -0
  264. data/tests/x86_64.rb +66 -18
  265. metadata +465 -219
  266. metadata.gz.sig +2 -0
  267. data/lib/metasm/arm/opcodes.rb +0 -177
  268. data/lib/metasm/gui.rb +0 -23
  269. data/lib/metasm/gui/dasm_graph.rb +0 -1354
  270. data/lib/metasm/ia32.rb +0 -14
  271. data/lib/metasm/ia32/opcodes.rb +0 -872
  272. data/lib/metasm/ppc/parse.rb +0 -52
  273. data/lib/metasm/x86_64.rb +0 -12
  274. data/lib/metasm/x86_64/opcodes.rb +0 -118
  275. data/samples/gdbclient.rb +0 -583
  276. data/samples/rubstop.rb +0 -399
@@ -0,0 +1,75 @@
1
+ # This file is part of Metasm, the Ruby assembly manipulation suite
2
+ # Copyright (C) 2006-2011 Yoann GUILLOT
3
+ #
4
+ # Licence is LGPL, see LICENCE in the top-level directory
5
+
6
+
7
+ # metasm dasm plugin
8
+ # scan for a given asm instruction sequence (all encodings)
9
+ # add the G dasm-gui shortcut, the input change ';' for line splits
10
+
11
+ def findgadget_asm_to_regex(asm)
12
+ fullre = ''
13
+ asm = asm.gsub(';', "\n")
14
+
15
+ sc = Shellcode.new(@cpu)
16
+ sc.parse asm
17
+ sc.source.each { |i|
18
+ case i
19
+ when Data
20
+ opts_edata = i.encode(@cpu.endianness)
21
+ when Instruction
22
+ opts_edata = @cpu.encode_instruction(sc, i)
23
+ else
24
+ raise "cant scan for #{i}"
25
+ end
26
+
27
+ opts_edata = [opts_edata] if opts_edata.kind_of?(EncodedData)
28
+
29
+ opts_re = opts_edata.map { |ed|
30
+ # Regexp.escape ed.data, with relocs replaced with '.'
31
+ re = ''
32
+ off = 0
33
+ ed.reloc.sort.each { |o, rel|
34
+ re << Regexp.escape(ed.data[off...o])
35
+ re << ('.' * rel.length)
36
+ off = o + rel.length
37
+ }
38
+ re << Regexp.escape(ed.data[off..-1])
39
+ }
40
+ fullre << '(' << opts_re.join('|') << ')'
41
+ }
42
+
43
+ Regexp.new(fullre, Regexp::MULTILINE, 'n')
44
+ end
45
+
46
+ # parse asm to a regexp, return the list of addresses matching
47
+ def findgadget_asm(asm)
48
+ pattern_scan(findgadget_asm_to_regex(asm))
49
+ end
50
+
51
+ def findgadget_prompt
52
+ gui.inputbox("source for the gadget - separate with ;") { |asm|
53
+ lst = findgadget_asm(asm)
54
+ list = [['address', 'section']]
55
+ sections = section_info
56
+ list += lst.map { |addr|
57
+ # [name, addr, len, misc]
58
+ if s = sections.find { |s_| s_[1] <= addr and s_[1] + s_[2] > addr }
59
+ s = s[0]
60
+ else
61
+ s = '?'
62
+ end
63
+ [Expression[addr], s]
64
+ }
65
+ gui.listwindow("gadgetscan for #{asm}", list) { |args| gui.focus_addr(args[0]) }
66
+ }
67
+ end
68
+
69
+ if gui
70
+ gui.keyboard_callback[?G] = lambda { |*a| findgadget_prompt }
71
+ w = gui.toplevel
72
+ w.addsubmenu(w.find_menu('Actions'), 'Scan for _Gadget', 'G') { findgadget_prompt }
73
+ w.update_menu
74
+ :success
75
+ end
@@ -0,0 +1,32 @@
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
+ # metasm dasm GUI plugin: hilight lines of code based on the opcode name
8
+ if gui
9
+ @gui_opcode_color = {
10
+ :call => :green_bg,
11
+ :jmp => :red_bg,
12
+ :jcc => :orange_bg,
13
+ }
14
+
15
+ obg = gui.bg_color_callback # chain old callback
16
+ gui.bg_color_callback = lambda { |a|
17
+ if di = di_at(a) and pr = di.opcode.props
18
+ if pr[:saveip] and (@function[di.block.to_normal.to_a.first] or di.block.to_subfuncret.to_a.first)
19
+ # don't color call+pop
20
+ @gui_opcode_color[:call]
21
+ elsif pr[:stopexec]
22
+ @gui_opcode_color[:jmp]
23
+ elsif pr[:setip]
24
+ @gui_opcode_color[:jcc]
25
+ else
26
+ obg[a] if obg
27
+ end
28
+ else
29
+ obg[a] if obg
30
+ end
31
+ }
32
+ end
@@ -0,0 +1,19 @@
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
+ #
8
+ # This plugin will create a monitoring process running samples/hotfix_gtk_dbg.rb on the current process (to fix a GTK crash when closing a window)
9
+ #
10
+
11
+ mypid = Process.pid
12
+
13
+ if (!Process.fork)
14
+ ARGV.clear
15
+ ARGV << mypid
16
+ $VERBOSE = false
17
+ Kernel.load File.join(Metasmdir, 'samples', 'hotfix_gtk_dbg.rb')
18
+ exit!
19
+ end
@@ -0,0 +1,34 @@
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
+ # metasm dasm plugin
8
+ # walks all disassembled instructions referencing an address
9
+ # if the address is a label, update the instruction to use the label
10
+ # esp. useful after a disassemble_fast, with a .map file
11
+
12
+ def addrtolabel
13
+ bp = prog_binding.invert
14
+ @decoded.each_value { |di|
15
+ next if not di.kind_of?(DecodedInstruction)
16
+ di.each_expr { |e|
17
+ next unless e.kind_of?(Expression)
18
+ if l = bp[e.lexpr]
19
+ add_xref(e.lexpr, Xref.new(:addr, di.address))
20
+ e.lexpr = Expression[l]
21
+ end
22
+ if l = bp[e.rexpr]
23
+ add_xref(e.rexpr, Xref.new(:addr, di.address))
24
+ e.rexpr = (e.lexpr ? Expression[l] : l)
25
+ end
26
+ }
27
+ }
28
+ nil
29
+ end
30
+
31
+ if gui
32
+ addrtolabel
33
+ gui.gui_update
34
+ end
@@ -0,0 +1,93 @@
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
+ # metasm dasm plugin: allow loading library signature files (see samples/generate_libsigs.rb)
8
+
9
+ class LibSignature
10
+ attr_accessor :sigs, :siglenmax, :giantregex
11
+ # load signatures from a signature file
12
+ def initialize(file)
13
+ # hash symbolname => signature
14
+ @sigs = {}
15
+
16
+ # populate sigs
17
+ symname = nil
18
+ sig = ''
19
+ File.read(file).each_line { |l|
20
+ case l
21
+ when /^ /
22
+ sig << l.strip
23
+ else
24
+ @sigs[symname] = sig
25
+ symname = l.strip
26
+ sig = ''
27
+ end
28
+ }
29
+ @sigs[symname] = sig
30
+ @sigs.delete nil
31
+ @siglenmax = @sigs.values.map { |v| v.length }.max
32
+
33
+ # compile a giant regex from the signatures
34
+ re = @sigs.values.uniq.map { |sigh|
35
+ sigh.gsub(/../) { |b| b == '..' ? '.' : ('\\x' + b) }
36
+ }.join('|')
37
+
38
+ # 'n' is a magic flag to allow high bytes in the regex (ruby1.9 + utfail)
39
+ @giantregex = Regexp.new re, Regexp::MULTILINE, 'n'
40
+ end
41
+
42
+ # we found a match on str at off, identify the specific symbol that matched
43
+ # on conflict, only return the first match
44
+ def matched_findsym(str, off)
45
+ str = str[off, @siglenmax].unpack('H*').first
46
+ @sigs.find { |sym, sig| str =~ /^#{sig}/i }[0]
47
+ end
48
+
49
+ # matches the signatures against a raw string
50
+ # yields offset, symname for each match
51
+ # returns nr of matches found
52
+ def match_chunk(str)
53
+ count = 0
54
+ off = 0
55
+ while o = (str[off..-1] =~ @giantregex)
56
+ count += 1
57
+ off += o
58
+ sym = matched_findsym(str, off)
59
+ yield off, sym
60
+ off += 1
61
+ end
62
+ count
63
+ end
64
+
65
+ # matches the signatures against a big raw string
66
+ # yields offset, symname for each match
67
+ # returns nr of matches found
68
+ def match(str)
69
+ chunksz = 1 << 20
70
+
71
+ chunkoff = 0
72
+ count = 0
73
+ while chunkoff < str.length
74
+ chunk = str[chunkoff, chunksz+@siglenmax]
75
+ count += match_chunk(chunk) { |o, sym| yield chunkoff+o, sym if o < chunksz }
76
+ chunkoff += chunksz
77
+ end
78
+ count
79
+ end
80
+ end
81
+
82
+ def match_libsigs(sigfile)
83
+ ls = LibSignature.new(sigfile)
84
+ count = 0
85
+ @sections.each { |b, s|
86
+ count += ls.match(s.data) { |off, sym| set_label_at(b+off, sym) }
87
+ }
88
+ count
89
+ end
90
+
91
+ if gui
92
+ gui.openfile('signature file to load') { |f| gui.messagebox "#{match_libsigs(f)} signatures found" }
93
+ end
@@ -0,0 +1,95 @@
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
+ # metasm dasm plugin: allow patching the file from the dasm interface
8
+ # use P to assemble a new instruction at the current address
9
+
10
+ # backup the executable file
11
+ def backup_program_file
12
+ f = @program.filename
13
+ if File.exist?(f) and not File.exist?(f + '.bak')
14
+ File.open(f + '.bak', 'wb') { |wfd|
15
+ File.open(f, 'rb') { |rfd|
16
+ while buf = rfd.read(1024*1024)
17
+ wfd.write buf
18
+ end
19
+ }
20
+ }
21
+ end
22
+ end
23
+
24
+ # create a backup and reopen the backend VirtualFile RW
25
+ def reopen_rw(addr=nil, edata=nil)
26
+ if not edata
27
+ sections.each { |k, v| reopen_rw(k, v) }
28
+ return true
29
+ end
30
+
31
+ return if not File.writable?(@program.filename)
32
+ backup_program_file
33
+ if not edata.data.kind_of? VirtualFile
34
+ # section too small, loaded as real String
35
+ # force reopen as VFile (allow hexediting in gui)
36
+ return if not off = addr_to_fileoff(addr)
37
+ len = edata.data.length
38
+ edata.data = VirtualFile.read(@program.filename, 'rb+').dup(off, len)
39
+ else
40
+ edata.data.fd.reopen @program.filename, 'rb+'
41
+ end
42
+ end
43
+
44
+ raise "cant find original file" if not @program.filename or not File.exist? @program.filename
45
+
46
+ reopen_rw
47
+
48
+ def patch_instrs(addr, asmsrc)
49
+ sc = Metasm::Shellcode.new(cpu, addr) # pfx needed for autorequire
50
+ sc.assemble(asmsrc, cpu)
51
+ sc.encoded.fixup! prog_binding # allow references to dasm labels in the shellcode
52
+ raw = sc.encode_string
53
+
54
+ if s = get_section_at(addr) and s[0].data.kind_of? VirtualFile
55
+ s[0][s[0].ptr, raw.length] = raw
56
+ elsif o = addr_to_fileoff(addr) # section too small, not loaded as a VirtFile
57
+ backup_program_file
58
+ File.open(@program.filename, 'rb+') { |fd|
59
+ fd.pos = o
60
+ fd.write raw
61
+ }
62
+ s[0][s[0].ptr, raw.length] = raw if s
63
+ else
64
+ return
65
+ end
66
+
67
+ b = split_block(addr)
68
+
69
+ # clear what we had in the rewritten space
70
+ raw.length.times { |rawoff|
71
+ next if not di = di_at(addr+rawoff)
72
+ di.block.list.each { |ldi| @decoded.delete ldi.address }
73
+ }
74
+
75
+ disassemble_fast(addr) if b
76
+ if b and @decoded[addr]
77
+ nb = @decoded[addr].block
78
+ nb.from_normal = b.from_normal
79
+ nb.from_subfuncret = b.from_subfuncret
80
+ nb.from_indirect = b.from_indirect
81
+ end
82
+ true
83
+ end
84
+
85
+ if gui
86
+ gui.keyboard_callback[?P] = lambda { |k|
87
+ addr = gui.curaddr
88
+ gui.inputbox('new instructions') { |src|
89
+ src = src.gsub(/;\s+/, "\n")
90
+ patch_instrs(addr, src)
91
+ gui.gui_update
92
+ }
93
+ true
94
+ }
95
+ 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
+ # metasm dasm plugin: scan the memory for a 'ret' which could indicate the beginning of the current function
8
+ # (x86 only)
9
+ def scanfuncstart(addr)
10
+ if o = (1..16).find { |off| @decoded[addr-off].kind_of? DecodedInstruction } and @decoded[addr-o].bin_length == o
11
+ addr -= o
12
+ end
13
+ if @decoded[addr].kind_of? DecodedInstruction
14
+ fs = find_function_start(addr)
15
+ return fs if fs != addr
16
+ end
17
+ edata = get_edata_at(addr)
18
+ if o = (1..1000).find { |off|
19
+ @decoded[addr-off-1] or
20
+ edata.data[edata.ptr-off-1] == ?\xcc or
21
+ edata.data[edata.ptr-off-1] == ?\xc3 or
22
+ edata.data[edata.ptr-off-3] == ?\xc2
23
+ }
24
+ o -= @decoded[addr-o-1].bin_length-1 if @decoded[addr-o-1].kind_of? DecodedInstruction
25
+ addr-o
26
+ end
27
+ end
28
+
29
+ if gui
30
+ gui.keyboard_callback_ctrl[?P] = lambda { |*a|
31
+ if o = scanfuncstart(gui.curaddr)
32
+ gui.focus_addr(o)
33
+ end
34
+ true
35
+ }
36
+ end
@@ -0,0 +1,29 @@
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
+ # metasm dasm plugin: scan for xrefs to the target address, incl. relative offsets (eg near call/jmp)
8
+ def scanxrefs(target)
9
+ ans = []
10
+ csz = cpu.size
11
+ msk = (1 << csz) - 1
12
+ upq = (csz == 64 ? 'q' : 'V')
13
+ sections.sort.each { |s_addr, edata|
14
+ raw = edata.data.to_str
15
+ (0..raw.length-csz/8).each { |off|
16
+ r = raw[off, csz/8].unpack(upq).first
17
+ ans << (s_addr + off) if (r + off+csz/8 + s_addr) & msk == target or r == target
18
+ }
19
+ }
20
+ ans
21
+ end
22
+
23
+ gui.keyboard_callback[?X] = lambda { |*a|
24
+ target = gui.curaddr
25
+ ans = scanxrefs(target)
26
+ list = [['addr']] + ans.map { |off| [Expression[off].to_s] }
27
+ gui.listwindow("scanned xrefs to #{Expression[target]}", list) { |i| gui.focus_addr i[0] }
28
+ true
29
+ } if gui
@@ -0,0 +1,197 @@
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
+ #
8
+ # This file tries to handle simple self-modifying code patterns
9
+ # To be used as a --plugin for a Disassembler object
10
+ #
11
+
12
+ module SMC
13
+
14
+ # a copy-on-write copy of dasm address space (continuous segment only)
15
+ class CoWData
16
+ attr_accessor :startaddr, :data
17
+ def initialize(dasm)
18
+ @dasm = dasm
19
+ @startaddr = 0
20
+ @data = ''
21
+ end
22
+
23
+ # return a substring, either from the local cache or from dasm
24
+ # handles overlap
25
+ def [](addr, len)
26
+ if @data.empty?
27
+ s, e = @dasm.get_section_at(addr)
28
+ return if not s
29
+ return s.read(len)
30
+ end
31
+ raddr = addr - @base
32
+ rstart = @startaddr - @base
33
+ if raddr >= rstart and raddr+len <= rstart+@data.length
34
+ @data[0, raddr+len-rstart]
35
+ else
36
+ s, e = @dasm.get_section_at(addr)
37
+ return if not s
38
+ obuf = s.read(len)
39
+ len = obuf.length
40
+ if raddr < rstart and raddr+len > rstart
41
+ olen = [raddr+len-rstart, @data.length].min
42
+ obuf[rstart-raddr, olen] = @data[0, olen]
43
+ elsif raddr < rstart+@data.length and raddr+len > rstart+@data.length
44
+ obuf[0, rstart+@data.length-raddr] = @data[raddr-rstart, rstart+@data.length-raddr]
45
+ end
46
+ obuf
47
+ end
48
+ end
49
+
50
+ # set a substring value in the cache
51
+ def []=(addr, len, newdata)
52
+ raise 'len mismatch' if len != newdata.length
53
+ if @data.empty?
54
+ @base = @startaddr = addr
55
+ @data << newdata
56
+ return
57
+ end
58
+ raddr = addr - @base
59
+ rstart = @startaddr - @base
60
+ if raddr+newdata.length < rstart
61
+ s, e = @dasm.get_section_at(addr)
62
+ raise if not s
63
+ obuf = s.read(rstart-(raddr+newdata.length))
64
+ raise if obuf.length != rstart-(raddr+newdata.length)
65
+ newdata += obuf
66
+ elsif raddr > rstart+@data.length
67
+ s, e = @dasm.get_section_at(@startaddr+@data.length)
68
+ raise if not s
69
+ obuf = s.read(raddr-(rstart+@data.length))
70
+ raise if obuf.length != raddr-(rstart+@data.length)
71
+ @data += obuf
72
+ end
73
+ if raddr < rstart
74
+ @data = newdata + @data[raddr+newdata.length-rstart..-1].to_s
75
+ @startaddr = addr
76
+ else
77
+ @data[raddr-rstart, newdata.length] = newdata
78
+ end
79
+ end
80
+ end
81
+
82
+ VirtSections = {}
83
+
84
+ # try to emulate the byte modifications
85
+ # creates a new virtual section in dasm holding decoded data
86
+ # adds the virtual section to the dasm, stores the addresses in VirtSections[dasm]
87
+ # returns true if successful
88
+ def self.emu(dasm, addr)
89
+ puts "emulate SMC @#{Metasm::Expression[addr]}" if $VERBOSE
90
+
91
+ writer = nil
92
+ dasm.each_xref(addr, :w) { |xr| writer = xr.origin }
93
+ return if not dasm.di_at(writer)
94
+
95
+ a_pre, a_entry, a_cond, a_out, loop_bd = find_loop(dasm, writer)
96
+ return if not a_pre
97
+
98
+ # expression checking if we get out of the loop
99
+ loop_again_cond = dasm.cpu.get_jump_condition(dasm.decoded[a_cond])
100
+ loop_again_cond = Expression[:'!', loop_again_cond] if dasm.decoded[a_cond].next_addr != a_out
101
+
102
+ init_bd = {}
103
+ loop_bd.values.map { |v| v.externals }.flatten.uniq.each { |ext|
104
+ bt = dasm.backtrace(ext, a_pre, :include_start => true)
105
+ init_bd[ext] = bt.first if bt.length == 1 and bt.first != Metasm::Expression::Unknown and bt.first != Metasm::Expression[ext]
106
+ }
107
+
108
+ # reject non-determinist memory write
109
+ loop_bd.delete_if { |k, v| k.kind_of? Metasm::Indirection and not dasm.get_section_at(k.pointer.bind(init_bd).reduce) }
110
+
111
+ cow_data = CoWData.new(dasm)
112
+
113
+ puts "emulation running..." if $VERBOSE
114
+ pre_bd = init_bd
115
+ loop do
116
+ # the effects of the loop
117
+ post_bd = loop_bd.inject({}) { |bd, (k, v)|
118
+ if k.kind_of? Metasm::Indirection
119
+ k = k.bind(pre_bd).reduce_rec
120
+ raise "bad ptr #{k}" if not dasm.get_section_at(k.pointer.reduce)
121
+ end
122
+ bd.update k => Metasm::Expression[v.bind(pre_bd).reduce]
123
+ }
124
+
125
+ # the indirections used by the loop
126
+ # read mem from cow_data
127
+ # ignores stacked indirections & keys
128
+ ind_bd = {}
129
+ post_bd.values.map { |v| v.expr_indirections }.flatten.uniq.each { |ind|
130
+ p = ind.pointer.reduce
131
+ raise "bad loop read #{ind}" if not p.kind_of? Integer
132
+ ind_bd[ind] = Metasm::Expression.decode_imm(cow_data[p, ind.len], "u#{ind.len*8}".to_sym, dasm.cpu.endianness)
133
+ }
134
+
135
+ post_bd.each { |k, v|
136
+ next if not k.kind_of? Metasm::Indirection
137
+ cow_data[k.pointer.reduce, k.len] = Metasm::Expression.encode_imm(v.bind(ind_bd).reduce, "u#{k.len*8}".to_sym, dasm.cpu.endianness)
138
+ }
139
+
140
+ break if loop_again_cond.bind(post_bd).reduce == 0
141
+
142
+ pre_bd.update(post_bd)
143
+ pre_bd.delete_if { |k, v| not k.kind_of? Symbol }
144
+ end
145
+
146
+ puts "emulation done (#{cow_data.data.length} bytes)" if $VERBOSE
147
+
148
+ VirtSections[dasm] ||= {}
149
+ newbase = "smc#{VirtSections[dasm].length}"
150
+ VirtSections[dasm][addr] = newbase
151
+ dasm.add_section(Metasm::EncodedData.new(cow_data.data), newbase)
152
+ dasm.comment[Metasm::Expression[newbase]] = "SelfModifyingCode from #{dasm.decoded[writer]}"
153
+
154
+ true
155
+ end
156
+
157
+ # find the loop containing addr
158
+ # only trivial loops handled
159
+ # returns [loop start, last instr before loop, loop conditionnal jump, 1st instr after loop, loop binding]
160
+ def self.find_loop(dasm, addr)
161
+ b = dasm.decoded[addr].block
162
+ return if not b.to_normal.to_a.include? b.address
163
+ b1 = b2 = b
164
+
165
+ pre = (b1.from_normal - [b2.list.last.address]).first
166
+ first = b1.address
167
+ last = b2.list.last.address
168
+ post = (b2.to_normal - [b1.address]).first
169
+ loop_bd = dasm.code_binding(first, post, :include_flags => true)
170
+
171
+ [pre, first, last, post, loop_bd]
172
+ end
173
+
174
+ # redirects the code flow from addr to the decoded section
175
+ def self.redirect(dasm, addr)
176
+ return if not VirtSections[dasm] or not newto = Metasm::Expression[VirtSections[dasm][addr]]
177
+ dasm.each_instructionblock { |b|
178
+ next if not b.to_normal.to_a.include? addr
179
+ b.to_normal.map! { |tn| dasm.normalize(tn) == addr ? newto : tn }
180
+ dasm.add_xref(newto, Metasm::Xref.new(:x, b.list.last.address))
181
+ b.list.last.add_comment "x:#{newto}"
182
+ dasm.addrs_todo << { :addr => newto, :from => b.list.last.address }
183
+ }
184
+ end
185
+ end
186
+
187
+ if self.kind_of? Metasm::Disassembler
188
+ # setup the smc callbacks
189
+ dasm = self
190
+ list = []
191
+ dasm.callback_selfmodifying = lambda { |addr| list << addr }
192
+ dasm.callback_finished = lambda {
193
+ while addr = list.pop
194
+ SMC.emu(dasm, addr) and SMC.redirect(dasm, addr)
195
+ end
196
+ }
197
+ end