Port-knocking daemon that opens a service port via source ports. The destination stays fixed, so the knock doesn't stand out to a destination-port scan.
Docs · Install · Quick start · Releases
client (sourceknock-knock) server (sourceknock)
------ --------------------
UDP sport=7000 -> dst=X:PORT \
UDP sport=8000 -> dst=X:PORT ==> state machine sees 7000,8000,9000
UDP sport=9000 -> dst=X:PORT / -> adds src IP to nft "allowed" set
TCP sport=<ephemeral> -> dst=X:22 -> port 22 now reachable for this IP
Standard port knocking puts the secret in the destination port sequence, and anyone watching sees a client hammering a list of ports. I flipped it: sourceknock keeps the destination fixed and puts the sequence in the source port, so the knock looks like a few ordinary datagrams to one port.
The daemon watches received frames on an AF_PACKET socket, follows each client partway through a knock, and when the ordered sequence lands inside the window it adds that client's IP to an nftables set. The service port opens for that IP only, then closes again after a fixed TTL, on first connect, or on an admin revoke. Sessions that are already up keep working when it closes.
Be clear about what this buys you. Source ports are as spoofable as any L4 header field, so this is stealth, not authentication. Your service's own auth (SSH keys) still does the real work.
- The daemon: Linux with kernel headers to build, nftables (
nft, validated on 1.0.9), andCAP_NET_RAW+CAP_NET_ADMINto run. The systemd units grant exactly those to an unprivilegedsourceknockuser. - The client: Linux or macOS. Source ports below 1024 need root on Linux.
- Building: a C11 compiler and
make;python3for the client tests. - Source ports that survive the path. Carrier-grade NAT and some corporate firewalls rewrite them, and then no knock arrives intact. See NAT.
Each release attaches sourceknock and sourceknock-knock, built on ubuntu-latest (x86-64 Linux).
From source on Linux, with systemd:
make daemon client
sudo make installThis puts both binaries in /usr/local/bin, the man pages in /usr/local/share/man/man1, and the units in /etc/systemd/system. On macOS, make client builds just the client.
-
Write
/etc/sourceknock/sourceknock.conf:[global] interface = eth0 [trigger:ssh] service = ssh port = 22 protocol = tcp sequence = 7000,8000,9000 window = 5 mode = fixed_ttl duration = 30
-
Create the service user and start the daemon:
sudo useradd --system --user-group --home-dir /var/lib/sourceknock --no-create-home sourceknock sudo systemctl enable --now sourceknockIt logs
listening for knocks on 'eth0'...once it's up. From here port 22 is closed to everyone who hasn't knocked. Sessions that predate the daemon, like the SSH session you installed it from, keep working. -
Knock from the client, then connect within 30 seconds:
sourceknock-knock server.example.com 7000,8000,9000 ssh user@server.example.com
The client sends one UDP datagram per sequence port, each from that source port, in order, then exits without printing anything. The daemon logs
knock OK from <ip>for each completed knock andprogress resetfor a wrong step.
Keep another way into the host the first time you try this. If the knock never arrives, port 22 stays closed, and stopping the daemon doesn't reopen it: the nftables table stays until you delete it (see How it works).
The parser strips only full-line comments (lines starting with # or ;). A comment after a value is not stripped and becomes part of the value, so keep value lines clean.
| Key | What it does |
|---|---|
interface |
The capture interface: the one that sees the client's frames. Ethernet, loopback, tun, WireGuard, PPP and ipip / ip6tnl / sit work. |
port |
22, 80, 443, or 8000-8002. A knock opens all of them. |
protocol |
tcp or udp. |
sequence |
The ordered source ports. Use three or more, outside 32768-60999, or the daemon warns. |
window |
Seconds the whole sequence has to finish in. |
mode |
fixed_ttl (open for duration seconds), close_on_connect (close on the first connection, with fallback_ttl as a kernel-enforced backstop), or permanent (until admin_sequence revokes it). |
knock_port |
Optional. Only frames sent to this destination port count, so other traffic from the same address can't reset a knock. Knock with -p. |
The daemon refuses to start on a config it can't use and says why. Every key, the closing rules per mode and protocol, and the startup checks are in the configuration reference. The examples cover SSH, web and WireGuard.
sourceknock-knock [-p PORT] [-d MS] [-s ADDR] [-4|-6] HOST SEQUENCE-p is the destination port (the trigger's knock_port, default 9), -d the delay between steps in milliseconds (default 50; keep the whole knock inside window), -s the local address. See man sourceknock-knock.
# a trigger with knock_port = 4444, over IPv6
sourceknock-knock -6 -p 4444 server.example.com 7000,8000,9000The daemon takes one argument, the config path, and reads /etc/sourceknock/sourceknock.conf without one. The template unit runs one daemon per config: systemctl enable --now sourceknock@myconf reads /etc/sourceknock/myconf.conf.
Only frames the host receives count; ordinary traffic creates no state, and at most 4096 clients per trigger are tracked mid-knock. IPv4 and IPv6 are both decoded, with IPv6 extension headers walked up to 8 hops. Each trigger gets its own inet sourceknock_<service> table with allowed_v4 and allowed_v6 sets, and the TTLs are nft element timeouts, so the kernel closes the port even if the daemon dies.
When the daemon stops, it removes every allowed client but leaves its table in place, so the port stays closed. To open it to everyone after removing sourceknock, run sudo nft delete table inet sourceknock_ssh (one table per trigger).
| Page | What's there |
|---|---|
| Configuration | Every key, the modes, what closing means for TCP and UDP, startup checks, IPv6. |
| Operations | Running directly and under systemd, NAT, limits, and troubleshooting. |
| Contributing | Build, tests, e2e and systemd tests, fuzzing, CI, releases, branches. |
man sourceknock, man sourceknock-knock |
The same reference, installed with make install. |
make test # unit tests + knock-client tests, any OS
make test-asan # the same under ASan + LeakSanitizer + UBSan
make daemon # Linux only
make fuzz # 60 s libFuzzer run on the packet parser (Linux, clang)The end-to-end tests run the real daemon in a network namespace with real nftables, so they need Linux and root:
sudo bash tests/e2e_integration.sh tests/e2e_fixed.conf
sudo bash tests/e2e_integration.sh tests/e2e_close.confmake test also checks that the Quick start config above parses as-is.
This is obfuscation. Anyone who can see a knock can replay it from the same address, and anyone who can spoof source ports and addresses can forge one. Keep your service's own authentication on, and don't rely on sourceknock as the only control.
MIT. See LICENSE.
Part of Other packets. Siblings: icmpchat · Built by Crank