Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ipv6 support #108

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# ipset-blacklist

A Bash shell script which uses ipset and iptables to ban a large number of IP addresses published in IP blacklists. ipset uses a hashtable to store/fetch IP addresses and thus the IP lookup is a lot (!) faster than thousands of sequentially parsed iptables ban rules. ~~However, the limit of an ipset list is 2^16 entries.~~
A Bash shell script which uses ipset, iptables and ip6tables to ban a large number of IP addresses published in IP blacklists. ipset uses a hashtable to store/fetch IP addresses and thus the IP lookup is a lot (!) faster than thousands of sequentially parsed iptables ban rules.
Thanks to @trick77 for original code and to https://github.com/leshniak/nft-blacklist for inspiration and code to add ipv6 support.

The ipset command doesn't work under OpenVZ. It works fine on dedicated and fully virtualized servers like KVM though.

## What's new

- 04/30/2023: Added ipv6 support
- 10/17/2018: Added support for CIDR aggregation if iprange command is available
- 10/17/2018: Merged Shellcheck PR from [@extremeshok](https://github.com/extremeshok)
- 05/10/2018: Added regex filter improvements from [@sbujam](https://github.com/sbujam)
Expand All @@ -18,40 +20,42 @@ The ipset command doesn't work under OpenVZ. It works fine on dedicated and full

## Quick start for Debian/Ubuntu based installations

1. `wget -O /usr/local/sbin/update-blacklist.sh https://raw.githubusercontent.com/trick77/ipset-blacklist/master/update-blacklist.sh`
2. `chmod +x /usr/local/sbin/update-blacklist.sh`
3. `mkdir -p /etc/ipset-blacklist ; wget -O /etc/ipset-blacklist/ipset-blacklist.conf https://raw.githubusercontent.com/trick77/ipset-blacklist/master/ipset-blacklist.conf`
1. `wget -O /usr/local/bin/update-blacklist.sh https://raw.githubusercontent.com/kiekerjan/ipset-blacklist/master/update-blacklist.sh`
2. `chmod +x /usr/local/bin/update-blacklist.sh`
3. `mkdir -p /etc/ipset-blacklist ; wget -O /etc/ipset-blacklist/ipset-blacklist.conf https://raw.githubusercontent.com/kiekerjan/ipset-blacklist/master/ipset-blacklist.conf`
4. Modify `ipset-blacklist.conf` according to your needs. Per default, the blacklisted IP addresses will be saved to `/etc/ipset-blacklist/ip-blacklist.restore`
5. `apt-get install ipset`
6. Create the ipset blacklist and insert it into your iptables input filter (see below). After proper testing, make sure to persist it in your firewall script or similar or the rules will be lost after the next reboot.
7. Auto-update the blacklist using a cron job
6. Get cidr-merger from https://github.com/zhanhb/cidr-merger/releases (optional)
7. Create the ipset blacklist and insert it into your iptables input filter (see below). After proper testing, make sure to persist it in your firewall script or similar or the rules will be lost after the next reboot.
8. Auto-update the blacklist using a cron job

## First run, create the list

to generate the `/etc/ipset-blacklist/ip-blacklist.restore`:

```sh
/usr/local/sbin/update-blacklist.sh /etc/ipset-blacklist/ipset-blacklist.conf
/usr/local/bin/update-blacklist.sh /etc/ipset-blacklist/ipset-blacklist.conf
```

## iptables filter rule

```sh
# Enable blacklists
ipset restore < /etc/ipset-blacklist/ip-blacklist.restore
iptables -I INPUT 1 -m set --match-set blacklist src -j DROP
iptables -I INPUT 1 -m set --match-set blacklist_v4 src -j DROP
ip6tables -I INPUT 1 -m set --match-set blacklist_v6 src -j DROP
```

Make sure to run this snippet in a firewall script or just insert it to `/etc/rc.local`.
Make sure to run this snippet in a firewall script or just insert it to `/etc/rc.local` or a cron job that runs at boot.

## Cron job

In order to auto-update the blacklist, copy the following code into `/etc/cron.d/update-blacklist`. Don't update the list too often or some blacklist providers will ban your IP address. Once a day should be OK though.

```sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
33 23 * * * root /usr/local/sbin/update-blacklist.sh /etc/ipset-blacklist/ipset-blacklist.conf
33 23 * * * root /usr/local/bin/update-blacklist.sh /etc/ipset-blacklist/ipset-blacklist.conf
```

## Check for dropped packets
Expand Down
5 changes: 3 additions & 2 deletions ansible/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
hosts: publicserver
become: true
vars:
download_script_url: https://raw.githubusercontent.com/trick77/ipset-blacklist/master/update-blacklist.sh
download_script_url: https://raw.githubusercontent.com/kiekerjan/ipset-blacklist/master/update-blacklist.sh
ipsets_confdir: /etc/ipset-blacklist
ipsets_blacklist_name: blacklist
ip_blacklist_restore: ip-blacklist.restore
Expand All @@ -14,6 +14,7 @@
- https://www.dshield.org/block.txt
- https://lists.blocklist.de/lists/ssh.txt
maxelements: 131072
maxelements_v6: 131072

tasks:

Expand All @@ -36,7 +37,7 @@

- name: Download update-blacklist.conf (for documentation)
get_url:
url: https://raw.githubusercontent.com/trick77/ipset-blacklist/master/ipset-blacklist.conf
url: https://raw.githubusercontent.com/kiekerjan/ipset-blacklist/master/ipset-blacklist.conf
dest: "{{ ipsets_confdir }}/ipset-blacklist.conf.orig"

- name: Generate update-blacklist.conf
Expand Down
10 changes: 7 additions & 3 deletions ansible/templates/update-blacklist.conf.j2
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
IPSET_BLACKLIST_NAME={{ ipsets_blacklist_name }}
IPSET_BLACKLIST_NAME={{ ipsets_blacklist_name }} # change it if it collides with a pre-existing ipset list
IPSET_TMP_BLACKLIST_NAME={{ ipsets_blacklist_name }}-tmp

IP_BLACKLIST_RESTORE="{{ ipsets_confdir }}/ip-blacklist.restore"
IP_BLACKLIST="{{ ipsets_confdir }}/ip-blacklist.list"
IP_BLACKLIST_FILE="{{ ipsets_confdir }}/ip-blacklist.list"
IP6_BLACKLIST_FILE="{{ ipsets_confdir }}/ip6-blacklist.list"

VERBOSE={{ verbose }} # probably set to "no" for cron jobs, default to yes
FORCE={{ force }} # will create the ipset-iptable binding if it does not already exist
let IPTABLES_IPSET_RULE_NUMBER=1 # if FORCE is yes, the number at which place insert the ipset-match rule (default to 1)

# Sample (!) list of URLs for IP blacklists. Currently, only IPv4 is supported in this script, everything else will be filtered.
# Sample (!) list of URLs for IP blacklists.
BLACKLISTS=(
"{{ blacklists|join("\"\n \"") }}"
)
MAXELEM={{ maxelements }}
MAXELEM={{ maxelements_v6 }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefining the same bash variable.



24 changes: 14 additions & 10 deletions ipset-blacklist.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@ IPSET_TMP_BLACKLIST_NAME=${IPSET_BLACKLIST_NAME}-tmp

# ensure the directory for IP_BLACKLIST/IP_BLACKLIST_RESTORE exists (it won't be created automatically)
IP_BLACKLIST_RESTORE=/etc/ipset-blacklist/ip-blacklist.restore
IP_BLACKLIST=/etc/ipset-blacklist/ip-blacklist.list
IP_BLACKLIST_FILE=/etc/ipset-blacklist/ip-blacklist.list
IP6_BLACKLIST_FILE=/etc/ipset-blacklist/ip6-blacklist.list

VERBOSE=yes # probably set to "no" for cron jobs, default to yes
FORCE=yes # will create the ipset-iptable binding if it does not already exist
let IPTABLES_IPSET_RULE_NUMBER=1 # if FORCE is yes, the number at which place insert the ipset-match rule (default to 1)

# Sample (!) list of URLs for IP blacklists. Currently, only IPv4 is supported in this script, everything else will be filtered.
# Sample (!) list of URLs for IP blacklists.
BLACKLISTS=(
# "file:///etc/ipset-blacklist/ip-blacklist-custom.list" # optional, for your personal nemeses (no typo, plural)
"https://www.projecthoneypot.org/list_of_ips.php?t=d&rss=1" # Project Honey Pot Directory of Dictionary Attacker IPs
"https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=1.1.1.1" # TOR Exit Nodes
"http://danger.rulez.sk/projects/bruteforceblocker/blist.php" # BruteForceBlocker IP List
"https://www.spamhaus.org/drop/drop.lasso" # Spamhaus Don't Route Or Peer List (DROP)
"https://cinsscore.com/list/ci-badguys.txt" # C.I. Army Malicious IP List
"https://lists.blocklist.de/lists/all.txt" # blocklist.de attackers
"https://blocklist.greensnow.co/greensnow.txt" # GreenSnow
#"https://www.projecthoneypot.org/list_of_ips.php?t=d&rss=1" # Project Honey Pot Directory of Dictionary Attacker IPs
#"https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=1.1.1.1" # TOR Exit Nodes
#"http://danger.rulez.sk/projects/bruteforceblocker/blist.php" # BruteForceBlocker IP List
"https://www.spamhaus.org/drop/drop.txt" # Spamhaus Don't Route Or Peer List (DROP)
#"https://cinsscore.com/list/ci-badguys.txt" # C.I. Army Malicious IP List
#"https://lists.blocklist.de/lists/all.txt" # blocklist.de attackers
#"https://blocklist.greensnow.co/greensnow.txt" # GreenSnow
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset" # Firehol Level 1
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/stopforumspam_7d.ipset" # Stopforumspam via Firehol
# "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/stopforumspam_7d.ipset" # Stopforumspam via Firehol
# "https://raw.githubusercontent.com/ipverse/rir-ip/master/country/zz/ipv4-aggregated.txt" # Ban an entire country(-code), see https://github.com/ipverse/rir-ip
Comment on lines +16 to 25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be part of the PR

# "https://raw.githubusercontent.com/ipverse/asn-ip/master/as/1234/ipv4-aggregated.txt" # Ban a specific autonomous system (ISP), see https://github.com/ipverse/asn-ip
"https://www.spamhaus.org/drop/dropv6.txt" # Spamhaus Don't Route Or Peer List ipv6 (DROPv6)
# "https://team-cymru.org/Services/Bogons/fullbogons-ipv6.txt" # Team Cymru Bogons
)
MAXELEM=131072
MAXELEM_V6=131072
Loading