Skip to content

Commit 9ed8c59

Browse files
author
HD Moore
committed
Bring options over from reverse_tcp (bind address, etc).
Also includes the SSLCert => HandlerSSLCert change
1 parent ba9c763 commit 9ed8c59

File tree

1 file changed

+79
-12
lines changed

1 file changed

+79
-12
lines changed

lib/msf/core/handler/reverse_tcp_double_ssl.rb

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ def initialize(info = {})
4747

4848
register_advanced_options(
4949
[
50-
OptBool.new('ReverseAllowProxy', [ true, 'Allow reverse tcp even with Proxies specified. Connect back will NOT go through proxy but directly to LHOST', false]),
50+
OptAddress.new('ReverseListenerBindAddress', [ false, 'The specific IP address to bind to on the local system']),
51+
OptInt.new('ReverseListenerBindPort', [ false, 'The port to bind to on the local system if different from LPORT' ]),
52+
OptBool.new('ReverseAllowProxy', [ true, 'Allow reverse TCP even with Proxies specified. Connect back will NOT go through proxy but directly to LHOST', false]),
53+
OptPath.new('HandlerSSLCert', [false, "Path to a SSL certificate in unified PEM format"])
5154
], Msf::Handler::ReverseTcpDoubleSSL)
5255

5356
self.conn_threads = []
@@ -62,17 +65,54 @@ def setup_handler
6265
if datastore['Proxies'] and not datastore['ReverseAllowProxy']
6366
raise RuntimeError, 'TCP connect-back payloads cannot be used with Proxies. Can be overriden by setting ReverseAllowProxy to true'
6467
end
65-
self.listener_sock = Rex::Socket::TcpServer.create(
66-
# 'LocalHost' => datastore['LHOST'],
67-
'LocalPort' => datastore['LPORT'].to_i,
68-
'Comm' => comm,
69-
'SSL' => true,
70-
'Context' =>
71-
{
72-
'Msf' => framework,
73-
'MsfPayload' => self,
74-
'MsfExploit' => assoc_exploit
75-
})
68+
69+
ex = false
70+
71+
comm = datastore['ReverseListenerComm']
72+
if comm.to_s == "local"
73+
comm = ::Rex::Socket::Comm::Local
74+
else
75+
comm = nil
76+
end
77+
78+
local_port = bind_port
79+
addrs = bind_address
80+
81+
addrs.each { |ip|
82+
begin
83+
84+
comm.extend(Rex::Socket::SslTcp)
85+
self.listener_sock = Rex::Socket::SslTcpServer.create(
86+
'LocalHost' => ip,
87+
'LocalPort' => local_port,
88+
'Comm' => comm,
89+
'SSLCert' => datastore['HandlerSSLCert'],
90+
'Context' =>
91+
{
92+
'Msf' => framework,
93+
'MsfPayload' => self,
94+
'MsfExploit' => assoc_exploit
95+
})
96+
97+
ex = false
98+
99+
comm_used = comm || Rex::Socket::SwitchBoard.best_comm( ip )
100+
comm_used = Rex::Socket::Comm::Local if comm_used == nil
101+
102+
if( comm_used.respond_to?( :type ) and comm_used.respond_to?( :sid ) )
103+
via = "via the #{comm_used.type} on session #{comm_used.sid}"
104+
else
105+
via = ""
106+
end
107+
108+
print_status("Started reverse double SSL handler on #{ip}:#{local_port} #{via}")
109+
break
110+
rescue
111+
ex = $!
112+
print_error("Handler failed to bind to #{ip}:#{local_port}")
113+
end
114+
}
115+
raise ex if (ex)
76116
end
77117

78118
#
@@ -204,6 +244,33 @@ def stop_handler
204244

205245
protected
206246

247+
protected
248+
249+
def bind_port
250+
port = datastore['ReverseListenerBindPort'].to_i
251+
port > 0 ? port : datastore['LPORT'].to_i
252+
end
253+
254+
def bind_address
255+
# Switch to IPv6 ANY address if the LHOST is also IPv6
256+
addr = Rex::Socket.resolv_nbo(datastore['LHOST'])
257+
# First attempt to bind LHOST. If that fails, the user probably has
258+
# something else listening on that interface. Try again with ANY_ADDR.
259+
any = (addr.length == 4) ? "0.0.0.0" : "::0"
260+
261+
addrs = [ Rex::Socket.addr_ntoa(addr), any ]
262+
263+
if not datastore['ReverseListenerBindAddress'].to_s.empty?
264+
# Only try to bind to this specific interface
265+
addrs = [ datastore['ReverseListenerBindAddress'] ]
266+
267+
# Pick the right "any" address if either wildcard is used
268+
addrs[0] = any if (addrs[0] == "0.0.0.0" or addrs == "::0")
269+
end
270+
271+
addrs
272+
end
273+
207274
attr_accessor :listener_sock # :nodoc:
208275
attr_accessor :listener_thread # :nodoc:
209276
attr_accessor :conn_threads # :nodoc:

0 commit comments

Comments
 (0)