Skip to content

Commit 8916982

Browse files
author
Tod Beardsley
committed
Land rapid7#4274, custom ssl certs on payload handlers
2 parents c683e7b + 8146019 commit 8916982

File tree

7 files changed

+93
-24
lines changed

7 files changed

+93
-24
lines changed

lib/msf/core/exploit/tcp_server.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def initialize(info = {})
1818
register_options(
1919
[
2020
OptBool.new('SSL', [ false, 'Negotiate SSL for incoming connections', false]),
21-
OptEnum.new('SSLVersion', [ false, 'Specify the version of SSL that should be used', 'TLS1', ['SSL2', 'SSL3', 'TLS1']]),
21+
# SSLVersion is currently unsupported for TCP servers (only supported by clients at the moment)
22+
# OptEnum.new('SSLVersion', [ false, 'Specify the version of SSL that should be used', 'TLS1', ['SSL2', 'SSL3', 'TLS1']]),
2223
OptPath.new('SSLCert', [ false, 'Path to a custom SSL certificate (default is randomly generated)']),
2324
OptAddress.new('SRVHOST', [ true, "The local host to listen on. This must be an address on the local machine or 0.0.0.0", '0.0.0.0' ]),
2425
OptPort.new('SRVPORT', [ true, "The local port to listen on.", 8080 ]),

lib/msf/core/handler/reverse_http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def setup_handler
138138
'MsfExploit' => self,
139139
},
140140
comm,
141-
(ssl?) ? datastore["SSLCert"] : nil
141+
(ssl?) ? datastore["HandlerSSLCert"] : nil
142142
)
143143

144144
self.service.server_name = datastore['MeterpreterServerName']

lib/msf/core/handler/reverse_https.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ def initialize(info = {})
3838

3939
register_options(
4040
[
41-
OptPort.new('LPORT', [ true, "The local listener port", 8443 ])
41+
OptPort.new('LPORT', [ true, "The local listener port", 8443 ]),
42+
], Msf::Handler::ReverseHttps)
43+
44+
register_advanced_options(
45+
[
46+
OptPath.new('HandlerSSLCert', [false, "Path to a SSL certificate in unified PEM format"])
4247
], Msf::Handler::ReverseHttps)
4348

4449
end

lib/msf/core/handler/reverse_tcp.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def wrap_aes_socket(sock)
197197
m.reset
198198
key = m.digest(datastore["AESPassword"] || "")
199199

200-
Rex::ThreadFactory.spawn('AESEncryption', false) {
200+
Rex::ThreadFactory.spawn('Session-AESEncrypt', false) {
201201
c1 = OpenSSL::Cipher.new('aes-128-cfb8')
202202
c1.encrypt
203203
c1.key=key
@@ -210,7 +210,7 @@ def wrap_aes_socket(sock)
210210
end
211211
sock.close()
212212
}
213-
Rex::ThreadFactory.spawn('AESEncryption', false) {
213+
Rex::ThreadFactory.spawn('Session-AESDecrypt', false) {
214214
c2 = OpenSSL::Cipher.new('aes-128-cfb8')
215215
c2.decrypt
216216
c2.key=key

lib/msf/core/handler/reverse_tcp_double_ssl.rb

Lines changed: 77 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,31 @@ def stop_handler
204244

205245
protected
206246

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

lib/msf/core/handler/reverse_tcp_ssl.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ def initialize(info = {})
4444
super
4545
register_advanced_options(
4646
[
47-
OptPath.new('SSLCert', [ false, 'Path to a custom SSL certificate (default is randomly generated)']),
48-
OptAddress.new('ReverseListenerBindAddress', [ false, 'The specific IP address to bind to on the local system']),
49-
OptInt.new('ReverseListenerBindPort', [ false, 'The port to bind to on the local system if different from LPORT' ])
47+
OptPath.new('HandlerSSLCert', [false, "Path to a SSL certificate in unified PEM format"])
5048
], Msf::Handler::ReverseTcpSsl)
5149

5250
end
@@ -57,8 +55,8 @@ def initialize(info = {})
5755
# if it fails to start the listener.
5856
#
5957
def setup_handler
60-
if datastore['Proxies']
61-
raise RuntimeError, 'TCP connect-back payloads cannot be used with Proxies'
58+
if datastore['Proxies'] and not datastore['ReverseAllowProxy']
59+
raise RuntimeError, 'TCP connect-back payloads cannot be used with Proxies. Can be overriden by setting ReverseAllowProxy to true'
6260
end
6361

6462
ex = false
@@ -81,7 +79,7 @@ def setup_handler
8179
'LocalHost' => ip,
8280
'LocalPort' => local_port,
8381
'Comm' => comm,
84-
'SSLCert' => datastore['SSLCert'],
82+
'SSLCert' => datastore['HandlerSSLCert'],
8583
'Context' =>
8684
{
8785
'Msf' => framework,

modules/exploits/unix/misc/distcc_exec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(info = {})
3939
'Compat' =>
4040
{
4141
'PayloadType' => 'cmd',
42-
'RequiredCmd' => 'generic perl ruby bash telnet',
42+
'RequiredCmd' => 'generic perl ruby bash telnet openssl',
4343
}
4444
},
4545
'Targets' =>

0 commit comments

Comments
 (0)