by Devin Yang

建立於: 1年前 ( 更新: 1年前 )

我主機基本上都設定了遠端日誌,全部往Synology NAS上的日誌中心送。
其中有一台當Router對外的樹莓派,會有一堆sshd驗證嘗試登,
Disconnected from invalid user slurm 128.199.177.36 port 60810 [preauth]
基本上我只允許公鑰驗證,
所以用帳號密碼也別想暴力破解呀,但一堆的驗證失敗的Log看了還真討厭。

所以我就Google了一下,看到了這個東西fail2ban,看起來很不錯,那就來設定看看吧。

以下就是我的安裝步驟,直接記下來:

第一步:  安裝

apt-get install fail2ban

我們可以切換目錄到/etc/fail2ban,然後瞄一下 jail.conf這個檔.

root@ptest:/etc/fail2ban# head jail.conf
#
# WARNING: heavily refactored in 0.9.0 release.  Please review and
#          customize settings for your setup.
#
# Changes:  in most of the cases you should not modify this
#           file, but provide customizations in jail.local file,
#           or separate .conf files under jail.d/ directory, e.g.:
#
# HOW TO ACTIVATE JAILS:
#

上面說啦,在大多數情況下,我們不應該變更這個檔案,而是提供一個自訂的jail.local

第二步: 在/etc/fail2ban目錄下

 cd /etc/fail2ban
 cp jail.conf jail.local

第三步: 修改jail.local

找到sshd的地方,這個檔案的其他地方也有#[sshd],別解除註解,那只是他的說明。
補兩行,「啟動」及「動作」。

enabled = true
action = %(action_)s

上例中,action_使用最簡單的設定

action_ : The simplest action to take: ban only
action_mw: ban & send an e-mail with whois report to the destemail.
action_mwl:  ban & send an e-mail with whois report and relevant log lines


完整如下

[sshd]

# To use more aggressive sshd modes set filter parameter "mode" in jail.local:
# normal (default), ddos, extra or aggressive (combines all).
# See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and details.
#mode   = normal
enabled = true
action = %(action_)s
port    = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s

第四步:啟動

(systemctl 這東西真好用,之前還覺的service指令好好的為何要換這個,現在用的很習慣了😝)

#開機時自動啟動
systemctl enable fail2ban
#啟動
systemctl start fail2ban

第五步: 查看啟動狀態,看到actvie代表運作中囉

● fail2ban.service - Fail2Ban Service
   Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-12-11 10:38:59 CST; 1h 12min ago
     Docs: man:fail2ban(1)
 Main PID: 26761 (fail2ban-server)
    Tasks: 3 (limit: 4915)
   CGroup: /system.slice/fail2ban.service
           └─26761 /usr/bin/python3 /usr/bin/fail2ban-server -xf start

如果無法啟動,可以用查看是否有什麼錯誤
    --dp, --dump-pretty     dump the configuration using more human readable representation

fail2ban-client --dp


第六步: 查看<JAIL>的狀態,sshd就是<JAIL>的名稱

fail2ban-client status sshd

結果畫面如下: 

(在第三步中的action = 很重要,要設定哦 ,不然你這裡看到的Baned IP list只是眼睛業障重的假象)

root@ptest:/etc/fail2ban# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed:	12
|  |- Total failed:	280
|  `- File list:	/var/log/auth.log
`- Actions
   |- Currently banned:	2
   |- Total banned:	6
   `- Banned IP list:	88.218.17.117 137.184.65.253

fail2ban-client有相當多的功能,足繁不及備載,請自己打指令就可以查看了

fail2ban-client

例如: 直接手動笨笨看

root@ptest:~# fail2ban-client |grep banip
    set <JAIL> banip <IP>                    manually Ban <IP> for <JAIL>
    set <JAIL> unbanip <IP>                  manually Unban <IP> in <JAIL>
root@ptest:~#

確認是否有用,可找台主機測看看是否能登,如果手動Ban IP後就不能登了。

fail2ban-client set sshd banip 34.81.227.39
fail2ban-client set sshd unbanip 34.81.227.39

測試組擋後,就真的連不上啦,下方是示意文圖

~# ssh ptest
ssh: connect to host 主機 port 22: Cannot assign requested address

觀查了一下我Synology上的日誌中心,果真少很多
 

Tags: security config

Devin Yang

文章內容無法一一說明,如果您有什麼不了解處,歡迎提問哦:)

No Comment

Post your comment

需要登入才可留言!

類似的文章


config

3C Tech Center轉移至GCP了

我受不了Bulehost的龜速,已將DNS拉回由networksolutions直接管理啦, 調整DNS設定TTL到兩小時,或許會再轉換到其他地方.. 以下是Bulehost我個人無法接受的幾點。 一、主機應該在美國,速度太慢。 二、無http/2。 三、mysql的時區無法調整。 所以決定移到Google Cloud Platform...

docker,goaccess,config

phpenv實戰GoAccess即實網站分析(docker版)

在開始前我先說一下,GoAcess也能產出靜態資料只要您有log,因為我覺的即時感覺比較炫,所以本文將專注在GoAccess即時顯示的部分進行介紹。雖然Google Analytics(GA)很好用,但試試另一種GA感覺也很不錯,例如您想了解網站使用者用什麼瀏覽器訪網站。

config,ssl,certbot

Apache及Nginx的ACME驗證通殺

有人或許會好奇記這個做什麼,我大概說明一下我的情境,超老舊主機,沒Docker也沒法裝HAProxy或certbo,就只有apache及nginx。但我需要在該主機上自動申請及更新憑證,所以我透過NFS,讓該主機掛載另一台可以跑certbot程式主機上的資料夾,讓他生成的驗證檔直接產生在舊主機上/home/nginx/acme-challenge/.well-known/acme-challenge/目錄內,網頁就能夠順利驗證,並取得憑證啦。有一堆vhost時,可以全部吃同一個資料夾,而不是每個vhost網站都去建目錄。