-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtls-sniff-ciphers
executable file
·168 lines (151 loc) · 5.53 KB
/
tls-sniff-ciphers
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
#!/usr/bin/env python
# tls-sniff-ciphers (part of ossobv/vcutil) // wdoekes/2016,2023
# // Public Domain
#
# Sniff TLS traffic off the wire and show which TLS/SSL cipher is being
# negotiated. Useful to debug TLS cipher selection issues, commonly
# caused by old clients or bad configuration.
#
# Sure, wireshark does that too, and more. But this is more convenient
# to run on the server directly.
#
# Uses scapy and scpya-ssl_tls to capture and decode packets. Prints a
# very coarse listing of TLS Client Hello and TLS Server Hello packets.
# The client lists what it accepts and the server chooses one, so the
# Server Hello will contain the selected cipher.
#
# Example:
#
# $ tls-sniff-ciphers host 1.2.3.4 and not port 22
#
# 2016-09-30 13:34:16.924273: 1.2.3.4:1782 -> 5.5.5.5:25: TLS Client Hello:
# VERSION 0x303: TLS_1_2
# CIPHER 0xc02f: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
# CIPHER 0xc02b: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
# CIPHER 0x009e: TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
# ...
#
# 2016-09-30 13:34:16.927713: 5.5.5.5:25 -> 1.2.3.4:1782: TLS Server Hello:
# VERSION 0x303: TLS_1_2
# CIPHER 0xc030: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
#
# Here we see that the TLS setup between 1.2.3.4 and 5.5.5.5 selected
# TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384. That's fine.
#
# At other times, you may see that the client sends ciphers that are not
# in your cipherlist. For instance, you have a cipherlist set to HIGH,
# and the client shows up with cipher 0x000a DES-CBC3-SHA. You can check
# that it's not compatible with your server cipherlist using "openssl
# ciphers":
#
# $ openssl ciphers -V HIGH | grep 0x00,0x0A
# (empty)
#
# $ openssl ciphers -V HIGH:DES-CBC3-SHA | grep 0x00,0x0A
# 0x00,0x0A - DES-CBC3-SHA SSLv3 Kx=RSA Au=RSA Enc=3DES(168) Mac=SHA1
#
# Or, you may see that the server has selected an unexpected cipher
# like 0xc019 TLS_ECDH_anon_WITH_AES_256_CBC_SHA (AECDH-AES256-SHA)
# after which the client reports "unable to get issuer certificate".
# (Well, maybe it shouldn't have provided anonymous ciphers in the if
# it wanted to do authentication.) You can then consider dropping the
# aNULL ciphers from the list:
#
# $ openssl ciphers -V 'HIGH:!aNULL' | grep 0xC0,x19
# (empty)
#
# Installing tls-sniff-ciphers:
#
# apt-get install python-scapy python-pip python-crypto
# pip install scapy-ssl_tls
# install tls-sniff-ciphers /usr/local/bin/
#
# Further reading:
#
# A mapping between the common and OpenSSL cipher names, and a
# description of how the cipher groups work can be found in the
# ciphers(1ssl) man-page.
#
from __future__ import print_function
import sys
from datetime import datetime
from scapy.all import IP, TCP, sniff # python3-scapy (no-install-recommends)
from scapy.main import load_layer
from scapy.layers.tls.crypto.suites import (
_tls_cipher_suites as TLS_CIPHER_SUITES)
assert TLS_CIPHER_SUITES[5].endswith('RSA_WITH_RC4_128_SHA'), TLS_CIPHER_SUITES
TLS_VERSIONS = {
# SSL
0x0002: "SSL_2_0",
0x0300: "SSL_3_0",
# TLS:
0x0301: "TLS_1_0",
0x0302: "TLS_1_1",
0x0303: "TLS_1_2",
0x0304: "TLS_1_3",
# DTLS
0x0100: "PROTOCOL_DTLS_1_0_OPENSSL_PRE_0_9_8f",
0x7f10: "TLS_1_3_DRAFT_16",
0x7f12: "TLS_1_3_DRAFT_18",
0xfeff: "DTLS_1_0",
0xfefd: "DTLS_1_1",
}
# Places lots of stuff in __builtins__. Like:
# TLS, TLSClientHello, TLSServerHello
load_layer('tls')
# Avoid flake8 complaining about us using them out of the blue.
TLS = __builtins__.TLS
TLSClientHello = __builtins__.TLSClientHello
TLSServerHello = __builtins__.TLSServerHello
def traffic_callback(pkt):
if TCP not in pkt:
print(pkt.show(), file=sys.stderr)
return
try:
pkt[TCP].decode_payload_as(TLS)
except TypeError:
pass
else:
if TLS in pkt:
tls_callback(pkt)
def tls_callback(pkt):
# pkt[TLS].show()
if TLSClientHello in pkt or TLSServerHello in pkt:
us = '%s:%s' % (pkt[IP].src, pkt[TCP].sport)
them = '%s:%s' % (pkt[IP].dst, pkt[TCP].dport)
tuple4 = '%s -> %s' % (us, them)
when = datetime.now()
if TLSClientHello in pkt:
hello = pkt[TLSClientHello]
print('%s: %s: TLS Client Hello:' % (when, tuple4))
print(' VERSION 0x%x: %s' % (
hello.version, TLS_VERSIONS.get(hello.version)))
for cipher in hello.ciphers:
print(' CIPHER 0x%04x: %s' % (
cipher, TLS_CIPHER_SUITES.get(cipher)))
print()
if TLSServerHello in pkt:
hello = pkt[TLSServerHello]
print('%s: %s: TLS Server Hello:' % (when, tuple4))
print(' VERSION 0x%x: %s' % (
hello.version, TLS_VERSIONS.get(hello.version)))
print(' CIPHER 0x%04x: %s' % (
hello.cipher, TLS_CIPHER_SUITES.get(hello.cipher)))
print()
def main():
if len(sys.argv) <= 1:
print(
'Listen for TCP packets and report TLS Hello packets.\n'
'Use this to debug SSL/TLS setup problems.\n'
'\n'
'Please supply a pcap-filter expression on the command line,\n'
'like: port 25 and host 1.2.3.4\n',
file=sys.stderr)
sys.exit(1)
pcap_filter = ' '.join(sys.argv[1:])
real_filter = 'tcp and (%s)' % (pcap_filter,)
print('Listening for TCP packets on: %s' % (pcap_filter,))
sniff(prn=traffic_callback, filter=real_filter, store=0)
if __name__ == '__main__':
main()
# vim: set ts=8 sw=4 sts=4 et ai: