Перш за все, не винаходити колесо. Саме це і denyhostsє для:
DenyHosts is a python program that automatically blocks ssh attacks by
adding entries to /etc/hosts.deny. DenyHosts will also inform Linux
administrators about offending hosts, attacked users and suspicious
logins.
Наскільки я знаю, denyhostsце лише для sshз'єднань, але є також і
fail2banте, що стосується майже нічого:
Fail2Ban consists of a client, server and configuration files to limit
brute force authentication attempts.
The server program fail2ban-server is responsible for monitoring log
files and issuing ban/unban commands. It gets configured through a
simple protocol by fail2ban-client, which can also read configuration
files and issue corresponding configuration commands to the server.
Обидва доступні у сховищах:
sudo apt-get install denyhosts fail2ban
Ви також можете це написати, якщо вам це подобається. Щось на зразок:
#!/usr/bin/env sh
netstat -an |
awk -vmax=100 '/tcp/{split($5,a,":"); if(a[1] > 0 && a[1]!="0.0.0.0"){c[a[1]]++}}
END{for(ip in c){if(c[ip]>max){print ip}}}' |
while read ip; do iptables -I INPUT 1 -s "$ip" -j DROP; done
awkВитягне IP - адреса і порахувати їх і друкувати тільки ті , які з'являються більш maxраз (тут, -vmax=100змініть його відповідним чином ). Потім IP-адреси подаються в цикл, який виконує відповідне iptablesправило.
Щоб запустити цю програму 24/7, я б зробив cronjob, який виконує команду вище кожної хвилини. Додайте цей рядок до/etc/crontab
* * * * * root /path/to/script.sh