2022/03 作者:ihunter 0 次 0
denyhosts也是一个防止sshd暴力破解的开源软件,他可以有效的阻止对ssd服务器的攻击。它具有以下的特点:
1. 对/var/log/secure日志文件进行分析,查找所有的登录尝试,并且过滤出失败和成功的尝试。
2.记录下所有失败的登录尝试的用户名和主机,如果超过阀值,则记录主机。
3.保持对每一个登录失败的用户(存在系统中或不存在系统中的用户)的跟踪
4.对每一个可疑的登录进行跟踪。(虽然登录成功,但是有很多次登录失败的记录)
5.将可疑地址的主机加入到/etc/hosts.deny文件中。
cd /opt
yum -y install python2
python2 setup.py install
touch /var/log/auth.log /var/lib/denyhosts/allowed-warned-hosts /etc/hosts.deny /etc/hosts.allow
chmod -R 777 /var/log/auth.log /var/run/denyhosts.pid /var/lib/denyhosts/allowed-warned-hosts /etc/hosts.deny /etc/hosts.allow
wget https://udomain.dl.sourceforge.net/project/denyhosts/denyhosts/3.0/denyhosts-3.0.tar.gz
tar -xzvf denyhosts-3.0.tar.gz
cd denyhosts-3.0/
cp denyhosts.conf /etc/denyhosts.conf
cp daemon-control-dist daemon-control
vim daemon-control
DENYHOSTS_BIN = "/opt/denyhosts-3.0/denyhosts.py" DENYHOSTS_LOCK = "/run/denyhosts.pid" DENYHOSTS_CFG = "/opt/denyhosts-3.0/denyhosts.conf" PYTHON_BIN = "/usr/bin/env python2"
vim /etc/denyhosts.conf
SECURE_LOG = /var/log/secure #要读取安全日志路径 HOSTS_DENY = /etc/hosts.deny #将阻止IP写入到hosts.deny PURGE_DENY = 1y #设定过多久后清除已阻止IP (m=分钟,h=小时,d=天,w=周,y=年) BLOCK_SERVICE = ALL #阻止服务名 DENY_THRESHOLD_INVALID = 2 #允许无效用户登录失败的次数 DENY_THRESHOLD_VALID = 2 #允许普通用户登录失败的次数 DENY_THRESHOLD_ROOT = 2 #允许root登录失败的次数 DENY_THRESHOLD_RESTRICTED = 1 #设定 deny host 写入到该资料夹 WORK_DIR = /var/lib/denyhosts #将deny的host或ip纪录到Work_dir中 ETC_DIR = /etc #配置文件默认目录 SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES #假如设定为YES,那么已经设为白名单中的IP登陆失败也会被设为可疑,也会被列入黑名 HOSTNAME_LOOKUP=NO #是否做域名反解 LOCK_FILE = /var/lock/subsys/denyhosts #将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务 IPTABLES = /sbin/iptables #ADMIN_EMAIL = root@localhost #SMTP_HOST = localhost #SMTP_PORT = 25 #SMTP_FROM = DenyHosts#SMTP_SUBJECT = DenyHosts Report AGE_RESET_VALID=5y AGE_RESET_ROOT=25y AGE_RESET_RESTRICTED=25y AGE_RESET_INVALID=10y ALLOWED_HOSTS_HOSTNAME_LOOKUP=NO DAEMON_LOG = /var/log/denyhosts #自己的日志文件 DAEMON_SLEEP = 30s DAEMON_PURGE =10y #该项与PURGE_DENY 设置成一样,也是清除hosts.deniedssh 用户的时间 SYNC_UPLOAD = no SYNC_DOWNLOAD = no
vi denyhosts.py -将第一行修改为 $!/usr/bin/env python2
vi daemon-control -将第一行修改为 $!/usr/bin/env python2
---启动denyhosts
/usr/bin/env python2 /opt/denyhosts-3.0/denyhosts.py --daemon --config=/etc/denyhosts.conf
---设置自启动
vi /usr/lib/systemd/system/denyhosts.service
[Unit] Description=SSH log watcher Before=sshd.service [Service] Type=forking #ExecStartPre=/bin/rm -rf /var/lib/denyhosts ExecStart=/opt/denyhosts-3.0/daemon-control start ExecStop=/opt/denyhosts-3.0/daemon-control stop #PIDFile=/var/lib/denyhosts/denyhosts.pid [Install] WantedBy=multi-user.target
---启动服务
systemctl stop denyhosts.service
systemctl status denyhosts.service
systemctl restart denyhosts.service
systemctl enable denyhosts.service
cat /var/lib/denyhosts/allowed-warned-hosts
cat /etc/hosts.deny
cat /etc/hosts.allow
====
rm -rf /var/lock/subsys/denyhosts
上篇:
常用红队安全攻防总结
下篇:
可视化web管理的内网穿透神器NPS