目 录CONTENT

文章目录

记录一次ssh被攻击

所念皆星河
2020-01-14 / 0 评论 / 0 点赞 / 15 阅读 / 1484 字

1.起因频繁被ssh

2.编辑脚本

vim /usr/local/bin/secure_ssh.sh

#! /bin/bash
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /usr/local/bin/black.list
for i in `cat  /usr/local/bin/black.list`
do
  IP=`echo $i |awk -F= '{print $1}'`
  NUM=`echo $i|awk -F= '{print $2}'`
  if [ ${#NUM} -gt 1 ]; then
    grep $IP /etc/hosts.deny > /dev/null
    if [ $? -gt 0 ];then
      echo "sshd:$IP:deny" >> /etc/hosts.deny
    fi
  fi
done

添加执行权限

chmod +x /usr/local/bin/secure_ssh.sh

添加计划任务

*/2 * * * * sh /usr/local/bin/secure_ssh.sh

执行效果

[root@qigx ~]# cat /etc/hosts.deny
#
# hosts.deny    This file contains access rules which are used to
#               deny connections to network services that either use
#               the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               The rules in this file can also be set up in
#               /etc/hosts.allow with a 'deny' option instead.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
#
sshd:103.129.208.74:deny
sshd:142.93.48.117:deny
sshd:167.71.239.92:deny
0

评论区