Egress Filtering and Logging Dropped Packets
Most firewall effort goes into what comes in. But nearly every serious intrusion needs to get something out: malware fetches its next stage and calls its command-and-control server, stolen data is uploaded, a compromised server is used to scan or attack others, a reverse tunnel gives the attacker a way back in (Bastion Hosts and SSH Tunnels). Egress filtering, restricting what internal systems may connect to, breaks or exposes all of these. The second half of the lesson is about the evidence: logging the packets a firewall drops, in both directions, so that blocked attempts become alerts.
Why egress matters, by example
| Attacker need | Without egress filtering | With egress filtering |
|---|---|---|
| Download a second stage | any HTTP/HTTPS server works | only via the proxy, which categorises and logs destinations |
| Command and control | direct TCP to any port | must use allowed paths (proxy, DNS), which are monitored |
| Exfiltrate data | direct upload anywhere | blocked, or squeezed through monitored channels |
| Send spam / scan the Internet from your servers | your IP gets blocklisted | blocked at the edge, alerts fire |
| Reverse shell / reverse SSH tunnel | an outbound connection to port 4444 or 22 just works | dropped and logged |
A server egress policy
Servers have the most predictable outbound needs, so they are the easiest place to start. The web server in the DMZ needs to reach: the internal DNS resolver, the database, the NTP servers, the log collector, and package updates through a proxy. Nothing else.
cat > egress.nft <<'RULES'
table inet www_egress {
chain output {
type filter hook output priority filter; policy drop;
ct state established,related accept
oif "lo" accept
ip daddr 10.10.2.53 meta l4proto { tcp, udp } th dport 53 accept comment "internal DNS only"
ip daddr 10.10.2.30 tcp dport 5432 accept comment "database"
ip daddr { 10.10.2.123, 10.10.2.124 } udp dport 123 accept comment "internal NTP"
ip daddr 10.10.2.50 tcp dport 6514 accept comment "syslog over TLS"
ip daddr 10.10.1.80 tcp dport 3128 accept comment "update proxy"
meta l4proto ipv6-icmp accept
limit rate 5/second burst 20 packets log prefix "www-out-drop: " group 0
counter reject with icmpx admin-prohibited
}
}
RULES
out=$(nft -c -f egress.nft 2>&1)
if grep 'Error:' <<<"$out" | grep -vq 'Operation not permitted'; then echo "$out"; else echo "egress.nft: syntax OK"; fi
grep -c 'accept comment' egress.nft | sed 's/$/ allowed destinations, everything else rejected and logged/'egress.nft: syntax OK
5 allowed destinations, everything else rejected and logged
Points a reviewer checks in this file:
- DNS only to the internal resolver. Direct DNS to the Internet is both a C2 channel and a way around DNS-based security filtering. The resolver itself is then the one place to watch for DNS tunnelling (long, random-looking query names to one domain).
- Updates through a proxy, not "allow 443 to anywhere". The proxy can allow just the distribution's mirrors.
rejectinstead ofdropon the way out: local applications fail fast instead of hanging, which makes misconfigurations visible during rollout.- Logging with
group 0sends the dropped packets to user space through NFLOG (forulogd2or a collector) instead of the kernel log, and thelimitprotects the logging pipeline during a burst.
For user networks the same idea is coarser: web through a filtering proxy, DNS through the internal resolvers, mail only through the mail server, nothing else outbound by default. SMB (445) and RDP (3389) to the Internet should never be allowed from anywhere.
Reading drop logs
A log rule writes one line per packet. With group 0 each packet becomes an NFLOG event for the collector; without group it goes to the kernel log, and collectors such as ulogd2 can write the same text format. A kernel-log line (lab session excerpt from journalctl -k -g www-out-drop, recorded while the rule was tested without group 0):
Feb 03 19:12:04 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=203.0.113.66 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=51244 DF PROTO=TCP SPT=48810 DPT=4444 WINDOW=64240 RES=0x00 SYN URGP=0IN= empty and OUT=eth0 say it was locally generated and leaving; DPT=4444 a port with no business on a web server. The program parses a synthetic hour of such lines and summarises them the way an analyst would: by destination port, by destination, and by repetition.
import re
import pandas as pd
LOG = """\
Feb 03 19:00:11 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=192.0.2.80 PROTO=TCP SPT=40100 DPT=443 SYN
Feb 03 19:00:12 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=192.0.2.80 PROTO=TCP SPT=40102 DPT=443 SYN
Feb 03 19:03:40 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=198.51.100.53 PROTO=UDP SPT=53122 DPT=53
Feb 03 19:12:04 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=203.0.113.66 PROTO=TCP SPT=48810 DPT=4444 SYN
Feb 03 19:13:04 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=203.0.113.66 PROTO=TCP SPT=48822 DPT=4444 SYN
Feb 03 19:14:04 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=203.0.113.66 PROTO=TCP SPT=48830 DPT=4444 SYN
Feb 03 19:15:04 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=203.0.113.66 PROTO=TCP SPT=48841 DPT=4444 SYN
Feb 03 19:16:04 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=203.0.113.66 PROTO=TCP SPT=48850 DPT=4444 SYN
Feb 03 19:31:55 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=198.51.100.25 PROTO=TCP SPT=51515 DPT=25 SYN
Feb 03 19:40:00 www kernel: www-out-drop: IN= OUT=eth0 SRC=10.10.1.10 DST=192.0.2.123 PROTO=UDP SPT=123 DPT=123
"""
pat = re.compile(r"^(\w{3} \d\d \d\d:\d\d:\d\d) .*?SRC=(\S+) DST=(\S+) PROTO=(\w+) SPT=(\d+) DPT=(\d+)")
rows = [pat.match(l).groups() for l in LOG.splitlines()]
df = pd.DataFrame(rows, columns=["time", "src", "dst", "proto", "spt", "dpt"])
df["t"] = pd.to_datetime("2026 " + df["time"], format="%Y %b %d %H:%M:%S")
summary = df.groupby(["dst", "proto", "dpt"]).agg(n=("t", "size"), first=("t", "min"), last=("t", "max")).reset_index()
gaps = df.sort_values("t").groupby(["dst", "dpt"])["t"].apply(lambda s: s.diff().dt.total_seconds().dropna().tolist())
summary["interval_s"] = [sorted(set(gaps[(r.dst, r.dpt)])) for r in summary.itertuples()]
summary["first"] = summary["first"].dt.strftime("%H:%M:%S"); summary["last"] = summary["last"].dt.strftime("%H:%M:%S")
print(summary.sort_values("n", ascending=False).to_string(index=False)) dst proto dpt n first last interval_s
203.0.113.66 TCP 4444 5 19:12:04 19:16:04 [60.0]
192.0.2.80 TCP 443 2 19:00:11 19:00:12 [1.0]
192.0.2.123 UDP 123 1 19:40:00 19:40:00 []
198.51.100.25 TCP 25 1 19:31:55 19:31:55 []
198.51.100.53 UDP 53 1 19:03:40 19:03:40 []
Five attempts to 203.0.113.66:4444, exactly 60 seconds apart: a regular interval to an odd port is the signature of beaconing, a program retrying its call home on a timer. That line is an incident, not noise. The others are configuration findings with different owners: HTTPS straight to the Internet (an application that ignores the proxy setting), DNS to a public resolver (a hard-coded resolver), SMTP to an outside mail server (a contact form sending mail directly), NTP to a public server (time not taken from the internal NTP). Each is fixed by configuring the application correctly, after which the log should be quiet, and quiet logs make the next beacon stand out.
Logging well
- Log drops at every boundary and on every host chain, rate-limited (
limit rate … burst …), with a prefix per chain so lines can be routed and searched. - Log selected accepts too, on sensitive paths (admin SSH, database access from new sources), ideally as flow logs (conntrack events with
conntrackd/ulogd2, NetFlow/IPFIX, cloud VPC flow logs) rather than per-packet lines. - Ship logs off the host at once (syslog over TLS, an agent); a compromised host's local logs can be edited.
- Alert on patterns, not lines: new destination ports, regular intervals, sudden volume, outbound attempts from servers that never make them.
Pitfalls
- Allowing "outbound 443 anywhere" because "everything uses HTTPS": that is also what malware uses. Prefer a proxy with destination policy.
- Logging every drop without limits: a scan fills the disk or drowns the journal, and real events vanish.
- Never reading the logs: a log rule with no alert and no review is only disk usage.
- Forgetting IPv6 and DNS-over-HTTPS: applications can bypass the internal resolver over 443 unless the proxy policy handles it.
Practice
1. A staff workstation's drop log shows outbound 445 attempts to many Internet addresses. What is likely happening?
A worm or malware scanning for SMB on the Internet (or misconfigured software). Isolate the workstation, investigate, and keep 445 outbound blocked everywhere.
2. Why does the egress ruleset use reject while the input chains used drop?
Outbound, the "client" is our own application: a reject makes it fail immediately with a clear error, which helps owners fix configurations. Inbound, the client is a stranger: a silent drop gives scanners less information and costs us nothing.
3. Add a line to LOG for a beacon to 192.0.2.200:8080 every 300 seconds, three times. How would you write an alert rule for beaconing in general?
The summary shows interval_s [300.0]. A rule: for each (source, destination, port) with at least 3 attempts in an hour, alert when the intervals vary by less than a few percent (low jitter). Real malware adds jitter, so also look at the standard deviation relative to the mean.
Test yourself
What does egress filtering mainly protect against?
- Inbound port scans
- Malware calling home, data exfiltration and abuse of your hosts against others
- Slow websites
- DNS spoofing of your domain
What made the 203.0.113.66:4444 entries stand out?
- They were UDP
- Repeated attempts exactly 60 s apart to an odd port: beaconing
- They came from the Internet
- They were accepted
Why is a log rule written with limit rate?
- To speed up packets
- So a burst of drops (a scan, a flood) can't overwhelm logging
- Because nftables requires it
- To drop fewer packets
What do IN= (empty) and OUT=eth0 in a kernel log line mean?
- A forwarded packet
- A packet generated by the host itself, leaving through eth0
- An inbound packet
- A loopback packet