-
Notifications
You must be signed in to change notification settings - Fork 1
/
tlsserial.py
executable file
·227 lines (212 loc) · 8.39 KB
/
tlsserial.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
""" grab some things from a TLS cert """
# TODO:
# - Report TLS1.3 negotiation for url lookups as NIST SP 800-52 requires support by Jan 2024
# - Swap back to the pyOpenSSL lib to allow getting entire chain from a host?
# - No need, we can use a private method from ssl
# - Show the SHA-256 hash of the subject Public Key Information
# - Clearly show SANs with specifier, eg.
# DNS:*.axiom-partners.com
# - Compare against INTB (and others) cipher suite?
# NB: Committee on National Security Systems Instruction (CNSSI) 1253 Intelligence Overlay B (INT-B)
# NB: INT-A for the least sensitive data through INT-C for the most sensitive data
# NB: CNSA: The algorithm is recommended by NSA (Commercial National Security Algorithm (CNSA) Suite formerly NSA's Suite B program).
# - Clarify ciphers in use
# eg.
# - peer signing digest
# - server temp keys
# - server public keys
# - TLS cipher?
import logging
import re
import sys
from ssl import OPENSSL_VERSION
import click
from cryptography import x509
from lib import helper
from lib.nice_certificate import NiceCertificate
from lib.color import bold, red, orange, blue
# https://click.palletsprojects.com/en/8.1.x/quickstart/
@click.command()
@click.option(
"--url",
cls=helper.MutuallyExclusiveOption,
mutually_exclusive=["file"],
help="host || host:port || https://host:port/other",
)
@click.option(
"--file",
cls=helper.MutuallyExclusiveOption,
mutually_exclusive=["url"],
help="filename containing a PEM certificate",
)
@click.option("--debug", is_flag=True, type=bool, default=False)
def main(url, file, debug) -> None:
"""tlsserial groks X509 certificates for your pleasure"""
level = logging.DEBUG
fmt = "[%(levelname)s] %(asctime)s - %(message)s"
logging.basicConfig(level=level, format=fmt)
if url:
host, port = get_args(url)
# Assigns all certificates found to tuple cert([c1, c2, ...], "SSL cert")
cert_chain = helper.get_certs_from_host(host, port)
if cert_chain[0] is not None:
for cert in reversed(cert_chain[0]):
display(host, parse_x509(cert), debug)
else:
print(cert_chain[1])
elif file:
host = ""
# Assigns all certificates found to tuple cert([c1, c2, ...], "SSL cert")
cert_chain = helper.get_certs_from_file(file)
if cert_chain[0] is not None:
for cert in reversed(cert_chain[0]):
display(host, parse_x509(cert), debug)
click.echo("")
else:
print(cert_chain[1])
else:
click.echo(f"Library version : {OPENSSL_VERSION}")
ctx = click.get_current_context()
click.echo(ctx.get_help())
def get_args(argv: str) -> tuple:
"""
Try to extract a hostname and port from input string
Returns a tuple of (host, port)
"""
args_matched = re.search(r"([0-9a-zA-Z\-\.]{6,63})[\ :]?([0-9]{2,5})?", argv)
if args_matched is not None:
if args_matched[1] is not None:
if args_matched[1] is not None:
if args_matched[2] is not None and args_matched[2].isdigit:
# host and specified port
return (args_matched[1], args_matched[2])
# host and default port
return (args_matched[1], 443)
print(f"Error parsing the input : {argv}")
sys.exit(1)
def parse_x509(cert: x509.Certificate) -> NiceCertificate:
"""Parse an ugly X509 object"""
"""Return a NiceCertificate object """
# We use helper functions where parsing is gnarly
notBefore, notAfter = helper.get_before_and_after(cert)
ocsp, ca_issuers = helper.get_ocsp_and_caissuer(cert)
return NiceCertificate(
# We use helper functions where parsing is gnarly
version=helper.get_version(cert),
issuer=helper.get_issuer(cert),
ca_issuers=ca_issuers,
subject=helper.get_subject(cert),
sans=helper.get_sans(cert),
basic_constraints=helper.get_basic_constraints(cert),
key_usage=helper.get_key_usage(cert),
ext_key_usage=helper.get_ext_key_usage(cert),
not_before=notBefore,
not_after=notAfter,
crls=helper.get_crls(cert),
ocsp=ocsp,
serial_as_int=cert.serial_number,
key_type=helper.get_key_type(cert),
key_bits=helper.get_key_bits(cert),
key_factors=helper.get_key_factors(cert),
sig_algo=helper.get_sig_algorithm(cert),
sig_algo_params=helper.get_sig_algorithm_params(cert),
)
def display(host: str, cert: NiceCertificate, debug: bool) -> None:
"""Print nicely-formatted attributes of a NiceCertificate object"""
print_items = [
"version",
"issuer",
"subject",
"subject_alt_name",
"basic_constraints",
"not_before",
"not_after",
"public_key_algorithm",
"signature_algorithm",
"key_usage",
"ext_key_usage",
"crls",
"ocsp",
"ca_issuers",
"chain",
"serial_number",
]
width = 24
matched_host = False
for item in print_items:
if "issuer" == item:
print(f"{orange(f'{item:<{width}}')} : {' '.join(cert.issuer)}")
elif "chain" == item:
if len(cert.__getattribute__(item)) > 0:
print(f"{orange(f'{item:<{width}}')} " f": {orange(' » ').join(cert.__getattribute__(item))}")
elif "subject" == item:
cert.subject = [
f"{c[:5]}{bold(blue(c[5:]))}" if c.endswith(f" {host}") else c
for c in cert.subject
]
print(f"{orange(f'{item:<{width}}')} " f": {' '.join(cert.subject)}")
elif "subject_alt_name" == item:
for san in sorted(cert.sans):
if host == str(san) and not matched_host:
# Our host arg matches an exact SAN
matched_host = True
print(f"{orange(f'{item:<{width}}')} " f": {bold(blue(san))}")
elif (
str(san).endswith(re.sub("^[a-z1-9_-]+", "*", host))
and not matched_host
):
# Our host arg matches a wildcard SAN
matched_host = True
print(f"{orange(f'{item:<{width}}')} " f": {orange(san)}")
else:
print(f"{orange(f'{item:<{width}}')} " f": {san}")
elif "basic_constraints" == item:
# Lets highlight any certs which are CAs
if cert.basic_constraints['ca'] == 'True':
cert.basic_constraints['ca'] = orange('True')
for item in ['ca', 'path_length']:
print(
f"{orange(f'{item:<{width}}')} "
f": {cert.basic_constraints[item]}"
)
elif "serial_number" == item:
print(
f"{orange(f'{item:<{width}}')} "
f": {bold(blue(str(cert.serial_as_hex)))}"
)
elif "not_after" == item:
warning = ""
if cert.not_after <= helper.today:
warning = f" {red('(expired!)')}"
elif cert.not_after <= helper.next_14d:
warning = f" {red('(expires within 14 days!)')}"
elif cert.not_after <= helper.next_30d:
warning = f" {red('(expires within 30 days!)')}"
elif cert.not_after <= helper.next_90d:
warning = f" {red('(expires within 90 days!)')}"
print(f"{orange(f'{item:<{width}}')} : {cert.not_after}" + warning)
elif "public_key_algorithm" == item:
print(
f"{orange(f'{item:<{width}}')} "
f": {cert.key_type} ({cert.key_bits} bit)"
)
if debug:
print(
f"{orange(f'{item:<{width}}')} "
f": Factors: {cert.key_factors}"
)
elif "signature_algorithm" == item:
print(
f"{orange(f'{item:<{width}}')} "
f": {cert.sig_algo} params({cert.sig_algo_params})"
)
elif "key_usage" in item:
print(
f"{orange(f'{item:<{width}}')} "
f": {', '.join(sorted(cert.__getattribute__(item)))}"
)
else:
print(f"{orange(f'{item:<{width}}')} " f": {cert.__getattribute__(item)}")
if __name__ == "__main__":
main()