解密SSL Premaster密钥

时间:2015-03-26 17:30:27

标签: python ssl encryption cryptography

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

这就是我的所作所为。我创建了一个测试公钥/私钥对。私钥是:

-----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握手并查看了标记为“客户端密钥交换,更改密码规范,加密握手消息”的第三个数据包。在“客户端密钥交换”下,有一个128字节的部分,我理解为加密的预主密钥。在十六进制中,它是:

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做了什么,我没做什么?是什么赋予了?任何和所有的帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您似乎应该使用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')