@@ -42,12 +42,7 @@ class Client
42
42
@@ext_hash = { }
43
43
44
44
#
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
51
46
#
52
47
@@ssl_cached_cert = nil
53
48
@@ -111,7 +106,6 @@ def init_meterpreter(sock,opts={})
111
106
self . capabilities = opts [ :capabilities ] || { }
112
107
self . commands = [ ]
113
108
114
-
115
109
self . conn_id = opts [ :conn_id ]
116
110
self . url = opts [ :url ]
117
111
self . ssl = opts [ :ssl ]
@@ -218,45 +212,40 @@ def swap_sock_ssl_to_plain
218
212
219
213
def generate_ssl_context
220
214
221
- # Initialize a null context
222
215
ctx = nil
216
+ ssl_cert_info = nil
223
217
224
- # Synchronize to prevent race conditions
225
- @@ssl_mutex . synchronize do
218
+ loop do
226
219
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
230
226
end
231
227
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
240
231
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
242
233
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
249
234
end
250
235
end
251
236
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 )
260
249
261
250
ctx
262
251
end
0 commit comments