解密 SSL 预主密钥

Decrypt SSL Premaster Key

我正在尝试通过 Wireshark 分析 SSL 3.0 会话。具体来说,我想解密加密的预主密钥。我的理解是pre-master是用证书public密钥加密的。这是基于 SSL RFC 和规范

这是我所做的。我创建了一个测试 public/private 密钥对。私钥是:

-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDCNh5qKloB7BZ2MgP5hWwc4h/LuH+2VRiriCiV3z/NS8NKK3Gy
edZTa0GL1gm6JoDzcnGg/JXLjnqI/aiIok1AwjaJ5LqErWlkYrkV/1iB6x0oViRT
NsN3hagHHi5Z7n3j1/vP+hfMkSiOeyZ3YwxSWOTDDg2hfmVpJ+5n5ysMwwIDAQAB
AoGAMovlVJpbPL7WhcK0uB+aqNXNkrcdPjZdql9QuNkXAPakCEzQVbRSLPnwYFW/
yFw3GaWCn3S+A/G8QKVMVOtxEWKKBkp3BFKmAsf9t3DeNY7lU8JRdF0f290lhanK
aKdej7OuBDFkY7kYhdXj7fq/EcDsRA7l+TD0sjIp7ikA3NkCQQDpfaYnnkayKl5o
/apDP3FjDeiK3n1xTX3SG9LuS37clzRIlIMh5NTxzRDDxE5jbIAXWE3ikI63Kpo1
zXZOkg3HAkEA1O8WfXg7frsMEaXv671570J+T0yjf8m87o4oZpN4F5ni3uC0sn0u
vc5SeHyAhJlQrDqSVDTGq9Ntc+PqEBR5JQJBAJnTFav0Mk8eaqRwucMkAOdpOlKC
0dHbY5EQk546TpG1a9SFQv8JauECYJEYuyv1R04Z9vXUlrFFd+MKQW7x+fECQQCa
OtbSaHzHbVnvin5+BM7GAyaT75HZZFkJfUQ8EAKDLb5K6v1W7x8k8cWAacV4xcfO
B9qnTVa9bDWyJFoL7ZhVAkEAm1j78aFqXKR8BK0Wk9ulmVJJGX6WZUENlBzln/hZ
LXL0G4fT7BVgvJbFWIElf80y8U1cbhH6S5N2kZ7Pb7rJ8w==
-----END RSA PRIVATE KEY-----

我启动了一个 OpenSSL 服务器如下:

openssl s_server -accept 8443 -cert server.crt -key server.key -ssl3 -cipher 'RC4-SHA'

并连接如下:

openssl s_client -connect localhost:993

然后我在 wireshark 中捕获了 SSL 握手并查看了第三个数据包,标记为 "Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message"。 "Client Key Exchange"下面有一段128字节,我理解为加密的pre-master key。在十六进制中,它是:

9601aa2a2768b58af647e03f23e3bdaa5d9ab12d648d85755aaaee430ea273946dec5348aaa61dc261138d50d2c80966b7113a659ad5a3998263e2dc0ce4bba5dbd22d2c6bc5c4c75802b8b130d1b0b39558d5516bbe1a1ce2852a6495e52fda5259626480ec1aede49a085b5f6005a1317cfabf72ff740cd038808e01fb3ac2

我正在尝试在 python 中解密它。考虑到我有私钥,这应该很简单。我执行以下操作:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

handshake = "9601aa2a2768b58af647e03f23e3bdaa5d9ab12d648d85755aaaee430ea273946dec5348aaa61dc261138d50d2c80966b7113a659ad5a3998263e2dc0ce4bba5dbd22d2c6bc5c4c75802b8b130d1b0b39558d5516bbe1a1ce2852a6495e52fda5259626480ec1aede49a085b5f6005a1317cfabf72ff740cd038808e01fb3ac2".decode('hex')

privkey = open('server.key', 'r').read()
rsakey = RSA.importKey(privkey)
rsakey = PKCS1_OAEP.new(rsakey)
decrypted = rsakey.decrypt(handshake)

print decrypted

但是,我得到这个错误:

Traceback (most recent call last):
  File "handshake.py", line 13, in <module>
    decrypted = rsakey.decrypt(handshake)
  File "/usr/lib/python2.7/dist-packages/Crypto/Cipher/PKCS1_OAEP.py", line 227, in decrypt
    raise ValueError("Incorrect decryption.")
ValueError: Incorrect decryption.

为什么我不能解密这个我有私钥。 OpenSSL 做了哪些我没有做的事情?是什么赋予了?感谢任何和所有帮助!

看来您应该使用 PKCS#1 v1.5 填充而不是 OAEP。

这应该有效:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

handshake = "9601aa2a2768b58af647e03f23e3bdaa5d9ab12d648d85755aaaee430ea273946dec5348aaa61dc261138d50d2c80966b7113a659ad5a3998263e2dc0ce4bba5dbd22d2c6bc5c4c75802b8b130d1b0b39558d5516bbe1a1ce2852a6495e52fda5259626480ec1aede49a085b5f6005a1317cfabf72ff740cd038808e01fb3ac2".decode('hex')

privkey = open('server.key', 'r').read()
rsakey = RSA.importKey(privkey)
rsa = PKCS1_v1_5.new(rsakey)
sentinel = 'Failure'
decrypted = rsa.decrypt(handshake, sentinel)

print decrypted.encode('hex')