samples/disassemble.rb in metasm-1.0.1 vs samples/disassemble.rb in metasm-1.0.2
- old
+ new
@@ -46,25 +46,38 @@
# load the file
if exename =~ /^live:(.*)/
raise 'no such live target' if not target = OS.current.find_process($1)
p target if $VERBOSE
- exe = Shellcode.decode(target.memory, Metasm.const_get(opts[:sc_cpu]).new)
+ opts[:sc_cpu] = eval(opts[:sc_cpu]) if opts[:sc_cpu] =~ /[.(\s:]/
+ opts[:sc_cpu] = Metasm.const_get(opts[:sc_cpu]) if opts[:sc_cpu].kind_of(::String)
+ opts[:sc_cpu] = opts[:sc_cpu].new if opts[:sc_cpu].kind_of?(::Class)
+ exe = Shellcode.decode(target.memory, opts[:sc_cpu])
else
- exefmt = opts[:exe_fmt] ? Metasm.const_get(opts[:exe_fmt]) : AutoExe.orshellcode { Metasm.const_get(opts[:sc_cpu]).new }
- exefmt = exefmt.withcpu(Metasm.const_get(opts[:sc_cpu]).new) if opts[:exe_fmt] == 'Shellcode' and opts[:sc_cpu]
+ opts[:sc_cpu] = eval(opts[:sc_cpu]) if opts[:sc_cpu] =~ /[.(\s:]/
+ opts[:exe_fmt] = eval(opts[:exe_fmt]) if opts[:exe_fmt] =~ /[.(\s:]/
+ if opts[:exe_fmt].kind_of?(::String)
+ exefmt = opts[:exe_fmt] = Metasm.const_get(opts[:exe_fmt])
+ else
+ exefmt = opts[:exe_fmt] || AutoExe.orshellcode {
+ opts[:sc_cpu] = Metasm.const_get(opts[:sc_cpu]) if opts[:sc_cpu].kind_of?(::String)
+ opts[:sc_cpu] = opts[:sc_cpu].new if opts[:sc_cpu].kind_of?(::Class)
+ opts[:sc_cpu]
+ }
+ end
+ exefmt = exefmt.withcpu(opts[:sc_cpu]) if exefmt.kind_of?(::Class) and exefmt.name.to_s.split('::').last == 'Shellcode'
exe = exefmt.decode_file(exename)
exe.disassembler.rebase(opts[:rebase]) if opts[:rebase]
if opts[:autoload]
basename = exename.sub(/\.\w\w?\w?$/, '')
opts[:map] ||= basename + '.map' if File.exist?(basename + '.map')
opts[:cheader] ||= basename + '.h' if File.exist?(basename + '.h')
(opts[:plugin] ||= []) << (basename + '.rb') if File.exist?(basename + '.rb')
end
end
# set options
-dasm = exe.init_disassembler
+dasm = exe.disassembler
makeint = lambda { |addr|
case addr
when /^[0-9].*h/; addr.to_i(16)
when /^[0-9]/; Integer(addr)
else dasm.normalize(addr)
@@ -73,11 +86,17 @@
dasm.load_map opts[:map] if opts[:map]
dasm.parse_c_file opts[:cheader] if opts[:cheader]
dasm.backtrace_maxblocks_data = -1 if opts[:nodatatrace]
dasm.debug_backtrace = true if opts[:debugbacktrace]
opts[:stopaddr].to_a.each { |addr| dasm.decoded[makeint[addr]] = true }
-opts[:plugin].to_a.each { |p| dasm.load_plugin p }
+opts[:plugin].to_a.each { |p|
+ begin
+ dasm.load_plugin p
+ rescue ::Exception
+ puts "Error with plugin #{p}: #{$!.class} #{$!}"
+ end
+}
opts[:hookstr].to_a.each { |f| eval f }
t1 = Time.now if opts[:benchmark]
# do the work
begin
@@ -96,10 +115,16 @@
dasm.save_file(opts[:savefile]) if opts[:savefile]
dasm.decompile(*dasm.entrypoints)
tdc = Time.now if opts[:benchmark]
end
-opts[:post_plugin].to_a.each { |p| dasm.load_plugin p }
+opts[:post_plugin].to_a.each { |p|
+ begin
+ dasm.load_plugin p
+ rescue ::Exception
+ puts "Error with plugin #{p}: #{$!.class} #{$!}"
+ end
+}
dasm.save_file(opts[:savefile]) if opts[:savefile]
# output
if opts[:outfile]