Skip to content

Commit 86a93e4

Browse files
committed
Added certificate mime type detection as well as correct file extension
1 parent a3cc52c commit 86a93e4

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

modules/auxiliary/scanner/ssl/ssl_version.rb

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,77 @@ def print_cert(cert, ip)
166166
end
167167
end
168168

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+
169226
# Process certificate with enhanced analysis
170227
def process_certificate(ip, cert)
171228
print_cert(cert, ip)
172229

230+
# Determine certificate MIME type
231+
mime_type = certificate_mime_type(cert.to_text)
232+
173233
# Store certificate in loot with rex-sslscan metadata
174234
loot_cert = store_loot(
175235
'ssl.certificate.rex_sslscan',
176-
'text/plain',
236+
mime_type,
177237
ip,
178238
cert.to_text,
179-
"ssl_cert_#{ip}_#{rport}.crt",
239+
"ssl_cert_#{ip}_#{rport}.#{file_extension_for_mime_type(mime_type)}",
180240
"SSL Certificate from #{ip}:#{rport}"
181241
)
182242
print_good("Certificate saved to loot: #{loot_cert}")

0 commit comments

Comments
 (0)