获取暴力破解密码的来源IP地址,并自动添加firewalld规则

 #!/bin/bash
#脚本用途:从日志文件/var/log/secure中获取暴力破解密码的来源IP地址,并自动添加firewalld规则(破解次数超过10次)。
#配合crontab使用
#
#文件路径
file='/var/log/secure';
logfile='./securelog.txt';
#
#获取当前时间
current=`date +%Y-%m-%d_%H:%M:%S`;
echo "当前时间是:" $current "begin..." >> $logfile;
#
#截取当前时间中的日期
var=`date | cut -c 5-11`;
#过滤/var/log/secure文件中当前日期,Failed password出现的次数;如果大于10次,则考虑增加firewalld规则
row_number=`cat /var/log/secure| grep "$var"| grep "Failed password"| wc -l`
if [ $row_number -lt 10 ];then
    echo "$row_number次破解,暂不处理" >> $logfile;
elif [ $row_number -ge 10 ];then
    echo "not secure"  >> $logfile;
    #
    #获取当前firewalld中IP地址
    #fire=`firewall-cmd --list-rich-rules | grep -v '/'|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'`;
    #
    #获取ip地址并去重,统计个数
    num=`cat $file | grep "Failed password"|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'| sort -u| wc -l`
    #echo $num;
    #
    #做if判断,如果为0,则没有攻击;如果为1,则有一个攻击来源ip,直接添加firewall规则;如果大于1个,则for循环抛出ip,添加规则。
    if [ $num -eq 0 ];then
        echo "very secure!">>$logfile;
    elif [ $num -eq 1 ];then
        #repeat 获取单个ip的重复次数,如果大于10,则认为是攻击,添加规则
        repeat=`cat $file | grep "Failed password"|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'| wc -l`
        if [ $repeat -gt 10 ];then
            ipcount=`cat $file | grep "Failed password"|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'| sort -u`
            firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address=$ipcount reject"
            if [ $? -eq 0 ]; then
                echo "only one ip;add firewall rule succeed! $ipcount">>$logfile;
            else
                echo "only one ip;add firewall rule failed! $ipcount">>$logfile;
            fi
        fi
    elif [ $num -gt 1 ];then
        echo "more then one ip;" >> $logfile;
        for ip in `cat $file | grep "Failed password"|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'| sort -u`
        do
            #    echo $ip;
            #    firewall-cmd --add-rich-rule="rule family='ipv4' source address=$ip reject"
            if [ `cat $file | grep "Failed password"|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'|grep $ip|wc -l` -gt 10 ];then
                firewall-cmd --add-rich-rule="rule family='ipv4' source address=$ip reject"
                if [ $? -eq 0 ]; then
                    echo "more then one ip;add firewall rule succeed! $ip">>$logfile;
                else
                    echo "more then one ip;add firewall rule failed! $ip">>$logfile;
                   fi
            fi
        done
    else
        echo "something error!!!" >> $logfile;
    fi
fi
#end
 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值