Guide

Blocking Malicious IPs: fail2ban, nftables, and Edge Firewalls

Which layer should block, practical fail2ban and nftables set configs, and how wide and long to ban.

IP-DB Team 3 min read 24 reads 0 comments

Repeated failed logins, port scans, and exploit probes in your logs make blocking the address the obvious reflex. Whether it actually helps depends on where you block, for how long, and how widely.

Which layer should block?

  • Edge / CDN — stops traffic before it ever reaches your server; for volumetric attacks it is the only realistic option.
  • Host firewall (nftables/iptables) — fast and cheap for machine-specific decisions; the packet is dropped before the application sees it.
  • Application layer — the most expensive place, because the request has already been processed, but the only layer with account, session, and behavioural context.

Rule of thumb: stop brute force and scanning at the firewall, and business-logic abuse (fake signups, coupon abuse) in the application.

fail2ban: automatic bans driven by behaviour

fail2ban reads logs and adds an address to the firewall once a pattern repeats often enough inside a time window. A typical SSH configuration:

# /etc/fail2ban/jail.local [sshd] enabled = true backend = systemd maxretry = 4 findtime = 10m bantime = 1h # escalate on repeat offenders [recidive] enabled = true bantime = 1w findtime = 1d maxretry = 5

The recidive jail keeps addresses that collect short bans out for weeks. Do add your own office and monitoring addresses to ignoreip — otherwise you will lock yourself out eventually.

Large lists: ipset and nftables sets

Writing one rule per address for thousands of entries slows the firewall down. The right structure is a single set matched by a single rule:

# a persistent blocklist set with nftables nft add table inet filter nft add set inet filter blocklist { type ipv4_addr \; flags interval \; } nft add rule inet filter input ip saddr @blocklist drop # load the list from a feed nft add element inet filter blocklist { 203.0.113.0/24, 198.51.100.7 }

This stays constant-cost even at hundreds of thousands of entries; a cron job simply repopulates the set from current feed data.

How wide, how long?

  • Make time-boxed bans the default. Permanent lists rot: addresses change hands, and yesterday's attacker is tomorrow's customer.
  • Be careful with /24 and wider. Closing a whole block over one bad server also hits the legitimate users inside it.
  • Be especially cautious with CGNAT and mobile ranges. Thousands of subscribers can sit behind one address.
  • Whitelist your own infrastructure — monitoring, backups, and payment provider callback addresses included.

Don't block search engines by accident

Not every aggressively crawling bot is malicious. Before blocking an address claiming to be a search engine bot, resolve its reverse DNS record and re-query the returned name forward; if the name and IP don't match, the bot is forged. The User-Agent header alone proves nothing.

Feed the list from IP-DB

Verifying addresses pulled from your own logs before blocking them cuts unnecessary bans sharply. The IP-DB.com API returns an address's blacklist records, ASN, and community reports; placed in front of your firewall automation, only genuinely bad-reputation addresses make it into the set. Use the bulk lookup for spot checks and the API for a continuous feed.

Share

Comments 0

Share a question, your experience or a correction. You can comment as a registered user or anonymously.

No comments yet. Be the first.

Plain text; links are not accepted. Comments appear at once and may be moderated. Sign in to comment as a member