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
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # Licence is LGPL, see LICENCE in the top-level directory
6
6
 
7
- # modifies the standard ruby class String to add #decode and #encode methods
7
+ # modifies the standard ruby class String to add #asm_decode and #asm_encode methods
8
8
  # they will respectively disassemble binary data / assemble asm source
9
9
  # the default CPU is x86 32bits, change it using eg String.cpu = Metasm::MIPS.new(:big) (mips bigendian)
10
10
  #
@@ -18,74 +18,85 @@
18
18
  # > exit
19
19
 
20
20
  require 'metasm'
21
+ require 'readline'
21
22
 
22
23
  class String
23
- @@cpu = Metasm::Ia32.new
24
- class << self
25
- def cpu() @@cpu end
26
- def cpu=(c)
27
- c = Metasm.const_get(c).new if c.kind_of? String
28
- @@cpu=c
29
- end
30
- end
24
+ @@cpu = Metasm::Ia32.new
25
+ class << self
26
+ def cpu() @@cpu end
27
+ def cpu=(c)
28
+ c = Metasm.const_get(c).new if c.kind_of? String
29
+ @@cpu=c
30
+ end
31
+ end
31
32
 
32
- # encodes the current string as a Shellcode, returns the resulting EncodedData
33
- def encode_edata
34
- s = Metasm::Shellcode.assemble @@cpu, self
35
- s.encoded
36
- end
33
+ # encodes the current string as a Shellcode, returns the resulting EncodedData
34
+ def asm_encode_edata
35
+ Metasm::Shellcode.assemble(@@cpu, self).encode.encoded
36
+ end
37
37
 
38
- # encodes the current string as a Shellcode, returns the resulting binary String
39
- # outputs warnings on unresolved relocations
40
- def encode
41
- ed = encode_edata
42
- if not ed.reloc.empty?
43
- puts 'W: encoded string has unresolved relocations: ' + ed.reloc.map { |o, r| r.target.inspect }.join(', ')
44
- end
45
- ed.fill
46
- ed.data
47
- end
38
+ # encodes the current string as a Shellcode, returns the resulting binary String
39
+ # outputs warnings on unresolved relocations
40
+ def asm_encode
41
+ ed = asm_encode_edata
42
+ if not ed.reloc.empty?
43
+ puts 'W: encoded string has unresolved relocations: ' + ed.reloc.map { |o, r| r.target.inspect }.join(', ')
44
+ end
45
+ ed.fill
46
+ ed.data
47
+ end
48
48
 
49
- # decodes the current string as a Shellcode, with specified base address
50
- # returns the resulting Disassembler
51
- def decode_blocks(base_addr=0, eip=base_addr)
52
- sc = Metasm::Shellcode.decode(self, @@cpu)
53
- sc.base_addr = base_addr
54
- sc.disassemble(eip)
55
- end
49
+ # decodes the current string as a Shellcode, with specified base address
50
+ # returns the resulting Disassembler
51
+ def asm_decode_blocks(base_addr=0, eip=base_addr)
52
+ sc = Metasm::Shellcode.decode(self, @@cpu)
53
+ sc.base_addr = base_addr
54
+ sc.disassemble(eip)
55
+ end
56
56
 
57
- # decodes the current string as a Shellcode, with specified base address
58
- # returns the asm source equivallent
59
- def decode(base_addr=0, eip=base_addr)
60
- decode_blocks(base_addr, eip).to_s
61
- end
57
+ # decodes the current string as a Shellcode, with specified base address
58
+ # returns the asm source equivallent
59
+ def asm_decode(base_addr=0, eip=base_addr)
60
+ asm_decode_blocks(base_addr, eip).to_s
61
+ end
62
62
  end
63
63
 
64
64
  # get in interactive assembler mode
65
65
  def asm
66
- puts 'type "exit" or "quit" to quit', 'use ";" for newline', ''
67
- while (print "asm> " ; $stdout.flush ; l = gets)
68
- break if %w[quit exit].include? l.chomp
69
- if l.chomp == 'help'
70
- puts "Metasm assembly shell: type in opcodes to see their binary form",
71
- "You can use ';' to type multi-line stuff",
72
- "e.g. 'nop nop' will display \"\\x90\\x90\""
73
- next
74
- end
75
-
76
- begin
77
- data = l.gsub(';', "\n")
78
- next if data.strip.empty?
79
- data = data.encode
80
- puts '"' + data.unpack('C*').map { |c| '\\x%02x' % c }.join + '"'
81
- rescue Metasm::Exception => e
82
- puts "Error: #{e.class} #{e.message}"
83
- end
84
- end
66
+ puts "[+] Metasm assembly shell"
67
+ puts "type help for usage..\n\n"
85
68
 
86
- puts
69
+ Readline.completion_proc = lambda { |line| %w[help exit quit].find_all { |w| line.downcase == w[0, line.length] } }
70
+ Readline.completion_append_character = ' '
71
+
72
+ while line = Readline.readline('asm> ', true)
73
+ case line
74
+ when /^help(\W|$)/
75
+ puts "",
76
+ "Type in opcodes to see their binary form",
77
+ "You can use ';' to type multi-line stuff",
78
+ "e.g. 'nop nop' will display \"\\x90\\x90\"",
79
+ "",
80
+ "exit/quit Quit the console",
81
+ "help Show this screen",
82
+ ""
83
+ when /^(quit|exit)(\W|$)/
84
+ break
85
+ else
86
+ begin
87
+ data = line.gsub(';', "\n")
88
+ next if data.strip.empty?
89
+ e_data = data.asm_encode
90
+ puts '"' + e_data.unpack('C*').map { |c| '\\x%02x' % c }.join + '"'
91
+ rescue Metasm::Exception => e
92
+ puts "Error: #{e.class} #{e.message}"
93
+ end
94
+ end
95
+ end
96
+
97
+ puts
87
98
  end
88
99
 
89
100
  if __FILE__ == $0
90
- asm
101
+ asm
91
102
  end
data/samples/peldr.rb CHANGED
@@ -148,7 +148,7 @@ class PeLdr
148
148
  end
149
149
  }
150
150
 
151
- cp.numeric_constants.each { |k, v|
151
+ cp.numeric_constants.each { |k, v, f|
152
152
  n = k.upcase
153
153
  n = "C#{n}" if n !~ /^[A-Z]/
154
154
  DL.const_set(n, v) if not DL.const_defined?(n) and v.kind_of? Integer
@@ -178,7 +178,7 @@ class PeLdr
178
178
 
179
179
  def self.populate_peb
180
180
  DL.memory_write(@@peb, 0.chr*4096)
181
- set = lambda { |off, val| DL.memory_write_int(@@peb+off, val) }
181
+ #set = lambda { |off, val| DL.memory_write_int(@@peb+off, val) }
182
182
  end
183
183
 
184
184
  def self.teb ; @@teb ; end
data/tests/all.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  # Licence is LGPL, see LICENCE in the top-level directory
5
5
 
6
6
 
7
- Dir['tests/*.rb'].each { |f| require f }
7
+ Dir[File.expand_path('../*.rb', __FILE__)].sort.each { |f| require f }
8
8
 
data/tests/arc.rb ADDED
@@ -0,0 +1,26 @@
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 'test/unit'
7
+ require 'metasm'
8
+
9
+ class TestArc < Test::Unit::TestCase
10
+ def test_arc_dec
11
+ hex_stream = "\x0A\x23\x80\x0F\x80\x0\x60\x0D" # mov r3, 0x800D60
12
+ hex_stream += "\x40\x83" # ld_s r2, [r3, 0]
13
+
14
+ dasm = Metasm::Shellcode.disassemble(Metasm::ARC.new, hex_stream)
15
+ assert_equal(2, dasm.decoded.length)
16
+
17
+ assert_equal('mov', dasm.decoded[0].instruction.opname)
18
+ assert_equal('r3', dasm.decoded[0].instruction.args[0].to_s)
19
+ assert_equal(0x800d60, dasm.decoded[0].instruction.args[1].reduce)
20
+
21
+ assert_equal('ld_s', dasm.decoded[8].instruction.opname)
22
+ assert_equal('r2', dasm.decoded[8].instruction.args[0].to_s)
23
+ assert_equal('r3', dasm.decoded[8].instruction.args[1].base.to_s)
24
+ assert_equal(0, dasm.decoded[8].instruction.args[1].disp.reduce)
25
+ end
26
+ end
data/tests/dynldr.rb CHANGED
@@ -7,29 +7,47 @@ require 'test/unit'
7
7
  require 'metasm'
8
8
 
9
9
  class TestDynldr < Test::Unit::TestCase
10
+ def d; Metasm::DynLdr; end
10
11
 
11
- def test_dynldr
12
+ def test_new_api_c
12
13
  str = "1234"
13
- d = Metasm::DynLdr
14
14
  d.new_api_c('int memcpy(char*, char*, int)')
15
15
  d.memcpy(str, "9999", 2)
16
16
  assert_equal('9934', str)
17
+ end
17
18
 
19
+ def test_new_func_c
18
20
  c_src = <<EOS
19
- int sprintf(char*, char*, ...);
21
+ int sprintf(char*, char*, ...); // stdlib external
20
22
  void fufu(int i, char* ptr)
21
23
  {
22
- sprintf(ptr, "lolzor %i\\n", i);
24
+ static int meh; // .data
25
+ meh = i;
26
+ sprintf(ptr, "lolzor %i\\n", meh); // string constant = .rodata
23
27
  }
24
28
  EOS
25
29
  buf = 'aaaaaaaaaaaaaaaaaa'
26
30
  d.new_func_c(c_src) { d.fufu(42, buf) }
27
31
  assert_equal("lolzor 42\n\000aaaaaaa", buf)
32
+ end
28
33
 
34
+ def test_new_func_asm
29
35
  if d.host_cpu.shortname == 'ia32'
30
36
  ret = d.new_func_asm('int __fastcall bla(int)', "lea eax, [ecx+1]\nret") { d.bla(42) }
31
37
  assert_equal(43, ret)
32
38
  assert_equal(false, d.respond_to?(:bla))
33
39
  end
34
40
  end
41
+
42
+ def test_callback
43
+ c1 = d.callback_alloc_c('int lol(int);') { |i| i+1 }
44
+ c2 = d.callback_alloc_c('int lol(int);') { |i| i+2 }
45
+ c3 = d.callback_alloc_c('int lol(int);') { |i| i/2 }
46
+
47
+ d.new_func_c "int blop(int i, int (*fp)(int)) { return fp(i); }"
48
+
49
+ assert_equal(2, d.blop(1, c1))
50
+ assert_equal(4, d.blop(2, c2))
51
+ assert_equal(6, d.blop(13, c3))
52
+ end
35
53
  end
@@ -0,0 +1,57 @@
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 'test/unit'
8
+ require 'metasm'
9
+
10
+ class TestExpression < Test::Unit::TestCase
11
+ E = Metasm::Expression
12
+ def test_encode
13
+ assert_equal("\x04\0\0\0", E[4].encode(:u32, :little).data)
14
+ end
15
+
16
+ def test_numeric
17
+ assert_equal(5, E[4, :+, 1].reduce)
18
+ assert_equal(4, E[5].reduce { |e| e == 5 ? 4 : e })
19
+ assert_equal(5, E[5].reduce) # test the previous lambda does not linger around
20
+ end
21
+
22
+ def test_reduce
23
+ assert_equal(0, E[:a, :-, :a].reduce)
24
+ assert_equal(0, E[:a, :^, :a].reduce)
25
+
26
+ assert_equal(E[:a, :^, 3], E[[1, :^, :a], :^, 2].reduce)
27
+ assert_equal(E[:a], E[[:b, :^, :a], :^, :b].reduce)
28
+
29
+ assert_equal(E[:a, :+, 2], E[:b, :+, [:a, :-, [4, :+, [:b, :-, 6]]]].reduce)
30
+ assert_equal(E[:a, :&, 0xff00], E[[[:a, :>>, 8], :&, 0xff], :<<, 8].reduce)
31
+
32
+ assert_equal(E[[:a, :>>, 1], :&, 0xff0], E[[[:a, :>>, 5], :&, 0xff], :<<, 4].reduce)
33
+
34
+ assert_equal(0, E[[:a, :&, 0xff00], :&, [:b, :&, 0xff]].reduce)
35
+ assert_equal(0, E[[:a, :&, 0xff], :>>, 8].reduce)
36
+
37
+ assert_equal(E[:a, :&, 0xffff], E[[:a, :&, 0x3333], :|, [[:a, :&, 0x8888], :+, [:a, :&, 0x4444]]].reduce)
38
+
39
+ assert_equal(E[:a, :&, 0xff], E[[:a, :|, [:b, :&, 0xff00]], :&, 0xff].reduce)
40
+
41
+ assert_equal(1, E[[2, :>, 1], :'||', [:a, :<=, :b]].reduce)
42
+ assert_equal(0, E[[:a, :>, :b], :'&&', [1, :>, 2]].reduce)
43
+
44
+ assert_equal(E[:a, :>, :b], E[[:'!', [:a, :<=, :b]], :==, 1].reduce)
45
+ end
46
+
47
+ def test_pattern
48
+ pat = E[:a, :+, [:b, :&, 0xffff]].match(E['a', :|, 'b'], 'a', 'b')
49
+ assert_equal(false, pat)
50
+
51
+ pat = E[:a, :+, [:b, :&, 0xffff]].match(E['a', :+, 'b'], 'a', 'b')
52
+ assert_equal(:a, pat['a'])
53
+ p2 = pat['b'].match(E[:a, :b, :c], :a, :b, :c)
54
+ assert_equal(0xffff, p2[:c])
55
+ assert_equal(:&, p2[:b])
56
+ end
57
+ end
@@ -0,0 +1,285 @@
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
+ # special file to test the graph layout engine
8
+ # call this file directly to run
9
+
10
+ require 'metasm'
11
+ include Metasm
12
+
13
+ def test_layout(lo)
14
+ $cur ||= 0
15
+ $cur += 1
16
+ if $target.to_i != 0
17
+ return if $cur != $target.to_i
18
+ else
19
+ return if not lo.include? $target
20
+ end if $target
21
+ puts $cur, lo, '' if $VERBOSE
22
+ w = Gui::Window.new
23
+ ww = w.widget = Gui::GraphViewWidget.new(nil, nil)
24
+ ww.grab_focus
25
+ Gui.idle_add {
26
+ ww.load_dot(lo)
27
+ ww.curcontext.auto_arrange_boxes
28
+ ww.zoom_all
29
+ false
30
+ }
31
+ Gui.main
32
+ end
33
+
34
+ def test_all
35
+ test_layout <<EOS
36
+ line -> 2 -> 3 -> 4 -> 5 -> 6 -> 7;
37
+ EOS
38
+ test_layout <<EOS
39
+ sep1 -> 1;
40
+ sep2 -> 2;
41
+ sep3 -> 3;
42
+ sep4 -> 4;
43
+ sep5 -> 5;
44
+ EOS
45
+ test_layout <<EOS
46
+ fork -> 2 -> 3;
47
+ 2 -> 4;
48
+ EOS
49
+ test_layout <<EOS
50
+ diamond -> 2 -> 3 -> 5;
51
+ 2 -> 4 -> 5;
52
+ EOS
53
+ test_layout <<EOS
54
+ ifthen -> 2 -> 3 -> 4;
55
+ 2 -> 4;
56
+ EOS
57
+ test_layout <<EOS
58
+ ufork -> 2 -> 3;
59
+ 1 -> 2;
60
+ EOS
61
+ test_layout <<EOS
62
+ multidiamond -> 2 -> 31 -> 32 -> 34 -> 5 -> 6 -> 8;
63
+ 2 -> 41 -> 42 -> 44 -> 5 -> 7 -> 8;
64
+ 41 -> 43 -> 44;
65
+ 31 -> 33 -> 34;
66
+ EOS
67
+ test_layout <<EOS
68
+ dmdout -> 2 -> 3a -> 4;
69
+ 2 -> 3b -> 4;
70
+ 3a -> 4a;
71
+ 3b -> 4b;
72
+ EOS
73
+ test_layout <<EOS
74
+ ifthenthen -> 2 -> 8;
75
+ 2 -> 3 -> 8;
76
+ 2 -> 4 -> 5 -> 8;
77
+ 2 -> 6 -> 7 -> 8;
78
+ EOS
79
+ test_layout <<EOS
80
+ multipod -> 2 -> 3;
81
+ 2 -> 4;
82
+ 2 -> 5;
83
+ 2 -> 6;
84
+ 2 -> 7;
85
+ 2 -> 8;
86
+ EOS
87
+ test_layout <<EOS
88
+ mplarge -> 1 -> 2;
89
+ 1 -> 3333333333333333333333333333333333;
90
+ EOS
91
+ test_layout <<EOS
92
+ multif -> 1
93
+ 1 -> a2 -> a3
94
+ a2 -> a222222222 -> a3
95
+ 1 -> b2 -> b3
96
+ b2 -> b222222222 -> b3
97
+ EOS
98
+ test_layout <<EOS
99
+ ifx -> 1 -> 2 -> 3 -> 4 -> 5
100
+ 4 -> eeeeeeeeeeee -> 5
101
+ EOS
102
+ test_layout <<EOS
103
+ llll -> 1 -> 22222222222222222222222222 -> e
104
+ 1 -> 33333333333333333333333333 -> e
105
+ 1 -> 4444444444444444444444 -> e
106
+ 1 -> 5 -> e
107
+ 5 -> 5t -> e
108
+ 1 -> 6 -> e
109
+ 6 -> 6t -> e
110
+ 1 -> 7 -> e
111
+ 7 -> 7t -> e
112
+ EOS
113
+ test_layout <<EOS
114
+ dangling -> 2 -> 11 -> 12 -> 13 -> 4;
115
+ 2 -> 21 -> 22 -> 23 -> 4;
116
+ 2 -> 31 -> 32 -> 33 -> 4;
117
+ 21 -> 2z;
118
+ 31 -> 3z;
119
+ EOS
120
+ test_layout <<EOS
121
+ dangin -> 2 -> 11 -> 12 -> 13;
122
+ 2 -> 21 -> 22 -> 13;
123
+ 2 -> 31 -> 32 -> 33;
124
+ 22 -> 33;
125
+ 21 -> z;
126
+ EOS
127
+ test_layout <<EOS
128
+ cascadeclean -> 2 -> 3 -> 4 -> 5 -> 6 -> 62 -> 52 -> 42 -> 32 -> 22 -> e;
129
+ 2 -> 21 -> 22;
130
+ 3 -> 31 -> 32;
131
+ 4 -> 41 -> 42;
132
+ 5 -> 51 -> 52;
133
+ 6 -> 61 -> 62;
134
+ EOS
135
+ test_layout <<EOS
136
+ cascade -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> e;
137
+ 2 -> 21 -> e;
138
+ 3 -> 31 -> e;
139
+ 4 -> 41 -> e;
140
+ 5 -> 51 -> e;
141
+ 6 -> 61 -> e;
142
+ EOS
143
+ test_layout <<EOS
144
+ rstair -> 2 -> 3 -> 4 -> 5 -> 6;
145
+ 2 -> 4;
146
+ 2 -> 5;
147
+ 2 -> 6;
148
+ EOS
149
+ test_layout <<EOS
150
+ drstair -> 2a -> 3 -> 4 -> 5 -> 6;
151
+ drstair -> 2b -> 4;
152
+ 2a -> 4;
153
+ 2a -> 5;
154
+ 2a -> 6;
155
+ 2b -> 4;
156
+ 2b -> 5;
157
+ 2b -> 6;
158
+ EOS
159
+ test_layout <<EOS
160
+ mrstair -> 2a -> 3a -> 4a -> 5a -> 6a;
161
+ mrstair -> 2b -> 4a;
162
+ 2a -> 4a;
163
+ 2a -> 5a;
164
+ 2a -> 6a;
165
+ 2b -> 4a;
166
+ 2b -> 5a;
167
+ 2b -> 6a;
168
+ 2a -> 3b -> 4b -> 5b -> 6b;
169
+ 2a -> 4b;
170
+ 2a -> 5b;
171
+ 2a -> 6b;
172
+ 2b -> 3b;
173
+ 2b -> 4b;
174
+ 2b -> 5b;
175
+ 2b -> 6b;
176
+ EOS
177
+ test_layout <<EOS
178
+ loop -> 2 -> 3 -> 4;
179
+ 3 -> 2;
180
+ EOS
181
+ test_layout <<EOS
182
+ loopbreak -> 2 -> 3 -> e;
183
+ 2 -> 4 -> 5 -> 6 -> 8 -> e;
184
+ 5 -> 7 -> 4;
185
+ EOS
186
+ test_layout <<EOS
187
+ loopbreak2 -> 2 -> 3 -> e;
188
+ 2 -> 4 -> 5 -> 6 -> 8 -> e;
189
+ 5 -> 7 -> 4;
190
+ 7 -> 8;
191
+ EOS
192
+ test_layout <<EOS
193
+ unbalance -> 2 -> 3 -> 4 -> 5 -> e;
194
+ 2 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> e;
195
+ EOS
196
+ test_layout <<EOS
197
+ unbalance2 -> 2 -> 3 -> e;
198
+ 2 -> 4 -> e;
199
+ 2 -> 5 -> e;
200
+ 2 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> e;
201
+ EOS
202
+ test_layout <<EOS
203
+ unbalance3 -> 2 -> 3 -> e;
204
+ 2 -> 4 -> e;
205
+ 2 -> 5 -> e;
206
+ 2 -> 6 -> e;
207
+ 8 -> 9 -> e;
208
+ 2 -> 7 -> e;
209
+ EOS
210
+ test_layout <<EOS
211
+ disjoint -> 1 -> 2 -> 3 -> 4 -> 5 -> 6;
212
+ l1 -> l2;
213
+ l1 -> l3;
214
+ l1 -> l4;
215
+ l1 -> l5;
216
+ l1 -> l6;
217
+ EOS
218
+
219
+ test_layout <<EOS
220
+ lol -> 2 -> 31 -> 41 -> 5;
221
+ 2 -> 32 -> 42 -> 5;
222
+ 31 -> 42;
223
+ 41 -> 32;
224
+ EOS
225
+ test_layout <<EOS
226
+ nestloop -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> e;
227
+ 6 -> 4;
228
+ 7 -> 3;
229
+ EOS
230
+ test_layout <<EOS
231
+ escape -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8;
232
+ 2 -> 21;
233
+ 4 -> 6;
234
+ EOS
235
+ test_layout <<EOS
236
+ loophead -> 1 -> loophead;
237
+ 2 -> 3 -> 2;
238
+ 3 -> 4;
239
+ 1 -> 4;
240
+ EOS
241
+ test_layout <<EOS
242
+ 1 -> e1;
243
+ l00pz -> 1 -> l00pz;
244
+ l2 -> 2 -> l2;
245
+ 2 -> e1;
246
+ 2 -> e2;
247
+ l3 -> 3 -> l3;
248
+ 3 -> e2;
249
+ EOS
250
+ test_layout <<EOS
251
+ 3loop -> 1 -> 3loop;
252
+ 1 -> 2 -> 3 -> 2;
253
+ 0 -> 00 -> 0 -> 2;
254
+ EOS
255
+ test_layout <<EOS
256
+ foo -> 0 -> 1
257
+ 0 -> 2 -> 3 -> 4 -> 5
258
+ 4 -> 6
259
+ 4 -> 7 -> 5
260
+ 4 -> 8 -> 6
261
+ 2 -> 1 -> 7
262
+ 3 -> 1 -> 8
263
+ EOS
264
+ test_layout <<EOS
265
+ dang -> 2 -> 3 -> 4 -> 5 -> 6 -> 4;
266
+ 2 -> 9;
267
+ 5 -> 9;
268
+ EOS
269
+ test_layout <<EOS
270
+ dang2 -> 2 -> 3 -> 4 -> 5 -> 6 -> 4
271
+ 2 -> 9
272
+ 5 -> 9
273
+ 9 -> a -> 9
274
+ EOS
275
+ test_layout <<EOS
276
+ onlyloop -> onlyloop
277
+ EOS
278
+
279
+ rescue Interrupt
280
+ end
281
+
282
+ if __FILE__ == $0
283
+ $target = ARGV[0]
284
+ test_all
285
+ end