Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 解码Scapy ASN1编码的SSL/TLS证书字段_Python_Ssl_Scapy_Asn.1 - Fatal编程技术网

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

Python 解码Scapy ASN1编码的SSL/TLS证书字段,python,ssl,scapy,asn.1,Python,Ssl,Scapy,Asn.1,我使用scapy-SSL_TLS库从serverhello数据包中提取SSL/TLS证书字段,我使用pip安装了该库 问题是,我无法找到从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_U

我使用scapy-SSL_TLS库从serverhello数据包中提取SSL/TLS证书字段,我使用pip安装了该库

问题是,我无法找到从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的原生解码或任何其他可能的方式来解决这个问题,那就太好了


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

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


或者,设计用于将X.509证书解码到Python对象树中。您还需要向其提供字符串(Python2)或字节(Python3)。

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

from asn1crypto import x509

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

从错误消息中可以看出,您试图传递一些表示ASN.1值的Python对象。

这一点已经讨论过,其中描述了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
。。。
#resp保存对客户端的原始套接字响应
tls_响应=tls(响应)
tls_response.show()#显示结构
#迭代所有证书
对于tls_响应[TLSCertificateList]中的证书。有效负载。证书:
#。有效负载,因为结构为[…][TLSCertificateList][TLS10Certificate]。证书=[x509Cert,x509Cert,…]
#此时我们将有一个TLSCertificateList对象;获取scapy X509Cert对象
tlscert=cert[X509Cert]
打印报告(str(tlscert.sign\u algo))#原始字节->'\x06\t*\x86H\x86\xf7\r\x01\x01\x0b'
打印报告(签名算法)
打印tlscert.sign_algo.val#1.2.840.113549.1.1.11
打印报告(tlscert.version)#
打印tlscert.version.val#2
当您收到错误消息时,您确切地输入了什么命令?
...
# 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