nasm_shell.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env ruby
  2. ##
  3. # This module requires Metasploit: https://metasploit.com/download
  4. # Current source: https://github.com/rapid7/metasploit-framework
  5. ##
  6. #
  7. # This tool provides an easy way to see what opcodes are associated with
  8. # certain x86 instructions by making use of nasm if it is installed and
  9. # reachable through the PATH environment variable.
  10. #
  11. begin
  12. msfbase = __FILE__
  13. while File.symlink?(msfbase)
  14. msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
  15. end
  16. $:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
  17. $:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
  18. require 'msfenv'
  19. require 'rex'
  20. require 'readline'
  21. # Check to make sure nasm is installed and reachable through the user's PATH.
  22. begin
  23. Rex::Assembly::Nasm.check
  24. rescue RuntimeError
  25. puts "#{$!}"
  26. exit
  27. end
  28. bits = ARGV.length > 0 ? ARGV[0].to_i : 32
  29. if ! [16, 32, 64].include?(bits) then
  30. puts "#{bits} bits not supported"
  31. exit 1
  32. end
  33. # Start a pseudo shell and dispatch lines to be assembled and then
  34. # disassembled.
  35. history_file = File.join(Msf::Config.config_directory, 'nasm_history')
  36. shell = Rex::Ui::Text::PseudoShell.new("%bldnasm%clr", '>', history_file)
  37. shell.init_ui(Rex::Ui::Text::Input::Stdio.new, Rex::Ui::Text::Output::Stdio.new)
  38. shell.history_manager = Rex::Ui::Text::Shell::HistoryManager.new
  39. shell.run { |line|
  40. line.gsub!(/(\r|\n)/, '')
  41. line.gsub!(/\\n/, "\n")
  42. break if (line =~ /^(exit|quit)/i)
  43. begin
  44. puts(Rex::Assembly::Nasm.disassemble(
  45. Rex::Assembly::Nasm.assemble(line, bits), bits))
  46. rescue RuntimeError
  47. puts "Error: #{$!}"
  48. end
  49. }
  50. rescue SignalException => e
  51. puts("Aborted! #{e}")
  52. end