lastUpdate

My Technology View

Subscribe

sshknock: Single Packet Authorization for SSH, with no exposed ports

Any server with port 22 reachable from the Internet gets scanned within minutes. Noisy login attempts can be contained with fail2ban and key-only authentication, but the port stays visible: anyone can talk to sshd and try to exploit a vulnerability, should one exist. sshknock starts from a different idea: if the port is closed to everyone, there is nothing to scan and nothing to attack.

sshknock in actionClient203.0.113.7SSH key / ssh-agentScanner / attacker198.51.100.7Serversshknockdreads frames (AF_PACKET)no listening portFirewalltcp/22 closedtcp/22 openset: emptyset: 203.0.113.7UDP: no rulesshdport 22ESTABLISHED✗✗✗✗signsigned UDPopenSSH connection (TCP/22)Idle: tcp/22 closed, no UDP port listening. Scanners get no answer at all.1 · The client signs {user, host, timestamp, nonce} with its own SSH key.2 · A single UDP packet: sshknockd reads it from the interface and checks signature and sshd policy.3 · Valid signature: the client’s IP enters the set (max 60 s) and gets “open”.4 · The client connects over SSH: tcp/22 is open only for its IP, everyone else stays blocked.5 · ESTABLISHED connection: the IP leaves the set, the port closes again and the session stays up.

The project is open source (BSD 3-Clause license) and the code is at github.com/axiom4/sshknock.

The idea: Single Packet Authorization

sshknock implements a Single Packet Authorization (SPA) for SSH. Port 22 stays closed to everyone. When the client sends a single signed UDP packet, the daemon on the server verifies the signature and opens the port only for the client’s IP. The window closes at the first connection and in any case after at most 60 seconds. Anyone without an authorized key gets no reply at all.

sshknock in action: manual knock, --ssh-after, automatic knock from ~/.ssh/config, and RHEL with firewalld.

Why it is not ordinary port knocking

  • No new secrets. The packet is signed with ssh-keygen -Y sign, so with the same SSH key you already use, even through ssh-agent. The server verifies the signature against the authorized_keys that sshd already reads (resolved via sshd -T). Certificates, cert-authority, from= and expiry-time are supported.
  • The same policy as sshd. Before opening anything, the daemon queries sshd -T -C user=...,addr=...,laddr=...,lport=.... It therefore honors Match blocks, AllowUsers/DenyUsers, AllowGroups/DenyGroups, PermitRootLogin, nologin shells and expired accounts: a knock cannot open access that SSH would refuse anyway.
  • No listening port. The daemon captures packets with an AF_PACKET socket, as tcpdump would, before netfilter. There is no UDP socket to scan. The confirmation is sent through a raw socket only after a valid signature.

How it works

  1. The client signs a message containing the user, the target host, an optional source IP, a timestamp and a nonce, and sends it over UDP.
  2. The daemon checks the timestamp, host, source and nonce.
  3. It verifies the signature against authorized_keys and checks the sshd policy.
  4. It adds the IP to the firewall set for 60 seconds and replies “open”.
  5. The client opens the SSH connection. As soon as conntrack sees it as ESTABLISHED, the IP is removed from the set.
Access flow with sshknockone signed UDP packet, then the regular SSH connectionClientsshknockdFirewallsshdSign {user, host, [IP], timestamp, nonce}with ssh-keygen -Y sign (ssh-agent works)1Signed UDP → port 22no listener, no firewall rule2AF_PACKET capture (frame copy, like tcpdump)Checks timestamp ±20 s, host, nonceSignature vs authorized_keyssshd policy (sshd -T -C user=…,addr=…)3Adds the IP to the set (60 s)4“open” (raw socket)only after a valid signature5SSH connection, TCP/22 only for the IP in the set6conntrack ESTABLISHED → IP removed from the set7If the signature is invalid the server stays silent: to a scanner, the port is simply closed.

No UDP port open, no daemon listening

This is what sets sshknock apart from almost every knocking solution. On the server there is no firewall rule that opens a UDP port and no process listening on UDP: ss -ulpn shows nothing from sshknock, and a scan from the outside sees port 22 closed and no UDP port open.

The knock packet is observed, not received. The daemon opens an AF_PACKET socket, the same capture mechanism that tcpdump and libpcap rely on, and reads frames straight from the network interface, before the firewall processes them. To the kernel, the UDP packet is a datagram addressed to a closed port and it is dropped like any other: no application receives it and no rule had to accept it. Meanwhile a copy has already been read by the daemon, which verifies its signature.

State of port 22 over timethe window opens only for the client’s IP and closes by itself1 · Idletcp/22 → DROP for everyoneset sshknock_allow: emptyno UDP port open2 · Valid knockclient IP in the settcp/22 open only for itexpires after max 60 s3 · ConnectedESTABLISHED sessionIP removed from the setthe session stays active4 · Closed againnew connections: DROPa new knock is neededempty setA valid knock never creates permanent access: sshd’s policy stays exactly as it was.

The practical consequences are three:

  • Zero network attack surface. With no listening socket there is no service to discover, probe or crash with malformed packets.
  • Total silence toward strangers. Probes get exactly the behavior of a closed port. Only after a valid signature does the daemon send the confirmation, through a raw socket.
  • Firewall unchanged. The only change to the rules is the temporary addition of the client’s IP to an existing set, and only for the SSH port, never for UDP.

A detail for the pedantic: sshknock does not use the libpcap library. It reads frames directly from the AF_PACKET socket using only the Python standard library, so it stays one file per side with no extra dependencies.

Where the knock packet is readthe daemon observes frames on the interface: no UDP socket is listeningInternetscanner or clientwith an authorized keyNetwork cardincoming frames(any interface)Network stack + firewalltcp/22: only IPs in the set, otherwise DROPUDP: datagram to a closed port → droppedno application receives it✗ no service to discover or attackframe copysshknockdread-only AF_PACKET socketsshknock user · CAP_NET_RAWsignature + sshd policy checked hereFirewallIP added to the settcp/22 for 60 s(iptables · nft · firewalld)opens tcp/22 for the IPSame principle as tcpdump/libpcap: the frame is read from the interface before the firewall, without opening any port.

Security by design

Anti-replay. The timestamp is accepted with a ±20 second tolerance, the nonce is persisted in /var/lib/sshknock, the target host is signed and, with --src-ip, so is the source IP (with auto the client discovers it via STUN, useful behind NAT).

Least privilege. The daemon runs as the sshknock system user with only the CAP_NET_RAW, CAP_NET_ADMIN and CAP_DAC_READ_SEARCH capabilities, inside the systemd sandbox. ssh-keygen runs without capabilities through setpriv, and the path to authorized_keys is walked safely.

Flood resistance. Four worker threads, a global limit of 20 signature verifications per second (burst 40) and rate-limited rejection logs. The iptables rule restores itself if fail2ban recreates its chains.

Compatibility

  • Firewalls: iptables (with ipset, compatible with ufw and fail2ban), nftables (sets inside your own ruleset) and firewalld (D-Bus and polkit, ipsets with timeout).
  • Network: IPv4 and IPv6, NAT, WireGuard.
  • System: SELinux, tested on Debian stable and on RHEL 9.9 with firewalld and SELinux in enforcing mode. The client also runs on macOS.
  • Dependencies: only the Python standard library (3.8+), one file per side; you need OpenSSH 8.0 or later, systemd, conntrack, setpriv and your chosen firewall backend.

Try it in a few minutes

On the server, after cloning the repository:

sudo useradd --system --no-create-home --shell /usr/sbin/nologin sshknock
sudo install -d /opt/sshknock
sudo install -m 755 server/sshknockd.py /opt/sshknock/sshknockd.py
sudo install -m 644 server/sshknockd.service /etc/systemd/system/sshknockd.service
sudo systemctl daemon-reload && sudo systemctl enable --now sshknockd
sudo journalctl -u sshknockd -f

Configuration lives in /etc/sshknock/sshknockd.conf (INI format; there is an example file in the repository). On the client:

install -D -m 755 client/sshknock-client.py ~/.local/bin/sshknock-client.py
sshknock-client.py --host <server> --identity <user> --key ~/.ssh/id_ed25519 --ssh-after

To make the knock transparent, even for scp and rsync, a single entry in ~/.ssh/config is enough:

Host myserver
    HostName <server>
    User <user>

Match originalhost myserver exec "~/.local/bin/sshknock-client.py --host %h --identity <user> --key ~/.ssh/id_ed25519 >/dev/null"

The client also offers --port, --src-ip, --stun, --wait and --ssh-user, and returns exit codes 0, 1 and 2 for use in scripts. On top of that, the daemon can send Telegram notifications for successful knocks, connections and denied requests.

Why RSA keys cannot be used

The whole knock must fit in a single UDP datagram: the signed JSON header and the ssh-keygen -Y sign signature travel together in one packet. The daemon reads frames straight from the interface and does not reassemble IP fragments, so a packet larger than the network MTU (1500 bytes, i.e. about 1472 bytes of UDP payload on IPv4 and 1452 on IPv6) would never be verified.

An Ed25519 signature is tiny, while an RSA signature is as long as the key. Estimating sizes from the sshsig format (a header of about 115 bytes plus the armored signature), the packet comes out as follows:

  • Ed25519: about 420 bytes, well within the limit;
  • ECDSA P-256: about 510 bytes, still comfortable;
  • RSA-4096: about 1700 bytes, over the limit and therefore unusable;
  • RSA-2048 and 3072: about 990 and 1330 bytes, close to the MTU, with no headroom for VPNs, tunnels or PPPoE.

The practical choice is simple: use an Ed25519 key, which is also what is recommended for SSH today. These sizes are estimates derived from the signature format, not measurements on real packets, and SSH certificates, being larger, are likely not to fit either.

Known limitations

A security component deserves an honest presentation. These are the limitations documented in the README:

  • without --src-ip the source IP is not signed;
  • behind a shared NAT the window opens for the public IP, so it applies to every client sharing it;
  • key options such as command= and restrict are not evaluated at knock time, and AuthorizedKeysCommand and PAM are not mirrored;
  • client and server clocks must differ by less than 20 seconds;
  • IPv6 extension headers are not handled;
  • a flood can exhaust the signature-verification budget, delaying legitimate knocks;
  • with firewalld the window lasts for the whole open_seconds, and a reload closes all open windows.

Project status

Version 1.0.0 came out on 20 November 2026 and 1.1.0 the day after. The project is young, so feedback, bug reports and pull requests are welcome. The code, INSTALL.md and the full changelog are on GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *