4 6f397d55 2024-01-30 xs # Export this variable properly to avoid locking you out
5 6f397d55 2024-01-30 xs : ${QUICK_WHITELIST:=127.0.0.1}
7 d2c27298 2024-01-30 xs alias log="logger -st http-ban"
8 d2c27298 2024-01-30 xs alias show="doas /sbin/pfctl -t players -T show"
9 d2c27298 2024-01-30 xs alias ban="doas /sbin/pfctl -t players -T add -f-"
10 d2c27298 2024-01-30 xs alias grace="doas /sbin/pfctl -t players -T delete -f-"
12 3115cd3d 2024-01-30 xs # Filter functions returning 'count IP' based on HTTP return code
13 3115cd3d 2024-01-30 xs IN_300_HOSTS() {
14 3115cd3d 2024-01-30 xs awk '$(NF-1) >= 300 && $(NF-1) < 400 { print $2 }' | sort | uniq -c
17 3115cd3d 2024-01-30 xs IN_400_HOSTS() {
18 3115cd3d 2024-01-30 xs awk '$(NF-1) >= 400 && $(NF-1) < 500 { print $2 }' | sort | uniq -c
21 3115cd3d 2024-01-30 xs IN_500_HOSTS() {
22 3115cd3d 2024-01-30 xs awk '$(NF-1) >= 500 && $(NF-1) < 600 { print $2 }' | sort | uniq -c
25 3115cd3d 2024-01-30 xs # Our local logs
27 3115cd3d 2024-01-30 xs doas /bin/cat /var/www/logs/access.log
28 d2c27298 2024-01-30 xs doas /usr/bin/zcat /var/www/logs/access.log.*gz 2>/dev/null
31 d2c27298 2024-01-30 xs apply_whitelist() {
32 3115cd3d 2024-01-30 xs GF=$(for ip in $(printf %s\\n $QUICK_WHITELIST); do printf -- '-ve %s\n' "$ip"; done)
33 d2c27298 2024-01-30 xs GF=${GF:-'.*'}
38 3115cd3d 2024-01-30 xs awk -vtrig="${1:-10}" ' $1 >= trig { print $2 }'
42 3115cd3d 2024-01-30 xs access | IN_500_HOSTS | limit 1
43 3115cd3d 2024-01-30 xs access | IN_400_HOSTS | limit 3
44 3115cd3d 2024-01-30 xs access | IN_300_HOSTS | limit 3
48 d2c27298 2024-01-30 xs pre_block | sort | uniq | apply_whitelist
53 d2c27298 2024-01-30 xs block | sort >/tmp/http-ban.new
55 d2c27298 2024-01-30 xs show | sort | awk '{ print $1 }' >/tmp/http-ban.current
57 d2c27298 2024-01-30 xs DIFF=$(comm /tmp/http-ban.new /tmp/http-ban.current)
58 d2c27298 2024-01-30 xs NEW=$(comm -23 /tmp/http-ban.new /tmp/http-ban.current)
59 d2c27298 2024-01-30 xs GRACE=$(comm -13 /tmp/http-ban.new /tmp/http-ban.current)
61 d2c27298 2024-01-30 xs rm /tmp/http-ban.*
63 d2c27298 2024-01-30 xs test -n "$NEW" -o -n "$GRACE" || exit 0
65 3115cd3d 2024-01-30 xs # There's new IP's
66 3115cd3d 2024-01-30 xs if test -n "$NEW"; then
67 3115cd3d 2024-01-30 xs log Banning new IPs:
76 3115cd3d 2024-01-30 xs # There's Old IP's not attacking anymore
77 3115cd3d 2024-01-30 xs if test -n "$GRACE"; then
78 3115cd3d 2024-01-30 xs log Gracing old IPs: