Guides, March 27, 2026

IP Blocklists with ipset and ufw: Stopping Known Attackers at the Firewall

Free blocklists such as Spamhaus DROP, blocklist.de and GreenSnow stop known attackers at the firewall. With an update script and ufw integration for IPv4 and IPv6.

IP Blocklists with ipset and ufw: Stopping Known Attackers at the Firewall

IP blocklists stop known attackers at the firewall, before your web server, mail server or WordPress ever sees a request. With ipset, even tens of thousands of addresses can be checked with no noticeable load. We show which free lists have proven themselves, how an update script keeps them reliably up to date and how to integrate everything cleanly into ufw, for IPv4 and IPv6.

Updated November 2026: IPv6 support, atomic list swaps, a dedicated firewall chain with an allowlist and a corrected note on exceptions.

Why blocklists at the firewall make sense

Every connection a server accepts, processes and then rejects costs computing time. Blocklists act earlier: packets from listed addresses are dropped before a TCP connection is established.

  • Mail servers: fewer spam connections putting load on Postfix, Amavis and SpamAssassin
  • Web servers: less bot traffic tying up nginx and PHP-FPM processes
  • WordPress: brute force on /wp-login.php and XML-RPC from known attackers ends at the firewall
  • Logs: far less noise, real problems stand out sooner

Blocklists are the first of several layers of protection. New attackers that are not on any list yet are caught by Fail2ban. We describe how this fits together in Brute-Force Attacks on the Login: How We Protect Servers with Fail2ban and Blocklists.

The lists at a glance

All lists are free, continuously maintained and designed to produce few false positives. The figures reflect the order of magnitude in November 2026.

ListContentsSizeNote
Spamhaus DROPHijacked address blocks and networks used purely for spam and attacksapprox. 1,700 IPv4 networks, approx. 90 IPv6 networksAvailable as JSON split by IPv4 and IPv6 since 2024; the former EDROP list has been merged into it
blocklist.deAddresses that attacked servers within the last 48 hoursapprox. 14,000 addresses (mail.txt)Use mail.txt for mail servers and all.txt for web servers; includes IPv6
GreenSnowBrute force and port scans on SSH, SMTP, HTTP, FTPapprox. 2,700 addressesIPv4 only
DShield (SANS ISC)The 20 most active attacking /24 networks of the last three days20 networksSmall, but very accurate
Your own listAttackers you observed yourselfvariableSame format as Spamhaus: network ; comment

Example of your own list. Only use documentation addresses as placeholders, never private ranges such as 192.168.x.x or 10.x.x.x, otherwise you may lock out your own network:

; Own blocklist
192.0.2.0/24     ; example network
198.51.100.7/32  ; example address

Requirements

apt install ipset curl
ufw status   # ufw must be active

Do not run nftables and ufw side by side

Some server packages, such as iRedMail, set up their own nftables service with a final DROP rule. If it runs alongside ufw, ports stay unreachable despite correct ufw rules. If ufw is your only firewall, disable the service:

systemctl disable --now nftables
nft list ruleset | grep "inet filter"   # must not print anything

The update script

The script downloads all lists, filters out valid addresses and separates IPv4 from IPv6. Three points matter:

  • Atomic swap: each list is first built in a temporary set and then exchanged in one step with ipset swap. There is never a moment in which the block is empty.
  • Sanity check: if a source returns too few entries or nothing at all, the current list stays active.
  • Fast: entries are loaded into the kernel in bulk via ipset restore, not one by one with ipset add.
nano /usr/local/bin/update-ipset.sh
chmod 755 /usr/local/bin/update-ipset.sh
#!/bin/bash
# /usr/local/bin/update-ipset.sh
# Downloads IP blocklists and swaps the ipsets atomically (IPv4 and IPv6).
set -u

STATE=/var/lib/ipset
mkdir -p "$STATE"

# Name | URL | type | family | minimum number of entries
LISTS=(
  "bl-spamhaus4|https://www.spamhaus.org/drop/drop_v4.json|hash:net|inet|500"
  "bl-spamhaus6|https://www.spamhaus.org/drop/drop_v6.json|hash:net|inet6|20"
  "bl-blde4|https://lists.blocklist.de/lists/mail.txt|hash:ip|inet|1000"
  "bl-blde6|https://lists.blocklist.de/lists/mail.txt|hash:ip|inet6|1"
  "bl-greensnow|https://blocklist.greensnow.co/greensnow.txt|hash:ip|inet|500"
  "bl-dshield|https://feeds.dshield.org/block.txt|hash:net|inet|5"
  "bl-own4|https://www.example.com/drop/drop.txt|hash:net|inet|1"
)

# Prints the addresses of one family from the downloaded file
extract() {
  local url=$1 family=$2 file=$3
  case "$url" in
    *drop_v*.json) grep -o '"cidr":"[^"]*"' "$file" | cut -d'"' -f4 ;;
    *dshield*)     grep -E '^[0-9]' "$file" | awk '{print $1"/24"}' ;;
    *)             grep -v -E '^[;#]' "$file" | awk '{print $1}' ;;
  esac | if [ "$family" = inet6 ]; then grep ':'; else grep -v ':'; fi | grep -E '^[0-9a-fA-F:.]+(/[0-9]+)?$'
}

for entry in "${LISTS[@]}"; do
  IFS='|' read -r name url type family min <<< "$entry"
  raw=$(mktemp) ; set_file=$(mktemp)

  if ! curl -fsS --max-time 60 -o "$raw" "$url"; then
    logger -t update-ipset "$name: download failed, keeping the current list"
    rm -f "$raw" "$set_file"; continue
  fi

  extract "$url" "$family" "$raw" | sort -u > "$set_file"
  count=$(wc -l < "$set_file")
  if [ "$count" -lt "$min" ]; then
    logger -t update-ipset "$name: only $count entries (minimum $min), keeping the current list"
    rm -f "$raw" "$set_file"; continue
  fi

  # Build the new list under a temporary name, then swap it in one step
  ipset list -n "$name" >/dev/null 2>&1 || ipset create "$name" "$type" family "$family" maxelem 262144
  ipset destroy "$name-tmp" 2>/dev/null
  if ! { echo "create $name-tmp $type family $family maxelem 262144"
         sed "s/^/add $name-tmp /" "$set_file"; } | ipset restore -exist; then
    logger -t update-ipset "$name: ipset restore failed, keeping the current list"
    ipset destroy "$name-tmp" 2>/dev/null; rm -f "$raw" "$set_file"; continue
  fi
  ipset swap "$name-tmp" "$name"
  ipset destroy "$name-tmp"
  ipset save "$name" > "$STATE/$name.save"

  logger -t update-ipset "$name: loaded $count entries"
  rm -f "$raw" "$set_file"
done

Replace the URL of your own list with your own or remove the line. On a pure web server, swap mail.txt for all.txt at blocklist.de. Run it once, then every six hours via cron; you should not fetch the lists more often than once an hour:

/usr/local/bin/update-ipset.sh
journalctl -t update-ipset -n 20

echo '15 */6 * * * root /usr/local/bin/update-ipset.sh' > /etc/cron.d/update-ipset

Integrating with ufw via after.init

ufw calls the script /etc/ufw/after.init when it starts, reloads and stops. Our version creates a dedicated firewall chain BLOCKLIST and attaches it to the start of the INPUT chain, separately for IPv4 and IPv6. This has several advantages:

  • The rules are rebuilt on every start, so a ufw reload no longer creates duplicate entries.
  • Logging is limited to 10 entries per minute, so syslog does not fill up.
  • An allowlist bl-allow-inet or bl-allow-inet6 is checked before any block.
  • After a reboot, the most recently loaded lists are restored immediately, without waiting for the next download.
nano /etc/ufw/after.init
chmod 755 /etc/ufw/after.init
#!/bin/bash
# /etc/ufw/after.init – called by ufw on start/stop/status
# Puts a dedicated BLOCKLIST chain in front of all ufw rules.

STATE=/var/lib/ipset
SETS4="bl-spamhaus4 bl-blde4 bl-greensnow bl-dshield bl-own4"
SETS6="bl-spamhaus6 bl-blde6"

setup() {  # $1 = iptables|ip6tables, $2 = inet|inet6, $3 = sets
  local ipt=$1 family=$2 sets=$3 s
  ipset create "bl-allow-$family" hash:net family "$family" -exist
  [ -f "$STATE/bl-allow-$family.save" ] && ipset restore -exist < "$STATE/bl-allow-$family.save"
  $ipt -N BLOCKLIST 2>/dev/null || $ipt -F BLOCKLIST
  $ipt -A BLOCKLIST -m set --match-set "bl-allow-$family" src -j RETURN
  for s in $sets; do
    [ -f "$STATE/$s.save" ] && ipset restore -exist < "$STATE/$s.save"
    ipset list -n "$s" >/dev/null 2>&1 || continue
    $ipt -A BLOCKLIST -m set --match-set "$s" src -m limit --limit 10/min \
      -j LOG --log-prefix "[BLOCKLIST $s] "
    $ipt -A BLOCKLIST -m set --match-set "$s" src -j DROP
  done
  $ipt -C INPUT -j BLOCKLIST >/dev/null 2>&1 || $ipt -I INPUT 1 -j BLOCKLIST
}

teardown() {  # $1 = iptables|ip6tables
  local ipt=$1
  while $ipt -D INPUT -j BLOCKLIST 2>/dev/null; do :; done
  $ipt -F BLOCKLIST 2>/dev/null
  $ipt -X BLOCKLIST 2>/dev/null
}

case "$1" in
  start)
    setup iptables  inet  "$SETS4"
    setup ip6tables inet6 "$SETS6"
    ;;
  stop|flush-all)
    teardown iptables
    teardown ip6tables
    ;;
  status)
    for s in $SETS4 $SETS6 bl-allow-inet bl-allow-inet6; do
      printf '%-16s %s entries\n' "$s" "$(ipset list "$s" 2>/dev/null | grep -c -E '^[0-9a-fA-F]')"
    done
    ;;
esac
exit 0
ufw reload
/etc/ufw/after.init status
iptables -S BLOCKLIST

Allowing individual addresses

A ufw rule such as ufw insert 1 allow from … does not help here: the block chain sits in front of all ufw rules, so the packet is dropped before ufw ever sees it. Exceptions therefore belong in the allowlist, and to survive a reboot it is saved:

ipset add bl-allow-inet 203.0.113.10
ipset save bl-allow-inet > /var/lib/ipset/bl-allow-inet.save

# IPv6:
ipset add bl-allow-inet6 2001:db8::10
ipset save bl-allow-inet6 > /var/lib/ipset/bl-allow-inet6.save

Monitoring in operation

# Which list blocked most recently?
journalctl -k --since today | grep "BLOCKLIST" | tail -20

# Is an address on a list?
ipset test bl-blde4 198.51.100.7

# Hits per rule since the last start
iptables -L BLOCKLIST -v -n

Because every list has its own log prefix, you can see at once which source triggered a block. If a customer reports that they cannot reach you, check their address with ipset test and add it to the allowlist if needed.

Limits of blocklists

Blocklists only block those who have already attracted attention. They do not replace updates, secure passwords, Fail2ban or a web application firewall. Even well-maintained lists occasionally contain addresses that have since been reassigned. That is why a regular look at the logs pays off, especially on mail servers that have to accept mail from unknown senders.

On our own servers we add a central blocklist to the public lists, fed by the firewall and Fail2ban blocks of all servers. If you would rather not secure your server or website yourself, we take care of it as part of hosting and operations.

More articles

Let's talk about your project.

The initial consultation is free. Write to us via the contact form or by email to kontakt@michael-busch.de.