metasploit-framework/tools/egghunter.rb

157 lines
5.2 KiB
Ruby
Raw Normal View History

2015-03-21 04:48:13 +00:00
#!/usr/bin/env ruby
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|
2015-03-21 01:55:30 +00:00
opt.banner = "Usage: #{__FILE__} [options]\nExample: #{__FILE__} -f python -e W00T"
2015-03-20 21:15:43 +00:00
opt.separator ''
opt.separator 'Specific options:'
2015-03-20 19:15:14 +00:00
2015-03-20 21:15:43 +00:00
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|
2015-03-20 21:34:58 +00:00
# Do not change this key. This should matching the one in Rex::Exploitation::Egghunter
2015-03-20 21:30:29 +00:00
options[:startreg] = v
end
opt.on('--forward', "(Optional) To search forward") do |v|
2015-03-20 21:34:58 +00:00
# Do not change this key. This should matching the one in Rex::Exploitation::Egghunter
2015-03-21 17:14:00 +00:00
options[:searchforward] = true
2015-03-20 21:30:29 +00:00
end
opt.on('--depreg <String>', "(Optional) The DEP register") do |v|
2015-03-20 21:34:58 +00:00
# Do not change this key. This should matching the one in Rex::Exploitation::Egghunter
2015-03-20 21:30:29 +00:00
options[:depreg] = v
end
opt.on('--depdest <String>', "(Optional) The DEP destination") do |v|
2015-03-20 21:34:58 +00:00
# Do not change this key. This should matching the one in Rex::Exploitation::Egghunter
2015-03-20 21:30:29 +00:00
options[:depdest] = v
end
opt.on('--depsize <Fixnum>', "(Optional) The DEP size") do |v|
2015-03-20 21:34:58 +00:00
# Do not change this key. This should matching the one in Rex::Exploitation::Egghunter
2015-03-20 21:30:29 +00:00
options[:depsize] = v
end
opt.on('--depmethod <String>', "(Optional) The DEP method to use (virtualprotect/virtualalloc/copy/copy_size)") do |v|
2015-03-20 21:34:58 +00:00
# Do not change this key. This should matching the one in Rex::Exploitation::Egghunter
2015-03-20 21:30:29 +00:00
options[:depmethod] = v
end
2015-03-20 21:15:43 +00:00
opt.on('-a', '--arch <String>', "(Optional) Architecture") do |v|
2015-03-20 21:37:42 +00:00
# Although this is an option, this is currently useless because we don't have x64 egghunters
2015-03-20 21:15:43 +00:00
options[:arch] = v
end
opt.on('--list-formats', "List all supported output formats") do
options[:list_formats] = true
end
2015-03-21 17:18:02 +00:00
opt.on('-v', '--var-name <name>', String, '(Optional) Specify a custom variable name to use for certain output formats') do |v|
options[:var_name] = v
end
2015-03-20 21:15:43 +00:00
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-21 01:53:04 +00:00
elsif options[:depsize] && options[:depsize] !~ /^\d+$/
2015-03-20 21:30:29 +00:00
raise OptionParser::InvalidOption, "--depsize must be a Fixnum"
2015-03-20 21:15:43 +00:00
end
2015-03-21 17:14:00 +00:00
options[:badchars] = '' unless options[:badchars]
options[:platform] = 'windows' unless options[:platform]
options[:arch] = ARCH_X86 unless options[:arch]
2015-03-21 17:20:29 +00:00
options[:var_name] = 'buf' unless options[:var_name]
2015-03-21 17:14:00 +00:00
2015-03-20 21:15:43 +00:00
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
2015-03-21 17:18:02 +00:00
output_stream.write ::Msf::Simple::Buffer.transform(raw_code, @opts[:format], @opts[:var_name])
2015-03-21 18:49:50 +00:00
$stderr.puts
2015-03-20 21:15:43 +00:00
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
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
2015-03-24 05:19:27 +00:00
end