iptables and nftables¶
Block every address on a list at a Linux firewall. Pick one of the two ways below.
- nftables is the default on current Debian, Ubuntu and Red Hat Enterprise Linux. Use it if you can.
- iptables with ipset works too. Red Hat has deprecated ipset since RHEL 9, so on Red Hat systems use nftables.
Tested on Debian 13 with nftables 1.1.3, iptables 1.8.11 (nf_tables backend) and ipset 7.22, with a list of 80,000 addresses.
You need aip-sync installed and your key stored. The examples use the
persistent list. For another list, change the name everywhere.
Each daily script also sets up what it needs when it's missing, after a reboot for example. If a download fails, it loads the last good list again and exits with an error, so cron can report it. Your firewall is never left empty.
nftables¶
1. Save the table. Save this as /etc/aip-sync/aip.nft. It's a table of its own, so it
doesn't touch your other rules. A drop here is final, whatever your other tables say.
table inet aip {
set persistent4 {
type ipv4_addr
size 262144
}
chain input {
type filter hook input priority filter - 10; policy accept;
ip saddr @persistent4 drop
}
chain forward {
type filter hook forward priority filter - 10; policy accept;
ip saddr @persistent4 drop
}
}
input protects the machine itself. forward protects the network behind it, if it routes.
Keep the one you need.
2. Save the daily script. Save this as /usr/local/sbin/aip-nft and make it executable
(chmod 755):
#!/bin/sh
# Get the newest persistent list and load it into the nftables set, in one step.
# If the download fails, load the last good list again, then report the failure.
set -u
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
OUT=/var/lib/aip-sync/persistent4.txt
LOAD=/run/aip-persistent.nft
exec 9> /run/aip-persistent.lock
flock -w 600 9 || exit 1 # one run at a time, the boot job and the daily one included
status=0
aip-sync --list persistent --out4 "$OUT" --changes --quiet || status=$?
[ -f "$OUT" ] || exit "$status"
# The table is gone after a reboot, or after a reload of the whole ruleset.
nft list table inet aip > /dev/null 2>&1 || nft -f /etc/aip-sync/aip.nft || exit 1
{
echo "flush set inet aip persistent4"
awk '
NR % 1000 == 1 { if (NR > 1) print " }"; printf "add element inet aip persistent4 { %s", $0; next }
{ printf ", %s", $0 }
END { if (NR) print " }" }' "$OUT" || exit 1
} > "$LOAD" || exit 1
nft -f "$LOAD" || exit 1
exit "$status"
nft -f applies the whole file at once, so the set is never empty while it loads.
3. Run it once, and check the set:
sudo /usr/local/sbin/aip-nft
sudo nft list set inet aip persistent4 | head
4. Run it every day, and at boot. Save this as /etc/cron.d/aip-sync, with a time after
03:30 UTC in your server's time zone:
40 3 * * * root /usr/local/sbin/aip-nft
@reboot root sleep 60 && /usr/local/sbin/aip-nft
If you reload your whole ruleset later, with systemctl restart nftables for example, the
table is gone until the next run. Run aip-nft again by hand.
IPv6. Add a set persistent6 with type ipv6_addr, and a rule ip6 saddr @persistent6
drop in each chain. In the script, add --out6 /var/lib/aip-sync/persistent6.txt to the
aip-sync line, and a second block that loads that file into persistent6.
iptables with ipset¶
1. Install ipset. On Debian and Ubuntu: sudo apt install ipset.
2. Save the daily script. Save this as /usr/local/sbin/aip-ipset and make it
executable (chmod 755):
#!/bin/sh
# Get the newest persistent list and swap it into the ipset in one step.
# If the download fails, load the last good list again, then report the failure.
set -u
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
OUT=/var/lib/aip-sync/persistent4.txt
LOAD=/run/aip-persistent.ipset
SET=aip-persistent
exec 9> /run/aip-persistent.lock
flock -w 600 9 || exit 1 # one run at a time, the boot job and the daily one included
status=0
aip-sync --list persistent --out4 "$OUT" --changes --quiet || status=$?
[ -f "$OUT" ] || exit "$status"
# The set and the rules are gone after a reboot: create them when they are missing.
ipset create "$SET" hash:ip family inet maxelem 262144 -exist || exit 1
for chain in INPUT FORWARD; do
iptables -C "$chain" -m set --match-set "$SET" src -j DROP 2> /dev/null ||
iptables -I "$chain" -m set --match-set "$SET" src -j DROP || exit 1
done
{
echo "create $SET-new hash:ip family inet maxelem 262144 -exist"
echo "flush $SET-new"
sed "s/^/add $SET-new /" "$OUT" || exit 1
echo "swap $SET-new $SET"
echo "destroy $SET-new"
} > "$LOAD" || exit 1
ipset restore < "$LOAD" || exit 1
exit "$status"
The rules drop traffic from the set in INPUT, for the machine itself, and in FORWARD, for
the network behind it. Remove the one you don't need from the for line.
The script fills a second set, then swaps the two, so the rules always see a full set.
maxelem matters: by default a set holds 65,536 addresses, and the biggest list has more.
3. Run it once, and check the set:
sudo /usr/local/sbin/aip-ipset
sudo ipset list aip-persistent | head
4. Run it every day, and at boot. Save this as /etc/cron.d/aip-sync, with a time after
03:30 UTC in your server's time zone:
40 3 * * * root /usr/local/sbin/aip-ipset
@reboot root sleep 60 && /usr/local/sbin/aip-ipset
IPv6. Add --out6 /var/lib/aip-sync/persistent6.txt to the aip-sync line, and load that
file the same way into a second set, created with family inet6, with rules added by
ip6tables.
Remove it¶
nftables: sudo nft delete table inet aip, then remove the cron file.
iptables: sudo iptables -D INPUT -m set --match-set aip-persistent src -j DROP, the same
for FORWARD, then sudo ipset destroy aip-persistent, and remove the cron file.