防止SSH暴力破解
# 今天发现有人“搞事情”, 随手lastb 一看估计在爆破用户名呢。。。
# 把出现“Invalid User”大于30次的就直接丢进"/etc/hosts.deny"
#!/bin/bash # Author: Loki # Date: 2020-01-13 # Readme: autocheck "Invalid user" and write down "/etc/hosts.deny" # Brute force "username" grep "Invalid user" /var/log/secure|awk '{print $10}'|sort|uniq -c|awk '{print $2"="$1;}' > /tmp/black_ip.txt max_count="30" for item in `cat /tmp/black_ip.txt`;do ip_addr=`echo $item |awk -F= '{print $1}'` ip_num=`echo $item |awk -F= '{print $2}'` if [ $ip_num -gt $max_count ]; then grep $ip_addr /etc/hosts.deny > /dev/null if [ $? -gt 0 ]; then echo "sshd:$ip_addr" >> /etc/hosts.deny fi fi done
# 最后加入到计划任务 5分钟执行一次
*/5 * * * * <执行用户名> /bin/bash /xxx/xxx/BP_user_protect.sh