feat: the managing client and sshd's per-source penalties (NetOrk/netork#1800) #63

Merged
christianmanivong merged 1 commits from feat/ssh-penalties-1800 into main 2026-10-31 23:29:09 +00:00
Owner

For NetOrk/netork#1800. OpenSSH 9.8+ (PerSourcePenalties) penalises a source whose connections do not authenticate, and then drops every connection from it. A security scanner's probes earn the managing client exactly that. On netOrk's test server, the post-provision Ansible run of a new Ubuntu 26.04 VM was dropped this way (sshd: penalty of 16 seconds for penalty: connections without attempting authentication).

Added

  • OpenSshServerMixin.exempt_from_ssh_penalties(verify=, log=)
    • Reads the address the host sees the managing client at ($SSH_CONNECTION) and adds it to the PerSourcePenaltyExemptList sshd uses now. The host's own entries stay.
    • Writes it to its own drop-in, /etc/ssh/sshd_config.d/00-netork-penalty-exempt.conf.
    • Writes nothing to an sshd without penalties. OpenSSH 9.2 (Debian 12) rejects the directive (sshd -t rc 255) and would not start. Checked in containers, also against Debian 13's OpenSSH 10.0 (persourcepenaltyexemptlist none by default, first value wins).
    • Does nothing when the address is already exempt, or when sshd_config does not include the drop-in directory. The result says so.
    • Then the same checks as harden_ssh_server: sshd -t, sshd -T shows the address, reload, verify. On any failure the previous state goes back. _apply_or_restore is now shared by both methods (an ignored() callback and a success message).
    • Helpers: parse_penalty_exemptions, client_address, is_exempt, render_penalty_dropin, PENALTY_DROPIN_PATH, SshPenaltyExemption.
  • retry_refused(attempt, waits=, sleep=) / refused_before_banner(exc) (ssh_host_keys)
    • A connection sshd closes before its banner ("Error reading SSH protocol banner", also when wrapped) is tried again after 1, 3, 7 and 15 s, 26 s in all.
    • A failed login, a timeout or a closed port is not retried.
    • HostKeyCheck.connect uses it, so every paramiko driver that connects through it gets it.

Tests

  • tests/test_ssh_penalties.py: parsing against the real Debian 13 output, and the mixin against a fake host that resolves first-value-wins across the main config and the drop-ins. Cases: no penalties, already exempt, no Include, rejected by sshd -t, shadowed by an earlier drop-in, failed new session, no client address.
  • tests/test_ssh_refused.py: the classifier, the backoff, and a real paramiko connect against a TCP server that drops the first two connections the way a penalised source sees it.
  • All 921 tests green. ruff and mypy clean. Coverage of ssh_server 96 %, of ssh_host_keys 98 %.

No version bump: the entries are under Unreleased in the CHANGELOG.

Next

  • napalm-linux wraps its netmiko ConnectHandler in retry_refused.
  • netOrk calls exempt_from_ssh_penalties during a new VM's bootstrap and in the SSH fix.
For NetOrk/netork#1800. OpenSSH 9.8+ (`PerSourcePenalties`) penalises a source whose connections do not authenticate, and then drops every connection from it. A security scanner's probes earn the managing client exactly that. On netOrk's test server, the post-provision Ansible run of a new Ubuntu 26.04 VM was dropped this way (sshd: `penalty of 16 seconds for penalty: connections without attempting authentication`). ## Added - **`OpenSshServerMixin.exempt_from_ssh_penalties(verify=, log=)`** - Reads the address the host sees the managing client at (`$SSH_CONNECTION`) and adds it to the `PerSourcePenaltyExemptList` sshd uses now. The host's own entries stay. - Writes it to its own drop-in, `/etc/ssh/sshd_config.d/00-netork-penalty-exempt.conf`. - **Writes nothing to an sshd without penalties.** OpenSSH 9.2 (Debian 12) rejects the directive (`sshd -t` rc 255) and would not start. Checked in containers, also against Debian 13's OpenSSH 10.0 (`persourcepenaltyexemptlist none` by default, first value wins). - Does nothing when the address is already exempt, or when sshd_config does not include the drop-in directory. The result says so. - Then the same checks as `harden_ssh_server`: `sshd -t`, `sshd -T` shows the address, reload, `verify`. On any failure the previous state goes back. `_apply_or_restore` is now shared by both methods (an `ignored()` callback and a success message). - Helpers: `parse_penalty_exemptions`, `client_address`, `is_exempt`, `render_penalty_dropin`, `PENALTY_DROPIN_PATH`, `SshPenaltyExemption`. - **`retry_refused(attempt, waits=, sleep=)` / `refused_before_banner(exc)`** (`ssh_host_keys`) - A connection sshd closes before its banner ("Error reading SSH protocol banner", also when wrapped) is tried again after 1, 3, 7 and 15 s, 26 s in all. - A failed login, a timeout or a closed port is not retried. - `HostKeyCheck.connect` uses it, so every paramiko driver that connects through it gets it. ## Tests - `tests/test_ssh_penalties.py`: parsing against the real Debian 13 output, and the mixin against a fake host that resolves first-value-wins across the main config and the drop-ins. Cases: no penalties, already exempt, no Include, rejected by `sshd -t`, shadowed by an earlier drop-in, failed new session, no client address. - `tests/test_ssh_refused.py`: the classifier, the backoff, and a real paramiko connect against a TCP server that drops the first two connections the way a penalised source sees it. - All 921 tests green. ruff and mypy clean. Coverage of `ssh_server` 96 %, of `ssh_host_keys` 98 %. No version bump: the entries are under Unreleased in the CHANGELOG. ## Next - napalm-linux wraps its netmiko `ConnectHandler` in `retry_refused`. - netOrk calls `exempt_from_ssh_penalties` during a new VM's bootstrap and in the SSH fix.
christianmanivong added 1 commit 2026-10-31 23:24:29 +00:00
feat: the managing client and sshd's per-source penalties (NetOrk/netork#1800)
driver / standard (pull_request) Failing after 9s
driver / lint (pull_request) Successful in 22s
driver / types (pull_request) Successful in 1m14s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / test (3.13) (pull_request) Successful in 1m7s
driver / lowest (pull_request) Successful in 33s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m4s
CI / driver (pull_request) Failing after 2m24s
driver / publish (pull_request) Skipped
5fffb3b886
OpenSSH 9.8+ penalises a source whose connections do not authenticate --
a scanner's probes -- and then drops every connection from it, the
managing client's included.

- OpenSshServerMixin.exempt_from_ssh_penalties puts the address the host
  sees the client at ($SSH_CONNECTION) into PerSourcePenaltyExemptList,
  next to the host's own exemptions, in its own drop-in. Only where sshd
  has the penalties (an older one refuses the directive) and includes the
  drop-in directory; checked with sshd -t, sshd -T and a new session, and
  taken back on any failure. The check-and-restore step of
  harden_ssh_server is shared for it.
- retry_refused tries a connection again after 1, 3, 7 and 15 s while
  sshd closes it before its banner; HostKeyCheck.connect uses it.
christianmanivong merged commit 28ff14589b into main 2026-10-31 23:29:09 +00:00
christianmanivong deleted branch feat/ssh-penalties-1800 2026-10-31 23:29:09 +00:00
Sign in to join this conversation.