-
Notifications
You must be signed in to change notification settings - Fork 114
Enforce LoginGraceTime in wolfsshd on Windows and make the grace flag per connection #1028
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
Open
yosuke-wolfssl
wants to merge
1
commit into
wolfSSL:master
Choose a base branch
from
yosuke-wolfssl:fix/f_5577
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−43
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| #!/usr/bin/env pwsh | ||
| # | ||
| # Windows regression test for wolfsshd LoginGraceTime enforcement. | ||
| # | ||
| # Opens a raw TCP connection that never authenticates and verifies that | ||
| # wolfsshd drops it at the login grace deadline. No Windows user account or | ||
| # authorized key is required, because the connection is closed before | ||
| # authentication ever completes - this exercises the pre-auth grace timer only. | ||
| # | ||
| # Enforcement is checked behaviorally (the server closes the connection at the | ||
| # grace deadline), not by reading the daemon log, so the test does not depend on | ||
| # debug logging being compiled into the wolfSSH Windows build. | ||
| # | ||
| # Usage: | ||
| # pwsh sshd_login_grace_test.ps1 -SshdExe <path-to-wolfsshd.exe> [-Port N] [-Grace N] | ||
| # (SshdExe also accepts the SSHD_PATH environment variable.) | ||
|
|
||
| param( | ||
| [string]$SshdExe = $env:SSHD_PATH, | ||
| [int]$Port = 22224, | ||
| [int]$Grace = 5 | ||
| ) | ||
|
|
||
| $ErrorActionPreference = "Stop" | ||
| $exitCode = 1 | ||
|
|
||
| $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path | ||
| $repoRoot = (Resolve-Path (Join-Path $scriptDir "..\..\..")).Path | ||
| $keyPath = (Resolve-Path (Join-Path $repoRoot "keys\server-key.pem")).Path | ||
| $confFile = Join-Path $scriptDir "sshd_config_test_login_grace" | ||
| $authFile = Join-Path $scriptDir "authorized_keys_test" | ||
|
|
||
| if (-not $SshdExe -or -not (Test-Path $SshdExe)) { | ||
| Write-Host "ERROR: wolfsshd.exe not found (pass -SshdExe or set SSHD_PATH)" | ||
| exit 1 | ||
| } | ||
|
|
||
| @" | ||
| Port $Port | ||
| Protocol 2 | ||
| LoginGraceTime $Grace | ||
| PermitRootLogin yes | ||
| PasswordAuthentication yes | ||
| PermitEmptyPasswords no | ||
| UseDNS no | ||
| HostKey $keyPath | ||
| AuthorizedKeysFile $authFile | ||
| "@ | Out-File -FilePath $confFile -Encoding ASCII | ||
|
|
||
| "" | Out-File -FilePath $authFile -Encoding ASCII | ||
|
|
||
| # Run wolfsshd in the foreground (-D selects the non-service path on Windows). | ||
| $sshd = Start-Process -FilePath $SshdExe ` | ||
| -ArgumentList "-D", "-f", "`"$confFile`"", "-p", "$Port" ` | ||
| -NoNewWindow -PassThru | ||
|
|
||
| try { | ||
| # Wait for the listener to accept connections. | ||
| $up = $false | ||
| for ($i = 0; $i -lt 20; $i++) { | ||
| try { | ||
| $probe = New-Object System.Net.Sockets.TcpClient | ||
| $probe.Connect("127.0.0.1", $Port) | ||
| $probe.Close() | ||
| $up = $true | ||
| break | ||
| } | ||
| catch { | ||
| Start-Sleep -Milliseconds 500 | ||
| } | ||
| } | ||
| if (-not $up) { | ||
| # throw rather than exit so the finally block still stops the daemon | ||
| throw "wolfsshd did not start listening on port $Port" | ||
| } | ||
|
|
||
| # Open a raw TCP connection and never authenticate. The server sends its | ||
| # banner, waits for ours, and must close the connection once the login grace | ||
| # time expires. Block on Read (with a timeout well past the grace time) and | ||
| # measure when the server closes the connection. | ||
| $stall = New-Object System.Net.Sockets.TcpClient | ||
| $stall.Connect("127.0.0.1", $Port) | ||
| $stream = $stall.GetStream() | ||
| $stream.ReadTimeout = ($Grace + 5) * 1000 | ||
|
|
||
| $buf = New-Object byte[] 4096 | ||
| $dropped = $false | ||
| $sw = [System.Diagnostics.Stopwatch]::StartNew() | ||
| try { | ||
| while ($true) { | ||
| $n = $stream.Read($buf, 0, $buf.Length) | ||
| if ($n -le 0) { | ||
| $dropped = $true # server closed the connection | ||
| break | ||
| } | ||
| # otherwise the server sent its banner; keep waiting for the drop | ||
| } | ||
| } | ||
| catch [System.IO.IOException] { | ||
| # A server close can surface as a reset (IOException), not a graceful | ||
| # EOF. Decide by elapsed time like the .sh test: a throw before the read | ||
| # timeout means the server closed the connection. | ||
| $dropped = $sw.Elapsed.TotalSeconds -lt ($Grace + 4) | ||
| } | ||
| $elapsed = [math]::Round($sw.Elapsed.TotalSeconds, 1) | ||
| $stall.Close() | ||
|
|
||
| Write-Host "connection closed=$dropped after ${elapsed}s (grace=$Grace)" | ||
|
|
||
| if ($dropped -and ($elapsed -ge ($Grace - 1)) -and ($elapsed -le ($Grace + 4))) { | ||
| Write-Host "PASS: unauthenticated connection dropped at login grace deadline" | ||
| $exitCode = 0 | ||
| } | ||
| elseif ($dropped) { | ||
| Write-Host "FAIL: connection closed at ${elapsed}s, not near the grace deadline ($Grace s)" | ||
| } | ||
| else { | ||
| Write-Host "FAIL: connection still open past the grace time (not enforced)" | ||
| } | ||
| } | ||
| catch { | ||
| Write-Host "ERROR: $_" | ||
| $exitCode = 1 | ||
| } | ||
| finally { | ||
| if ($sshd -and -not $sshd.HasExited) { | ||
| Stop-Process -Id $sshd.Id -Force -ErrorAction SilentlyContinue | ||
| } | ||
| } | ||
|
|
||
| exit $exitCode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.