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