解码 Scapy ASN1 编码的 SSL/TLS 证书字段

Decoding Scapy ASN1 encoded SSL/TLS certificate fields

我正在使用我使用 pip 安装的 scapy-ssl_tls 库从 serverhello 数据包中提取 SSL/TLS 证书字段。

问题是,我无法找到从 ASN1 编码字段中提取值的方法:

sign_algo2:  <ASN1_OID['.1.2.840.113549.1.1.11']>
sa2_value:  <ASN1_NULL[0L]>
not_before:  <ASN1_UTC_TIME['170321131500Z']>
not_after:  <ASN1_UTC_TIME['200321131500Z']>
pubkey_algo:  <ASN1_OID['.1.2.840.113549.1.1.1']>
version:  <ASN1_INTEGER[2L]>
sn:  <ASN1_INTEGER[6348220899422160075L]>
sign_algo:  <ASN1_OID['.1.2.840.113549.1.1.11']>
pubkey:  <ASN1_BIT_STRING['\x000\x']>

我已经挖掘出 scapy.layers.ssl_tls、ssl_tls_crypto、scapy.layers.x509 模块,但无法获得任何解码提示。我也尝试使用 asn1crypto.core 包中的 Asn1Value.load() 但它失败并出现以下错误:

TypeError: encoded_data must be a byte string, not scapy.asn1.asn1.ASN1_UTC_TIME

如果有人能帮助我最好使用 scapy 的本机解码或任何其他可能的方式解决这个问题,那就太好了。

注意:请注意,我必须从 SSL/TLS serverhello 数据包中提取这些值,我正在从 pcap 文件中读取这些数据包,因为我还需要数据包 headers 中的其他字段。我知道 Whosebug 或 wireshark/tshark 上存在许多解决方案,用于来自 .pem 文件或可能来自 .der 文件的 extracting/reading 证书(在明确地从 wireshark 导出之后),它们在我的情况下不起作用,因为我需要一个解决方案来解决从数据包中提取证书或证书字段的问题。

确保您传递的是字节字符串 asn1crypto,而不是某些内部 scapy 对象。可能您需要将后者转换为字节字符串。

或者,this tool 旨在将 X.509 证书解码为 Python 个对象的树。您还需要为它提供一个字符串 (Python2) 或字节 (Python 3).

如果您有 X.509 证书的 DER 编码字节字符串,使用 asn1crypto 的正确方法是:

from asn1crypto import x509

cert = x509.Certificate.load(der_byte_string)
print(cert.native)

从错误消息看来,您已尝试传递一些代表 ASN.1 值的 Python 对象。

这已在 scapy-ssl_tls issue #116 中讨论,其中描述了 scapy ASN.1 字段的基本处理:

...
# resp holds the raw socket response to a client_hello

tls_response = TLS(resp)
tls_response.show()  # show the structure

# iterate all certificates
for cert in tls_response[TLSCertificateList].payload.certificates:
    # .payload as the structure is [...][TLSCertificateList][TLS10Certificate].certificates = [x509Cert,x509Cert,...]
    # we'll have a TLSCertificateList object at this point; get the scapy X509Cert Object
    tlscert = cert[X509Cert]
    print repr(str(tlscert.sign_algo)) # raw bytes -> '\x06\t*\x86H\x86\xf7\r\x01\x01\x0b'
    print repr(tlscert.sign_algo) # <ASN1_OID['.1.2.840.113549.1.1.11']>
    print tlscert.sign_algo.val # 1.2.840.113549.1.1.11
    print repr(tlscert.version) # <ASN1_INTEGER[2L]>
    print tlscert.version.val # 2