|
| 1 | +#!/usr/bin/env python |
| 2 | +# Hack-and-slash derived from https://github.com/pquerna/tls-client-hello-stats |
| 3 | + |
| 4 | +import os, sys, dpkt |
| 5 | +TLS_HANDSHAKE = 22 |
| 6 | + |
| 7 | +def pcap_reader(fp): |
| 8 | + return dpkt.pcap.Reader(fp) |
| 9 | + |
| 10 | +def grab_negotiated_ciphers(cap): |
| 11 | + for ts, buf in cap: |
| 12 | + eth = dpkt.ethernet.Ethernet(buf) |
| 13 | + if not isinstance(eth.data, dpkt.ip.IP): |
| 14 | + continue |
| 15 | + ip = eth.data |
| 16 | + if not isinstance(ip.data, dpkt.tcp.TCP): |
| 17 | + continue |
| 18 | + |
| 19 | + tcp = ip.data |
| 20 | + if (tcp.dport != 443 and tcp.sport != 443) or (len(tcp.data) <= 0) or (ord(tcp.data[0]) != TLS_HANDSHAKE): |
| 21 | + continue |
| 22 | + |
| 23 | + records = [] |
| 24 | + try: |
| 25 | + records, bytes_used = dpkt.ssl.TLSMultiFactory(tcp.data) |
| 26 | + except dpkt.ssl.SSL3Exception, e: |
| 27 | + continue |
| 28 | + except dpkt.dpkt.NeedData, e: |
| 29 | + continue |
| 30 | + |
| 31 | + if len(records) <= 0: |
| 32 | + continue |
| 33 | + |
| 34 | + for record in records: |
| 35 | + # TLS handshake only |
| 36 | + if (record.type == 22 and len(record.data) != 0 and ord(record.data[0]) == 2): |
| 37 | + try: |
| 38 | + handshake = dpkt.ssl.TLSHandshake(record.data) |
| 39 | + except dpkt.dpkt.NeedData, e: |
| 40 | + continue |
| 41 | + if isinstance(handshake.data, dpkt.ssl.TLSServerHello): |
| 42 | + ch = handshake.data |
| 43 | + print '%s\t0x%0.2x,0x%0.2x' %(dpkt.ssl.ssl3_versions_str[ch.version], (ch.cipher_suite&0xff00)>>8, ch.cipher_suite&0xff) |
| 44 | + else: |
| 45 | + continue |
| 46 | + |
| 47 | +def main(argv): |
| 48 | + if len(argv) != 2: |
| 49 | + print "Tool to grab and print TLS Server Hello cipher_suite" |
| 50 | + print "" |
| 51 | + print "Usage: parser.py <pcap file>" |
| 52 | + print "" |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + with open(argv[1], 'rb') as fp: |
| 56 | + capture = pcap_reader(fp) |
| 57 | + stats = grab_negotiated_ciphers(capture) |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + main(sys.argv) |
0 commit comments