@@ -22,9 +22,10 @@ class RemoteHTTPDataService
22
22
#
23
23
# @param [String] endpoint A valid http or https URL. Cannot be nil
24
24
#
25
- def initialize ( endpoint )
25
+ def initialize ( endpoint , https_opts = { } )
26
26
validate_endpoint ( endpoint )
27
27
@endpoint = URI . parse ( endpoint )
28
+ @https_opts = https_opts
28
29
build_client_pool ( 5 )
29
30
end
30
31
@@ -244,12 +245,39 @@ def build_client_pool(size)
244
245
http = Net ::HTTP . new ( @endpoint . host , @endpoint . port )
245
246
if @endpoint . is_a? ( URI ::HTTPS )
246
247
http . use_ssl = true
247
- http . verify_mode = OpenSSL ::SSL ::VERIFY_NONE
248
+ http . verify_mode = OpenSSL ::SSL ::VERIFY_PEER
249
+ unless @https_opts . empty?
250
+ if @https_opts [ :skip_verify ]
251
+ http . verify_mode = OpenSSL ::SSL ::VERIFY_NONE
252
+ else
253
+ # https://stackoverflow.com/questions/22093042/implementing-https-certificate-pubkey-pinning-with-ruby
254
+ user_passed_cert = OpenSSL ::X509 ::Certificate . new ( File . read ( @https_opts [ :cert ] ) )
255
+
256
+ http . verify_callback = lambda do |preverify_ok , cert_store |
257
+ server_cert = cert_store . chain [ 0 ]
258
+ return true unless server_cert . to_der == cert_store . current_cert . to_der
259
+ same_public_key? ( server_cert , user_passed_cert )
260
+ end
261
+ end
262
+ end
248
263
end
249
264
@client_pool << http
250
265
}
251
266
end
252
267
268
+ # Tells us whether the private keys on the passed certificates match
269
+ # and use the same algo
270
+ def same_public_key? ( ref_cert , actual_cert )
271
+ pkr , pka = ref_cert . public_key , actual_cert . public_key
272
+
273
+ # First check if the public keys use the same crypto...
274
+ return false unless pkr . class == pka . class
275
+ # ...and then - that they have the same contents
276
+ return false unless pkr . to_pem == pka . to_pem
277
+
278
+ true
279
+ end
280
+
253
281
def try_sound_effect ( )
254
282
sound_file = ::File . join ( Msf ::Config . data_directory , "sounds" , "Goliath_Online_Sound_Effect.wav" )
255
283
Rex ::Compat . play_sound ( sound_file )
0 commit comments