基于捕获数据的 SSL 密码套件使用统计

Mik*_*ike 1 web-server tls

是否有基于捕获的数据包构建 SSL 密码使用统计信息的合理方法?

假设我的 Web 服务器支持一组密码,我想知道有多少客户端协商每个密码套件。

gow*_*awr 5

是的。如果您有捕获的数据包,只需从Server Hello 握手数据包中提取协商密码:

密码套件

  The single cipher suite selected by the server from the list in
  ClientHello.cipher_suites.  For resumed sessions, this field is
  the value from the state of the session being resumed.
Run Code Online (Sandbox Code Playgroud)

数据包本身很容易识别,所选的密码位于其中的一个设定位置,很容易解析。因此,捕获所有 SSL 连接的前几个数据包,提取所选的密码,然后您就获得了所需的信息。

这个想法很有趣,我决定尝试一下。通过一点点黑客和削减,我能够改编一个 Python 脚本来做到这一点:

$ ./parser.py random2.pcap | sort -u
TLS 1.0 0x00,0x14
TLS 1.2 0x00,0x2f
TLS 1.2 0x00,0x30
$
Run Code Online (Sandbox Code Playgroud)

有了这些信息,您可以将密码套件 ID 与IANA的TLS 密码套件注册表相关联:

$ ./parser.py random2.pcap  | sort -u | awk '{print $3}' | grep -if - ~/Downloads/tls-parameters-4.csv 
"0x00,0x14",TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA,Y,[RFC4346]
"0x00,0x2F",TLS_RSA_WITH_AES_128_CBC_SHA,Y,[RFC5246]
"0x00,0x30",TLS_DH_DSS_WITH_AES_128_CBC_SHA,Y,[RFC5246]
$ 
Run Code Online (Sandbox Code Playgroud)

这是代码。这是TLS 客户端 Hello 工具的真正精简版,因此如果您想使用它,请考虑回到那里以获得不那么激烈的版本(并记住最初的重点是 Client Hello,而我们关心的是 Server Hello) .

#!/usr/bin/env python
# Hack-and-slash derived from https://github.com/pquerna/tls-client-hello-stats

import os, sys, dpkt
TLS_HANDSHAKE = 22

def pcap_reader(fp):
    return dpkt.pcap.Reader(fp)

def grab_negotiated_ciphers(cap):
    for ts, buf in cap:
        eth = dpkt.ethernet.Ethernet(buf)
        if not isinstance(eth.data, dpkt.ip.IP):
            continue
        ip = eth.data
        if not isinstance(ip.data, dpkt.tcp.TCP):
            continue

        tcp = ip.data
        if (tcp.dport != 443 and tcp.sport != 443) or (len(tcp.data) <= 0) or (ord(tcp.data[0]) != TLS_HANDSHAKE):
            continue

        records = []
        try:
            records, bytes_used = dpkt.ssl.TLSMultiFactory(tcp.data)
        except dpkt.ssl.SSL3Exception, e:
            continue
        except dpkt.dpkt.NeedData, e:
            continue

        if len(records) <= 0:
            continue

        for record in records:
            # TLS handshake only
            if (record.type == 22 and len(record.data) != 0 and ord(record.data[0]) == 2):
                try:
                    handshake = dpkt.ssl.TLSHandshake(record.data)
                except dpkt.dpkt.NeedData, e:
                    continue
                if isinstance(handshake.data, dpkt.ssl.TLSServerHello):
                    ch = handshake.data
                    print '%s\t0x%0.2x,0x%0.2x' %(dpkt.ssl.ssl3_versions_str[ch.version], (ch.cipher_suite&0xff00)>>8, ch.cipher_suite&0xff)
                else:
                    continue

def main(argv):
    if len(argv) != 2:
        print "Tool to grab and print TLS Server Hello cipher_suite"
        print ""
        print "Usage: parser.py <pcap file>"
        print ""
        sys.exit(1)

    with open(argv[1], 'rb') as fp:
        capture = pcap_reader(fp)
        stats = grab_negotiated_ciphers(capture)

if __name__ == "__main__":
    main(sys.argv)
Run Code Online (Sandbox Code Playgroud)