bot: Update ssl parsing
All checks were successful
Build Docker / Build Docker (push) Successful in 15s

This commit is contained in:
Nathan Woodburn 2023-09-27 18:29:09 +10:00
parent 5c1259c1e2
commit e9d40a337c
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

30
bot.py
View File

@ -9,7 +9,8 @@ import subprocess
import tempfile
import re
import binascii
import ssl
from cryptography import x509
from cryptography.hazmat.backends import default_backend
load_dotenv()
@ -203,22 +204,19 @@ async def ssl(ctx, domain: str):
# Get domains
x509 = ssl.load_certificate(ssl.PEM_cert_to_DER_cert(cert.encode("utf-8")))
domains = []
try:
san_list = x509.get_extension_by_oid("subjectAltName")
san_list = san_list._subjectAltNameString().split(", ")
domains.extend(san_list)
except Exception as e:
print(e, flush=True)
cert_obj = x509.load_pem_x509_certificate(cert.encode("utf-8"), default_backend())
domains = []
for ext in cert_obj.extensions:
if ext.oid == x509.ExtensionOID.SUBJECT_ALTERNATIVE_NAME:
san_list = ext.value.get_values_for_type(x509.DNSName)
domains.extend(san_list)
# Extract the common name (CN) from the subject
common_name = cert_obj.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME)
if common_name:
domains.append(common_name[0].value)
try:
subject = x509.get_subject()
common_name = subject.CN
if common_name:
domains.append(common_name)
except Exception as e:
print(e, flush=True)
if domains:
message = message + "\n## SSL Domains:\n"