# 今天發現有人“搞事情”, 隨手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