【问题标题】:Decoding Scapy ASN1 encoded SSL/TLS certificate fields解码 Scapy ASN1 编码的 SSL/TLS 证书字段
【发布时间】:2018-01-26 11:04:10
【问题描述】:

我正在使用我使用 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 的本机解码或任何其他可能的方式解决这个问题,那就太好了。

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

【问题讨论】:

  • 收到错误信息时准确输入了什么命令?

标签: python ssl scapy asn.1


【解决方案1】:

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

from asn1crypto import x509

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      这已在 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-17
        相关资源
        最近更新 更多