Linux利用hosts.deny防止暴力破解ssh

Myth丶恋晨 2023-05-31 14:00 97阅读 0赞

Linux利用hosts.deny防止暴力破解ssh

1.背景

最近服务器被同一个ip多次访问我的22号端口,能用22号端口的无非是ssh连接。

1.1 查看端口连接信息
[root@docker denyhosts]# ss -anp | grep tcp | grep sshd
tcp LISTEN 0 128 *:22 : users:((“sshd”,pid=3737,fd=3))
tcp ESTAB 23 0 172.16.102.26:22 147.16.11.17:56402 users:((“sshd”,pid=11463,fd=3))
tcp ESTAB 0 36 172.16.102.26:22 116.24.66.59:25224 users:((“sshd”,pid=11263,fd=3))

1.2 查看系统安全日志
[root@docker denyhosts]# tail -f /var/log/secure
Oct 17 19:33:48 izbp150ikdomqe3b32qaubz sshd[11417]: refused connect from 47.106.141.17 (147.16.11.17)
Oct 17 19:34:12 izbp150ikdomqe3b32qaubz sshd[11421]: Failed password for root from 147.16.11.17 port 56398 ssh2
Oct 17 19:34:13 izbp150ikdomqe3b32qaubz sshd[11421]: Failed password for root from 147.16.11.17 port 56398 ssh2
Oct 17 19:34:13 izbp150ikdomqe3b32qaubz sshd[11421]: Connection closed by 147.16.11.17 [preauth]
Oct 17 19:35:26 izbp150ikdomqe3b32qaubz sshd[11428]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=147.16.11.17 user=root
Oct 17 19:35:26 izbp150ikdomqe3b32qaubz sshd[11428]: pam_succeed_if(sshd:auth): requirement “uid >= 1000” not met by user “root”
Oct 17 19:35:28 izbp150ikdomqe3b32qaubz sshd[11428]: Failed password for root from 147.16.11.17 port 56400 ssh2
Oct 17 19:36:23 izbp150ikdomqe3b32qaubz sshd[11428]: Failed password for root from 147.16.11.17 port 56400 ssh2
Oct 17 19:36:24 izbp150ikdomqe3b32qaubz sshd[11428]: Failed password for root from 147.16.11.17 port 56400 ssh2
Oct 17 19:36:24 izbp150ikdomqe3b32qaubz sshd[11428]: Connection closed by 147.16.11.17 [preauth]

1.3 查询ip归属地

2. 利用denyhosts 防止暴力破解ssh

DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP的功能。

思路:利用读取到/var/log/secure登陆失败的IP信息,我们可以设定一个规则,如果登陆失败超过多少次,我们把这个IP写到 /etc/hosts.deny中,拒绝访问!

2.1 先把始终允许的IP填入 /etc/hosts.allow ,因为自己访问超过了次数那就尴尬了。

  1. [root@docker denyhosts]# cat /etc/hosts.allow
  2. #
  3. # hosts.allow This file contains access rules which are used to
  4. # allow or deny connections to network services that
  5. # either use the tcp_wrappers library or that have been
  6. # started through a tcp_wrappers-enabled xinetd.
  7. #
  8. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  9. # for information on rule syntax.
  10. # See 'man tcpd' for information on tcp_wrappers
  11. #
  12. sshd: 147.16.11.17

2.2 安装DenyHosts 官方网站为:http://denyhosts.sourceforge.net

  1. tar -zxvf DenyHosts-2.6.tar.gz
  2. cd DenyHosts-2.6
  3. python setup.py install

2.3 配置

  1. cd /usr/share/denyhosts/
  2. cp denyhosts.cfg-dist denyhosts.cfg
  3. vi denyhosts.cfg
  4. SECURE_LOG = /var/log/secure #要读取安全日志路径,默认的不管它
  5. HOSTS_DENY = /etc/hosts.deny #将阻止IP写入到hosts.deny,默认的不管它
  6. PURGE_DENY = 1d #设定过多久后清除已阻止IP (m=分钟,h=小时,d=天,w=周)
  7. BLOCK_SERVICE = sshd #阻止服务名
  8. DENY_THRESHOLD_INVALID = 5 #允许无效用户登录失败的次数**
  9. DENY_THRESHOLD_VALID = 10 #允许普通用户登录失败的次数**
  10. DENY_THRESHOLD_ROOT = 3 #允许root登录失败的次数**
  11. (上面3个登陆失败的次数建议不要设定太苛刻,以免自己输错了被列入黑名单了)
  12. DENY_THRESHOLD_RESTRICTED = 1 #设定 deny host 写入到该资料夹
  13. WORK_DIR = /usr/local/share/denyhosts/data #将deny的host或ip纪录到Work_dir中
  14. SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES #假如设定为YES,那么已经设为白名单中的IP登陆失败也会被设为可疑,也会被列入黑名单中,设定NO的意思就相反。
  15. HOSTNAME_LOOKUP=YES #如果可以的话记录登陆失败的hostname
  16. LOCK_FILE = /var/lock/subsys/denyhosts #将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务。
  17. HOSTNAME_LOOKUP=NO #是否做域名反解
  18. ADMIN_EMAIL = #设置管理员邮件地址
  19. DAEMON_LOG = /var/log/denyhosts #自己的日志文件
  20. DAEMON_PURGE = 1d #该项与PURGE_DENY 设置成一样,也是清除hosts.deniedssh 用户的时间。

2.4 设置启动脚本

  1. cp daemon-control-dist daemon-control
  2. chown root daemon-control
  3. chmod 700 daemon-control
  4. 完了之后执行daemon-contron start就可以了。
  5. **./daemon-control start**
  6. 如果要使DenyHosts每次重起后自动启动还需做如下设置:
  7. ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts
  8. chkconfig --add denyhosts
  9. chkconfig denyhosts on
  10. ps -ef|grep denyhosts #检查是否启动
  11. [root@docker denyhosts]# ps -ef|grep denyhosts
  12. root 11309 1 0 19:29 ? 00:00:00 python /usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg
  13. root 11523 11265 0 19:51 pts/0 00:00:00 grep --color=auto denyhosts

vim /etc/hosts.deny #查看内是否有禁止的IP,有的话说明已经成功了。

  1. [root@docker denyhosts]# cat /etc/hosts.deny
  2. #
  3. # hosts.deny This file contains access rules which are used to
  4. # deny connections to network services that either use
  5. # the tcp_wrappers library or that have been
  6. # started through a tcp_wrappers-enabled xinetd.
  7. #
  8. # The rules in this file can also be set up in
  9. # /etc/hosts.allow with a 'deny' option instead.
  10. #
  11. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  12. # for information on rule syntax.
  13. # See 'man tcpd' for information on tcp_wrappers
  14. #
  15. # DenyHosts: Thu Oct 17 19:44:52 2019 | sshd: 147.16.11.17
  16. sshd: 147.16.11.17

发表评论

表情:
评论列表 (有 0 条评论,97人围观)

还没有评论,来说两句吧...

相关阅读

    相关 限制ssh暴力破解方法

    暴力破解ssh的方法是很常见的。要减少这个暴力攻击对我们的伤害。我总结了两种方法。   方法一:直接把sshd的22端口改成一个陌生的非其他服务的端口   步骤如下:

    相关 linux 防止ssh暴力破解的方法

    上两个星期发生了一件事情,让我感觉到安全是多么的重要,因为租了一天学生机Linux作为服务器,没想到用了没两个月就出现问题了。给外国黑客ssh暴力破解,然后安装挖矿病毒,导致c

    相关 linux暴力破解工具

    对于 Linux 操作系统来说,一般通过 VNC、Teamviewer 和 SSH 等工具来进行远程管理,SSH 是 Secure Shell 的缩写,由 IETF 的网络小组