云服务器暴露在公网上,天天都有大量的暴力密码破解,更换端口,无济于事,该脚本监控安全日志,获取暴力破解的对方ip,加入hosts黑名单python
描述 | 路径 |
---|---|
登陆安全日志 | /var/log/secure |
hosts黑名单 | /etc/hosts.deny |
hosts白名单 | /etc/hosts.allow |
subprocess.Popen('tail -f XXXXX')
打开登陆安全日志while True
循环经过 readline
的形式,达到实时监控日志文件的目的Invalid user \w+ from (\d+\.\d+\.\d+\.\d+)
说明是在尝试登陆不存在的用户名,那么直接拉黑当前ipFailed password for \w+ from (\d+\.\d+\.\d+\.\d+)
说明是在尝试已有用户的密码,而后判断当前ip的尝试次数,达到阈值的时候,拉黑当前ipnohup python3 -u deny_login_ip.py >> your_log_name.log 2>&1 &
your_log_name.log
sshd:172.168.0.1
, 一行一个详细逻辑,见详细的代码注释git
github地址:https://github.com/luocfa/deny_login_ipgithub