metasploit-framework/tools/egghunter.rb

144 lines
4.2 KiB
Ruby
Raw Normal View History

2015-03-20 19:15:14 +00:00
msfbase = __FILE__
while File.symlink?(msfbase)
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', 'lib')))
require 'msfenv'
require 'rex'
require 'msf/core'
2015-03-20 21:15:43 +00:00
require 'msf/base'
2015-03-20 19:15:14 +00:00
require 'optparse'
module Egghunter
2015-03-20 21:15:43 +00:00
class OptsConsole
def self.parse(args)
options = {}
parser = OptionParser.new do |opt|
opt.banner = "Usage: #{__FILE__} [options]"
opt.separator ''
opt.separator 'Specific options:'
2015-03-20 19:15:14 +00:00
2015-03-20 21:15:43 +00:00
options[:badchars] = ''
options[:platform] = 'windows'
options[:arch] = ARCH_X86 # 'x86'
opt.on('-f', '--format <String>', "See --list-formats for a list of supported output formats") do |v|
options[:format] = v
end
opt.on('-b', '--badchars <String>', "(Optional) Bad characters to avoid for the egg") do |v|
options[:badchars] = v
end
2015-03-20 21:30:29 +00:00
opt.on('-e', '--egg <String>', "The egg (Please give 4 bytes)") do |v|
2015-03-20 21:15:43 +00:00
options[:eggtag] = v
end
opt.on('-p', '--platform <String>', "(Optional) Platform") do |v|
options[:platform] = v
end
2015-03-20 21:30:29 +00:00
opt.on('--startreg <String>', "(Optional) The starting register") do |v|
options[:startreg] = v
end
opt.on('--forward', "(Optional) To search forward") do |v|
options[:startreg] = true
end
opt.on('--depreg <String>', "(Optional) The DEP register") do |v|
options[:depreg] = v
end
opt.on('--depdest <String>', "(Optional) The DEP destination") do |v|
options[:depdest] = v
end
opt.on('--depsize <Fixnum>', "(Optional) The DEP size") do |v|
options[:depsize] = v
end
opt.on('--depmethod <String>', "(Optional) The DEP method to use (virtualprotect/virtualalloc/copy/copy_size)") do |v|
options[:depmethod] = v
end
2015-03-20 21:15:43 +00:00
opt.on('-a', '--arch <String>', "(Optional) Architecture") do |v|
options[:arch] = v
end
opt.on('--list-formats', "List all supported output formats") do
options[:list_formats] = true
end
opt.on_tail('-h', '--help', 'Show this message') do
$stdout.puts opt
exit
end
end
parser.parse!(args)
if options.empty?
raise OptionParser::MissingArgument, 'No options set, try -h for usage'
elsif options[:format].blank? && !options[:list_formats]
raise OptionParser::MissingArgument, '-f is required'
2015-03-20 21:33:19 +00:00
elsif options[:eggtag].blank? && !options[:list_formats]
raise OptionParser::MissingArgument, '-e is required'
2015-03-20 21:15:43 +00:00
elsif options[:format] && !::Msf::Simple::Buffer.transform_formats.include?(options[:format])
raise OptionParser::InvalidOption, "#{options[:format]} is not a valid format"
2015-03-20 21:30:29 +00:00
elsif options[:depsize] && options[:depsize] =~ /^\d+$/
raise OptionParser::InvalidOption, "--depsize must be a Fixnum"
2015-03-20 21:15:43 +00:00
end
options
end
end
class Driver
def initialize
begin
@opts = OptsConsole.parse(ARGV)
rescue OptionParser::ParseError => e
$stderr.puts "[x] #{e.message}"
exit
end
2015-03-20 19:15:14 +00:00
end
def run
2015-03-20 21:15:43 +00:00
# list_formats should check first
if @opts[:list_formats]
list_formats
return
end
egghunter = Rex::Exploitation::Egghunter.new(@opts[:platform], @opts[:arch])
raw_code = egghunter.hunter_stub('', @opts[:badchars], @opts)
output_stream = $stdout
output_stream.binmode
output_stream.write ::Msf::Simple::Buffer.transform(raw_code, @opts[:format])
end
private
def list_formats
$stderr.puts "[*] Supported output formats:"
$stderr.puts ::Msf::Simple::Buffer.transform_formats.join(", ")
2015-03-20 19:15:14 +00:00
end
end
end
if __FILE__ == $PROGRAM_NAME
driver = Egghunter::Driver.new
begin
driver.run
rescue Interrupt
$stdout.puts
$stdout.puts "Good bye"
2015-03-20 21:15:43 +00:00
rescue ::Exception => e
elog("#{e.class}: #{e.message}\n#{e.backtrace * "\n"}")
$stderr.puts "[x] #{e.class}: #{e.message}"
$stderr.puts "[*] If necessary, please refer to framework.log for more details."
2015-03-20 19:15:14 +00:00
end
end