4 4dabe9d4 2025-06-05 xs # Copyright 2025 xs <xs@inda.re>
6 4dabe9d4 2025-06-05 xs # Permission to use, copy, modify, and/or distribute this software for any
7 4dabe9d4 2025-06-05 xs # purpose with or without fee is hereby granted, provided that the above
8 4dabe9d4 2025-06-05 xs # copyright notice and this permission notice appear in all copies.
10 4dabe9d4 2025-06-05 xs # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11 4dabe9d4 2025-06-05 xs # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
12 4dabe9d4 2025-06-05 xs # FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13 4dabe9d4 2025-06-05 xs # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 4dabe9d4 2025-06-05 xs # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15 4dabe9d4 2025-06-05 xs # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 4dabe9d4 2025-06-05 xs # PERFORMANCE OF THIS SOFTWARE.
18 d43799e5 2024-01-30 xs : "${HTTPBAN_WHITELIST:=127.0.0.1}"
19 d43799e5 2024-01-30 xs : "${HTTPBAN_LIMIT_500:=10}"
20 d43799e5 2024-01-30 xs : "${HTTPBAN_LIMIT_400:=10}"
21 d43799e5 2024-01-30 xs : "${HTTPBAN_LIMIT_300:=10}"
22 d43799e5 2024-01-30 xs : "${HTTPBAN_TABLE:=httpban}"
24 d2c27298 2024-01-30 xs alias log="logger -st http-ban"
25 bcc5ac46 2024-01-31 xs alias show="doas /sbin/pfctl -t \$HTTPBAN_TABLE -T show"
26 bcc5ac46 2024-01-31 xs alias ban="doas /sbin/pfctl -t \$HTTPBAN_TABLE -T add -f-"
27 bcc5ac46 2024-01-31 xs alias grace="doas /sbin/pfctl -t \$HTTPBAN_TABLE -T delete -f-"
29 88f3e13d 2024-01-30 xs # Filter functions returning 'count IP' based on HTTP return code
30 3115cd3d 2024-01-30 xs IN_300_HOSTS() {
31 3115cd3d 2024-01-30 xs awk '$(NF-1) >= 300 && $(NF-1) < 400 { print $2 }' | sort | uniq -c
34 3115cd3d 2024-01-30 xs IN_400_HOSTS() {
35 3115cd3d 2024-01-30 xs awk '$(NF-1) >= 400 && $(NF-1) < 500 { print $2 }' | sort | uniq -c
38 3115cd3d 2024-01-30 xs IN_500_HOSTS() {
39 3115cd3d 2024-01-30 xs awk '$(NF-1) >= 500 && $(NF-1) < 600 { print $2 }' | sort | uniq -c
42 3115cd3d 2024-01-30 xs # Our local logs
44 3115cd3d 2024-01-30 xs doas /bin/cat /var/www/logs/access.log
45 d2c27298 2024-01-30 xs doas /usr/bin/zcat /var/www/logs/access.log.*gz 2>/dev/null
49 3115cd3d 2024-01-30 xs awk -vtrig="${1:-10}" ' $1 >= trig { print $2 }'
52 d43799e5 2024-01-30 xs # shellcheck disable=SC2086,SC2046
54 88f3e13d 2024-01-30 xs set -- $HTTPBAN_WHITELIST
55 d43799e5 2024-01-30 xs set -- $(for ip; do printf -- '-e %s ' "$ip"; done)
57 88f3e13d 2024-01-30 xs access | IN_500_HOSTS | limit "$HTTPBAN_LIMIT_500"
58 d43799e5 2024-01-30 xs access | IN_400_HOSTS | limit "$HTTPBAN_LIMIT_400"
59 88f3e13d 2024-01-30 xs access | IN_300_HOSTS | limit "$HTTPBAN_LIMIT_300"
60 88f3e13d 2024-01-30 xs } | sort | uniq | {
61 b72ca3a5 2024-01-30 xs if test -n "$*"; then grep -v "$@"; else cat; fi
67 d2c27298 2024-01-30 xs block | sort >/tmp/http-ban.new
69 88f3e13d 2024-01-30 xs # Diff processing
70 d2c27298 2024-01-30 xs show | sort | awk '{ print $1 }' >/tmp/http-ban.current
71 d2c27298 2024-01-30 xs NEW=$(comm -23 /tmp/http-ban.new /tmp/http-ban.current)
72 d2c27298 2024-01-30 xs GRACE=$(comm -13 /tmp/http-ban.new /tmp/http-ban.current)
74 d2c27298 2024-01-30 xs rm /tmp/http-ban.*
76 d2c27298 2024-01-30 xs test -n "$NEW" -o -n "$GRACE" || exit 0
78 3115cd3d 2024-01-30 xs # There's new IP's
79 3115cd3d 2024-01-30 xs if test -n "$NEW"; then
80 3115cd3d 2024-01-30 xs log Banning new IPs:
89 3115cd3d 2024-01-30 xs # There's Old IP's not attacking anymore
90 3115cd3d 2024-01-30 xs if test -n "$GRACE"; then
91 3115cd3d 2024-01-30 xs log Gracing old IPs: