Skip to content

Add a debug helper for reading traffic with Wireshark #1627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ The tests always log to the console. See the [Logging documentation](https://ssh
### Wireshark

Wireshark is able to dissect initial connection packets, such as key exchange, before encryption happens. Enter "ssh" as the display filter. See https://wiki.wireshark.org/SSH.md for more information.

The Debug build of SSH.NET has helpers to also allow dissection of the encrypted traffic by dumping the session keys in a format that Wireshark understands. Set a value for `SshNetLoggingConfiguration.WiresharkKeyLogFilePath` before connecting, and supply the same value to Wireshark in Edit -> Preferences -> Protocols -> SSH -> "Key log filename".

```c#
using Renci.SshNet;

SshNetLoggingConfiguration.WiresharkKeyLogFilePath = @"C:\tmp\sshkeylogfile.txt";
```
12 changes: 11 additions & 1 deletion src/Renci.SshNet/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private set
/// Gets the client init message.
/// </summary>
/// <value>The client init message.</value>
public Message ClientInitMessage { get; private set; }
public KeyExchangeInitMessage ClientInitMessage { get; private set; }

/// <summary>
/// Gets the server version string.
Expand Down Expand Up @@ -1582,6 +1582,16 @@ internal void OnNewKeysReceived(NewKeysMessage message)
_clientCompression = _keyExchange.CreateCompressor();
_serverDecompression = _keyExchange.CreateDecompressor();

#if DEBUG
if (SshNetLoggingConfiguration.WiresharkKeyLogFilePath is string path
&& _keyExchange is KeyExchange kex)
{
System.IO.File.AppendAllText(
path,
$"{ToHex(ClientInitMessage.Cookie)} SHARED_SECRET {ToHex(kex.SharedKey)}{Environment.NewLine}");
}
#endif

// Dispose of old KeyExchange object as it is no longer needed.
_keyExchange.HostKeyReceived -= KeyExchange_HostKeyReceived;
_keyExchange.Dispose();
Expand Down
12 changes: 12 additions & 0 deletions src/Renci.SshNet/SshNetLoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,17 @@ public static void InitializeLogging(ILoggerFactory loggerFactory)
ThrowHelper.ThrowIfNull(loggerFactory);
LoggerFactory = loggerFactory;
}

#if DEBUG
/// <summary>
/// Gets or sets the path to which to write session secrets which
/// Wireshark can read and use to inspect encrypted traffic.
/// </summary>
/// <remarks>
/// To configure in Wireshark, go to Edit -> Preferences -> Protocols
/// -> SSH and set the same value for "Key log filename".
/// </remarks>
public static string? WiresharkKeyLogFilePath { get; set; }
#endif
}
}