@@ -166,17 +166,77 @@ def print_cert(cert, ip)
166
166
end
167
167
end
168
168
169
+ # Analyze the certificate data format to determine appropriate MIME type
170
+ def certificate_mime_type ( cert )
171
+ return 'text/plain' unless cert . is_a? ( String )
172
+
173
+ # Check for PEM format
174
+ if cert . include? ( '-----BEGIN CERTIFICATE-----' ) && cert . include? ( '-----END CERTIFICATE-----' )
175
+ 'application/x-pem-file'
176
+ end
177
+
178
+ # Check for PKCS#12 format
179
+ if cert . include? ( '-----BEGIN PKCS12-----' ) && cert . include? ( '-----END PKCS12-----' )
180
+ 'application/x-pkcs12'
181
+ end
182
+
183
+ # Check for PKCS#8 format
184
+ if cert . include? ( '-----BEGIN PRIVATE KEY-----' ) || cert . include? ( '-----BEGIN ENCRYPTED PRIVATE KEY-----' )
185
+ 'application/pkcs8'
186
+ end
187
+
188
+ # Check for PKCS#7 format
189
+ if cert . include? ( '-----BEGIN PKCS7-----' ) || cert . include? ( '-----BEGIN CERTIFICATE-----' )
190
+ 'application/pkcs7-mime'
191
+ end
192
+
193
+ # Check for DER format
194
+ if cert . encoding == ::Encoding ::ASCII_8BIT || cert . force_encoding ( 'ASCII-8BIT' ) . valid_encoding?
195
+ 'application/x-x509-ca-cert'
196
+ end
197
+
198
+ # Check for OpenSSL text output
199
+ if cert . include? ( 'Certificate:' ) && cert . include? ( 'Subject:' ) && cert . include? ( 'Issuer:' )
200
+ 'text/x-x509-certificate'
201
+ end
202
+
203
+ 'application/x-x509-ca-cert'
204
+ end
205
+
206
+ # Map MIME types to appropriate certificate file extensions
207
+ def file_extension_for_mime_type ( mime_type )
208
+ case mime_type
209
+ when 'application/x-pem-file'
210
+ '.pem'
211
+ when 'application/x-x509-ca-cert'
212
+ '.crt'
213
+ when 'application/x-pkcs12'
214
+ '.p12'
215
+ when 'application/pkcs8'
216
+ '.p8'
217
+ when 'application/pkcs7-mime'
218
+ '.p7c'
219
+ when 'text/x-x509-certificate'
220
+ '.txt'
221
+ else
222
+ '.crt'
223
+ end
224
+ end
225
+
169
226
# Process certificate with enhanced analysis
170
227
def process_certificate ( ip , cert )
171
228
print_cert ( cert , ip )
172
229
230
+ # Determine certificate MIME type
231
+ mime_type = certificate_mime_type ( cert . to_text )
232
+
173
233
# Store certificate in loot with rex-sslscan metadata
174
234
loot_cert = store_loot (
175
235
'ssl.certificate.rex_sslscan' ,
176
- 'text/plain' ,
236
+ mime_type ,
177
237
ip ,
178
238
cert . to_text ,
179
- "ssl_cert_#{ ip } _#{ rport } .crt " ,
239
+ "ssl_cert_#{ ip } _#{ rport } .#{ file_extension_for_mime_type ( mime_type ) } " ,
180
240
"SSL Certificate from #{ ip } :#{ rport } "
181
241
)
182
242
print_good ( "Certificate saved to loot: #{ loot_cert } " )
0 commit comments