Linux

未應用一些 mod_security 規則

  • November 23, 2017

我們已經看到很多推薦垃圾郵件到我們的一台伺服器,所以我決定添加一些自定義 modsecurity 規則來嘗試至少阻止其中一些。

我添加了幾條規則,例如:

SecRule REQUEST_HEADERS:User-Agent "/\byourekillingme.org\b/" \
   "phase:1,log,deny,status:503,msg:'Referer spam1',id:101"

SecRule REQUEST_HEADERS:User-Agent "/\bahrefs.com/robot\b/" \
   "phase:1,log,deny,status:503,msg:'Referer spam2',id:102"

SecRule REQUEST_HEADERS:User-Agent "/\bsemrush.com/bot\b/" \
   "phase:1,log,deny,status:503,msg:'Referer spam6',id:106"

但正如您從日誌輸出中看到的那樣,雖然正在應用 ahrefs 規則(上面的 id 102),但其他規則卻沒有(還有其他規則,但 ahrefs 是唯一有效的規則):

107.180.120.23 - - [23/Nov/2017:11:08:00 +0000] "GET /tri-levlen-28-side-effects-3f1 HTTP/1.1" 200 50965 "http://www.mydomain.co.uk/tri-levlen-28-side-effects-3f1#elephant" "WordPress/4.9; http://yourekillingme.org"

51.255.65.42 - - [23/Nov/2017:10:40:51 +0000] "GET /pink-viagra-price-52c HTTP/1.1" 503 315459 "-" "Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)"

46.229.168.73 - - [23/Nov/2017:11:07:50 +0000] "GET /viagra-for-sale-online-cheap-52c HTTP/1.1" 200 51060 "-" "Mozilla/5.0 (compatible; SemrushBot/1.2~bl; +http://www.semrush.com/bot.html)"

我還檢查了 /usr/local/apache/logs/modsec_audit.log 以確認是我的規則導致了 503 而不是其他地方的另一個規則,並且只顯示了很多條目:

Message: Access denied with code 503 (phase 1). Pattern match "/\\bahrefs.com/robot\\b/" at REQUEST_HEADERS:User-Agent. [file "/usr/local/apache/conf/modsec2.myrules.conf"] [line "8"] [id "102"] [msg "Referer spam2"]

表明這是我的 modsec 規則阻止它。

誰能看到為什麼我的其他規則沒有被應用?

是斜線。ModSecurity 中的正則表達式不受斜杠限制,因此它們被解釋為正則表達式的文字部分。

您的所有表達式都是匹配的 URL,因此第一個斜杠與 URL 部分中的最後一個斜杠匹配http://,但只有 AHrefs URL 有一個尾部斜杠,所以這是唯一一個最終匹配這些正則表達式的。

我也不確定它\b是否在做任何有用的事情。它似乎在字元串的 the.或 the/或末尾匹配這三個。如果 ModSecurity 真正理解它,它可能會導致其他機器人的未來表達式在您期望它們匹配時無法匹配。

引用自:https://serverfault.com/questions/884833