Skip to content

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

sflib: Move sslDerToPem to SpiderFootHelpers.sslDerToPem() (#1761)

bcoles9746aa13a952
diff --git a/modules/sfp_sslcert.py b/modules/sfp_sslcert.py
index dcc06ccb..aab7fb0d 100644
--- a/modules/sfp_sslcert.py
+++ b/modules/sfp_sslcert.py
@@ -12,7 +12,7 @@
from urllib.parse import urlparse
-from spiderfoot import SpiderFootEvent, SpiderFootPlugin
+from spiderfoot import SpiderFootEvent, SpiderFootPlugin, SpiderFootHelpers
class sfp_sslcert(SpiderFootPlugin):
@@ -109,7 +109,7 @@ class sfp_sslcert(SpiderFootPlugin):
sock = self.sf.safeSSLSocket(fqdn, port, self.opts['ssltimeout'])
sock.do_handshake()
dercert = sock.getpeercert(True)
- pemcert = self.sf.sslDerToPem(dercert)
+ pemcert = SpiderFootHelpers.sslDerToPem(dercert)
cert = self.sf.parseCert(str(pemcert), fqdn, self.opts['certexpiringdays'])
except Exception as x:
self.info("Unable to SSL-connect to " + fqdn + " (" + str(x) + ")")
diff --git a/sflib.py b/sflib.py
index 2c565327..4b459910 100644
--- a/sflib.py
+++ b/sflib.py
@@ -973,24 +973,6 @@ class SpiderFoot:
sock.do_handshake()
return sock
- def sslDerToPem(self, der_cert: bytes) -> str:
- """Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded string version of the same certificate.
-
- Args:
- der_cert (bytes): certificate in DER format
-
- Returns:
- str: PEM-encoded certificate as a byte string
-
- Raises:
- TypeError: arg type was invalid
- """
-
- if not isinstance(der_cert, bytes):
- raise TypeError(f"der_cert is {type(der_cert)}; expected bytes()")
-
- return ssl.DER_cert_to_PEM_cert(der_cert)
-
def parseCert(self, rawcert: str, fqdn: str = None, expiringdays: int = 30) -> dict:
"""Parse a PEM-format SSL certificate.
diff --git a/spiderfoot/helpers.py b/spiderfoot/helpers.py
index c70df92e..18285378 100644
--- a/spiderfoot/helpers.py
+++ b/spiderfoot/helpers.py
@@ -5,6 +5,7 @@ import os
import os.path
import random
import re
+import ssl
import urllib.parse
import uuid
from pathlib import Path
@@ -1091,6 +1092,24 @@ class SpiderFootHelpers():
# https://tools.ietf.org/html/rfc3986#section-3.3
return re.findall(r"(https?://[a-zA-Z0-9-\.:]+/[\-\._~!\$&'\(\)\*\+\,\;=:@/a-zA-Z0-9]*)", html.unescape(content))
+ @staticmethod
+ def sslDerToPem(der_cert: bytes) -> str:
+ """Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded string version of the same certificate.
+
+ Args:
+ der_cert (bytes): certificate in DER format
+
+ Returns:
+ str: PEM-encoded certificate as a byte string
+
+ Raises:
+ TypeError: arg type was invalid
+ """
+ if not isinstance(der_cert, bytes):
+ raise TypeError(f"der_cert is {type(der_cert)}; expected bytes()")
+
+ return ssl.DER_cert_to_PEM_cert(der_cert)
+
@staticmethod
def countryNameFromCountryCode(countryCode: str) -> str:
"""Convert a country code to full country name.
diff --git a/test/unit/spiderfoot/test_spiderfoothelpers.py b/test/unit/spiderfoot/test_spiderfoothelpers.py
index 77b64156..297012b8 100644
--- a/test/unit/spiderfoot/test_spiderfoothelpers.py
+++ b/test/unit/spiderfoot/test_spiderfoothelpers.py
@@ -441,6 +441,19 @@ class TestSpiderFootHelpers(unittest.TestCase):
self.assertIsInstance(cards, list)
self.assertEqual(["4111111111111111"], cards)
+ def test_sslDerToPem_should_return_a_certificate_as_a_string(self):
+ pem = SpiderFootHelpers.sslDerToPem(
+ b"0\x82\x07J0\x82\x062\xa0\x03\x02\x01\x02\x02\x10\x0c\x1f\xcb\x18E\x18\xc7\xe3\x86gA#mks\xf10\r\x06\t*\x86H\x86\xf7\r\x01\x01\x0b\x05\x000O1\x0b0\t\x06\x03U\x04\x06\x13\x02US1\x150\x13\x06\x03U\x04\n\x13\x0cDigiCert Inc1)0\'\x06\x03U\x04\x03\x13 DigiCert TLS RSA SHA256 2020 CA10\x1e\x17\r230113000000Z\x17\r240213235959Z0\x81\x961\x0b0\t\x06\x03U\x04\x06\x13\x02US1\x130\x11\x06\x03U\x04\x08\x13\nCalifornia1\x140\x12\x06\x03U\x04\x07\x13\x0bLos Angeles1B0@\x06\x03U\x04\n\x0c9Internet\xc2\xa0Corporation\xc2\xa0for\xc2\xa0Assigned\xc2\xa0Names\xc2\xa0and\xc2\xa0Numbers1\x180\x16\x06\x03U\x04\x03\x13\x0fwww.example.org0\x82\x01\x220\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00\x03\x82\x01\x0f\x000\x82\x01\n\x02\x82\x01\x01\x00\xc2\x80w\x89Y\xb8Eo\xbaJ\xd9\x11\xfa{\xad\xc7W\xd0z\xfb\xb6\xfa\xdd\x05\xbb\xa2\x81q\xbb\xe1\x7f!\xd2_.\xf0\xd2rNu4\xf8\x8db\xe3J\xdaQ\x90\xd4\x01=\x9c\x0c\xc0q\xf7\xe6/\xb6\xd6\x07g&\xd0\xde\xff\x17\xce\xf0\x85\xfd1\xc1f\xca\x87e\x05G*_\xc0\xab\xb8\x8c\xc3\xbf\xd0\x17\x7fc\xa3\\\xf0F\xfb\x86\xaa\xfbM\xd7*^\x7f\x9a\xe0\x13\x97}\xbe\xfb}5W\r]^\x81\x985\xea\x16B\xa2\xd3\xb0t\xf7Y-\xed8\xe7\xfez\x1b\xb36\xe6~\xae?\x9e\xa6\x16\x83\xdeS\x01N\x81\x00\xae\xbbB\xf5\x1fu)4\xcd\xe9\x84\x808\xae<7\x14\xc0\xf0\'\xce0R\xb9\x8a\xdc_\x22\xa0y\xf8ONI\x04\xe2u|\xaa/*\x1e\x03\xecqL\xa3*a\xfco\xca\x91\x1e\x93Z.x\x08X\xf6\xee\xbb4 ]\x9a\xe6\xaf\xc6\xd7\xf2\xbf\n{\xfa\x8e\x92w\xe3l{\x0c@\x86dJ\x15\xecp\xd7r\x8ec0\xe1\x0b\xefZ0\x97.%\x02\x03\x01\x00\x01\xa3\x82\x03\xd80\x82\x03\xd40\x1f\x06\x03U\x1d#\x04\x180\x16\x80\x14\xb7k\xa2\xea\xa8\xaa\x84\x8cy\xea\xb4\xda\x0f\x98\xb2\xc5\x95v\xb9\xf40\x1d\x06\x03U\x1d\x0e\x04\x16\x04\x14\xb0\x93?\xe8\x17\x82\xfdl\xb2\xb6\x17\x87\xcb\xe3\x80\xfe\x82\x9b\x01\x9e0\x81\x81\x06\x03U\x1d\x11\x04z0x\x82\x0fwww.example.org\x82\x0bexample.net\x82\x0bexample.edu\x82\x0bexample.com\x82\x0bexample.org\x82\x0fwww.example.com\x82\x0fwww.example.edu\x82\x0fwww.example.net0\x0e\x06\x03U\x1d\x0f\x01\x01\xff\x04\x04\x03\x02\x05\xa00\x1d\x06\x03U\x1d%\x04\x160\x14\x06\x08+\x06\x01\x05\x05\x07\x03\x01\x06\x08+\x06\x01\x05\x05\x07\x03\x020\x81\x8f\x06\x03U\x1d\x1f\x04\x81\x870\x81\x840@\xa0>\xa0<\x86:http://crl3.digicert.com/DigiCertTLSRSASHA2562020CA1-4.crl0@\xa0>\xa0<\x86:http://crl4.digicert.com/DigiCertTLSRSASHA2562020CA1-4.crl0>\x06\x03U\x1d \x0470503\x06\x06g\x81\x0c\x01\x02\x020)0\'\x06\x08+\x06\x01\x05\x05\x07\x02\x01\x16\x1bhttp://www.digicert.com/CPS0\x7f\x06\x08+\x06\x01\x05\x05\x07\x01\x01\x04s0q0$\x06\x08+\x06\x01\x05\x05\x070\x01\x86\x18http://ocsp.digicert.com0I\x06\x08+\x06\x01\x05\x05\x070\x02\x86=http://cacerts.digicert.com/DigiCertTLSRSASHA2562020CA1-1.crt0\t\x06\x03U\x1d\x13\x04\x020\x000\x82\x01\x7f\x06\n+\x06\x01\x04\x01\xd6y\x02\x04\x02\x04\x82\x01o\x04\x82\x01k\x01i\x00v\x00\xee\xcd\xd0d\xd5\xdb\x1a\xce\xc5\\\xb7\x9d\xb4\xcd\x13\xa22\x87F|\xbc\xec\xde\xc3QHYFq\x1f\xb5\x9b\x00\x00\x01\x85\xabH\x05#\x00\x00\x04\x03\x00G0E\x02!\x00\xaa\xdf\x9f+\xa8\xc5t`:\xb6\xfd\x04Z\xdfkk\x1d\x16`\x15x\xad\xefc\x81\x98*\xd38\xb8\xd9\x05\x02 @a\xd7\x22\xa9>\xf8\x17\xd4\x1a\xde\x13L\x01Rj\xe29U!%.\xfb*\x01u\xf7w\xd3\xdb\xce\xfb\x00w\x00s\xd9\x9e\x89\x1bL\x96x\xa0 }G\x9d\xe6\xb2\xc6\x1c\xd0Q^q\x19*\x8ck\x80\x10z\xc1wr\xb5\x00\x00\x01\x85\xabH\x05\x9f\x00\x00\x04\x03\x00H0F\x02!\x00\xd7d\x94\x14\xaek\x80\xba\x91\xce\xf8\x1c\xaf\xb6sW\x89\xe5\xf9\x9b}\x96Z\x00\xcd\x12\xdf=\xce\xefH\xf0\x02!\x00\x97=\xbc\x12s\x1dk\x13\xe0c\x15\xac\x19\x95X\xcb\x8f\xfdO\xb0\xcd\nA\x07,|p\xd9%D\xcb\xc0\x00v\x00H\xb0\xe3k\xda\xa6G4\x0f\xe5j\x02\xfa\x9d0\xeb\x1cR\x01\xcbV\xdd,\x81\xd9\xbb\xbf\xab9\xd8\x84s\x00\x00\x01\x85\xabH\x05^\x00\x00\x04\x03\x00G0E\x02!\x00\xde[\x84{a\xa3%\x8c\'p\x90\x07\xfdb`Q!2\x05\x15\x90XG\x0c\xcf\xe7\x94OS\x84,!\x02 \x0f\xbc\xf2W\xca\x9e\xda\xdaL\xf0%}\xcf\xed\xfa\x87\xe5y(\xde\xb3\xe1\x0b4h]\x87z[\xe4$\n0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x0b\x05\x00\x03\x82\x01\x01\x00Y\xe4J\xd8\xa9\x82\xba\x9aJ\xf1c\x0cmv&u\xb3<t\xbe\xc5\xf7=\xa7\x91\x92\xf8\xcf\x06-X\x10\xed\xf3\xb8\xd6\xfcl\xff\x13\x962\xcdO\xe9\x87$\x85\x0bt\xa2\xc2\xf6\x0f\xf5\xa7\xd8}v\x8a\xae\xe9\xc9X+n\x00o\xb9\xcd$\xee\xc4B\xc5L\x16\x85\x9d4a9#\xbf\xc6\x8e\x95\xc9\x84\xa9\xb2\xe5A\x0fDx\xd7\x95\xb9\xcf\xd9t\xbfXO\xe7\x16\xff|@0\xc4lN\x22M\xcb\x83g:\x93\xbf+\xc5\xc5\x9c\x1a\xf2C\xa1%;\x84\xf6\xf7Sn\xa8\x85\xae\xde\x14t\x910\x06\r\xf2\x07\xd4\xc4\x08\xbaCd\xc5\xe2?\xda\xac\xc5A\xaf\xa47\xe8Bvt\xf7\x13\xbbJ}6Y\x81\x9b\xc7D\xdf\x89s\xb93B\xe8`\xc2Ma]\x12Z\x10\xf6\xef\xff3\x89\x14P\xe8\xd6\x9f\xc6\xb9\\+5\xdb\xad\xed\xdd6\xb6%\xf2\x95\x8a\xaci?\x9a\xfe\x1a\xf8\x15(m\xea\x18Z\xc2\xd2b\x18\xaf@x\xb5\xfa^\t\x8fS\xf9\xcc\xf8#\xa1\x831#\xf4\xc6"
+ )
+ self.assertIsInstance(pem, str)
+ self.assertTrue(pem.startswith('-----BEGIN CERTIFICATE-----'))
+
+ def test_sslDerToPem_invalid_cert_should_raise_TypeError(self):
+ invalid_types = [None, "", list(), dict()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type), self.assertRaises(TypeError):
+ SpiderFootHelpers.sslDerToPem(invalid_type)
+
def test_extractUrlsFromText_should_extract_urls_from_string(self):
invalid_types = [None, "", bytes(), list(), dict()]
for invalid_type in invalid_types:
diff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.py
index d94d2995..ae55b3c3 100644
--- a/test/unit/test_spiderfoot.py
+++ b/test/unit/test_spiderfoot.py
@@ -561,23 +561,6 @@ class TestSpiderFoot(unittest.TestCase):
sf.safeSSLSocket(None, None, None, None)
self.assertEqual('TBD', 'TBD')
- @unittest.skip("todo")
- def test_sslDerToPem(self):
- sf = SpiderFoot(self.default_options)
- pem = sf.sslDerToPem(None)
-
- self.assertEqual(pem, None)
-
- self.assertEqual('TBD', 'TBD')
-
- def test_sslDerToPem_invalid_cert_should_raise_TypeError(self):
- sf = SpiderFoot(self.default_options)
-
- invalid_types = [None, "", list(), dict()]
- for invalid_type in invalid_types:
- with self.subTest(invalid_type=invalid_type), self.assertRaises(TypeError):
- sf.sslDerToPem(invalid_type)
-
def test_parse_cert_should_return_a_dict(self):
sf = SpiderFoot(self.default_options)