GH-013 T1205.001 PHASE RED INSIDE

The Secret Knock

The attacker's daemon sits on this box holding three ports that answer every connection and instantly hang up, and a fourth port that does not exist until it hears the right three knocks in the right order. This file knocks wrong, knocks right, and watches the door appear, then inventories the listeners the defender's scanner never saw.

TechniqueTraffic Signaling: Port Knocking (T1205.001)
Target OSUbuntu 24.04.5 LTS
Toolchainpython3, bash, coreutils, iptables
LevelIntermediate
Time25 min

OBJECTIVES

  1. Knock a three-port sequence and watch a dark service port open for one banner
  2. Inventory the listeners and correlate the single python process behind four ports
  3. Kill the daemon, drop the ports at the firewall, baseline the listeners on a schedule
  4. Take the IOC checklist and the one-sentence lesson
01

Intel Brief

CONTEXT

MITRE ATT&CK T1205.001: adversaries use port knocking, a connect sequence that signals a hidden service to open, so the service port stays closed to everyone else. The actors, named once: the attacker planted the knock daemon on this box during the initial breach and knows the sequence; the defender owns the box, does not know the daemon is there, and runs the scans. This file runs the knock wrong, runs it right, and shows the one inventory that sees through it.

Why this technique. An open port is an advertisement. Every scan on the network sees it, fingerprints the banner, and hammers the login. The port is the problem before the password is: the defender's scan finds the door, and after that the only question is how long the lock holds. Port knocking deletes the advertisement. The service port stays closed, so the defender's scan finds nothing to attack. There is no door to fingerprint until the daemon hears the right sequence, and the sequence is just three connects to three ports that each accept and instantly close.

Why not leave the port open and protect it with a password. That is the obvious defense, and this file breaks it on the concrete input: the defender's scanner sees an open 4000 before any password is ever typed. The banner gives away the service, the open port gives away the address, and the password only changes what happens after the knock on the door is already heard. A password protects the login; it does not hide the door.

Why not scan for the door. The defender's scanner sweeps 3999 to 4004 and finds three ports that accept and close with nothing behind them, and no 4000 at all. It did not miss the door; the door was never there. Scanning answers the question "what is open"; port knocking makes the answer "nothing worth attacking".

The mechanism, in one line. Port knocking is traffic signaling with connects: a knock sequence is an ordered list of connection attempts to specific ports inside a time window. A listener is a process holding a port open. The daemon watches its knock ports, and when the last three knocks match the sequence inside ten seconds, it binds the door port. The baseline is the known-good list of listeners; the allowlist is the set of ports permitted inbound. Terms earned, no borrowing.

The concrete case, before the abstraction. The attacker's daemon holds 4001, 4002, 4003. Knock 4003, 4002, 4001 and nothing happens. Knock 4001, 4002, 4003 inside ten seconds and port 4000 answers with one banner. One python process, four ports, ten seconds.

Scope. T1205 covers traffic signaling broadly: port knocking, and the port-sequence variant where the signal is a specific order of connects. This file works the connect-sequence case end to end: the knock, the door, and the listener inventory that catches it.

02

Target Environment

LOADOUT

Ubuntu 24.04.5 LTS, x86-64, in a disposable namespace used only for this lab. The knock demo runs as the current user; the firewall demo needs root. The lab is lab-local: 127.0.0.1 only, nothing leaves the box.

1

Build the lab

One disposable directory. Everything the lab touches lives inside it, so cleanup is a single rm.

bash
$ rm -rf /tmp/gh013 && mkdir -p /tmp/gh013 && echo "lab built: /tmp/gh013"
lab built: /tmp/gh013
2

The daemon: 51 lines

Line by line. #!/usr/bin/env python3 runs the file under python3. The three imports: socket for the listeners, threading so the three knock ports listen at once, time for the knock window. HOST = '127.0.0.1' keeps the lab local, nothing leaves the box. DOOR = 4000 is the service port, dark until the right knock. KNOCK_PORTS = [4001, 4002, 4003] is the knock sequence, in order. WINDOW = 10.0 means the full sequence must land inside ten seconds. BANNER is the one line the door serves. state holds the knock log and the door flag; lock guards them because three listener threads write the log at once.

serve_door() is the service: it builds a socket, sets SO_REUSEADDR so a restart rebinds cleanly, binds (HOST, DOOR), listens, then loops forever: accept a connect, sendall(BANNER), close. Every connect gets the banner, and once this function starts the door stays up. listen_on(port) holds one knock port the same way, and its loop is the whole technique: accept each connect and close it immediately, so the knock leaves no service to attack, then under the lock append (port, time.monotonic()) to the knock log. last3 is the last three knocks. The if checks four things: three knocks exist, the door is not already up, the three ports are [4001, 4002, 4003] in order, and the third knock landed within ten seconds of the first. All four true, and the daemon spawns serve_door in a background thread. The bottom three lines start one listen_on thread per knock port, then threading.Event().wait() sleeps forever; killing the process stops the daemon.

python: daemon
$ cat > /tmp/gh013/knockd.py <<'EOF'
#!/usr/bin/env python3
"""GH-013 lab: the knock daemon. A door that opens only for the right knock."""
import socket
import threading
import time

HOST = '127.0.0.1'               # lab-local; nothing leaves this box
DOOR = 4000                      # the service port, dark until the right knock
KNOCK_PORTS = [4001, 4002, 4003]  # the knock sequence, in order
WINDOW = 10.0                    # the full sequence must land inside 10 seconds
BANNER = b'GH-013 door open\r\n'

state = {'knocks': [], 'door_up': False}
lock = threading.Lock()


def serve_door():
    """Serve the door: every connect gets the banner, then the door stays up."""
    srv = socket.socket()
    srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    srv.bind((HOST, DOOR))
    srv.listen(5)
    while True:
        conn, _ = srv.accept()
        conn.sendall(BANNER)
        conn.close()


def listen_on(port):
    """Hold one knock port: accept each connect, close it, log the knock."""
    srv = socket.socket()
    srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    srv.bind((HOST, port))
    srv.listen(5)
    while True:
        conn, _ = srv.accept()
        conn.close()
        with lock:
            state['knocks'].append((port, time.monotonic()))
            last3 = state['knocks'][-3:]
            if (len(last3) == 3 and not state['door_up']
                    and [p for p, _ in last3] == KNOCK_PORTS
                    and last3[-1][1] - last3[0][1] <= WINDOW):
                state['door_up'] = True
                threading.Thread(target=serve_door, daemon=True).start()


for port in KNOCK_PORTS:
    threading.Thread(target=listen_on, args=(port,), daemon=True).start()

threading.Event().wait()  # sleep forever; kill stops the daemon
EOF
chmod +x /tmp/gh013/knockd.py && echo "daemon written: $(wc -l < /tmp/gh013/knockd.py) lines"
daemon written: 51 lines
3

Start the daemon

python3 /tmp/gh013/knockd.py & starts the daemon in the background; $! is its pid, kept in KNOCKD so the blue phase kills exactly this process later. ss -tlnp lists the listening TCP sockets with the owning process: one python process, the same pid as KNOCKD, holds 4001, 4002, and 4003. The door port 4000 is not listed. It does not exist yet.

bash
$ python3 /tmp/gh013/knockd.py & KNOCKD=$!; sleep 1; echo "daemon pid=$KNOCKD"; ss -tlnp | grep -E "400[0-3]"
[1] 10
daemon pid=10
LISTEN 0      0          127.0.0.1:4002      0.0.0.0:*    users:(("python3",pid=10,fd=5))
LISTEN 0      0          127.0.0.1:4003      0.0.0.0:*    users:(("python3",pid=10,fd=7))
LISTEN 0      0          127.0.0.1:4001      0.0.0.0:*    users:(("python3",pid=10,fd=3))
03

Phase Red: knock, and the door appears

ATTACK

First, the defender's scanner's view before any knock: you are the attacker, you know the sequence, so you never scan; this is the view your technique defeats. Then the wrong knock, which the daemon ignores. Then the right knock, which builds the door. Watch what the defender's scan sees, and what it never sees.

Real screen recording 246.8s
What you are watching: The defender's scanner sweeps six ports and finds nothing to attack. The wrong sequence is ignored. The right sequence opens the door, and the banner answers. Every command below is copy-pasteable.
1

The defender's scanner, before the knock

You are the attacker. You know the knock sequence, so you never scan this box; the scan below is the defender's, the view this technique defeats. The sweep covers 3999 to 4004, before any knock, and it hits the ports out of order on purpose: sweeping 4001, 4002, 4003 in order would itself be the knock. 4001, 4002, and 4003 accept and close instantly: nothing to attack. 4000, the service port, is closed. To the defender's scanner the box offers three dead ports and no service. The scan is honest; the door simply is not there yet.

bash
$ for p in 3999 4004 4000 4002 4001 4003; do printf '%s ' $p; nc -z -w1 127.0.0.1 $p >/dev/null 2>&1 && echo open || echo closed; done
3999 closed
4004 closed
4000 closed
4002 open
4001 open
4003 open
2

The wrong knock

You knock: connect 4003, 4002, 4001. The daemon logs all three, checks the last three against the sequence, and ignores them: the order is wrong. nc -z 127.0.0.1 4000 still fails. The wrong sequence is not a failed login; it is silence.

bash
$ for p in 4003 4002 4001; do nc -z 127.0.0.1 $p >/dev/null 2>&1; done; echo "wrong sequence knocked (4003 4002 4001)"; nc -z 127.0.0.1 4000 && echo OPEN || echo 'door stays shut: connection refused'
wrong sequence knocked (4003 4002 4001)
door stays shut: connection refused
3

The right knock

You knock again: connect 4001, 4002, 4003 inside the ten-second window. The daemon sees the match, spawns the door, and nc 127.0.0.1 4000 answers with the banner. The service did not start at boot; it started at the knock.

bash
$ for p in 4001 4002 4003; do nc -z 127.0.0.1 $p >/dev/null 2>&1; done; echo "right sequence knocked (4001 4002 4003, inside 10s)"; sleep 1
right sequence knocked (4001 4002 4003, inside 10s)
$ timeout 3 nc 127.0.0.1 4000 </dev/null
GH-013 door open
4

The door is open

The banner answered. The attacker's work is done: the door exists, the knock is known, and the service is reachable to anyone who knocks. What the defender sees in this box, the scan that misses and the inventory that catches, is the next phase's business.

Stealth confirmed

The defender's scan found nothing to attack: three ports that accept and close, no service port. The wrong sequence earned silence. The right sequence built the door out of nothing. A defender's scanner that never knocks will never find 4000, because until the knock it does not exist.

04

Phase Blue: inventory the listeners

DEFENSE

The defender does not scan for the service; the defender inventories the listeners. One ss, one pid, one binary, one window, then the kill.

Here is the trap: sweeping the ports again after the knock and calling the open 4000 the finding. The open door is the one thing the technique shows only to someone who already knows the knock. The detection is the listener inventory: one python process squatting on a run of adjacent ports that serve nothing.

Real screen recording 214.6s
What you are watching: The listener baseline names one python process on four ports. The pid leads to the binary and the incident window. The knock burst leaves completed connects in ss. The daemon is killed by pid, its ports dropped at the firewall, and the door stays shut on the re-knock. Every command below is copy-pasteable.
1

The baseline

ss -tlnp shows one python process holding THREE knock ports plus the door port. That is the anomaly: a single pid on multiple closed-looking ports. Good versus bad, concrete: good is one daemon per listening port with a known name (sshd on 22, a database on its port); bad is one python pid squatting on a run of adjacent ports that accept and close and serve nothing.

bash
$ echo "--- listener baseline: who holds what ---"; ss -tlnp | grep -E "400[0-3]"
--- listener baseline: who holds what ---
LISTEN 0      0          127.0.0.1:4002      0.0.0.0:*    users:(("python3",pid=10,fd=5))
LISTEN 0      0          127.0.0.1:4003      0.0.0.0:*    users:(("python3",pid=10,fd=7))
LISTEN 0      0          127.0.0.1:4000      0.0.0.0:*    users:(("python3",pid=10,fd=9))
LISTEN 0      0          127.0.0.1:4001      0.0.0.0:*    users:(("python3",pid=10,fd=3))
2

Correlate the pid

The pid comes out of the ss line. /proc/<pid>/cmdline names the binary: knockd.py, no disguise. /proc/<pid>/exe points at the python interpreter, which says how it runs. ps -o lstart gives the process start time: that is the start of the incident window, everything before it is clean.

bash
$ KP=$(ss -tlnp | grep "127.0.0.1:4001" | grep -oP "pid=\K[0-9]+" | head -1); echo "daemon pid=$KP"; tr "\0" " " < /proc/$KP/cmdline; echo; ls -l /proc/$KP/exe; ps -o lstart= -p $KP
daemon pid=10
python3 /tmp/gh013/knockd.py
lrwxrwxrwx 1 root root 0 Sep 20 13:28 /proc/10/exe -> /usr/bin/python3.12
Sun Sep 20 13:28:31 2026
3

The knock burst, in ss

Knock the three ports, then ss -tn state time-wait shows the recent completed connections to the knock ports, sitting in TIME_WAIT. The burst is the tell: three connects to three adjacent ports inside seconds, from one source, to ports that serve nothing. The honest limit, stated plainly: this demo daemon accepts full TCP connects, so the tell is completed connections. Classic single-packet knocking would show as failed SYNs instead, which is the D3-CAA failed-connection tell. Know which knock you are hunting.

bash
$ for p in 4001 4002 4003; do nc -z 127.0.0.1 $p >/dev/null 2>&1; done; sleep 1; echo "--- completed connects to the knock ports (the knock burst) ---"; ss -tn state time-wait | grep -E ':400[123]'
--- completed connects to the knock ports (the knock burst) ---
0      0          127.0.0.1:59368    127.0.0.1:4002
0      0          127.0.0.1:59408    127.0.0.1:4002
0      0          127.0.0.1:4003     127.0.0.1:56306
0      0          127.0.0.1:4002     127.0.0.1:59368
0      0          127.0.0.1:4003     127.0.0.1:56304
0      0          127.0.0.1:4001     127.0.0.1:46158
0      0          127.0.0.1:4001     127.0.0.1:46162
0      0          127.0.0.1:4002     127.0.0.1:59400
0      0          127.0.0.1:4003     127.0.0.1:56324
0      0          127.0.0.1:4002     127.0.0.1:59384
0      0          127.0.0.1:4001     127.0.0.1:46144
0      0          127.0.0.1:4003     127.0.0.1:56310
0      0          127.0.0.1:4001     127.0.0.1:46150
4

Stop and drop

iptables-save first: never touch the firewall without a way back. The DROP goes in while the daemon is still alive, so the control is proven, not assumed: iptables -A INPUT -p tcp --dport 4000:4003 -j DROP, and the next knock to 4001 dies in the firewall while the packet counter on the rule climbs. (iptables prints a warning about the tcp extension revision; the blocked knock and the counter prove the rule is working.) Then kill $KNOCKD stops the daemon by pid, ps confirms it is dead, and ss -tln confirms all four listeners are gone. Stopping the process is not the whole mitigation; the drop is the part that survives a respawn.

bash
$ iptables-save > /tmp/gh013/iptables.bak && echo 'host rules saved to iptables.bak'; iptables -A INPUT -p tcp --dport 4000:4003 -j DROP && echo "--- drop in place: the knocker is cut off ---"; nc -z -w2 127.0.0.1 4001 >/dev/null 2>&1 && echo REACHABLE || echo 'knock blocked by the drop rule'; iptables -L INPUT -v -n | grep 4000; kill $KNOCKD; sleep 1; ps -p $KNOCKD >/dev/null 2>&1 && echo "STILL ALIVE" || echo "daemon dead"; ss -tln | grep -E ":(4000|4001|4002|4003) " || echo "all four listeners gone"
host rules saved to iptables.bak
Warning: Extension tcp revision 0 not supported, missing kernel module?
--- drop in place: the knocker is cut off ---
knock blocked by the drop rule
Warning: Extension tcp revision 0 not supported, missing kernel module?
    2   120 DROP       6    --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpts:4000:4003
daemon dead
all four listeners gone
5

Scope the window and sweep persistence

The window runs from the daemon start (step 2) to the kill: that is when the door could open. The persistence sweep checks the three places a respawn hides: crontab -l (not installed on this lab box, so there is nothing to check there), the systemd units, and stray files in /tmp. Then the firewall goes back to normal: remove the DROP rules and iptables-restore the saved host rules, and confirm no knock rules remain. The drops were scaffolding; the restore is the proof the box is back to its own policy.

bash
$ echo "--- persistence sweep ---"; crontab -l 2>&1 | head -2; ls /etc/systemd/system/ 2>/dev/null | grep -iE 'knock|gh013' || echo 'no knock systemd units'; ls -la /tmp/gh013; echo "--- remove the drops, restore the saved host rules ---"; iptables -D INPUT -p tcp --dport 4000:4003 -j DROP; iptables-restore < /tmp/gh013/iptables.bak && echo 'host rules restored'; iptables -S INPUT | grep 4000 || echo 'no knock rules remain'
--- persistence sweep ---
bash: crontab: command not found
no knock systemd units
total 8
drwxrwx--- 2 root root   80 Sep 20 13:28 .
drwxrwxrwt 3 root root   60 Sep 20 13:28 ..
-rw-rw---- 1 root root  207 Sep 20 13:28 iptables.bak
-rwxrwx--- 1 root root 1756 Sep 20 13:28 knockd.py
--- remove the drops, restore the saved host rules ---
Warning: Extension tcp revision 0 not supported, missing kernel module?
host rules restored
no knock rules remain
6

Re-knock: the door stays shut

Run the right sequence again: 4001, 4002, 4003. The door stays shut, connection refused. There is no daemon, so there is no door; the sequence is meaningless without the listener that was waiting for it.

bash
$ for p in 4001 4002 4003; do nc -z 127.0.0.1 $p >/dev/null 2>&1; done; echo "right sequence knocked again, daemon dead"; nc -z 127.0.0.1 4000 && echo OPEN || echo 'door stays shut: connection refused'
right sequence knocked again, daemon dead
door stays shut: connection refused
7

Prevention, demonstrated

The durable form is default-deny inbound with an explicit allowlist: nothing reaches the box unless it is named. The targeted DROP is demonstrated again, then removed, because a targeted drop is not the durable form. The honest limit: targeted drops do not stop a daemon that rebinds to an allowed port, so the allowlist must be small and the listener baseline runs regardless; and none of this stops a local process from binding 127.0.0.1, which is why the allowlist plus the baseline travel together. The known-good baseline snapshot is saved, and the scheduled check is one diff: diff listeners.baseline <(ss -tlnp).

bash
$ echo "--- prevention: targeted drop, demonstrated then removed ---"; iptables -A INPUT -p tcp --dport 4000:4003 -j DROP; iptables -S INPUT | grep 4000; iptables -D INPUT -p tcp --dport 4000:4003 -j DROP; echo "targeted drop removed (the durable form is default-deny plus a listener baseline)"; ss -tlnp > /tmp/gh013/listeners.baseline && echo "known-good baseline snapshot saved: $(wc -l < /tmp/gh013/listeners.baseline) lines"; echo "the scheduled check runs: diff listeners.baseline <(ss -tlnp)"
--- prevention: targeted drop, demonstrated then removed ---
Warning: Extension tcp revision 0 not supported, missing kernel module?
Warning: Extension tcp revision 0 not supported, missing kernel module?
-A INPUT -p tcp -m tcp --dport 4000:4003 -j DROP
Warning: Extension tcp revision 0 not supported, missing kernel module?
targeted drop removed (the durable form is default-deny plus a listener baseline)
known-good baseline snapshot saved: 2 lines
the scheduled check runs: diff listeners.baseline <(ss -tlnp)
Detection verified

One ss named the anomaly: a single python pid on four ports. The pid led to the binary and the window. The kill was confirmed, the ports were dropped, the window was scoped, persistence was swept, and the re-knock proved the door is gone. The defender's scanner never had a chance; the inventory had everything.

8

Cleanup

Kill any stray daemon, remove the lab, confirm it is gone: no directory, no knock listeners on the box. The namespace dies with the session regardless; the cleanup keeps the record honest.

bash
$ pkill -f knockd.py 2>/dev/null; rm -rf /tmp/gh013; ls /tmp | grep gh013 || echo "lab gone"; ss -tln | grep -E ":(4000|4001|4002|4003) " || echo "no knock listeners on the box"
lab gone
no knock listeners on the box
05

Countermeasures

DEFENSE

Three moves, in order: stop the daemon and drop its ports, watch the listeners on a schedule, deny by default. Each is one way, not the way.

1

Mitigate: stop, drop, scope

Proven first against the live daemon: the DROP goes in, the next knock dies in the firewall, the counter climbs. Then stop the daemon by pid, and confirm the listeners are gone before touching anything else; never kill without confirming, or you will not know whether it respawned. Scope the window from the daemon start to the kill, sweep persistence (crontab, systemd units, /tmp strays), then remove the DROP rules and restore the saved host rules. D3FEND names the filtering Inbound Traffic Filtering (D3-ITF): restricting network traffic from untrusted networks toward a private host.

bash
$ iptables-save > /tmp/gh013/iptables.bak && echo 'host rules saved to iptables.bak'; iptables -A INPUT -p tcp --dport 4000:4003 -j DROP && echo "--- drop in place: the knocker is cut off ---"; nc -z -w2 127.0.0.1 4001 >/dev/null 2>&1 && echo REACHABLE || echo 'knock blocked by the drop rule'; iptables -L INPUT -v -n | grep 4000; kill $KNOCKD; sleep 1; ps -p $KNOCKD >/dev/null 2>&1 && echo "STILL ALIVE" || echo "daemon dead"; ss -tln | grep -E ":(4000|4001|4002|4003) " || echo "all four listeners gone"
host rules saved to iptables.bak
Warning: Extension tcp revision 0 not supported, missing kernel module?
--- drop in place: the knocker is cut off ---
knock blocked by the drop rule
Warning: Extension tcp revision 0 not supported, missing kernel module?
    2   120 DROP       6    --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpts:4000:4003
daemon dead
all four listeners gone
$ echo "--- persistence sweep ---"; crontab -l 2>&1 | head -2; ls /etc/systemd/system/ 2>/dev/null | grep -iE 'knock|gh013' || echo 'no knock systemd units'; ls -la /tmp/gh013; echo "--- remove the drops, restore the saved host rules ---"; iptables -D INPUT -p tcp --dport 4000:4003 -j DROP; iptables-restore < /tmp/gh013/iptables.bak && echo 'host rules restored'; iptables -S INPUT | grep 4000 || echo 'no knock rules remain'
--- persistence sweep ---
bash: crontab: command not found
no knock systemd units
total 8
drwxrwx--- 2 root root   80 Sep 20 13:28 .
drwxrwxrwt 3 root root   60 Sep 20 13:28 ..
-rw-rw---- 1 root root  207 Sep 20 13:28 iptables.bak
-rwxrwx--- 1 root root 1756 Sep 20 13:28 knockd.py
--- remove the drops, restore the saved host rules ---
Warning: Extension tcp revision 0 not supported, missing kernel module?
host rules restored
no knock rules remain

Honest limit. iptables rules on the box only stop network reachability. A local attacker with a shell can still reach 127.0.0.1 only if the rules do not cover lo (ours do: INPUT covers loopback), and the rules do not remove the binary or undo what the service did while it was reachable. The drop buys time and blocks the remote knocker; it does not clean the box.

2

Monitor: baseline the listeners on a schedule

The scheduled form of the phase-blue baseline: snapshot ss -tlnp known-good, and diff it on a timer. One new listener on an unexpected port pages someone. D3FEND's grounded set for this technique: Connection Attempt Analysis (D3-CAA), completed and failed connection attempts as the tell; Inbound Session Volume Analysis (D3-ISVA), bursts of inbound sessions to adjacent ports, the knock burst exactly.

bash
$ echo "--- listener baseline: who holds what ---"; ss -tlnp | grep -E "400[0-3]"
--- listener baseline: who holds what ---
LISTEN 0      0          127.0.0.1:4002      0.0.0.0:*    users:(("python3",pid=10,fd=5))
LISTEN 0      0          127.0.0.1:4003      0.0.0.0:*    users:(("python3",pid=10,fd=7))
LISTEN 0      0          127.0.0.1:4000      0.0.0.0:*    users:(("python3",pid=10,fd=9))
LISTEN 0      0          127.0.0.1:4001      0.0.0.0:*    users:(("python3",pid=10,fd=3))
$ for p in 4001 4002 4003; do nc -z 127.0.0.1 $p >/dev/null 2>&1; done; sleep 1; echo "--- completed connects to the knock ports (the knock burst) ---"; ss -tn state time-wait | grep -E ':400[123]'
--- completed connects to the knock ports (the knock burst) ---
0      0          127.0.0.1:59368    127.0.0.1:4002
0      0          127.0.0.1:59408    127.0.0.1:4002
0      0          127.0.0.1:4003     127.0.0.1:56306
0      0          127.0.0.1:4002     127.0.0.1:59368
0      0          127.0.0.1:4003     127.0.0.1:56304
0      0          127.0.0.1:4001     127.0.0.1:46158
0      0          127.0.0.1:4001     127.0.0.1:46162
0      0          127.0.0.1:4002     127.0.0.1:59400
0      0          127.0.0.1:4003     127.0.0.1:56324
0      0          127.0.0.1:4002     127.0.0.1:59384
0      0          127.0.0.1:4001     127.0.0.1:46144
0      0          127.0.0.1:4003     127.0.0.1:56310
0      0          127.0.0.1:4001     127.0.0.1:46150

Honest limit. ss shows the current truth only. A daemon that binds, serves, and exits between polls is missed, so the poll interval sets the detection floor. Host firewall logs of dropped knocks are the backstop: the knocker still has to touch the ports, and the log keeps what ss forgot.

3

Prevent: default-deny with an explicit allowlist

The durable form: INPUT defaults to DROP, and only named ports are permitted. A knock daemon cannot be reached on ports the firewall never allows, and the listener baseline still runs, because prevention and detection are not substitutes. Demonstrated above in targeted form, then removed; the production form is the default-deny policy.

bash
$ echo "--- prevention: targeted drop, demonstrated then removed ---"; iptables -A INPUT -p tcp --dport 4000:4003 -j DROP; iptables -S INPUT | grep 4000; iptables -D INPUT -p tcp --dport 4000:4003 -j DROP; echo "targeted drop removed (the durable form is default-deny plus a listener baseline)"; ss -tlnp > /tmp/gh013/listeners.baseline && echo "known-good baseline snapshot saved: $(wc -l < /tmp/gh013/listeners.baseline) lines"; echo "the scheduled check runs: diff listeners.baseline <(ss -tlnp)"
--- prevention: targeted drop, demonstrated then removed ---
Warning: Extension tcp revision 0 not supported, missing kernel module?
Warning: Extension tcp revision 0 not supported, missing kernel module?
-A INPUT -p tcp -m tcp --dport 4000:4003 -j DROP
Warning: Extension tcp revision 0 not supported, missing kernel module?
targeted drop removed (the durable form is default-deny plus a listener baseline)
known-good baseline snapshot saved: 2 lines
the scheduled check runs: diff listeners.baseline <(ss -tlnp)

Honest limit. Targeted drops do not stop a daemon that rebinds to an allowed port, so the allowlist must be small and the baseline runs regardless. And none of this stops a local process from binding 127.0.0.1, which is why the allowlist plus the baseline travel together. A firewall filters the network; the inventory watches the box.

Countermeasures verified

The daemon was stopped by pid with its listeners confirmed gone, its ports dropped and the drops removed, the window scoped and persistence swept. The listeners are baselined on a schedule, and the durable posture is default-deny with an allowlist. The knock still works against a box nobody watches; against a watched box it is just noise in the log.

06

Debrief

TAKEAWAY

A closed port is not an absent service: inventory the listeners, not the scan.

IOC checklist: signs this technique was used

  • One python process holding several adjacent ports that accept and close with no service behind them
  • Bursts of completed connections (TIME_WAIT) to adjacent ports on a box with no known service there
  • A daemon binary in /tmp whose loop accepts and immediately closes connects
  • A listener that appears only after a burst of connects to neighboring ports
  • Firewall logs showing dropped or completed connects to a tight run of ports

Reproducibility checklist

  • Verified on Ubuntu 24.04.5 LTS, x86-64, on 2026-09-20
  • Both recordings are real executions on that platform, not simulations
  • The knock demo runs as the current user (the assumed breach: the attacker's implant runs the daemon on the box); the firewall demo needs root
  • Cleanup: kill the daemon pid, iptables-restore the saved host rules, rm -rf /tmp/gh013; verify with ss -tln

Sources

MITRE ATT&CK T1205.001: Port Knocking
ss(8) (the listener inventory)
nc(1) (the knocks)
iptables(8) (the drops)
MITRE D3FEND: D3-CAA, D3-ISVA, D3-ITF (each modeled against T1205.001 in the technique mapping)

Next file

GH-014 The Patient Fuse

A payload that waits out the sandbox clock. The next file times the wait.