Skip to content

Commit 335d1ef

Browse files
author
HD Moore
committed
Only cache auto-generated certificates
1 parent 8becf41 commit 335d1ef

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

lib/rex/post/meterpreter/client.rb

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ class Client
4242
@@ext_hash = {}
4343

4444
#
45-
# Cached SSL context (required to scale)
46-
#
47-
@@ssl_cert_info = nil
48-
49-
#
50-
# Cached SSL certificate
45+
# Cached auto-generated SSL certificate
5146
#
5247
@@ssl_cached_cert = nil
5348

@@ -111,7 +106,6 @@ def init_meterpreter(sock,opts={})
111106
self.capabilities = opts[:capabilities] || {}
112107
self.commands = []
113108

114-
115109
self.conn_id = opts[:conn_id]
116110
self.url = opts[:url]
117111
self.ssl = opts[:ssl]
@@ -218,45 +212,40 @@ def swap_sock_ssl_to_plain
218212

219213
def generate_ssl_context
220214

221-
# Initialize a null context
222215
ctx = nil
216+
ssl_cert_info = nil
223217

224-
# Synchronize to prevent race conditions
225-
@@ssl_mutex.synchronize do
218+
loop do
226219

227-
# If the user specified a certificate and its not the cached one, delete the cached info
228-
if self.ssl_cert && self.ssl_cert != @@ssl_cached_cert
229-
@ssl_ctx = nil
220+
# Load a custom SSL certificate if one has been specified
221+
if self.ssl_cert
222+
wlog("Loading custom SSL certificate for Meterpreter session")
223+
ssl_cert_info = Rex::Socket::SslTcpServer.ssl_parse_pem(self.ssl_cert)
224+
wlog("Loaded custom SSL certificate for Meterpreter session")
225+
break
230226
end
231227

232-
# If the user did not specify a certificate and we have cached one, delete the cached info
233-
if ! self.ssl_cert && @@ssl_cached_cert
234-
@@ssl_cert_info = nil
235-
end
236-
237-
unless @@ssl_cert_info
238-
# If no certificate was specified, generate one
239-
unless self.ssl_cert
228+
# Generate a certificate if necessary and cache it
229+
if ! @@ssl_cached_cert
230+
@@ssl_mutex.synchronize do
240231
wlog("Generating SSL certificate for Meterpreter sessions")
241-
@@ssl_cert_info = Rex::Socket::SslTcpServer.ssl_generate_certificate
232+
@@ssl_cached_cert = Rex::Socket::SslTcpServer.ssl_generate_certificate
242233
wlog("Generated SSL certificate for Meterpreter sessions")
243-
# Load the user's specified certificate
244-
else
245-
wlog("Loading custom SSL certificate for Meterpreter sessions")
246-
@@ssl_cert_info = Rex::Socket::SslTcpServer.ssl_parse_pem(self.ssl_cert)
247-
wlog("Loaded custom SSL certificate for Meterpreter sessions")
248-
@@ssl_cached_cert = self.ssl_cert
249234
end
250235
end
251236

252-
# Create a new context for each session
253-
ctx = OpenSSL::SSL::SSLContext.new()
254-
ctx.key = @@ssl_cert_info[0]
255-
ctx.cert = @@ssl_cert_info[1]
256-
ctx.extra_chain_cert = @@ssl_cert_info[2]
257-
ctx.options = 0
258-
ctx.session_id_context = Rex::Text.rand_text(16)
259-
end # End of mutex.synchronize
237+
# Use the cached certificate
238+
ssl_cert_info = @@ssl_cached_cert
239+
break
240+
end
241+
242+
# Create a new context for each session
243+
ctx = OpenSSL::SSL::SSLContext.new()
244+
ctx.key = ssl_cert_info[0]
245+
ctx.cert = ssl_cert_info[1]
246+
ctx.extra_chain_cert = ssl_cert_info[2]
247+
ctx.options = 0
248+
ctx.session_id_context = Rex::Text.rand_text(16)
260249

261250
ctx
262251
end

0 commit comments

Comments
 (0)