- 论坛徽章:
- 0
|
倒是以前写过这样的脚本,我发上来吧,你看下,但是效率比较底,不过,你要实施的时候最好给自己留条后路,否则。。。。
- #!/bin/bash
- ######################################################################
- #This scripts is for check the sshd server ,version 0.1 Bate
- #write by finddream
- #if you have some advise,you can give me a mail:finddream863@163.com
- ######################################################################
- #setup the local language is english
- export LANG=en
- #setup the around file path
- SSHD_LOG=/var/log/secure
- SSHD_FAILED_FILE=/root/ssh_faild.txt
- FAILED_WORDS="Failed password"
- IPTABLES_RULE=/etc/sysconfig/iptables
- #setup the around command path
- CAT=/bin/cat
- GREP=/bin/grep
- AWK=/bin/awk
- UNIQ=/usr/bin/uniq
- IPTABLES=/sbin/iptables
- ECHO=/bin/echo
- SLEEP=/bin/sleep
- TOUCH=/bin/touch
- SORT=/bin/sort
- SERVICE=/sbin/service
- #setup the ssh failed count
- SSH_FAILED_COUNT=3
- SSHD_PORT=22
- DATE_MONTH=`date +%b`
- DATE_DAY=`date +%d`
- #check the sshd log
- if [ -e $SSHD_LOG ]
- then
- $ECHO "The sshd log is exits!"
- else
- $ECHO "The sshd log is not exits,please check you OS!"
- exit 1
- fi
- #check the sshd failed file
- if [ -e $SSHD_FAILED_FILE ]
- then
- $ECHO "The sshd failed file is exits!"
- else
- $ECHO "The sshd failed file is not exits,OS will touch it............"
- $SLEEP 3
- $TOUCH $SSHD_FAILED_FILE
- fi
- #check the sshd failed IP
- $CAT $SSHD_LOG |$GREP "$FAILED_WORDS"|$GREP "\<$DATE_MONTH\>"|$GREP "\<$DATE_DAY\>" |$AWK -F " " '{print $11}' |$AWK -F ":" '{print $4}' |$SORT|$UNIQ -c > $SSHD_FAILED_FILE
- #if the ssh failed beyond the sshd_failed_count,OS will DROP it
- while read COUNT IP
- do
- if [ $COUNT -ge $SSH_FAILED_COUNT ]
- then
- $GREP "\-A\ INPUT\ \-s\ $IP\ \-p\ tcp \-m\ tcp\ \-\-dport\ SSHD_PORT\ \-j\ DROP" $IPTABLES_RULE
- if [ $? -ne 0 ]
- then
- $IPTABLES -t filter -A INPUT -s $IP -p tcp -m tcp --dport $SSHD_PORT -j DROP
- $SERVICE iptables save
- else
- $ECHO "the rule is exits!"
- fi
- else
- echo " "
- fi
- done < $SSHD_FAILED_FILE
- exit 0
复制代码 |
|