]# vi denyip.sh
#!/bin/bash
# Script by Yunkiman
# Modified by icworld
# Second Modified by dksniper(http://blog.naver.com/dksniper)
# Convert oops-firewall to hosts.deny
days=`date +%d`
nowdir=`pwd`
savedir="$nowdir/denylist"
cleanfile="$nowdir/hosts.dat"
cnt=0
if [ -e /var/log/secure ]
then
for i in $( tail -n50 /var/log/secure | grep "^[A-Za-z ]\{4,\}$days" | grep "Failed password" | awk -F "from" '{print $2}' | awk '{prin
t $1}' | sort | uniq -c | awk '$1 > 8 {print $2}'); do
denyip=${i#::ffff:} #ip format ::ffff:0.0.0.0
echo "ALL : $denyip" >> $savedir
cnt=$(($cnt+1))
done
fi
#If not found then exit
if [ $cnt = 0 ]
then
exit
fi
for a in $( cat /etc/hosts.deny | grep "ALL : " | awk '{print $3}' | sort | uniq -c | awk '{print $2}'); do
echo "ALL : $a" >> $savedir
done
#cp $cleanfile /etc/hosts.deny
echo "#
# hosts.deny This file describes the names of the hosts which are
# *not* allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow. In particular
# you should know that NFS uses portmap!
" > /etc/hosts.deny
for b in $( cat $savedir | grep "ALL : " | awk '{print $3}' | sort | uniq -c | awk '{print $2}'); do
echo "ALL : $b" >> /etc/hosts.deny
done
rm -rf $savedir
/etc/init.d/xinetd reload