Skip to content

Blocking lists of IP addresses from blocklists / blacklists (e.g. blocklist.de, spamhaus.org) via ipset and ufw

License

Notifications You must be signed in to change notification settings

ngandrass/ufw-ipset-blocklist-autoupdate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ufw-ipset-blocklist-autoupdate

Latest Version Maintenance Status License GitHub Issues GitHub Pull Requests Donate with PayPal Sponsor with GitHub GitHub Stars GitHub Forks GitHub Contributors

This collection of scripts automatically pulls IP blocklists (e.g. Spamhaus, Blocklist, ...) and drops packages from listed IP addresses. It integrates with the uncomplicated firewall (ufw) and makes use of ipset for storing IP addresses and network ranges. Both IPv4 and IPv6 blocklists are supported.

Installation

  1. Install ufw and ipset.
  2. Deploy after.init script via executing: ./setup-ufw.sh
  3. Determine the blocklist you would like to use.
  4. Get initial set of blocklists: ./update-ip-blocklists.sh -l "blocklist https://lists.blocklist.de/lists/all.txt" -l "spamhaus https://www.spamhaus.org/drop/drop.txt"
  5. Add update-ip-blocklists.sh to your crontab:
@daily /path/to/update-ip-blocklists.sh  -l "blocklist https://lists.blocklist.de/lists/all.txt" -l "spamhaus https://www.spamhaus.org/drop/drop.txt"

Usage

Usage: ./update-ip-blocklists.sh [-h]
Blocking lists of IPs from public blocklists / blacklists (e.g. blocklist.de, spamhaus.org)

Options:
  -l     : Blocklist to use. Can be specified multiple times.
           Format: "$name $url" (space-separated). See examples below.
  -4     : Run in IPv4 only mode. Ignore IPv6 addresses.
  -6     : Run in IPv6 only mode. Ignore IPv4 addresses.
  -q     : Quiet mode. Outputs are suppressed if flag is present.
  -v     : Verbose mode. Prints additional information during execution.
  -h     : Print this help message.

Example usage:
./update-ip-blocklists.sh -l "spamhaus https://www.spamhaus.org/drop/drop.txt"
./update-ip-blocklists.sh -l "blocklist https://lists.blocklist.de/lists/all.txt" -l "spamhaus https://www.spamhaus.org/drop/drop.txt"
./update-ip-blocklists.sh -l "spamhaus https://www.spamhaus.org/drop/drop.txt" -l "spamhaus6 https://www.spamhaus.org/drop/dropv6.txt"

Supplying blocklist sources

Blocklists can be passed to the script using the -l CLI argument. Each entry consists of a name and download URL, separated by a space. Examples:

  • -l "spamhaus https://www.spamhaus.org/drop/drop.txt"
  • -l "mylist http://mylist.local/list.txt"
  • -l "spamhaus6 https://www.spamhaus.org/drop/dropv6.txt"

Lists are stripped of comments. This means all text after one of the following characters is removed before parsing: ;, #. Valid IPv4/IPv6 addresses with an optional CIDR are loaded into the ipset to block.

Processing of either IPv6 or IPv4 addresses can be disabled by supplying the -4 (IPv4 only) or -6 (IPv6 only) flags respectively.

Listing blocked IPs

The total number of blocked IPs is indicated by running ipset -t list. A full list of all blocked addresses is given by ipset list.

Components

  • update-ip-blocklist.sh: Pulls the latest versions of requested blocklists, updates ipsets, and exports created ipsets to $IPSET_DIR (default: /var/lib/ipset). Ipsets are swapped during update to minimize the update downtime.
  • ufw/after.init: Inserts and deletes the required iptables rules on ufw reloads. Ipsets are loaded from $IPSET_DIR.
  • setup-ufw.sh: Helper script to deploy ufw/after.init.

Available blocklists

This script can parse all blocklists that list IPv4 or IPv6 addresses with optional CIDR notation row per row in a plain text format (see Supplying blocklist sources).

The following blocklists are known to work. They can be enabled by passing the respective -l argument to the update-ip-blocklists.sh script.

Acknowledgments

This project is inspired by this post on Xela's Linux Blog.

About

Blocking lists of IP addresses from blocklists / blacklists (e.g. blocklist.de, spamhaus.org) via ipset and ufw

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages