| Lantzvillian |
03-03-2011 09:21 PM |
Howto? threshold value for failed SSH comms' script
Hi all,
I was playing around with a script that seems to work relatively for my needs when SSH comms fail - AKA a user or someone attacking. This works...
But, how would I add a threshold value? lets say if there are 3 entries, then perform add the IP to iptables as a rule.
Code:
#!/bin/sh
# ------------------------------------
# FIREWALL SCRIPT
#
# March 1st, 2011
#
# Purpose:
# Add offending IP from failed SSH connections
# to the iptables (firewall) rules.
#
# ------------------------------------
## Vars:
TIMETHRESHOLD="10"
## Explanation:
# This program will match lines
# Illegal user (userid) from (host)
# Failed password for (userid) from (host) (...)
# and adds (host) to the iptables blacklist chain
# $blockchain
## Misc preparations:
rm -f hostfile
touch temphostfile
# Disabled IPs can be obtained from /etc/sysconfig/iptables
grep DROP /etc/sysconfig/iptables|awk '{print $5}' >temphostfile
# ------------------------ SSHD FAILURES -------------------------
grep Did /var/log/secure|awk '{print $12}' >>temphostfile
grep "Invalid user" /var/log/secure|awk '{print $10}' >>temphostfile
grep "Maximum login" /var/log/secure|awk '{print $7}'|sed 's/.*\[\(.*\)\])/\1/g' >>temphostfile
grep "Failed password for" /var/log/secure |awk '{print $11}' >>temphostfile
#
# ------------------ REDUCE IPS IN TMP FILE -------------------
size=`/usr/bin/wc temphostfile|awk '{print $1}'`
i=0
echo $(/usr/bin/wc temphostfile|awk '{print $1}')
echo $(cat temphostfile)
while test $i -lt $size
do
us=`sed -n 1p temphostfile`
sed /$us/d temphostfile >temphostfiles
echo $us >>hostfile
cp -f temphostfiles temphostfile
size=`/usr/bin/wc temphostfile|awk '{print $1}'`
done
rm -f temphostfile temphostfiles temp0 temp
#
# ------------------ CREATE THE FIREWALL RULES--------------------------
size=`wc hostfile|awk '{print $1}'`
size=`expr $size + 1`
/sbin/iptables -F
i=1
while test $i -lt $size
do
ip=`sed -n "$i"p hostfile`
i=`expr $i + 1`
/sbin/iptables -A INPUT -s $ip -j DROP
done
|