本篇文章更新時間:2023/07/03
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~
如果本站內容對你有幫助,歡迎使用 BFX Pay 加密貨幣新台幣 贊助支持。


昨天網站開始大量收到一組來自中國 IP 的請求,請求時間都很短,實屬惡意爬蟲的砍站行為。

收到警示通知後就把那整組 IP 丟給 Cloudflare WAF 防火牆功能給設定封鎖。

直到今天都還在很努力地爬,完全沒發現自己已經被封鎖了。

file

更多類似的資安防護操作筆記可以參考: [Fail2ban] 同步 Cloudflare WAF 防火牆封鎖 IP 清單的方法[VPS] PHP 網站應用程式防火牆 WAF 實作筆記 等相關文章

剛好是這樣的攻擊行為,讓我又重新檢視一次目前有採取的哪些設定來防堵,以下是目前的 WAF 防火牆設定:

  • 後台僅允許台灣 IP 來連線登入
  • 把一些比較誇張的機器人與 IP 做一個封鎖清單排除
  • 禁止使用 POST 方法請求網站

設定與設計細節下面補充。

後台僅允許台灣 IP 來連線登入

Cloudflare WAF 規則如下:

(http.request.method eq "POST" or http.request.method eq "GET") and 
http.host eq "www.mxp.tw" and 
(http.request.uri.path contains "wp-admin" and not http.request.uri.path contains "/wp-admin/admin-ajax.php") and
ip.geoip.country ne "TW"

解釋:封鎖非 TW 台灣使用者,使用 POSTGET 方法連線至包含 wp-adminwp-login.php 等路徑的瀏覽。

以此來封鎖其他國家的 IP 存取網站後台。

惡意機器人封鎖清單

Cloudflare WAF 規則如下:

(
    http.user_agent eq "" or 
    http.user_agent contains "Go-http-client" or
    http.user_agent contains "BLEXBot" or 
    http.user_agent contains "EyeMonIT" or 
    http.user_agent contains "fetcher" or 
    http.user_agent contains "ZoominfoBot" or 
    http.user_agent contains "python" or 
    http.user_agent contains "comscore" or 
    http.user_agent contains "thetradedesk" or 
    http.user_agent contains "criteo" or 
    ip.src in {1.161.61.221 1.161.1.172 80.248.237.132 125.71.214.63 1.161.25.64 47.91.252.138 178.176.77.73 183.69.137.0/24 113.207.1.0/24 161.123.55.0/24}
)

解釋:封鎖 User Agent 空值上述一些不會自律超速亂爬的爬蟲 User Agent 特徵 還有 特徵不明顯,針對使用的 IP

這份名單算是時常要更新,畢竟攻擊真的隨時都在發生變化,今天筆記的只是當下我的設定值,可能過一陣子就又改了。

禁止使用 POST 方法請求網站

Cloudflare WAF 規則如下:

http.request.method eq "POST" and 
http.host eq "www.mxp.tw" and 
not http.request.full_uri contains "wp-json" and 
not http.request.full_uri contains "wp-admin" and 
not http.request.full_uri contains "admin-ajax.php"

解釋: 針對指定網域 www.mxp.tw 與指定 POST 方法來瀏覽的請求,僅排除 wp-json, wp-adminadmin-ajax.php 這三個路徑關鍵字,其餘路徑全部封鎖。

但這個規則要小心使用,僅適用於像我這種單向提供內容,不提供網站上的其他互動的網站類型。高互動類型像是會員網站、購物商城等,就不適合使用了!

後記

其實第一個方法「限制登入」與第三個方法「排除 POST 方法」這兩個可以合併成一組規則如下:

(
    http.request.method eq "POST" and 
    http.host eq "www.mxp.tw" and 
    not http.request.full_uri contains "wp-json" and 
    not http.request.full_uri contains "wp-admin/admin-ajax.php" and 
    not http.request.full_uri contains "wp-cron"
) or (
    (http.request.method eq "POST" or http.request.method eq "GET") and 
    http.host eq "www.mxp.tw" and 
    (http.request.uri.path contains "wp-admin" and not http.request.uri.path contains "/wp-admin/admin-ajax.php") and
    ip.geoip.country ne "TW"
)

可以減少使用珍貴的 WAF 規則!

最後最後,上述提供的規則,為了「方便閱讀」我有排版過。實際上編輯運算式的時候必須把斷行去除才不會顯示驗證錯誤喔!

還有記得要使用這些規則,也是必須在開啟橘色雲朵的 Proxy 模式下才會啟用。

把網站的 DNS 代管在 Cloudflare 上,免費的帳號就有這些功能真的是很強大,撇開台灣的瀏覽會繞路稍微慢一點外,個人覺得還是值得推薦使用的~

以下為 2023/07/03 更新補充

假日花了點時間好好的測試與設計,目前我使用的規則如下:


(
    (
        http.request.method eq "POST" and 
        not http.referer contains "https://" and
        http.cookie contains ""
    ) or (
        http.request.method eq "POST" and 
        (
            (http.request.uri.path contains "/wp-admin/" and not http.request.uri.path contains "/wp-admin/admin-ajax.php") and 
            not http.cookie contains "wordpress_logged_in_"
        )
    ) or (
        http.request.method eq "GET" and 
        (http.request.uri.path contains "/wp-admin/" and not http.request.uri.path contains "/wp-admin/admin-ajax.php") and 
        ip.geoip.country ne "TW"
    )
)

相較本文上面比較「通範」的做法就是從路徑與方法還有國家去阻擋,其實還是可以從一個請求中的細節去要求,不滿足的應該大多都是不好的流量。

推薦閱讀這篇 Cloudflare Firewall Rules for Securing WordPress Websites ,作者從很多面向來逐一最佳化這符合 WordPress 使用的相關規則,大致方向與本文差不多,但介紹的更細!


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[///////////____34%_________]

文章
Filter
Apply Filters