2014-08-25 04:55:07 +00:00
|
|
|
# -*- coding: binary -*-
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Payloads
|
|
|
|
module Meterpreter
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Provides methods to patch options into metsrv stagers
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Patch
|
|
|
|
|
2014-08-25 17:03:23 +00:00
|
|
|
# Replace the transport string
|
2014-12-16 02:40:41 +00:00
|
|
|
def self.patch_transport! blob, ssl
|
2014-08-25 17:03:23 +00:00
|
|
|
|
2014-08-25 04:55:07 +00:00
|
|
|
i = blob.index("METERPRETER_TRANSPORT_SSL")
|
|
|
|
if i
|
|
|
|
str = ssl ? "METERPRETER_TRANSPORT_HTTPS\x00" : "METERPRETER_TRANSPORT_HTTP\x00"
|
|
|
|
blob[i, str.length] = str
|
|
|
|
end
|
2014-08-25 17:03:23 +00:00
|
|
|
|
2014-09-15 05:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Replace the URL
|
2014-12-16 02:40:41 +00:00
|
|
|
def self.patch_url! blob, url
|
2014-09-15 05:01:23 +00:00
|
|
|
|
2014-08-25 04:55:07 +00:00
|
|
|
i = blob.index("https://" + ("X" * 256))
|
|
|
|
if i
|
|
|
|
str = url
|
|
|
|
blob[i, str.length] = str
|
|
|
|
end
|
|
|
|
|
2014-09-15 05:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Replace the session expiration timeout
|
2014-12-16 02:40:41 +00:00
|
|
|
def self.patch_expiration! blob, expiration
|
2014-09-15 05:01:23 +00:00
|
|
|
|
2014-08-25 04:55:07 +00:00
|
|
|
i = blob.index([0xb64be661].pack("V"))
|
|
|
|
if i
|
|
|
|
str = [ expiration ].pack("V")
|
|
|
|
blob[i, str.length] = str
|
|
|
|
end
|
|
|
|
|
2014-09-15 05:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Replace the session communication timeout
|
2014-12-16 02:40:41 +00:00
|
|
|
def self.patch_comm_timeout! blob, comm_timeout
|
2014-09-15 05:01:23 +00:00
|
|
|
|
2014-08-25 04:55:07 +00:00
|
|
|
i = blob.index([0xaf79257f].pack("V"))
|
|
|
|
if i
|
|
|
|
str = [ comm_timeout ].pack("V")
|
|
|
|
blob[i, str.length] = str
|
|
|
|
end
|
|
|
|
|
2014-08-25 17:03:23 +00:00
|
|
|
end
|
2014-08-25 04:55:07 +00:00
|
|
|
|
|
|
|
# Replace the user agent string with our option
|
2014-12-16 02:40:41 +00:00
|
|
|
def self.patch_ua! blob, ua
|
2014-08-25 04:55:07 +00:00
|
|
|
|
2014-09-17 18:11:37 +00:00
|
|
|
ua = ua[0,255] + "\x00"
|
2014-08-25 04:55:07 +00:00
|
|
|
i = blob.index("METERPRETER_UA\x00")
|
|
|
|
if i
|
|
|
|
blob[i, ua.length] = ua
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# Activate a custom proxy
|
2014-12-16 02:40:41 +00:00
|
|
|
def self.patch_proxy! blob, proxyhost, proxyport, proxy_type
|
2014-08-25 04:55:07 +00:00
|
|
|
|
|
|
|
i = blob.index("METERPRETER_PROXY\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
|
|
|
|
if i
|
|
|
|
if proxyhost
|
|
|
|
if proxyhost.to_s != ""
|
|
|
|
proxyhost = proxyhost.to_s
|
|
|
|
proxyport = proxyport.to_s || "8080"
|
|
|
|
proxyinfo = proxyhost + ":" + proxyport
|
|
|
|
if proxyport == "80"
|
|
|
|
proxyinfo = proxyhost
|
|
|
|
end
|
|
|
|
if proxy_type.to_s == 'HTTP'
|
|
|
|
proxyinfo = 'http://' + proxyinfo
|
|
|
|
else #socks
|
|
|
|
proxyinfo = 'socks=' + proxyinfo
|
|
|
|
end
|
|
|
|
proxyinfo << "\x00"
|
|
|
|
blob[i, proxyinfo.length] = proxyinfo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# Proxy authentification
|
2014-12-16 02:40:41 +00:00
|
|
|
def self.patch_proxy_auth! blob, proxy_username, proxy_password, proxy_type
|
2014-08-25 04:55:07 +00:00
|
|
|
|
|
|
|
unless (proxy_username.nil? or proxy_username.empty?) or
|
|
|
|
(proxy_password.nil? or proxy_password.empty?) or
|
|
|
|
proxy_type == 'SOCKS'
|
|
|
|
|
|
|
|
proxy_username_loc = blob.index("METERPRETER_USERNAME_PROXY\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
|
|
|
|
proxy_username = proxy_username << "\x00"
|
|
|
|
blob[proxy_username_loc, proxy_username.length] = proxy_username
|
|
|
|
|
|
|
|
proxy_password_loc = blob.index("METERPRETER_PASSWORD_PROXY\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
|
|
|
|
proxy_password = proxy_password << "\x00"
|
|
|
|
blob[proxy_password_loc, proxy_password.length] = proxy_password
|
|
|
|
end
|
|
|
|
|
2014-09-17 18:11:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Patch options into metsrv for reverse HTTP payloads
|
2014-12-16 02:40:41 +00:00
|
|
|
def self.patch_passive_service! blob, options
|
|
|
|
|
|
|
|
patch_transport! blob, options[:ssl]
|
|
|
|
patch_url! blob, options[:url]
|
|
|
|
patch_expiration! blob, options[:expiration]
|
|
|
|
patch_comm_timeout! blob, options[:comm_timeout]
|
|
|
|
patch_ua! blob, options[:ua]
|
|
|
|
patch_proxy!(blob,
|
2014-09-17 18:11:37 +00:00
|
|
|
options[:proxyhost],
|
|
|
|
options[:proxyport],
|
|
|
|
options[:proxy_type]
|
|
|
|
)
|
2014-12-16 02:40:41 +00:00
|
|
|
patch_proxy_auth!(blob,
|
2014-09-17 18:11:37 +00:00
|
|
|
options[:proxy_username],
|
|
|
|
options[:proxy_password],
|
|
|
|
options[:proxy_type]
|
|
|
|
)
|
|
|
|
|
2014-08-25 04:55:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|