Fail2Ban正規表現 - NGNIX - カスタム正規表現

Fail2Ban正規表現 - NGNIX - カスタム正規表現

特定のイベントをキャプチャするために、Fail2ban NGNIXログのカスタムログファイルを生成しようとしています。正規表現は私にとってまだ比較的新しいものなので、改善に努めています。これはカスタムフィルタに入りますが、今はFail2ban-regexを使います。

以下はログの例です。

1.1.1.1 - - [05/Feb/2021:11:19:48 +0000] "GET /images.php?id=bXlzcWwgLS11c2VyPWFuZ2VscyAtLXBhc3N3b3JkPXNjb690ZXJzCg== HTTP/1.1" 200 31 "-" "curl/7.58.0"

私は"bXlzcWwg"を一致させたいと思います。その理由は、これが一貫性があり、同様に見えると信じているからです。

^<HOST>.*bXlzcWwg

テストを実行すると、項目は問題ないようですが、エラーが発生します。

sudo fail2ban-regex "^<HOST>.*bXlzcWwg" /var/log/nginx/access.log 

Running tests
=============

Use   failregex file : /var/log/nginx/access.log
Wrong config file: File contains no section headers.
file: '/var/log/nginx/access.log', line: 1
'192.168.6.254 - - [05/Feb/2021:10:54:55 +0000] "GET / HTTP/1.1" 200 241 "-" "curl/7.58.0"\n'
ERROR: failed to load filter /var/log/nginx/access.log

テストでは日付形式をサポートしていることがわかります。

sudo failure2ban - 正規表現 "05/2月/2021:11:33:25 +0000"

Running tests
=============

Use   failregex line : <HOST>
Use      single line : 05/Feb/2021:11:33:25 +0000


    Results
    =======
    Failregex: 0 total
    Ignoreregex: 0 total
    Date template hits:
    |- [# of hits] date format
    |  [1] {^LN-BEG}Day(?P<_sep>[-/])MON(?P=_sep)ExYear[ :]?24hour:Minute:Second(?:\.Microseconds)?(?: Zone offset)?
    `-
    Lines: 1 lines, 0 ignored, 0 matched, 1 missed
    [processed in 0.01 sec]
    |- Missed line(s):
    |  05/Feb/2021:11:33:25 +0000

ベストアンサー1

sudo fail2ban-regex /var/log/nginx/access.log "^<HOST>.*bXlzcWwg"

Regex and log options were reversed..

Running tests
=============

Use   failregex line : ^<HOST>.*bXlzcWwg
Use         log file : /var/log/nginx/access.log
Use         encoding : UTF-8


Results
=======

Failregex: 268 total
|-  #) [# of hits] regular expression
|   1) [268] ^<HOST>.*bXlzcWwg
`-

Ignoreregex: 0 total

Date template hits:
|- [# of hits] date format
|  [841] Day(?P<_sep>[-/])MON(?P=_sep)ExYear[ :]?24hour:Minute:Second(?:\.Microseconds)?(?: Zone offset)?
`-

Lines: 841 lines, 0 ignored, 268 matched, 573 missed
[processed in 0.06 sec]

Missed line(s): too many to print.  Use --print-all-missed to print all 573 lines

この記事は、他の人が壁に頭をぶつけるのに役立ちます。

おすすめ記事