Skip to content

Instantly share code, notes, and snippets.

@shubhamgupta0x0
Forked from hakluke/dumpCN.py
Created June 30, 2020 17:44
Show Gist options
  • Save shubhamgupta0x0/d63a324a3025009472816e972aabb460 to your computer and use it in GitHub Desktop.
Save shubhamgupta0x0/d63a324a3025009472816e972aabb460 to your computer and use it in GitHub Desktop.

Revisions

  1. @hakluke hakluke revised this gist Jun 30, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions dumpCN.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    #!/usr/bin/python3

    import ssl,sys
    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
    print(sys.argv[1] + ":" + cert[begin:end].decode('utf-8')) #Retrieve the CN from the DER encoded certificate
    jsondata = {"ip": sys.argv[1], "cn": cert[begin:end].decode('utf-8')}
    print(json.dumps(jsondata))
  2. @hakluke hakluke revised this gist Jun 30, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dumpCN.py
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,4 @@
    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
    print(sys.argv[1] + ":" + cert[begin:end].decode('utf-8')) #Retrieve the CN from the DER encoded certificate
  3. @hakluke hakluke created this gist Jun 30, 2020.
    9 changes: 9 additions & 0 deletions dumpCN.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #!/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