【问题标题】:.NET SslStream: How to extract session key?.NET SslStream:如何提取会话密钥?
【发布时间】:2020-04-15 21:09:40
【问题描述】:

我正在编写一个桌面应用程序,并希望让用户能够验证网络流量,以便他们知道自己没有被滥用。我的应用程序使用.NET's SslStreamAuthenticateAsClient 方法建立到服务器的TLS 连接。 Wireshark 用户可以使用NSS key logs 解码 TLS 流量。我可以看到 Firefox 和 Chrome 都有记录加密密钥的选项。如何在我的 .NET 应用程序中做同样的事情?即如何以编程方式从SslStream 中提取会话密钥?

【问题讨论】:

  • 我认为您必须使用提供的证书解密 http 正文并检查它stackoverflow.com/questions/44705282/…
  • 没有。我需要 Diffie-Hellman 算法返回的临时密钥,否则 Wireshark 无法解码流量。

标签: c# .net ssl .net-core sslstream


【解决方案1】:

在撰写本文时,dotnet 的SslStream 无法做到这一点。以下是使用BouncyCastle 导出会话密钥的方法:

internal static class BouncyCastleTls
{
    public static Stream WrapWithTls(Stream stream)
    {
        var client = new MyTlsClient();
        var tlsClientProtocol = new TlsClientProtocol(stream, new SecureRandom());
        tlsClientProtocol.Connect(client);
        return tlsClientProtocol.Stream;
    }
}

internal sealed class MyTlsClient : DefaultTlsClient
{
    public override TlsAuthentication GetAuthentication()
    {
        return new MyTlsAuthentication();
    }

    public override void NotifyHandshakeComplete()
    {
        var clientRandom = mContext.SecurityParameters.ClientRandom;
        var masterSecret = mContext.SecurityParameters.MasterSecret;

        Console.WriteLine("CLIENT_RANDOM {0} {1}", ToHex(clientRandom), ToHex(masterSecret));
    }

    private static string ToHex(byte[] bytes)
    {
        var sb = new StringBuilder(bytes.Length * 2);
        for (var i = 0; i < bytes.Length; ++i)
            sb.Append($"{bytes[i]:x2}");
        return sb.ToString();
    }

}

internal sealed class MyTlsAuthentication : TlsAuthentication
{
    public void NotifyServerCertificate(Certificate serverCertificate)
    {
    }

    public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest)
    {
        return null;
    }
}

【讨论】:

  • 有没有办法将它与HttpClient 结合起来?
猜你喜欢
  • 2021-11-25
  • 1970-01-01
  • 2018-03-16
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 2017-02-14
  • 2018-03-17
相关资源
最近更新 更多