Skip to content

Instantly share code, notes, and snippets.

@hakluke
Last active September 18, 2022 10:52
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save hakluke/da949bde8a59c73541b44ad4d946d511 to your computer and use it in GitHub Desktop.
Save hakluke/da949bde8a59c73541b44ad4d946d511 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import ssl,sys,json
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
jsondata = {"ip": sys.argv[1], "cn": cert[begin:end].decode('utf-8')}
print(json.dumps(jsondata))
@hakluke
Copy link
Author

hakluke commented Jun 30, 2020

For those finding this now - I recommend you don't use it. There are more elegant solutions to this problem such as https://github.com/samirettali/dumpcn and https://github.com/owasp/amass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment