linuxSSHD服務(wù)搭建管理和防止暴力破解

1 ??清空關(guān)閉防火墻

由于前期尚未學習“防火墻”為了不受到防火墻影響實驗的順利進行,因此清空并關(guān)閉防火墻。

[root@root??~]# iptables -F #清空防火墻規(guī)則。

[root@root??~]# systemctl stop firewalld #關(guān)閉防火墻。

[root@root??~]# systemctl disable firewalld #設(shè)置開機不啟動防火墻。

2 ?關(guān)閉SELinux

[root@root??~]# getenforce? ? ? ? ? ? ?#查看SElinux狀態(tài)。

Disabled

[if !supportLists]1.?[endif]永久關(guān)閉SElinux:

[root@root??~]# vim /etc/selinux/config #修改SElinux配置文件,修改SELINUX=disabled,如圖 1-1 所示,修改完成后并保存退出,修改文件后需要重啟主機則會生效。


注意:永久關(guān)閉SElinux需要重啟主機則生效,如果當前主機是生產(chǎn)環(huán)境不能重啟主機的條件下,但又要實現(xiàn)永久關(guān)閉SElinux的情況下,需要先做臨時關(guān)閉,再修改其配置文件實現(xiàn)永久關(guān)閉,但不需要重啟主機。?f??

3 ?配置靜態(tài)IP

[root@root??~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE="Ethernet"

PROXY_METHOD="none"

BROWSER_ONLY="no"

BOOTPROTO="static"

DEFROUTE="yes"

IPV4_FAILURE_FATAL="no"

IPADDR=192.168.0.63

GATEWAY=192.168.0.1

NETMASK=255.255.255.0

DNS1=114.114.114.114

NAME="ens32"

UUID="5e02ab66-a084-404a-bb4c-50bf47bd1bd5"

DEVICE="ens32"

ONBOOT="yes"

?

注釋:修改網(wǎng)卡配置文件,主要修改內(nèi)容項BOOTPROTO="static" 、ONBOOT="yes" 、IPADDR=192.168.0.63 、GATEWAY=192.168.0.1 、NETMASK=255.255.255.0 、DNS1=114.114.114.114 ?以上6項。

?

關(guān)閉NetworkManager 服務(wù):

[root@root?? ~]# systemctl stop NetworkManager? ? ? ? ? ? ? ? ? ??#關(guān)閉 NetworkManager 。

[root@root??~]# systemctl disable NetworkManager? ? ? ? ? ? ? ??#設(shè)置開啟不啟動。

?4 ?配置主機和IP映射關(guān)系

[root@xuegod63 ~]# vim /etc/hosts

127.0.0.1 ??localhostlocalhost.localdomain localhost4 localhost4.localdomain4

::1 ????????localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.63 ???xuegod63.cn ????xuegod63

192.168.1.64 ???xuegod64.cn ????xuegod64

?

注釋:修改?/etc/hosts配置文件,添加以下兩項。

192.168.1.63 ???xuegod63.cn ????xuegod63

192.168.1.64 ???xuegod64.cn ????xuegod64

5 ?修改主機名

[root@root??~]# vim /etc/hostname #修改主機名配置文件(永久生效,需要重啟)。

xuegod63

[root@root??~]# hostname xuegod63 ? #臨時設(shè)置,立即生效(需要重啟當前終端)。

xuegod63

6 ?配置Yum源

[if !supportLists]1.[endif]配置本地Yum源:

[root@root??~]# mount /dev/sr0 ?/mnt/ #掛載光驅(qū)。

[root@root??~]# echo "/dev/sr0 /mnt iso9660 defaults 0 0" >>?/etc/fstab #設(shè)置開機自動掛載光驅(qū)。

[root@root??~]# rm -rf /etc/yum.repos.d/* #刪除/etc/yum.repos.d/目錄的所有文件。

[root@root??~]# cat > /etc/yum.repos.d/centos7.repo <<EOF

> [centos7-source]

>name=?centos7-source

>baseurl=file:///mnt

> enabled=1

>gpgcheck=0

> EOF

注釋:生成文檔,并在< EOF結(jié)束符以內(nèi)的內(nèi)容。

[if !supportLists]2.[endif]配置網(wǎng)絡(luò)yum源:

阿里云鏡像源站點(http://mirrors.aliyun.com/)。

CentOS鏡像參考:http://mirrors.aliyun.com/help/centos

[if !supportLists](1)?[endif]備份:

把CentOS-Base.repo文件重新命名為CentOS-Base.repo.backup 。

[root@root??~]# mv /etc/yum.repos.d/CentOS-Base.repo{,.backup}

[if !supportLists](2)?[endif]下載新的CentOS-Base.repo 到/etc/yum.repos.d/

安裝wget工具:

[root@root??~]# rpm -ivh /mnt/Packages/wget-1.14-15.el7.x86_64.rpm

下載Yum源:

[root@root??~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

生成Yum緩存:

[root@root??~]# yum makecache

百度:163源




7 ?epel源

[root@root??~]# yum install epel-release -y

8 ?開機配置成: init 3 ?模式

[root@xuegod63 ~]# ln -svf /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target

重啟主機,使以上設(shè)置生效:

[root@xuegod63 ~] reboot

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sshd服務(wù)安裝-ssh命令使用方法

1 ?SSHD服務(wù)

介紹:SSH協(xié)議:安全外殼協(xié)議。為Secure Shell的縮寫。SSH為建立在應用層和傳輸層基礎(chǔ)上的安全協(xié)議。

?

作用:SSHD服務(wù)使用SSH協(xié)議可以用來進行遠程控制,或在計算機之間傳送文件。

相比較之前用Telnet方式來傳輸文件要安全很多,因為Telnet使用明文傳輸,SSH是加密傳輸。

服務(wù)安裝:

需要安裝OpenSSH 是個安裝包:

OpenSSH軟件包,提供了服務(wù)端后臺程序和客戶端工具,用來加密遠程控件和文件傳輸過程中的數(shù)據(jù),并由此來代替原來的類似服務(wù)Telnet或Ftp。

安裝包:

OpenSSH服務(wù)需要4個軟件包。

openssh-5.3p1-114.el6_7.x86_64:包含OpenSSH服務(wù)器及客戶端需要的核心文件。

openssh-clients-5.3p1-114.el6_7.x86_64:OpenSSH客戶端軟件包。

openssh-server-5.3p1-114.el6_7.x86_64:OpenSSH服務(wù)器軟件包。

openssh-askpass-5.3p1-114.el6_7.x86_64:支持對話框窗口的顯示,是一個基于X系統(tǒng)的密碼診斷工具

注釋:這四個軟件包在我們的CentOS7鏡像軟件安裝包里有。

查看鏡像中的openssh*開頭的程序包:

[root@xuegod63 Packages]# ls /mnt/Packages/openssh*

/mnt/Packages/openssh-5.3p1-104.el6.x86_64.rpm

/mnt/Packages/openssh-askpass-5.3p1-104.el6.x86_64.rpm

/mnt/Packages/openssh-clients-5.3p1-104.el6.x86_64.rpm

/mnt/Packages/openssh-server-5.3p1-104.el6.x86_64.rpm

1.2.2 ?安裝SSH服務(wù)

安裝方法有兩種:

[if !supportLists]1.?[endif]通過Yum安裝(推薦使用):

[root@xuegod63 ~]# yum install openssh openssh-clients openssh-server -y

[if !supportLists]2.?[endif]本地直接安裝rpm包文件:

[root@xuegod63 ~]# rpm -ivh /mnt/Packages/openssh*.rpm

以上選擇一種安裝方式即可。

[if !supportLists]3.?[endif]確認軟件包是否已經(jīng)安裝:

[root@xuegod63 ~]# rpm -qa | grep openssh #使用rpm -qa查看所有安裝的程序包,并過慮openssh的程序包。

openssh-askpass-5.3p1-114.el6_7.x86_64

openssh-clients-5.3p1-114.el6_7.x86_64

openssh-5.3p1-114.el6_7.x86_64

openssh-server-5.3p1-114.el6_7.x86_64

[if !supportLists]4.?[endif]查看軟件安裝生產(chǎn)的文件:

[root@xuegod63 ~]# rpm -ql openssh

/etc/ssh

/etc/ssh/moduli

/usr/bin/ssh-keygen

/usr/libexec/openssh

/usr/libexec/openssh/ssh-keysign

/usr/share/doc/openssh-5.3p1

[if !supportLists]5.?[endif]OpenSSH配置文件:

OpenSSH常用配置文件有兩個/etc/ssh/ssh_config和/etc/sshd_config。

ssh_config為客戶端配置文件,設(shè)置與客戶端相關(guān)的應用可通過此文件實現(xiàn)。

sshd_config為服務(wù)器端配置文件,設(shè)置與服務(wù)端相關(guān)的應用可通過此文件實現(xiàn)。

[if !supportLists]6.?[endif]服務(wù)啟動關(guān)閉腳本:

[root@xuegod~]# systemctl restart|stop|start|status sshd

注釋:Linux下一般用“|”表示或者(多選一),其中 restart|stop|start|status,在同一時間僅使用一個。

[if !supportLists]7.?[endif]開機啟動服務(wù):

[root@xuegod63 ~]# chkconfig sshd on

注意:正在將請求轉(zhuǎn)發(fā)到“systemctl enable sshd.service”。

查看所有開機管理服務(wù),過慮sshd服務(wù)。

[root@xuegod63 ssh]# systemctl list-unit-files | grep sshd

anaconda-sshd.service ????????????????????????static

sshd-keygen.service ??????????????????????????static ?

sshd.service ?????????????????????????????????enabled

sshd@.service ????????????????????????????????static ?

sshd.socket ??????????????????????????????????disabled

1.2.3 ?如何使用SSH來遠程連接主機

方法一:

ssh [遠程主機用戶名] @[遠程服務(wù)器主機名或IP地址] -p port

當在Linux主機上遠程連接另一臺Linux主機時,如當前所登錄的用戶是root的話,當連接另一臺主機時也是用root用戶登錄時,可以直接使用ssh IP,端口默認即可,如果端口不是默認的情況下,需要使用-p 指定端口。

[root@xuegod ssh]# ssh 192.168.1.64

[root@xuegod64 ~]# useradd cat && echo 123456 | passwd --stdin cat

更改用戶?cat 的密碼 。

passwd:所有的身份驗證令牌已經(jīng)成功更新。

[root@xuegod64 ~]#?exit

.

普通用戶:

創(chuàng)建用戶并設(shè)置密碼為123456。

[root@xuegod63 ~]# useradd cat && echo 123456 | passwd --stdin cat

[root@xuegod63 ~]# ssh?cat@192.168.1.64

第一次登錄服務(wù)器時系統(tǒng)沒有保存遠程主機的信息,為了確認該主機身份會提示用戶是否繼續(xù)連

接,輸入yes 后登錄,這時系統(tǒng)會將遠程服務(wù)器信息寫入用戶主目錄下的$HOME/.ssh/known_hosts文件中,下次再進行登錄時因為保存有該主機信息就不會再提示了,如圖 1-5 所示。


RSA算法基于一個十分簡單的數(shù)論事實:將兩個大素數(shù)相乘十分容易,但是想要對其乘積進行因式分解卻極其困難,因此可以將乘積公開作為加密密鑰。

方法二:

ssh -l [遠程主機用戶名] [遠程服務(wù)器主機名或IP 地址] -p port

[root@xuegod63 ~]# ssh -l cat 192.168.1.64

-l :-l選項,指定登錄名稱。

-p:-p選項,指定登錄端口(當服務(wù)端的端口非默認時,需要使用-p指定端口進行登錄)。

SSHD服務(wù)配置和管理

1.3.1 ?配置文件詳解

注:在配置文件中參數(shù)前面有#號,表示是默認值,當然#號也表示注釋。

/etc/ssh/sshd_config配置文件內(nèi)容詳解。

[if !supportLists]1.?[endif]Port 22

設(shè)置SSHD監(jiān)聽端口號。

[if !supportLists](1)?[endif]SSH 預設(shè)使用 22 這個port,也可以使用多個port,即重復使用 port 這個設(shè)定項目!

[if !supportLists](2)?[endif]例如想要開放SSHD端口為 22和222,則多加一行內(nèi)容為: Port 222 即可。

[if !supportLists](3)?[endif]然后重新啟動SSHD這樣就好了。 建議大家修改 port number 為其它端口,防止別人暴力破解。

?

例1.1:修改SSHD服務(wù)默認監(jiān)聽的端口為222.

[root@xuegod63 ~]# vim /etc/ssh/sshd_config #修改ssh服務(wù)端配置文件。

改:

Port 22

為:

Port 222

[root@xuegodssh]# systemctl restart sshd #重啟sshd服務(wù)。

測試:

?[root@xuegod74 ~]# netstat -tlunp | grep sshd

tcp ???????0 ?????0 0.0.0.0:222 ????????????????0.0.0.0:* ??????????????????LISTEN ?????4139/sshd

tcp ???????0 ?????0 :::222 ?????????????????????:::* ???????????????????????LISTEN ?????4139/sshd

修改完端口默認端口后,登錄方法:

[root@xuegod63 ~]# ssh -p 222 192.168.1.63

[if !supportLists]2.?[endif]ListenAddress 0.0.0.0

設(shè)置SSHD服務(wù)器綁定的IP 地址,0.0.0.0 表示偵聽所有地址

安全建議:如果主機不需要從公網(wǎng)ssh訪問,可以把監(jiān)聽地址改為內(nèi)網(wǎng)地址

這個值可以寫成本地IP地址,也可以寫成所有地址,即0.0.0.0 表示所有IP。

?

[if !supportLists]3.?[endif]Protocol 2

選擇的SSH 協(xié)議版本,可以是 1 也可以是 2,CentOS 5.x 預設(shè)是僅支援V2版本,出于安全考慮,設(shè)置為最新的協(xié)議版本。

?

[if !supportLists]4.?[endif]#HostKey /etc/ssh/ssh_host_key

設(shè)置包含計算機私人密鑰的文件

[if !supportLists]5.?[endif]SyslogFacility AUTHPRIV

當有人使用SSH 登入系統(tǒng)的時候,SSH 會記錄信息,這個信息要記錄的類型為AUTHPRIV,sshd服務(wù)日志存放在:/var/log/secure。

例:為什么sshd配置文件中沒有指定日志,但日志卻存放在了/var/log/secure?

[root@xuegod ssh]# vim /etc/rsyslog.conf ? #查看日志配置文件,如圖 1-6 所示。

日志文件定義

1.3.2 ?安全調(diào)優(yōu)的重點

[if !supportLists]1.[endif]LoginGraceTime 2m ??????

[if !supportLists](1)?[endif]grace意思是系統(tǒng)給與多少秒來進行登錄。

[if !supportLists](2)?[endif]當使用者連上SSH server 之后,會出現(xiàn)輸入密碼的畫面,在該畫面中。

[if !supportLists](3)?[endif]在多久時間內(nèi)沒有成功連上SSH server 就強迫斷線!若無單位則默認時間為秒。

可以根據(jù)實際情況來修改實際

?

[if !supportLists]2.[endif]# PermitRootLogin yes

是否允許root 登入,默認是允許的但是建議設(shè)定成no,真實的生產(chǎn)環(huán)境服務(wù)器,是不允許root賬號直接登陸的,僅允許普通用戶登錄,需要用到root用戶再切換到root用戶。

?

[if !supportLists]3.[endif]PasswordAuthentication yes

密碼驗證當然是需要的!所以這里寫yes,也可以設(shè)置為no,在真實的生產(chǎn)服務(wù)器上,根據(jù)不同安全級別要求,有的是設(shè)置不需要密碼登陸的,通過認證的秘鑰來登陸。

?

[if !supportLists]4.[endif]# PermitEmptyPasswords no

是否允許空密碼的用戶登錄,默認為no,不允許空密碼登錄。

?

例1.2:給sshd服務(wù)添加一些警告信息。

[root@xuegod ~]# cat /etc/motd

[root@xuegod ~]# echo 'Warning ! From now on, all of your operation has been record!'> /etc/motd

?

測試:

[root@xuegod ~]# ssh 192.168.1.64 ?? ?#登錄主機。

root@192.168.0.64's password:

Last login: Thu Jun 23 14:02:38 2016 from 192.168.0.1

Warning ! From now on, all of your operation has been record!

?

[if !supportLists]5.[endif]# PrintLastLog yes

顯示上次登入的信息!默認為yes 。

?

例1.3:

[root@xuegod63 ~]# ssh 192.168.1.63 #登錄主機。

Last login: Tue Nov ?4 19:57:31 2014 from 192.168.1.107 ? #PrintLastLog yes項定義即是該回顯上次登錄的信息。

?

[if !supportLists]6.[endif]# UseDNS yes

一般來說,為了要判斷客戶端來源是正常合法的,因此會使用DNS 去反查客戶端的主機名,但通常在內(nèi)網(wǎng)互連時,該項設(shè)置為no,因此使聯(lián)機速度會快些。

?

1.4 ?SSHD服務(wù)防止暴力破解

防止暴力破解的方法有兩種:

1.4.1 ?配置安全的SSHD服務(wù)(方法一)

[if !supportLists](1)?[endif]密碼足夠的復雜,密碼的長度要大于8位最好大于20位。密碼的復雜度是密碼要盡可能有數(shù)字、大小寫字母和特殊符號混合組成。

[if !supportLists](2)?[endif]修改默認端口號。

[if !supportLists](3)?[endif]不允許root賬號直接登陸,添加普通賬號,授予root的權(quán)限。

思考:是否可以禁止root身份登錄?

答:不行,因為有些程序需要使用root身份登錄并運行。另外判斷一個用戶是不是超級管理員,看的是用戶的ID是否為0。

[if !supportLists](4)?[endif]不允許密碼登陸,只能通過認證的密鑰來登陸系統(tǒng)。

?

[if !supportLists]1.?[endif]通過密鑰認證實現(xiàn)SSHD認證。

實驗環(huán)境:

服務(wù)端:xuegod63 ?IP:192.168.1.63

客戶端:xuegod64 ?IP:192.168.1.64

客戶端生成密鑰對,然后把公鑰傳輸?shù)椒?wù)端

?

[root@xuegod64 ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): #提示輸入密鑰文件的保存路徑,選擇默認,回車繼續(xù)

Enter passphrase (empty for no passphrase): #下面要求輸入密碼,這里的passphrase 密碼是對生成的私鑰文件(/root/.ssh/id_dsa)的保護口令,如果不設(shè)置可以回車。

Enter same passphrase again: #直接回車。

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

da:2c:d8:53:92:6e:ff:4a:54:14:cd:23:28:b3:bb:3b root@xuegod64

The key's randomart image is:

+--[ RSA 2048]-----+

| ????????.o+ |

| ??? ???o ... + |

| ??????+ ?.. . |

| ???????.. . |

| ?????o.S |

| ????+.B |

| ???. B.+ |

| ????.E= |

| ?????.ooo. |

+--------------------+

[root@xuegod64 ~]# cd /root/.ssh/ #切換工作目錄至家目錄下的.ssh目錄下。

[root@xuegod64 .ssh]# ls #可查看到生成的id_rsa、id_rsa.pub文件。

id_rsa ?id_rsa.pub ?known_hosts

?

[if !supportLists]2.?[endif]發(fā)布公鑰到服務(wù)端。

使用ssh-copy-id 命令將客戶端生成的公鑰發(fā)布到遠程服務(wù)器192.168.1.63 xuegod63。

[root@xuegod64 .ssh]# ssh-copy-id -i 192.168.1.63

The authenticity of host '192.168.1.63(192.168.0.63)' can't be established.

RSA key fingerprint is d9:17:d7:db:38:7c:e8:56:9c:4b:7e:00:7f:9e:1c:74.

Are you sure you want to continue connecting (yes/no)? yes #輸入yes

Warning: Permanently added '192.168.0.64' (RSA) to the list of known hosts.

root@192.168.1.63's password: #輸入192.168.1.63主機登錄密碼。

Now try logging into the machine, with "ssh '192.168.1.63'", and check in:

?

??.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting

#這個時候可以通過ssh 無密鑰直接登陸主機

?

注意:如果服務(wù)器不是監(jiān)聽22端口,則需要指定端口傳輸密鑰:

[root@xuegod64 .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 222 root@192.168.1.63

?

1.4.2 ?通過開源的防護軟件來防護安全(方法二)

優(yōu)點:使用簡單、靈活、功能強大

?

實戰(zhàn)背景:

最近公網(wǎng)網(wǎng)站一直被別人暴力破解SSHD服務(wù)密碼。雖然沒有成功,但會導致系統(tǒng)負載很高,原因是在暴力破解的時候,系統(tǒng)會不斷地認證用戶,從而增加了系統(tǒng)資源額外開銷,導致訪問公司網(wǎng)站速度很慢。

然而fail2ban程序可以監(jiān)視你的系統(tǒng)日志,然后匹配日志的錯誤信息(正則式匹配)執(zhí)行相應的屏蔽動作(一般情況下是防火墻),而且可以發(fā)送e-mail通知系統(tǒng)管理員,很實用、很強大!

簡單來說其功能就是防止暴力破解。工作的原理是通過分析一定時間內(nèi)的相關(guān)服務(wù)日志,將滿足動作的相關(guān)IP利用iptables加入到dorp列表一定時間。

注:重啟iptables服務(wù)的話,所有DORP將重置。

?

下載軟件包

官方地址:http://www.fail2ban.org

fail2ban程序下載地址:http://www.fail2ban.org/wiki/index.php/Downloads

[if !supportLists]1.?[endif]需要安裝python開發(fā)環(huán)境,并且版本要大于2.4。

[root@xuegod63 ~]# python -V #查看當前系統(tǒng)中python的版本。

Python 2.7.5

[if !supportLists]2.?[endif]使用Yum安裝fail2ban

[root@xuegod63 ~]# yum -y install epel-release ??#已經(jīng)安裝好了epel源,這一步可忽略。

[root@xuegod63 ~]# yum -y install fail2ban

?

[if !supportLists]3.?[endif]相關(guān)主要文件說明

[root@xuegod63 ~]# ls /etc/fail2ban/


/etc/fail2ban/action.d ?#動作文件夾,內(nèi)含默認文件。iptables以及mail等動作配置。

/etc/fail2ban/fail2ban.conf??? #定義了fai2ban日志級別、日志位置及sock文件位置。

/etc/fail2ban/filter.d???????????????#與條件相關(guān)的目錄,內(nèi)含默認文件。過濾日志關(guān)鍵內(nèi)容設(shè)置。

/etc/fail2ban/jail.conf???? #主要配置文件,模塊化。主要設(shè)置啟用ban動作的服務(wù)及動作閥值。

應用實例

設(shè)置條件:SSH遠程登錄5分鐘內(nèi)3次密碼驗證失敗,禁止用戶IP訪問主機1小時,1小時該限制自動解除,用戶可重新登錄。

因為動作文件(action.d/iptables.conf)以及日志匹配條件文件(filter.d/sshd.conf )安裝后是默認存在的?;静挥米鋈魏涡薷摹K兄饕枰O(shè)置的就只有jail.conf文件。啟用SSHD服務(wù)的日志分析,指定動作閥值即可。

?

實例文件/etc/fail2ban/jail.conf及說明如下:

[root@xuegod63 ~]# vim /etc/fail2ban/jail.conf

[DEFAULT]?????????????? #全局設(shè)置。

ignoreip = 127.0.0.1/8?????? #忽略的IP列表,不受設(shè)置限制。

bantime ?= 600???????????? #屏蔽時間,單位:秒。

findtime ?= 600???????????? #這個時間段內(nèi)超過規(guī)定次數(shù)會被ban掉。

maxretry = 5??????????????? #最大嘗試次數(shù)。

backend = auto??????????? #日志修改檢測機制(gamin、polling和auto這三種)。

?

[sshd]?????????????? #單個服務(wù)檢查設(shè)置,如設(shè)置bantime、findtime、maxretry和全局沖突,服務(wù)優(yōu)先級大于全局設(shè)置。

port ???= ssh

logpath = %(sshd_log)s

backend = %(sshd_backend)s

#加入如下內(nèi)容

enabled ?= true???????????? #是否激活此項(true/false)修改成 true。

filter = sshd????????????? #過濾規(guī)則filter的名字,對應filter.d目錄下的sshd.conf。

action = iptables[name=SSH, port=ssh, protocol=tcp]???????????? #動作的相關(guān)參數(shù),對應action.d/iptables.conf文件。

sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"] #觸發(fā)報警的收件人。

logpath = /var/log/secure?? #檢測的系統(tǒng)的登陸日志文件。這里要寫sshd服務(wù)日志文件。 默認為logpath = /var/log/sshd.log 。

#5分鐘內(nèi)3次密碼驗證失敗,禁止用戶IP訪問主機1小時。 配置如下。

bantime = 3600 ??#禁止用戶IP訪問主機1小時。

findtime = 300 ???#在5分鐘內(nèi)內(nèi)出現(xiàn)規(guī)定次數(shù)就開始工作。

maxretry = 3 ??? #3次密碼驗證失敗。

?

[if !supportLists]2.?[endif]啟動服務(wù)

[root@xuegod63 ~]#?systemctl start fail2ban #啟動fail2ban服務(wù)。

[root@xuegod63 ~]#?systemctl enable fail2ban #設(shè)置開機自動啟動。

?

[if !supportLists]3.?[endif]測試

[root@xuegod63 ~]# > /var/log/secure ? #清空日志內(nèi)容。

[root@xuegod63 fail2ban]# systemctl restart fail2ban #重啟fail2ban服務(wù)。

?

[root@xuegod63 ~]# iptables -L -n ???#在fail2ban服務(wù)啟動后,iptables會多生成一個規(guī)則鏈,如圖 1-10 所示。


?iptables規(guī)則

測試:故意輸入錯誤密碼3次,再進行登錄時,會拒絕登錄。

[root@xuegod64 ~]# ssh 192.168.1.63

root@192.168.1.63's password: #故意輸入錯誤密碼。

Permission denied, please try again.

root@192.168.1.63's password: #故意輸入錯誤密碼。

Permission denied, please try again.

root@192.168.1.63's password: #故意輸入錯誤密碼。

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

?

[root@xuegod64 ~]# ssh 192.168.1.63

ssh: connect to host 192.168.1.63 port 22: Connection refused

?

在xuegod63上查看:

[root@xuegod63 ~]# ?iptables -L |tail -4 #可以查看到192.168.1.64該IP被iptables禁止所有訪問。

Chain fail2ban-SSH (1 references)

target ????prot opt source ??????????????destination

REJECT ?????all ?-- ?192.168.1.64 ???????anywhere

RETURN ????all ?-- ?anywhere ??????????anywhere

?

[root@xuegod63 ~]# fail2ban-client status #配置好之后我們檢測下fail2ban是否工作。

Status

|- Number of jail: 1

`- Jail list: sshd #具體看某一項的狀態(tài)也可以看,如果顯示被ban的ip和數(shù)目就表示成功了,如果都是0,說明沒有成功。

?

[root@xuegod63 ~]# fail2ban-client status sshd

Status for the jail: sshd

|- Filter

| ?|- Currently failed: 0

| ?|- Total failed: 0

| ?`- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd

`- Actions

???|- Currently banned: 1

???|- Total banned: 1

???`- Banned IP list: 192.168.1.64

?

[if !supportLists]2.?[endif]查看fail2ban的日志能夠看到相關(guān)的信息。

[root@xuegod63 ~]# tail /var/log/fail2ban.log

2018-11-07 16:11:01,476 fail2ban.actions ???????[27932]: NOTICE ?[sshd] Ban 192.168.1.64

?

需要注意的2點:

?

[if !supportLists](1)?[endif]另外如果后期需要把iptables清空后或iptables重啟后,也需要把fail2ban重啟一下。

?

[if !supportLists](2)?[endif]如果修改ssh默認端口22為2015后,配置fail2ban來監(jiān)控SSHD服務(wù)需要修改配置文件

?

例:

[root@xuegod63 ~]# vim /etc/ssh/sshd_config

改17 #Port 22

為17 Port 2015

?

[root@xuegod63 ~]# systemctl restart sshd

?

[root@xuegod63 ~]# vim /etc/fail2ban/jail.conf

#修改iptables動作中的端口號,默認為SSH,如圖 1-11 所示。

改:port=ssh

為:port=2015


重啟服務(wù)即可:

[root@xuegod63 ~]# systemctl restart fail2ban

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容