天天看点

ssh爆破攻击,denyhosts.service服务,hosts.deny拒绝ssh登录文档

查询最近50次的失败尝试登录记录,可以发现哪些IP在尝试暴力登录ssh

实际访问的文件/var/log/btmp,该日志文件为二进制文件,不可以直接访问,只能通过lastb命令访问,不可更改,但可以删除,从而达到删除相应记录。

[root@sz-nginx02 ~]# lastb -n 50
help     ssh:notty    218.17.149.227   Sun Jan 24 16:28 - 16:28  (00:00)    
hdfs     ssh:notty    218.17.149.227   Sun Jan 24 15:16 - 15:16  (00:00)    
michael  ssh:notty    59-120-151-118.h Sun Jan 24 15:03 - 15:03  (00:00)    
mfs      ssh:notty    59-120-151-118.h Sun Jan 24 14:20 - 14:20  (00:00)    
hadoop   ssh:notty    218.17.149.227   Sun Jan 24 14:03 - 14:03  (00:00)    
media    ssh:notty    59-120-151-118.h Sun Jan 24 13:40 - 13:40  (00:00)    
guest    ssh:notty    218.17.149.227   Sun Jan 24 12:50 - 12:50  (00:00)    
a        ssh:notty    ec2-54-165-101-6 Sun Jan 24 11:57 - 11:57  (00:00)    
grayson  ssh:notty    218.17.149.227   Sun Jan 24 11:37 - 11:37  (00:00)    
admin    ssh:notty    212-83-174-199.r Sun Jan 24 11:06 - 11:06  (00:00)    
ubnt     ssh:notty    212-83-174-199.r Sun Jan 24 11:06 - 11:06  (00:00)    
。。。。      

last 查看最近的ssh登录记录

实际访问文件/var/log/wtmp,二进制文件,不可直接cat等访问,只能通过last命令访问。不能修改,但是可以删除,以清除所有ssh登录记录。

[root@mop-general log]# last -20
root     pts/0        122.9.2.2        Wed Sep  7 18:42 - 18:43  (00:00)    
root     pts/4        180.190.112.198  Wed Sep  7 16:06   still logged in   
root     pts/3        180.191.152.214  Wed Sep  7 15:58 - 18:10  (02:12)    
root     pts/1        180.191.96.24    Wed Sep  7 15:35 - 18:01  (02:26)    
root     pts/0        180.191.100.87   Wed Sep  7 14:37 - 17:16  (02:38)    
root     pts/2        180.191.100.87   Wed Sep  7 11:42 - 17:16  (05:34)    
root     pts/0        49.144.88.139    Tue Sep  6 15:56 - 13:38  (21:42)    
root     pts/1        49.144.88.139    Tue Sep  6 14:39 - 13:38  (22:59)    
root     pts/0        49.144.124.87    Tue Sep  6 10:59 - 15:04  (04:04)    
root     pts/3        49.144.64.155    Mon Sep  5 16:32 - 11:25  (18:53)    
root     pts/2        49.144.64.155    Mon Sep  5 16:32 - 11:25  (18:53)    
root     pts/0        49.144.64.155    Mon Sep  5 14:51 - 18:39  (03:47)    
root     pts/1        49.144.64.155    Mon Sep  5 11:02 - 18:39  (07:37)    
root     pts/0        49.144.64.155    Sun Sep  4 14:41 - 11:15  (20:33)    
root     pts/0        45.116.146.134   Fri Aug 26 18:21 - 18:21  (00:00)    
root     pts/0        45.116.146.134   Fri Aug 26 18:21 - 18:21  (00:00)    
root     pts/0        45.116.146.134   Fri Aug 26 18:21 - 18:21  (00:00)    
root     pts/0        45.116.146.134   Fri Aug 26 18:21 - 18:21  (00:00)    
root     pts/0        45.116.146.134   Fri Aug 26 18:21 - 18:21  (00:00)    
root     pts/0        45.116.146.134   Fri Aug 26 18:21 - 18:21  (00:00)      

安全日志/var/log/secure,记录登录成功和失败的详细信息,last和lastb查看信息的综合,可以直接查看访问。

tail -100 /var/log/secure
Jan 24 14:41:41 sz-nginx02 sshd[5497]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:41:42 sz-nginx02 sshd[5499]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:41:43 sz-nginx02 sshd[5501]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:41:44 sz-nginx02 sshd[5503]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:41:45 sz-nginx02 sshd[5505]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:41:46 sz-nginx02 sshd[5507]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:41:46 sz-nginx02 sshd[5509]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:41:47 sz-nginx02 sshd[5511]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:42:39 sz-nginx02 sshd[5513]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:42:40 sz-nginx02 sshd[5515]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:43:20 sz-nginx02 sshd[5517]: fatal: Read from socket failed: Connection reset by peer [preauth]
Jan 24 14:43:21 sz-nginx02 sshd[5519]: fatal: Read from socket failed: Connection reset by peer [preauth]
。。。。      

防止ssh爆破攻击方式:

    (1)制作密钥,ssh-keygen

    (2)使用DenyHost服务,对尝试暴力ssh的ip进行限制。默认连续尝试登录失败5次,将其IP添加到/etc/hosts.deny文件,从而限制其ip登录。DenyHost是python写的一个程序,它会分析sshd的日志文件,当发现重复的攻击时会记录IP到/etc/hosts.deny文件,从而达到自动屏蔽IP的功能。

denyhost服务安装

(1)yum源安装

yum -y install denyhosts.noarch

(2)编译安装

官网:http://nchc.dl.sourceforge.net/project/denyhosts/denyhosts/2.6/DenyHosts-2.6.tar.gz

解压,编译安装

DenyHosts配置文件解析: vi /etc/denyhosts.conf

SECURE_LOG = /var/log/secure
#ssh 日志文件,如果是redhat系列是根据/var/log/secure文件来判断的。
#Mandrake、FreeBSD是根据 /var/log/auth.log来判断的,而SUSE则是用/var/log/messages来判断的。这些在配置文件里面都有很详细的解释。
HOSTS_DENY = /etc/hosts.deny
#控制用户登陆的文件
PURGE_DENY = 30m
#过多久后清除已经禁止的,空表示永久不清除

#            'm' = minutes
#            'h' = hours
#            'd' = days
#            'w' = weeks
#            'y' = years

BLOCK_SERVICE = sshd
#禁止的服务名,当然DenyHost不仅仅用于SSH服务,还可用于SMTP等等。
DENY_THRESHOLD_INVALID = 1
#允许无效用户失败的次数
DENY_THRESHOLD_VALID = 5
#允许普通用户登陆失败的次数
DENY_THRESHOLD_ROOT = 3
#允许root登陆失败的次数
HOSTNAME_LOOKUP=NO
#是否做域名反解
ADMIN_EMAIL = 
#管理员邮件地址,它会给管理员发邮件
DAEMON_LOG = /var/log/denyhosts
#DenyHosts日志文件存放的路径      
[root@fortress02-new ~]# cat /etc/hosts.deny
# hosts.denyThis file contains access rules which are used to
#deny connections to network services that either use
#the tcp_wrappers library or that have been
#started through a tcp_wrappers-enabled xinetd.
#
#The rules in this file can also be set up in
#/etc/hosts.allow with a 'deny' option instead.
#
#See 'man 5 hosts_options' and 'man 5 hosts_access'
#for information on rule syntax.
#See 'man tcpd' for information on tcp_wrappers
#
# DenyHosts: Wed Aug 17 14:51:21 2016 | sshd: 104.227.138.234
sshd: 104.227.138.234
# DenyHosts: Wed Aug 17 14:51:21 2016 | sshd: 61.161.250.27
sshd: 61.161.250.27
。。。。。。      

继续阅读