CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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