Super Simple SSHd IP blocking
So I dont have everything up on GitHub yet, and in fact there is some small redesign needed for what I have, purely for tracking whats been block from where. In the meantime I thought someone looking throught this thread might just want something fairly simple (without IP tracking/logging), so here is SS:FragWhare:
#!/bin/sh
### SS:FragWhare (run as root)
### Super Simple (fake) Fire Wall
### (Whare is Maori for "house")
### - to verify everything is working:
### ./ss-fragwhare.sh > verify.ssfw
### - to start as a "service", use:
### ./ss-fragwhare.sh false &
# default interval & increment, in seconds
I=1;
# maximum time between checks, in seconds
M=10;
# default fails before blocking an IP address
K=4;
# easier option to change "tail lines"
# default for tail is 10, dont go above 20
N=10
# where to record the Process ID
#echo "$$" > "ss-fragwhare.pid";
# verbose(-ish);
Q=true;
if [ "$1" = "false" ]; then
Q=false;
fi;
# main loop;
O=""; S=""; T=0;
while true; do
O="$S";
# we can use `uniq` here because its only 10 lines max
S=$(tail -n $N /var/log/messages | grep "auth.info sshd" | cut -d \: -f 4 | grep -E -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | uniq);
if [ "$S" = "$O" ]; then
# relieve pressure if no change;
T=$(expr $T + $I);
if [ $T -gt $M ]; then
T=$M;
# dont do other things here, as it will exponentially use more processor time
# this script will only consume about 0.16% of CPU on a single core system
# BusyBox will increase memory usage by 1.5MB x2 (1x for script, 1x for sleep)
fi;
$Q && echo "$$:ss-fragwhare: waiting: $T";
else
for xADDR in $S ; do
# minimum length of an IPv4 address is 7
X=$(echo "$xADDR" | wc -c);
# but input might be broken, so double check
Z=$(echo "$IP" | cut -d \. -f 4);
if [ $X -lt 7 -o -z "$Z"]; then
$Q && echo "$$:ss-fragwhare: ..hmm.. '$xADDR'";
else
# make sure this is th correct name for your system
C=$(grep "$xADDR" /var/log/messages | wc -l);
if [ $C -gt $K ]; then
# silence "wrong input"
ip route add blackhole $T/32 2>/dev/null;
$Q && echo "$$:ss-fragwhare: added $xADDR";
fi;
fi;
done;
T=$I;
fi;
sleep $T;
done;
### - to work across reboots:
### ip route show | grep blackhole > blackhole.ssfw
### cat blackhole.ssfw | xargs -n 1 -I {} ip route add {}
### - after 400-500 IPv4 are blocked, SSHd will be "quiet"
Whare - said: furry (or farry) as in ferry, not furry as in animal - is Maori for house, so SS:FW for short, implies FireWall, but is it really? After all it just blocks some IPv4 address from sshd entries in a tail /var/log/messages (10 lines) list. It does not record anything, and needs to be restarted after a reboot. It’s not a cron job, because it needs to run every second (well it could).
The only difference between this and what I use (yes I will be puuting it up on GitHub too), is that all my scripts pass to a dedicated ./block-this-ipv4.sh IPv4-address script, which means I can offload (or consolidate) some of the checks.
You can try and use ip route save 1> dump.rt and ip route restore < dump.rt, but that produces a binary format suitable for manipulation with rtnetlink format tools. Better to just use ip route show | grep blackhole > dump.blackhole, and then after reboot, use something like cat dump.blackhole | xargs -n 1 -I {} ip route add {}/32, which is human readable.
EDIT:
grep was teaked from . to \.