Skip to content

Instantly share code, notes, and snippets.

@abanoub-nasser
Forked from hakluke/dumpCN.py
Created June 30, 2020 12:22
Show Gist options
  • Save abanoub-nasser/eef2201caa694207dfaa93eb4e39099c to your computer and use it in GitHub Desktop.
Save abanoub-nasser/eef2201caa694207dfaa93eb4e39099c to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import ssl,sys
cert = ssl.get_server_certificate((sys.argv[1], 443)) #Retrieve SSL server certificate
cert = ssl.PEM_cert_to_DER_cert(cert) #Convert certificate to DER format
begin = cert.rfind(b'\x06\x03\x55\x04\x03') + 7 #Find the last occurence of this byte string indicating the CN, add 7 bytes to startpoint to account for length of byte string and padding
end = begin + cert[begin - 1] #Set endpoint to startpoint + the length of the CN
print(cert[begin:end].decode('utf-8')) #Retrieve the CN from the DER encoded certificate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment