hihttps教你在Wireshark中提取旁路https解密源码

大家好,我是hihttps,专注SSL web安全研究,今天本文就是教大家怎样从wireshark源码中,提取旁路https解密的源码,非常值得学习和商业应用。

 

一、旁路https解密条件

众所周知,都知道wireshark中设置一定的条件,可以解密出https的通信成明文。

https是加密传输的,旁路一般情况是无法解密的,但为什么服务器和客户端可以解密成明文呢,那就是双方都有密钥。所以旁路https解密的条件是:

1、知道服务端的私钥。(如RSA静态密钥配置)

2、知道客户端浏览器的密钥。(如chrome的开发者模式会把密钥存到文件)

如何知道和设置密钥,请大家在百度搜索相关文章。如https://blog.csdn.net/wangyiyungw/article/details/82178339

 

二、wiresharkSSL解密密钥计算流程

1、通过抓包ClientHelloServerHello报文得到两边随机数client_randomserver_random

2、客户端将permaster 通过Sever端证书公钥密钥加密后,发送给服务器,Server用私钥的解密得到解密后的permaster

3、Client端和Server端分别根据client_randomserver_randompermaster_secret生成master secret就是能解密正常报文。

 

三、代码查找 

首先在wireshark官网下载源码,解压到本地硬盘,进入...\epan\dissectors目录,搜索带有“ssl”关键字的.c文件,找到packet-ssl-utils.cpacket-ssl.c,核心的解密算法全部在packet-ssl-utils.c这个文件里面。哈哈,加密解密虽然复杂,但wireshark用了libgcrypt这个库,事半功倍,我们来欣赏一下核心函数。

1packet-ssl-utils.h几个重要的结构体,用来存储SSL会话相关解密信息。

/* SSL 对话信息结构体 */

typedef struct _SslDecryptSession {

    guchar _master_secret[SSL_MASTER_SECRET_LENGTH]; //非常重要的解密密钥

    guchar _session_id[256]; //sesstion id

    guchar _client_random[32]; //客户端随机数

    guchar _server_random[32];//服务端随机数

    StringInfo session_id;//session id 用于ssl/tls 快速握手,不必每次计算密钥

    StringInfo session_ticket;//session ticket,用于ssl/tls 快速握手,不必每次计算密钥

    StringInfo server_random;//服务端随机数

    StringInfo client_random;//客户端及随机数

    StringInfo master_secret;//主解密密钥

    StringInfo handshake_data;//握手报文

    /* the data store for this StringInfo must be allocated explicitly with a capture lifetime scope */

    StringInfo pre_master_secret;//pre_master_secret

    guchar _server_data_for_iv[24];//服务端解密向量

    StringInfo server_data_for_iv;

    guchar _client_data_for_iv[24];//可客户端解密向量

    StringInfo client_data_for_iv;

 

    gint state;//状态

    const SslCipherSuite *cipher_suite;//加密套件

    SslDecoder *server;//服务端解密结构体指针

    SslDecoder *client;//肯定解密结构体指针

    SslDecoder *server_new;

    SslDecoder *client_new;

#if defined(HAVE_LIBGNUTLS)

    gcry_sexp_t private_key;

#endif

    StringInfo psk;

    StringInfo app_data_segment;

    SslSession session;

    gboolean   has_early_data;

 

} SslDecryptSession;

 

 

 

2packet-ssl-utils.c里面的几个重要的函数:

 

//HMAC消息摘要信息计算

ssl_hmac/md_init(SSL_HMAC* md, const void * key, gint len, gint algo)

ssl_hmac/md_update(SSL_HMAC* md, const void* data, gint len)

ssl_hmac/md_final(SSL_HMAC* md, guchar* data, guint* datalen)

ssl_hmac/md_cleanup(SSL_HMAC* md)

 

// sha/md5计算

ssl_sha/md5_init(SSL_SHA_CTX* md)

ssl_sha/md5_update(SSL_SHA_CTX* md, guchar* data, gint len)

ssl_sha/md5_final(guchar* buf, SSL_SHA_CTX* md)

ssl_sha/md5_cleanup(SSL_SHA_CTX* md)

 

//加载解密私钥和证书

ssl_private_decrypt(const guint len, guchar* data, gcry_sexp_t pk)

ssl_load_keyfile(const gchar *ssl_keylog_filename, FILE **keylog_file,

                 const ssl_master_key_map_t *mk_map)

 

//验证HMAC和计算伪随机数

tls_hash(StringInfo *secret, StringInfo *seed, gint md,

         StringInfo *out, guint out_len)

tls12_prf(gint md, StringInfo* secret, const gchar* usage,

          StringInfo* rnd1, StringInfo* rnd2, StringInfo* out, guint out_len)

 

//计算pre_master_secret

ssl_generate_pre_master_secret(SslDecryptSession *ssl_session,

                               guint32 length, tvbuff_t *tvb, guint32 offset,

                               const gchar *ssl_psk,

                               const ssl_master_key_map_t *mk_map)

//计算master_secret

ssl_generate_keyring_material(SslDecryptSession*ssl_session)

 

.........

 

 

3、解密核心入口函数,输入密文,输出明文

ssl_decrypt_record(SslDecryptSession *ssl, SslDecoder *decoder, guint8 ct, guint16 record_version,

        gboolean ignore_mac_failed,

        const guchar *in, guint16 inl, StringInfo *comp_str, StringInfo *out_str, guint *outl)

{

tls_decrypt_aead_record...

ssl_cipher_suite_dig...

ssl_cipher_decrypt...

tls_check_mac...

 

}
  代码很长,就不贴出来来,大家自行去下wireshark源码吧,全部的解密过程都在packet-ssl-utils.c这个文件中。

 

  总体说来:旁路解密https涉及到密码学的很多东西,没这方面的基础,看不懂也很正常。好在有很多开源大神贡献了源码,我们只需要复制拷贝,掌握关键的函数入口,就可以成功抽丝剥茧,把核心的源码剥离出来了,有需要的可以访问https://github.com/qq4108863联系获取。 

  所以如果任意一方泄漏了密钥,HTTPS也是不安全的,并不能阻止被攻击。如果有网站,可以在github上下载一个SSL web应用防火墙,如hihttps是开源免费的。

 

posted @ 2019-09-30 15:37  hihttps  阅读(1769)  评论(0编辑  收藏  举报