2015-04-25 11:25:29 +00:00
|
|
|
# -*- coding: binary -*-
|
|
|
|
require 'msf/core/payload/uuid'
|
|
|
|
require 'msf/core/reflective_dll_loader'
|
2015-04-26 02:11:14 +00:00
|
|
|
require 'rex/parser/x509_certificate'
|
2015-04-25 11:25:29 +00:00
|
|
|
|
|
|
|
class Rex::Payloads::Meterpreter::Config
|
|
|
|
|
|
|
|
include Msf::ReflectiveDLLLoader
|
|
|
|
|
|
|
|
UUID_SIZE = 64
|
|
|
|
URL_SIZE = 512
|
2015-04-26 02:11:14 +00:00
|
|
|
UA_SIZE = 256
|
|
|
|
PROXY_HOST_SIZE = 128
|
|
|
|
PROXY_USER_SIZE = 64
|
|
|
|
PROXY_PASS_SIZE = 64
|
|
|
|
CERT_HASH_SIZE = 20
|
2015-04-25 11:25:29 +00:00
|
|
|
|
|
|
|
def initialize(opts={})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_b
|
2015-04-26 02:11:14 +00:00
|
|
|
config_block
|
2015-04-25 11:25:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-04-26 02:11:14 +00:00
|
|
|
def is_x86?
|
|
|
|
@opts[:arch] == ARCH_X86
|
|
|
|
end
|
|
|
|
|
2015-04-25 11:25:29 +00:00
|
|
|
def to_wchar_t(item, size)
|
|
|
|
item.to_s.ljust(size, "\x00").unpack("C*").pack("v*")
|
|
|
|
end
|
|
|
|
|
2015-04-26 02:11:14 +00:00
|
|
|
def session_block(opts)
|
2015-04-25 11:25:29 +00:00
|
|
|
uuid = to_wchar_t(opts[:uuid], UUID_SIZE)
|
|
|
|
|
|
|
|
session_data = [
|
2015-04-26 02:11:14 +00:00
|
|
|
0, # comms socket, patched in by the stager
|
|
|
|
0, # listen socket, patched in by the stager
|
|
|
|
opts[:expiration], # Session expiry
|
|
|
|
uuid, # the URL to use
|
|
|
|
]
|
|
|
|
|
|
|
|
if is_x86?
|
|
|
|
session_data.pack("VVVA*")
|
|
|
|
else
|
|
|
|
session_data.pack("QQVA*")
|
|
|
|
end
|
2015-04-25 11:25:29 +00:00
|
|
|
end
|
|
|
|
|
2015-04-26 02:11:14 +00:00
|
|
|
def transport_block(opts)
|
2015-04-25 11:25:29 +00:00
|
|
|
# Build the URL from the given parameters, and pad it out to the
|
|
|
|
# correct size
|
|
|
|
url = "#{opts[:scheme]}://#{opts[:lhost]}:#{opts[:lport]}"
|
|
|
|
url << "#{opts[:uri]}/" if opts[:uri]
|
|
|
|
url = to_wchar_t(url, URL_SIZE)
|
|
|
|
|
2015-04-26 02:11:14 +00:00
|
|
|
# if the transport URI is for a HTTP payload we need to add a stack
|
|
|
|
# of other stuff
|
|
|
|
pack = 'VVVA*'
|
2015-04-25 11:25:29 +00:00
|
|
|
transport_data = [
|
2015-04-26 02:11:14 +00:00
|
|
|
opts[:comm_timeout], # communications timeout
|
|
|
|
opts[:retry_total], # retry total time
|
|
|
|
opts[:retry_wait], # retry wait time
|
|
|
|
url # transport URL
|
|
|
|
]
|
|
|
|
|
|
|
|
if url.start_with?('http')
|
|
|
|
proxy_host = to_wchar_t(opts[:proxy_host] || '', PROXY_HOST_SIZE)
|
|
|
|
proxy_user = to_wchar_t(opts[:proxy_user] || '', PROXY_USER_SIZE)
|
|
|
|
proxy_pass = to_wchar_t(opts[:proxy_pass] || '', PROXY_PASS_SIZE)
|
|
|
|
ua = to_wchar_t(opts[:ua] || '', UA_SIZE)
|
|
|
|
|
|
|
|
cert_hash = "\x00" * CERT_HASH_SIZE
|
|
|
|
if opts[:cert_file]
|
|
|
|
cert_hash = Rex::Parser::X509Certificate.get_cert_file_hash(opts[:cert_file])
|
|
|
|
end
|
|
|
|
|
|
|
|
# add the HTTP specific stuff
|
|
|
|
transport_data << proxy_host # Proxy host name
|
|
|
|
transport_data << proxy_user # Proxy user name
|
|
|
|
transport_data << proxy_pass # Proxy password
|
|
|
|
transport_data << ua # HTTP user agent
|
|
|
|
transport_data << cert_hash # SSL cert hash for verification
|
|
|
|
|
|
|
|
# update the packing spec
|
|
|
|
pack << 'A*A*A*A*A*'
|
|
|
|
end
|
|
|
|
|
|
|
|
# return the packed transport information
|
|
|
|
transport_data.pack(pack)
|
2015-04-25 11:25:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def extension_block(ext_name, file_extension)
|
|
|
|
ext_name = ext_name.strip.downcase
|
|
|
|
ext, o = load_rdi_dll(MeterpreterBinaries.path("ext_server_#{ext_name}",
|
|
|
|
file_extension))
|
|
|
|
|
|
|
|
extension_data = [ ext.length, ext ].pack("VA*")
|
|
|
|
end
|
|
|
|
|
2015-04-26 02:11:14 +00:00
|
|
|
def config_block
|
2015-04-25 11:25:29 +00:00
|
|
|
# start with the session information
|
2015-04-26 02:11:14 +00:00
|
|
|
config = session_block(@opts)
|
2015-04-25 11:25:29 +00:00
|
|
|
|
|
|
|
# then load up the transport configurations
|
2015-04-26 02:11:14 +00:00
|
|
|
(@opts[:transports] || []).each do |t|
|
2015-04-25 11:25:29 +00:00
|
|
|
config << transport_block(t)
|
|
|
|
end
|
|
|
|
|
|
|
|
# terminate the transports with a single NULL byte
|
|
|
|
config << "\x00"
|
|
|
|
|
|
|
|
# configure the extensions
|
2015-04-26 02:11:14 +00:00
|
|
|
file_extension = 'x86.dll'
|
|
|
|
unless is_x86?
|
|
|
|
file_extension = 'x64.dll'
|
|
|
|
end
|
|
|
|
|
|
|
|
(@opts[:extensions] || []).each do |e|
|
|
|
|
config << extension_block(e, file_extension)
|
2015-04-25 11:25:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# terminate the extensions with a 0 size
|
2015-04-26 02:11:14 +00:00
|
|
|
if is_x86?
|
|
|
|
config << [0].pack("V")
|
|
|
|
else
|
|
|
|
config << [0].pack("Q")
|
|
|
|
end
|
2015-04-25 11:25:29 +00:00
|
|
|
|
|
|
|
# and we're done
|
|
|
|
config
|
|
|
|
end
|
|
|
|
end
|