2005-12-17 07:46:23 +01:00
#!/usr/bin/env ruby
2010-05-03 19:13:09 +02:00
#
# $Id$
# $Revision$
#
2005-10-01 23:25:42 +02:00
2009-01-30 07:27:10 +01:00
msfbase = __FILE__
while File.symlink?(msfbase)
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end
2006-07-31 17:36:08 +02:00
$:.unshift(File.join(File.dirname(msfbase), 'lib'))
2008-02-02 22:29:46 +01:00
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
2005-10-01 23:25:42 +02:00
require 'rex'
require 'msf/ui'
require 'msf/base'
OutStatus = "[*] "
OutError = "[-] "
2010-09-21 02:13:30 +02:00
# Load supported formats
supported_formats = Msf::Simple::Buffer.transform_formats + Msf::Util::EXE.to_executable_fmt_formats
2005-10-01 23:25:42 +02:00
$args = Rex::Parser::Arguments.new(
2010-09-21 02:13:30 +02:00
"-h" => [ false, "Help banner" ],
"-l" => [ false, "List available encoders" ],
2010-09-21 04:38:09 +02:00
"-v" => [ false, "Increase verbosity" ],
2010-09-21 02:13:30 +02:00
# input/output
2005-10-01 23:25:42 +02:00
"-i" => [ true, "Encode the contents of the supplied file path" ],
2007-02-18 13:27:17 +01:00
"-m" => [ true, "Specifies an additional module search path" ],
2010-09-21 02:13:30 +02:00
"-o" => [ true, "The output file" ],
# architecture/platform
2005-10-01 23:25:42 +02:00
"-a" => [ true, "The architecture to encode as" ],
2009-12-13 20:03:35 +01:00
"-p" => [ true, "The platform to encode for" ],
2010-09-21 02:13:30 +02:00
# format options
"-t" => [ true, "The output format: #{supported_formats.join(',')}" ],
# encoder options
"-e" => [ true, "The encoder to use" ],
"-n" => [ false, "Dump encoder information" ],
2005-10-01 23:25:42 +02:00
"-b" => [ true, "The list of characters to avoid: '\\x00\\xff'" ],
"-s" => [ true, "The maximum size of the encoded data" ],
2009-11-01 05:11:43 +01:00
"-c" => [ true, "The number of times to encode the data" ],
2010-09-21 02:13:30 +02:00
# EXE generation options
"-d" => [ true, "Specify the directory in which to look for EXE templates" ],
"-x" => [ true, "Specify an alternate executable template" ],
"-k" => [ false, "Keep template working; run payload in new thread (use with -x)" ]
)
2005-10-01 23:25:42 +02:00
#
# Dump the list of encoders
#
def dump_encoders(arch = nil)
tbl = Rex::Ui::Text::Table.new(
'Indent' => 4,
'Header' => "Framework Encoders" + ((arch) ? " (architectures: #{arch})" : ""),
2009-11-01 05:11:43 +01:00
'Columns' =>
2005-10-01 23:25:42 +02:00
[
"Name",
"Rank",
"Description"
])
cnt = 0
$framework.encoders.each_module(
'Arch' => arch ? arch.split(',') : nil) { |name, mod|
2006-01-08 02:12:00 +01:00
tbl << [ name, mod.rank_to_s, mod.new.name ]
2005-10-01 23:25:42 +02:00
cnt += 1
}
(cnt > 0) ? "\n" + tbl.to_s + "\n" : "\nNo compatible encoders found.\n\n"
end
#
# Returns the list of encoders to try
#
def get_encoders(arch, encoder)
encoders = []
if (encoder)
encoders << $framework.encoders.create(encoder)
else
$framework.encoders.each_module_ranked(
'Arch' => arch ? arch.split(',') : nil) { |name, mod|
encoders << mod.new
}
end
encoders
end
#
# Nuff said.
#
def usage
2005-10-01 23:32:11 +02:00
$stderr.puts("\n" + " Usage: #{$0} <options>\n" + $args.usage)
2005-10-01 23:25:42 +02:00
exit
end
2010-08-19 21:07:41 +02:00
def write_encoded(buf)
if (not $output)
$stdout.write(buf)
else
File.open($output, "wb") do |fd|
fd.write(buf)
end
end
end
2005-10-01 23:25:42 +02:00
# Defaults
2010-09-21 04:38:09 +02:00
verbose = 0
2005-10-01 23:25:42 +02:00
cmd = "encode"
arch = nil
badchars = ''
space = nil
encoder = nil
2009-10-19 04:42:39 +02:00
fmt = nil
2005-10-01 23:25:42 +02:00
input = $stdin
2005-10-01 23:32:11 +02:00
options = ''
2006-03-16 17:33:32 +01:00
delim = '_|_'
2008-09-27 00:34:51 +02:00
output = nil
2009-03-11 05:10:42 +01:00
ecount = 1
2010-09-21 02:13:30 +02:00
plat = nil
2009-11-01 05:11:43 +01:00
altexe = nil
2010-03-24 16:55:24 +01:00
inject = false
2010-09-21 02:13:30 +02:00
exedir = nil # use default
2005-10-01 23:25:42 +02:00
# Parse the argument and rock that shit.
$args.parse(ARGV) { |opt, idx, val|
case opt
when "-i"
begin
2010-12-14 10:07:19 +01:00
input = File.open(val, 'rb')
2005-10-01 23:25:42 +02:00
rescue
$stderr.puts(OutError + "Failed to open file #{val}: #{$!}")
exit
end
2007-02-18 13:27:17 +01:00
when "-m"
$framework.modules.add_module_path(val)
2005-10-01 23:25:42 +02:00
when "-l"
cmd = "list"
when "-n"
cmd = "dump"
when "-a"
arch = val
2009-03-11 05:10:42 +01:00
when "-c"
ecount = val.to_i
2005-10-01 23:25:42 +02:00
when "-b"
badchars = Rex::Text.hex_to_raw(val)
2009-12-13 20:03:35 +01:00
when "-p"
plat = Msf::Module::PlatformList.transform(val)
2005-10-01 23:25:42 +02:00
when "-s"
space = val.to_i
when "-t"
2010-09-21 02:13:30 +02:00
if supported_formats.include?(val)
2005-10-01 23:25:42 +02:00
fmt = val
else
$stderr.puts(OutError + "Invalid format: #{val}")
exit
end
2008-09-27 00:34:51 +02:00
when "-o"
2010-08-19 21:07:41 +02:00
$output = val
2005-10-01 23:25:42 +02:00
when "-e"
encoder = val
2010-09-21 02:13:30 +02:00
when "-d"
exedir = val
2009-11-01 05:11:43 +01:00
when "-x"
altexe = val
2010-03-24 16:55:24 +01:00
when "-k"
inject = true
2010-09-21 02:13:30 +02:00
2005-10-01 23:25:42 +02:00
when "-h"
usage
2010-09-21 04:38:09 +02:00
when "-v"
verbose += 1
2005-10-01 23:32:11 +02:00
else
if (val =~ /=/)
2006-03-16 17:33:32 +01:00
options += ((options.length > 0) ? delim : "") + "#{val}"
2005-10-01 23:32:11 +02:00
end
2009-11-01 05:11:43 +01:00
end
2005-10-01 23:25:42 +02:00
}
2009-10-19 04:42:39 +02:00
2009-10-20 23:58:08 +02:00
if(not fmt and output)
2009-10-19 04:42:39 +02:00
pre,ext = output.split('.')
if(ext and not ext.empty?)
fmt = ext
end
end
2010-03-24 16:55:24 +01:00
if inject and not altexe
$stderr.puts "[*] Error: the injection option must use a custom EXE template via -x, otherwise the injected payload will immediately exit when the main process dies."
exit(1)
end
2010-09-21 02:13:30 +02:00
exeopts = {
:inject => inject,
:template => altexe,
:template_path => exedir
}
2010-03-24 16:55:24 +01:00
2009-01-02 22:25:25 +01:00
# Initialize the simplified framework instance.
$framework = Msf::Simple::Framework.create(
2010-09-18 08:47:59 +02:00
:module_types => [ Msf::MODULE_ENCODER, Msf::MODULE_NOP ],
'DisableDatabase' => true
2009-01-02 22:25:25 +01:00
)
2005-10-01 23:25:42 +02:00
# Get the list of encoders to try
encoders = get_encoders(arch, encoder)
# Process the actual command
case cmd
when "list"
$stderr.puts(dump_encoders(arch))
when "dump"
2006-09-26 18:03:53 +02:00
enc = encoder ? $framework.encoders.create(encoder) : nil
2005-10-01 23:25:42 +02:00
if (enc)
$stderr.puts(Msf::Serializer::ReadableText.dump_module(enc))
else
$stderr.puts(OutError + "Invalid encoder specified.")
end
when "encode"
2010-12-14 10:07:19 +01:00
input.binmode # ensure its in binary mode
2006-12-17 08:03:00 +01:00
buf = input.read
2009-11-01 05:11:43 +01:00
2005-10-01 23:25:42 +02:00
encoders.each { |enc|
2006-01-29 22:47:51 +01:00
next if not enc
2005-10-01 23:25:42 +02:00
begin
2005-10-01 23:32:11 +02:00
# Imports options
2006-03-16 17:33:32 +01:00
enc.datastore.import_options_from_s(options, delim)
2005-10-01 23:32:11 +02:00
2009-03-11 05:10:42 +01:00
skip = false
eout = buf.dup
raw = nil
2009-11-01 05:11:43 +01:00
2009-03-11 05:10:42 +01:00
1.upto(ecount) do |iteration|
2005-10-01 23:25:42 +02:00
2009-03-11 05:10:42 +01:00
# Encode it up
2009-12-13 20:03:35 +01:00
raw = enc.encode(eout, badchars, nil, plat)
2009-03-11 05:10:42 +01:00
# Is it too big?
if (space and space > 0 and raw.length > space)
$stderr.puts(OutError + "#{enc.refname} created buffer that is too big (#{raw.length})")
skip = true
break
end
2005-10-01 23:25:42 +02:00
2009-03-11 05:10:42 +01:00
# Print it out
$stderr.puts(OutStatus + "#{enc.refname} succeeded with size #{raw.length} (iteration=#{iteration})\n\n")
eout = raw
end
2009-11-01 05:11:43 +01:00
2009-03-11 05:10:42 +01:00
next if skip
2009-11-01 05:11:43 +01:00
2010-09-21 02:13:30 +02:00
output = Msf::Util::EXE.to_executable_fmt($framework, arch, plat, raw, fmt, exeopts)
2009-12-28 23:41:43 +01:00
2010-09-21 02:13:30 +02:00
if not output
2009-11-04 22:30:00 +01:00
fmt ||= "ruby"
2010-09-21 02:13:30 +02:00
output = Msf::Simple::Buffer.transform(raw, fmt)
2008-09-27 00:34:51 +02:00
end
2009-11-01 05:11:43 +01:00
2010-09-21 02:13:30 +02:00
if exeopts[:fellback]
$stderr.puts(OutError + "Warning: Falling back to default template: #{exeopts[:fellback]}")
end
write_encoded(output)
2005-10-01 23:25:42 +02:00
exit
2009-06-14 23:30:56 +02:00
rescue => e
2010-09-21 04:38:09 +02:00
$stderr.puts(OutError + "#{enc.refname} failed: #{e}")
if verbose > 0
e.backtrace.each { |el|
$stderr.puts(OutError + el.to_s)
}
end
2005-10-01 23:25:42 +02:00
end
}
$stderr.puts(OutError + "No encoders succeeded.")
2008-11-12 20:15:24 +01:00
end