小贴士:SSL协议的定义
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。
正文
前不久从网上得到一个https会话数据包文件https.pcap,用32位的wireshark打开(64位的会增加逆向难度,因为用到寄存器传递参数)
如图:
就这么多数据包,非常适合用来研究ssl协议的实现。 wireshark只要导入密钥是可以自动解密出明文的。
点开Edit菜单下的Preferences项,再点开左侧的Protocols里面的SSL,新建一个SSL解密任务。
确定之后,就可以看到明文数据了。
原来的第24帧就多了一个http协议块,25帧也是如此,这就是wireshark的ssl解密功能。
那么它是怎么实现的呢?这需要调试才能搞清楚。
我调试的版本是Wireshark_win32_V1.12.4_setup.1427188207.exe 大小22.4 MB (23,588,136 bytes)
费了九牛二虎之力定位到了libwireshark.dll文件,所有的ssl加解密都是在这模块中实现的。
贴几张调试截图,这些断点都是比较关键的。
通过调试,终于知道了ssl解密的算法,写了个python脚本,实现了wireshark的ssl解密核心算法。
结果如图:
Default
1 2 3 4 5 6 7 8 9 10 11 12 |
-----BEGIN RSA PRIVATE KEY----- MIIBywIBAAJhAMrZhFV8l+A5Qxoiatcn8MbUPvPUGEafGzdQSbIphD7p+Dsfl3OK wnT19h9AHyHxkT5LZLsxtVo405jA3+0AsTkvCIlxHESzWeeXbGF/zHNPBuPpXCZH YJG1L0YueUE9tQIDAQABAmB0DeSHYEQoNbqtXhmQRTqdFtt5dtP4u5i/mcDAHL6b nBK4CMgGg9HjRsFseawWKHTyjKYQwbl+Xh/66VclzgxrAxw+GIsXGHp5OzIsxABM Vo52ybJYVC6iotbs1GL/9AECMQDuvm3SPOfpnA4iSf7MRBjDSvdOQYv6cUw3kYKE FKsY8y/X4JMGKkmwMCJcyEX5mrUCMQDZgux7RA4oadJTXlH5G6zD6266BC4Qbm+H XD0X5T22X//W5OmjYITOYPg9dU3X9wECMQCnEe/8Xc7U9fYWHL4H5+eEUuO5ibkR K1Pw1w0ErQoGzbe/VFLOz6z9dNG3KBd/0rkCMQCXWi353DJJ1tDe6Bv8TlCah+Gl mLEBCAedVgbA8OhPVl+tBd65q7jd7sXt5glDxQECMGPaTUJkasmL/oHWpol6MdKQ dntcO36IGfmwHw6H2TJLFpeozkoCUIj7+MWl4ZXaag== -----END RSA PRIVATE KEY----- |
大致脚本如下:
Default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import hashlib import hmac from Crypto.Cipher import AES def calc_n_d(p,q): ... return N,d def calc_master_secret(pre_master_secret,hash_seed): .... return master_secret def key_expansion(master_secret,hash_seed): .... return sess_keys RSA_Encrypted_PreMaster_Secret=0x32b350bd547fcb7d916ee0264549d76c82e9a996f0a3787e077e77f2484ed82cc137a14e7816ef8ec7a509a882af0e8659c077e365519a706fb5324470a0a222e2d57ccf6d6b48350102651cce155ad76b2717dcf83e0dd60b5bef9f0a9d3870 p=0xeebe6dd23ce7e99c0e2249fecc4418c34af74e418bfa714c3791828414ab18f32fd7e093062a49b030225cc845f99ab5 q=0xd982ec7b440e2869d2535e51f91bacc3eb6eba042e106e6f875c3d17e53db65fffd6e4e9a36084ce60f83d754dd7f701 n,d=calc_n_d(p,q) pre_master_secret =hex(pow(RSA_Encrypted_PreMaster_Secret, d, n)).rstrip("L")[-96:].decode('hex') client_random=[0x4b,0x99,0x46,0xaf,0xd7,0x08,0x3c,0xa9,0x12,0xb1,0xd1,0x57,0x1c,0xfe,0x5c,0x37,0xdc,0xc2,0xa1,0xcc,0x5a,0x12,0x4d,0x38,0x95,0x76,0x79,0x06,0x89,0xfe,0xdd,0xf5] server_random=[0x4b,0x99,0x46,0xaf,0xb1,0xb2,0x1c,0xb5,0xf2,0x30,0x88,0x70,0x4f,0xbe,0x6d,0xb5,0x62,0xde,0x5c,0xd1,0x64,0xec,0xd1,0x98,0x0e,0xc5,0x10,0x02,0x0c,0x30,0x28,0x69] hash_seed='master secret'+''.join([ chr(n) for n in client_random])+''.join([ chr(n) for n in server_random]) master_secret = calc_master_secret(pre_master_secret,hash_seed) hash_seed='key expansion'+''.join([ chr(n) for n in server_random])+''.join([ chr(n) for n in client_random]) sess_keys=key_expansion(master_secret,hash_seed) ..... print 'Client_MAC_key: ' + Client_MAC_key.encode('hex') print 'Server_MAC_key: ' + Server_MAC_key.encode('hex') print 'Client_Write_key ' + Client_Write_key.encode('hex') print 'Server_Write_key: ' + Server_Write_key.encode('hex') print 'Client_Write_IV: ' + Client_Write_IV.encode('hex') print 'Server_Write_IV: ' + Server_Write_IV.encode('hex') _Server_Write_IV=''.join(map(chr,[0xe6,0x0b,0x07,0x2c,0x87,0x5c,0x97,0x36,0xa2,0x00, 0x6c,0x7c,0xfe,0x50,0x9d,0x33])) Application_Data='f497d7d8bca3533967206fb9ba08b483082cbc48445b9bebf8db05bc7a10ea197bfe6a1381842169bba7e92e23c5380db9f0005d363732920044a61871fcd8bbcba468eb9e7a4cf009e8b3add198359873ea3892f898768ace4d3ca471a8e6a36ecd6fb907d35cf514224ce2494cb58a9c703ade20463c08845098664c92fc17a73614213b926eb17497b0220d8064c8b731767b00fa096a4dd43fb392cb48e14172f89e9104b44dc70ce68b383bc7f9'.decode('hex') aes = AES.new(Server_Write_key, AES.MODE_CBC,_Server_Write_IV) plain = aes.decrypt(Application_Data) print '\n'+plain+'\n' print plain.encode('hex') |
上面的RSA_Encrypted_PreMaster_Secret、client_random、server_random、_Server_Write_IV、Application_Data都是用wireshark从https.pcap文件中抠出来的。
简单的科普一下,水平有限!
https://www.91ri.org/15396.html