| 1 |
#!/usr/bin/env ruby |
|---|
| 2 |
|
|---|
| 3 |
msfbase = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__ |
|---|
| 4 |
$:.unshift(File.join(File.dirname(msfbase), 'lib')) |
|---|
| 5 |
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB'] |
|---|
| 6 |
|
|---|
| 7 |
require 'rex' |
|---|
| 8 |
require 'msf/ui' |
|---|
| 9 |
require 'msf/base' |
|---|
| 10 |
|
|---|
| 11 |
# |
|---|
| 12 |
# Dump the list of payloads |
|---|
| 13 |
# |
|---|
| 14 |
def dump_payloads |
|---|
| 15 |
tbl = Rex::Ui::Text::Table.new( |
|---|
| 16 |
'Indent' => 4, |
|---|
| 17 |
'Header' => "Framework Payloads (#{$framework.stats.num_payloads} total)", |
|---|
| 18 |
'Columns' => |
|---|
| 19 |
[ |
|---|
| 20 |
"Name", |
|---|
| 21 |
"Description" |
|---|
| 22 |
]) |
|---|
| 23 |
|
|---|
| 24 |
$framework.payloads.each_module { |name, mod| |
|---|
| 25 |
tbl << [ name, mod.new.description ] |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
"\n" + tbl.to_s + "\n" |
|---|
| 29 |
end |
|---|
| 30 |
|
|---|
| 31 |
# Initialize the simplified framework instance. |
|---|
| 32 |
$framework = Msf::Simple::Framework.create |
|---|
| 33 |
|
|---|
| 34 |
if (ARGV.length <= 1) |
|---|
| 35 |
puts "\n" + " Usage: #{$0} <payload> [var=val] <S[ummary]|C|P[erl]|[Rub]y|R[aw]|J[avascript]|e[X]ecutable>\n" |
|---|
| 36 |
puts dump_payloads |
|---|
| 37 |
exit |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
# Get the payload name we'll be using |
|---|
| 41 |
payload_name = ARGV.shift |
|---|
| 42 |
|
|---|
| 43 |
# Process special var/val pairs... |
|---|
| 44 |
Msf::Ui::Common.process_cli_arguments($framework, ARGV) |
|---|
| 45 |
|
|---|
| 46 |
# Create the payload instance |
|---|
| 47 |
payload = $framework.payloads.create(payload_name) |
|---|
| 48 |
|
|---|
| 49 |
if (payload == nil) |
|---|
| 50 |
puts "Invalid payload: #{payload_name}" |
|---|
| 51 |
exit |
|---|
| 52 |
end |
|---|
| 53 |
|
|---|
| 54 |
# Evalulate the command |
|---|
| 55 |
cmd = ARGV.pop.downcase |
|---|
| 56 |
|
|---|
| 57 |
# Populate the framework datastore |
|---|
| 58 |
options = ARGV.join(',') |
|---|
| 59 |
|
|---|
| 60 |
if (cmd =~ /^(p|y|r|c|j|x|b)/) |
|---|
| 61 |
fmt = 'perl' if (cmd =~ /^p/) |
|---|
| 62 |
fmt = 'ruby' if (cmd =~ /^y/) |
|---|
| 63 |
fmt = 'raw' if (cmd =~ /^(r|x)/) |
|---|
| 64 |
fmt = 'c' if (cmd == 'c') |
|---|
| 65 |
fmt = 'js_be' if (cmd =~ /^j/ and Rex::Arch.endian(payload.arch) == ENDIAN_BIG) |
|---|
| 66 |
fmt = 'js_le' if (cmd =~ /^j/ and ! fmt) |
|---|
| 67 |
fmt = 'java' if (cmd =~ /^b/) |
|---|
| 68 |
enc = options['ENCODER'] |
|---|
| 69 |
|
|---|
| 70 |
begin |
|---|
| 71 |
buf = payload.generate_simple( |
|---|
| 72 |
'Format' => fmt, |
|---|
| 73 |
'OptionStr' => options, |
|---|
| 74 |
'Encoder' => enc) |
|---|
| 75 |
rescue |
|---|
| 76 |
puts "Error generating payload: #{$!}" |
|---|
| 77 |
exit |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
$stdout.binmode |
|---|
| 81 |
|
|---|
| 82 |
if (cmd =~ /^x/) |
|---|
| 83 |
note = |
|---|
| 84 |
"Created by msfpayload (http://www.metasploit.com).\n" + |
|---|
| 85 |
"Payload: " + payload.refname + "\n" + |
|---|
| 86 |
" Length: " + buf.length.to_s + "\n" + |
|---|
| 87 |
"Options: " + options + "\n" |
|---|
| 88 |
|
|---|
| 89 |
arch = payload.arch |
|---|
| 90 |
plat = payload.platform.platforms |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
if (arch.index(ARCH_X86)) |
|---|
| 95 |
|
|---|
| 96 |
# XXX: Automatically prepend stack adjustment |
|---|
| 97 |
# XXX: buf = Rex::Arch.adjust_stack_pointer('x86', -3500) + buf |
|---|
| 98 |
|
|---|
| 99 |
if (plat.index(Msf::Module::Platform::Windows)) |
|---|
| 100 |
buf = Rex::Text.to_win32pe(buf, note) |
|---|
| 101 |
$stderr.puts(note) |
|---|
| 102 |
$stdout.write(buf) |
|---|
| 103 |
exit(0) |
|---|
| 104 |
end |
|---|
| 105 |
|
|---|
| 106 |
if (plat.index(Msf::Module::Platform::Linux)) |
|---|
| 107 |
buf = Rex::Text.to_linux_x86_elf(buf, note) |
|---|
| 108 |
$stderr.puts(note) |
|---|
| 109 |
$stdout.write(buf) |
|---|
| 110 |
exit(0) |
|---|
| 111 |
end |
|---|
| 112 |
|
|---|
| 113 |
if(plat.index(Msf::Module::Platform::OSX)) |
|---|
| 114 |
buf = Rex::Text.to_osx_x86_macho(buf, note) |
|---|
| 115 |
$stderr.puts(note) |
|---|
| 116 |
$stdout.write(buf) |
|---|
| 117 |
exit(0) |
|---|
| 118 |
end |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
if(arch.index(ARCH_ARMLE)) |
|---|
| 122 |
if(plat.index(Msf::Module::Platform::OSX)) |
|---|
| 123 |
buf = Rex::Text.to_osx_arm_macho(buf, note) |
|---|
| 124 |
$stderr.puts(note) |
|---|
| 125 |
$stdout.write(buf) |
|---|
| 126 |
exit(0) |
|---|
| 127 |
end |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
if(arch.index(ARCH_PPC)) |
|---|
| 131 |
if(plat.index(Msf::Module::Platform::OSX)) |
|---|
| 132 |
buf = Rex::Text.to_osx_ppc_macho(buf, note) |
|---|
| 133 |
$stderr.puts(note) |
|---|
| 134 |
$stdout.write(buf) |
|---|
| 135 |
exit(0) |
|---|
| 136 |
end |
|---|
| 137 |
end |
|---|
| 138 |
|
|---|
| 139 |
$stderr.puts "No executable format support for this arch/platform" |
|---|
| 140 |
exit(-1) |
|---|
| 141 |
end |
|---|
| 142 |
|
|---|
| 143 |
$stdout.puts(buf) |
|---|
| 144 |
|
|---|
| 145 |
elsif (cmd =~ /^(s|o)/) |
|---|
| 146 |
puts Msf::Serializer::ReadableText.dump_module(payload) |
|---|
| 147 |
end |
|---|