Skip to content

Commit 9c9d333

Browse files
committed
Create verify ssl mixin, adjust some formatting
1 parent 9d20d05 commit 9c9d333

File tree

5 files changed

+26
-61
lines changed

5 files changed

+26
-61
lines changed

lib/msf/core/handler/reverse_http.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'msf/core/handler/reverse_http/uri_checksum'
55
require 'rex/payloads/meterpreter/patch'
66
require 'rex/parser/x509_certificate'
7+
require 'msf/core/payload/windows/verify_ssl'
78

89
module Msf
910
module Handler
@@ -17,6 +18,7 @@ module ReverseHttp
1718

1819
include Msf::Handler
1920
include Msf::Handler::ReverseHttp::UriChecksum
21+
include Msf::Payload::Windows::VerifySsl
2022

2123
#
2224
# Returns the string representation of the handler type
@@ -292,21 +294,23 @@ def on_request(cli, req, obj)
292294

293295
blob = obj.stage_payload
294296

297+
verify_cert_hash = get_ssl_cert_hash(datastore['StagerVerifySSLCert'],
298+
datastore['HandlerSSLCert'])
295299
#
296300
# Patch options into the payload
297301
#
298-
Rex::Payloads::Meterpreter::Patch.patch_passive_service! blob,
302+
Rex::Payloads::Meterpreter::Patch.patch_passive_service!(blob,
299303
:ssl => ssl?,
300304
:url => url,
301-
:ssl_cert_hash => get_ssl_cert_hash,
305+
:ssl_cert_hash => verify_cert_hash,
302306
:expiration => datastore['SessionExpirationTimeout'],
303307
:comm_timeout => datastore['SessionCommunicationTimeout'],
304308
:ua => datastore['MeterpreterUserAgent'],
305309
:proxy_host => datastore['PayloadProxyHost'],
306310
:proxy_port => datastore['PayloadProxyPort'],
307311
:proxy_type => datastore['PayloadProxyType'],
308312
:proxy_user => datastore['PayloadProxyUser'],
309-
:proxy_pass => datastore['PayloadProxyPass']
313+
:proxy_pass => datastore['PayloadProxyPass'])
310314

311315
resp.body = encode_stage(blob)
312316

@@ -357,20 +361,6 @@ def bind_port
357361
port > 0 ? port : datastore['LPORT'].to_i
358362
end
359363

360-
def get_ssl_cert_hash
361-
unless datastore['StagerVerifySSLCert'].to_s =~ /^(t|y|1)/i
362-
return nil
363-
end
364-
365-
unless datastore['HandlerSSLCert']
366-
raise ArgumentError, "StagerVerifySSLCert is enabled but no HandlerSSLCert is configured"
367-
end
368-
369-
hash = Rex::Parser::X509Certificate.get_cert_file_hash(datastore['HandlerSSLCert'])
370-
print_status("Meterpreter will verify SSL Certificate with SHA1 hash #{hash.unpack("H*").first}")
371-
hash
372-
end
373-
374364
end
375365

376366
end

lib/msf/core/payload/windows/reverse_winhttps.rb

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'msf/core'
44
require 'msf/core/payload/windows/reverse_winhttp'
5-
require 'rex/parser/x509_certificate'
5+
require 'msf/core/payload/windows/verify_ssl'
66

77
module Msf
88

@@ -17,6 +17,7 @@ module Msf
1717
module Payload::Windows::ReverseWinHttps
1818

1919
include Msf::Payload::Windows::ReverseWinHttp
20+
include Msf::Payload::Windows::VerifySsl
2021

2122
#
2223
# Register reverse_winhttps specific options
@@ -49,7 +50,8 @@ def generate_reverse_winhttps(opts={})
4950
#
5051
def generate
5152

52-
verify_cert_hash = get_ssl_cert_hash
53+
verify_cert_hash = get_ssl_cert_hash(datastore['StagerVerifySSLCert'],
54+
datastore['HandlerSSLCert'])
5355

5456
# Generate the simple version of this stager if we don't have enough space
5557
if self.available_space.nil? || required_space > self.available_space
@@ -97,23 +99,6 @@ def required_space
9799
space
98100
end
99101

100-
#
101-
# Get the SSL hash from the certificate, if required.
102-
#
103-
def get_ssl_cert_hash
104-
unless datastore['StagerVerifySSLCert'].to_s =~ /^(t|y|1)/i
105-
return nil
106-
end
107-
108-
unless datastore['HandlerSSLCert']
109-
raise ArgumentError, "StagerVerifySSLCert is enabled but no HandlerSSLCert is configured"
110-
end
111-
112-
hash = Rex::Parser::X509Certificate.get_cert_file_hash(datastore['HandlerSSLCert'])
113-
print_status("Meterpreter will verify SSL Certificate with SHA1 hash #{hash.unpack("H*").first}")
114-
hash
115-
end
116-
117102
end
118103

119104
end

lib/rex/parser/x509_certificate.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def self.parse_pem_file(ssl_cert_file)
5858

5959
#
6060
# Parse a certificate in unified PEM format and retrieve
61-
# the SHA1 hash.
61+
# the SHA1 hash.
6262
#
6363
# @param [String] ssl_cert
6464
# @return [String]
@@ -74,7 +74,7 @@ def self.get_cert_hash(ssl_cert)
7474

7575
#
7676
# Parse a file that contains a certificate in unified PEM
77-
# format and retrieve the SHA1 hash.
77+
# format and retrieve the SHA1 hash.
7878
#
7979
# @param [String] ssl_cert_file
8080
# @return [String]

lib/rex/payloads/meterpreter/patch.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ def self.patch_ssl_check!(blob, ssl_cert_hash)
9999
# Patch options into metsrv for reverse HTTP payloads
100100
def self.patch_passive_service!(blob, options)
101101

102-
patch_transport! blob, options[:ssl]
103-
patch_url! blob, options[:url]
104-
patch_expiration! blob, options[:expiration]
105-
patch_comm_timeout! blob, options[:comm_timeout]
106-
patch_ua! blob, options[:ua]
107-
patch_ssl_check! blob, options[:ssl_cert_hash]
102+
patch_transport!(blob, options[:ssl])
103+
patch_url!(blob, options[:url])
104+
patch_expiration!(blob, options[:expiration])
105+
patch_comm_timeout!(blob, options[:comm_timeout])
106+
patch_ua!(blob, options[:ua])
107+
patch_ssl_check!(blob, options[:ssl_cert_hash])
108108
patch_proxy!(blob,
109109
options[:proxy_host],
110110
options[:proxy_port],

modules/payloads/singles/windows/meterpreter_reverse_https.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Metasploit3
1616

1717
include Msf::Payload::Windows::StagelessMeterpreter
1818
include Msf::Sessions::MeterpreterOptions
19+
include Msf::Payload::Windows::VerifySsl
1920

2021
def initialize(info = {})
2122

@@ -55,34 +56,23 @@ def generate
5556
# end
5657
#end
5758

58-
Rex::Payloads::Meterpreter::Patch.patch_passive_service! dll,
59+
verify_cert_hash = get_ssl_cert_hash(datastore['StagerVerifySSLCert'],
60+
datastore['HandlerSSLCert'])
61+
62+
Rex::Payloads::Meterpreter::Patch.patch_passive_service!(dll,
5963
:url => url,
6064
:ssl => true,
61-
:ssl_cert_hash => get_ssl_cert_hash,
65+
:ssl_cert_hash => verify_cert_hash,
6266
:expiration => datastore['SessionExpirationTimeout'].to_i,
6367
:comm_timeout => datastore['SessionCommunicationTimeout'].to_i,
6468
:ua => datastore['MeterpreterUserAgent'],
6569
:proxyhost => datastore['PROXYHOST'],
6670
:proxyport => datastore['PROXYPORT'],
6771
:proxy_type => datastore['PROXY_TYPE'],
6872
:proxy_username => datastore['PROXY_USERNAME'],
69-
:proxy_password => datastore['PROXY_PASSWORD']
70-
end
71-
72-
end
73-
74-
def get_ssl_cert_hash
75-
unless datastore['StagerVerifySSLCert'].to_s =~ /^(t|y|1)/i
76-
return nil
77-
end
78-
79-
unless datastore['HandlerSSLCert']
80-
raise ArgumentError, "StagerVerifySSLCert is enabled but no HandlerSSLCert is configured"
73+
:proxy_password => datastore['PROXY_PASSWORD'])
8174
end
8275

83-
hash = Rex::Parser::X509Certificate.get_cert_file_hash(datastore['HandlerSSLCert'])
84-
print_status("Meterpreter will verify SSL Certificate with SHA1 hash #{hash.unpack("H*").first}")
85-
hash
8676
end
8777

8878
end

0 commit comments

Comments
 (0)