-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssl-certs-checker.py
executable file
·65 lines (50 loc) · 1.25 KB
/
ssl-certs-checker.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
import OpenSSL.crypto as crypto
import click
import socket
import ssl
from datetime import datetime
from prettytable import PrettyTable
pt = PrettyTable()
pt.field_names = [
"Host",
"Common Name",
"NotBefore",
"NotAfter",
"Issuer",
]
pt.align = "l"
def expiration_check(target: str) -> None:
ctx = ssl.create_default_context()
conn = ctx.wrap_socket(
socket.socket(socket.AF_INET),
server_hostname=target,
)
conn.settimeout(1.0)
conn.connect((target, 443))
cert_info = conn.getpeercert(True)
x509 = crypto.load_certificate(
crypto.FILETYPE_ASN1,
cert_info
)
pt.add_row([
target,
x509.get_subject().CN,
datetime.strptime(
x509.get_notBefore().decode('ascii'),
'%Y%m%d%H%M%SZ'
),
datetime.strptime(
x509.get_notAfter().decode('ascii'),
'%Y%m%d%H%M%SZ'
),
x509.get_issuer().CN,
])
@click.command()
@click.option('-H', '--hosts', help='target hosts, splits by comma')
def check(hosts):
for host in hosts.split(','):
expiration_check(host)
print(pt.get_string(sortby="NotAfter", reversesort=False))
if __name__ == '__main__':
check()