-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcertrip.py
executable file
·44 lines (43 loc) · 1.36 KB
/
certrip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sys
from socket import socket
from cryptography import x509
from cryptography.hazmat.backends import default_backend
import cryptography
import socket
import ssl
def certrip(host, port):
host = host.rstrip()
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
try:
with socket.create_connection((host, port), timeout=0.3) as sock:
with context.wrap_socket(sock, server_hostname=host) as ssock:
pem_data = ssl.DER_cert_to_PEM_cert(ssock.getpeercert(True))
cert = x509.load_pem_x509_certificate(str.encode(pem_data), default_backend())
san = cert.extensions.get_extension_for_oid(x509.oid.ExtensionOID.SUBJECT_ALTERNATIVE_NAME)
dns_names = []
for dns_name in san.value:
dns_names.append(dns_name.value)
sock.close()
return dns_names
except ssl.SSLError:
# sys.exit()
pass
except socket.timeout:
# sys.exit()
pass
except TimeoutError:
# sys.exit()
pass
except socket.gaierror:
# sys.exit()
pass
except ConnectionRefusedError:
# sys.exit()
pass
except cryptography.x509.extensions.ExtensionNotFound:
pass
except OSError:
# sys.exit()
pass