81 lines
2.7 KiB
Bash
81 lines
2.7 KiB
Bash
#!/bin/bash
|
|
# ##################################################
|
|
# ufw-ipset-blocklist-autoupdate
|
|
#
|
|
# Blocking lists of IPs from public blocklists / blacklists (e.g. blocklist.de, spamhaus.org)
|
|
#
|
|
# Version: 1.1.1
|
|
#
|
|
# See: https://github.com/ngandrass/ufw-ipset-blocklist-autoupdate
|
|
#
|
|
#
|
|
# MIT License
|
|
#
|
|
# Copyright (c) 2023 Niels Gandraß <niels@gandrass.de>
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
# copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
# SOFTWARE.
|
|
# ##################################################
|
|
|
|
IPSET_BIN="$(which ipset)"
|
|
IPSET_DIR="/var/lib/ipset"
|
|
|
|
# Check prerequisites
|
|
if [ ! -x "${IPSET_BIN}" ]; then
|
|
echo "ERROR: ipset binary not found in ${IPSET_BIN}"
|
|
return
|
|
fi
|
|
|
|
if [ ! -d "${IPSET_DIR}" ]; then
|
|
echo "ERROR: ipset data directory does not exist: ${IPSET_DIR}" >&2
|
|
return
|
|
fi
|
|
|
|
savefiles=$(find "$IPSET_DIR" -name "*-inet.save")
|
|
case "$1" in
|
|
start)
|
|
for f in $savefiles; do
|
|
listname=$(basename -s ".save" "$f")
|
|
|
|
$IPSET_BIN restore -! <"$f"
|
|
iptables -I INPUT -m set --match-set "$listname" src -j DROP
|
|
iptables -I INPUT -m set --match-set "$listname" src -j LOG --log-prefix "[UFW BLOCK $listname] "
|
|
done
|
|
;;
|
|
stop)
|
|
for f in $savefiles; do
|
|
listname=$(basename -s ".save" "$f")
|
|
|
|
iptables -D INPUT -m set --match-set "$listname" src -j DROP || true
|
|
iptables -D INPUT -m set --match-set "$listname" src -j LOG --log-prefix "[UFW BLOCK $listname] " || true
|
|
$IPSET_BIN destroy -q "$listname" || true
|
|
done
|
|
;;
|
|
status)
|
|
echo "= after.init ="
|
|
$IPSET_BIN -t list
|
|
# show iptables hit/byte counts
|
|
iptables -L -nvx | grep "$listname" | grep 'match-set'
|
|
echo ""
|
|
;;
|
|
*)
|
|
echo "'$1' not supported"
|
|
echo "Usage: after.init {start|stop|status}"
|
|
;;
|
|
esac
|