查詢最近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
。。。。。。