• CENTOS设置禁止部分IP访问


        近期发现,我的服务器每次root登陆都有N次失败的登陆尝试,很明显被暴力破解ing。寻思着得提前“自救”。

        思路:

          ①、暴力破解都是按照字典尝试登陆,登陆失败20次就把该ip给拉黑(反正我的IP又不会输错密码登陆)

          ②、只允许密钥登陆

        由于我的环境还是需要密码登陆,故而选择方法1。

     

    原理:

    对于能过xinetd程序启动的网络服务,比如ftp telnet,我们就可以修改/etc/hosts.allow和/etc/hosts.deny的配制,来许可或者拒绝哪些IP、主机、用户可以访问。

    但不是任何服务程序都能使用TCP_wrappers的,例如使用命令ldd /usr/sbin/sshd,如果输出中有libwrap,则说明可以使用TCP_wrappers, 即该服务可以使用/etc/hosts.allow和/etc/hosts.deny,如果输出没有libwrap则不可使用。

     

    从上图看,我的server是可以用这种方法的。直接写脚本secure_ssh.sh,

    #! /bin/bash
    lastb > /tmp/lastb.txt
    cat /tmp/lastb.txt |grep ssh|awk '{print $3}' |uniq -c|awk '($1>"1"){print}' > /tmp/black.txt
    
    cat /tmp/black.txt | while read line
    do
      IP=`echo $line |awk -F ' ' '{print $2}'`
      NUM=`echo $line |awk -F ' ' '{print $1}'`
      if [ $NUM -gt 20 ];then
        grep $IP /etc/hosts.deny > /dev/null
        if [ $? -gt 0 ];then
          echo "sshd:$IP:deny" >> /etc/hosts.deny
        fi
      fi
    done
    

        加定时任务。

    #someone try to login server by ssh fail more than 20 times, then put it in black
    */5  *  *  *  *      /bin/sh /home/hik/secure_ssh.sh
    

        结果验证:没问题,自己用其他服务器试过,错20次就拉黑了。

    PS:

    1、可能会遇到脚本执行了,但是不生效,原因是openssh版本可能过低,最好yum重装一下最新版本openssh; 

    2、方法比较拙,有更好的方法可以留言赐教,谢谢;

  • 相关阅读:
    React简明学习
    react-router简明学习
    react组件生命周期
    在vue中使用css modules替代scroped
    深入理解javascript中的事件循环event-loop
    javascript中的内存管理和垃圾回收
    移动端中的陀螺仪
    基于create-react-app的再配置
    vscode常用设置
    更高效地使用搜索引擎
  • 原文地址:https://www.cnblogs.com/lynsen/p/14720693.html
Copyright © 2020-2023  润新知