2005-07-10 09:42:49 +00:00
|
|
|
require 'msf/base'
|
|
|
|
|
|
|
|
module Msf
|
|
|
|
module Simple
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Payload
|
|
|
|
# -------
|
|
|
|
#
|
|
|
|
# Simple payload wrapper class for performing generation.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Payload
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generate a payload with the mad skillz. The payload can be generated in
|
|
|
|
# a number of ways.
|
|
|
|
#
|
|
|
|
# opts can have:
|
|
|
|
#
|
2005-07-10 19:21:40 +00:00
|
|
|
# Encoder => A encoder module instance.
|
|
|
|
# Badchars => A string of bad characters.
|
|
|
|
# Format => The format to represent the data as: ruby, perl, c, raw
|
|
|
|
# Options => A hash of options to set.
|
|
|
|
# OptionStr => A string of options in VAR=VAL form separated by
|
|
|
|
# whitespace.
|
|
|
|
# NoComment => Disables prepention of a comment
|
2005-07-10 09:42:49 +00:00
|
|
|
#
|
2005-07-10 19:35:46 +00:00
|
|
|
# raises:
|
|
|
|
#
|
|
|
|
# BadcharError => If the supplied encoder fails to encode the payload
|
|
|
|
# NoKeyError => No valid encoder key could be found
|
|
|
|
# ArgumentParseError => Options were supplied improperly
|
|
|
|
#
|
2005-07-10 09:42:49 +00:00
|
|
|
def self.generate(payload, opts)
|
2005-07-10 19:21:40 +00:00
|
|
|
# If options were supplied, import them into the payload's
|
|
|
|
# datastore
|
|
|
|
if (opts['Option'])
|
|
|
|
payload.datastore.import_options_from_hash(opts['Options'])
|
|
|
|
elsif (opts['OptionStr'])
|
|
|
|
payload.datastore.import_options_from_s(opts['OptionStr'])
|
|
|
|
end
|
|
|
|
|
2005-07-10 09:42:49 +00:00
|
|
|
# Generate the payload
|
|
|
|
buf = payload.generate
|
|
|
|
|
|
|
|
# If an encoder was specified, encode the generated payload
|
|
|
|
if (opts['Encoder'])
|
|
|
|
buf = opts['Encoder'].encode(buf, opts['Badchars'])
|
|
|
|
end
|
|
|
|
|
2005-07-10 19:21:40 +00:00
|
|
|
fmt = opts['Format'] || 'raw'
|
|
|
|
|
2005-07-11 05:25:50 +00:00
|
|
|
# Save off the original payload length
|
|
|
|
len = buf.length
|
|
|
|
|
2005-07-10 09:42:49 +00:00
|
|
|
# Serialize the generated payload to some sort of format
|
2005-07-10 19:21:40 +00:00
|
|
|
buf = Buffer.transform(buf, fmt)
|
|
|
|
|
|
|
|
# Prepend a comment
|
|
|
|
if (fmt != 'raw' and opts['NoComment'] != true)
|
2005-07-10 21:01:05 +00:00
|
|
|
((ds = payload.datastore.to_s) and ds.length > 0) ? ds += "\n" : ds = ''
|
|
|
|
|
2005-07-10 19:21:40 +00:00
|
|
|
buf = Buffer.comment(
|
2005-07-11 05:25:50 +00:00
|
|
|
"#{payload.refname} - #{len} bytes - http://www.metasploit.com\n" +
|
2005-07-10 21:01:05 +00:00
|
|
|
"#{ds}" +
|
2005-07-10 19:35:46 +00:00
|
|
|
((opts['Encoder']) ? "Encoder=" + opts['Encoder'].refname + "\n" : ''), fmt) + buf
|
2005-07-10 19:21:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return buf
|
2005-07-10 09:42:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|