Website/UFW-Blocklist/after6.init
2024-06-26 15:43:16 -05:00

94 lines
3.2 KiB
Bash

#!/bin/sh
# ##################################################
# 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")
savefiles6=$(find "$IPSET_DIR" -name "*-inet6\.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
for f in $savefiles6; do
listname=$(basename -s ".save" "$f")
$IPSET_BIN restore -! <"$f"
ip6tables -I INPUT -m set --match-set "$listname" src -j DROP
ip6tables -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
for f in $savefiles6; do
listname=$(basename -s ".save" "$f")
ip6tables -D INPUT -m set --match-set "$listname" src -j DROP || true
ip6tables -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
echo ""
;;
*)
echo "'$1' not supported"
echo "Usage: after.init {start|stop|status}"
;;
esac