Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

Add getSubjectAltNames to sfp_ssltools

bcoles368030231617
diff --git a/modules/sfp_ssltools.py b/modules/sfp_ssltools.py
index 555652ea..42ea0897 100644
--- a/modules/sfp_ssltools.py
+++ b/modules/sfp_ssltools.py
@@ -174,7 +174,7 @@ class sfp_ssltools(SpiderFootPlugin):
self.checkExpiry(m2cert, event)
- # Report back who the certificate was issued to
+ # Retrieve the entity to whom the certificate was issued
def getIssued(self, cert):
try:
issued = cert.get_subject().as_text().encode('raw_unicode_escape')
@@ -184,41 +184,65 @@ class sfp_ssltools(SpiderFootPlugin):
return issued
- # Report back the certificate issuer
+ # Retrieve the certificate issuer
def getIssuer(self, cert):
try:
issuer = cert.get_issuer().as_text().encode('raw_unicode_escape')
except BaseException as e:
- self.sf.debug("Error parsing certificate.")
+ self.sf.error("Error processing certificate: " + str(e), False)
return None
return issuer
+ # Extract the Subject Alternative Names from the certificate subject
+ def getSubjectAltNames(self, cert):
+ names = list()
+
+ try:
+ sans = cert.get_ext('subjectAltName').get_value().encode('raw_unicode_escape')
+
+ if sans is None:
+ return None
+
+ for san in sans.split(','):
+ names.append(san.strip())
+ except LookupError as e:
+ self.sf.debug("No alternative name found in certificate.")
+ return None
+ except BaseException as e:
+ self.sf.debug("Error parsing certificate:" + str(e))
+ return None
+
+ return names
+
# Check if the hostname matches the name of the server
def checkHostMatch(self, cert, fqdn, sevt):
fqdn = fqdn.lower()
- hosts = ""
+ hosts = list()
- try:
- # Extract the CN from the issued section
- issued = self.getIssued(cert)
+ # Extract the CN from the issued section
+ issued = self.getIssued(cert)
- self.sf.debug("Checking for " + fqdn + " in " + issued.lower())
+ if "cn=" + fqdn in issued.lower():
+ hosts.append('dns:' + fqdn)
- if "cn=" + fqdn in issued.lower():
- hosts = 'dns:' + fqdn
+ # Extract subject alternative names
+ for host in self.getSubjectAltNames(cert):
+ hosts.append(host.lower())
- # Extract hosts from subjectAltName
- hosts = hosts + " " + cert.get_ext('subjectAltName').get_value().encode('raw_unicode_escape').lower()
- except BaseException as e:
- self.sf.debug("Error parsing certificate.")
- return None
+ self.sf.debug("Checking for " + fqdn + " in certificate subject")
+ fqdn_tld = ".".join(fqdn.split(".")[1:]).lower()
- fqdn_tld = '.'.join(fqdn.split('.')[1:]).lower()
+ for host in hosts:
+ if host == "dns:" + fqdn:
+ return True
+ if host == "dns:*." + fqdn_tld:
+ return True
- if "dns:" + fqdn not in hosts and "dns:*." + fqdn_tld not in hosts:
- evt = SpiderFootEvent('SSL_CERTIFICATE_MISMATCH', hosts, self.__name__, sevt)
- self.notifyListeners(evt)
+ evt = SpiderFootEvent('SSL_CERTIFICATE_MISMATCH', ', '.join(hosts), self.__name__, sevt)
+ self.notifyListeners(evt)
+
+ return False
# Check if the expiration date is in the future
def checkExpiry(self, cert, sevt):
@@ -227,7 +251,7 @@ class sfp_ssltools(SpiderFootPlugin):
expstr = cert.get_not_after().get_datetime().strftime("%Y-%m-%d %H:%M:%S")
now = int(time.time())
warnexp = now + self.opts['certexpiringdays'] * 86400
- except BaseException as e:
+ except ValueError as e:
self.sf.error("Error processing date in certificate.", False)
return None