logwacthのhttpdレポートをカスタマイズする

Chromeのpreconnectによりlogwatchに408のレポートが大量に表示されるようになり、はや一年。前回は打つ手なしと諦めてましたが、logwatchのhttpフィルタを自分でカスタマイズすれば良いことに今更ですが気づきましたので早速弄ってみましょう。

前回の記事はこちら
apacheで408エラーが突然大量発生したけれど・・・
昨日・今日になってApacheのアクセスログに大量の408が記録されるようになりました。何が起きたのか確認してみます。
logwtachのhttpdを再確認

logwatchに対し修正したいことは主に2つ。1つめは転送量カテゴリの「Other」が多すぎること、2つめは「Attempts to use known hacks」と「A total of XXX sites probed the server」が邪魔なこと。これを解決するのが今回の目的です。

# logwatch --print

 ################### Logwatch 7.3.6 (05/19/07) #################### 
        Processing Initiated: Tue May 29 04:49:06 2018
        Date Range Processed: yesterday
                              ( 2018-May-28 )
                              Period is day.
      Detail Level of Output: 5
              Type of Output: unformatted
           Logfiles for Host: foobarserver
  ################################################################## 
--------------------- httpd Begin ------------------------

439.78 MB transferred in 22864 responses  (1xx 0, 2xx 20172, 3xx 1886, 4xx 806, 5xx 0) 
    10348 Images (347.75 MB),
       26 Documents (0.03 MB),
     6723 Content pages (15.81 MB),
      169 Redirects (0.02 MB),
      390 Fonts (5.75 MB),
     5208 Other (70.42 MB) 

 Attempts to use known hacks by 239 hosts were logged 726 time(s) from:
    X06.186.201.107: 62 Time(s)

    2X8.222.134.252: 45 Time(s)

    36.X.188.190: 34 Time(s)

    61.4X.123.235: 25 Time(s)
    .....

A total of 239 sites probed the server 
    X.72.8.25
    XX.X5.230.76
    XXX.7X.8.178
    XX.79.X3.201
    .....

A total of 1 possible successful probes were detected (the following URLs
 contain strings that match one or more of a listing of strings that
 indicate a possible exploit):

    /archives/tag/\xe3\x81\xa9\xe3\x82\x89\xe3\x82\x84\xe3\x81\x8d HTTP Response 301 

 Requests with error response codes
    400 Bad Request
logwatchのhttpフィルタをカスタマイズするため/etcにコピーする

デフォルトのhttpフィルタを直接編集してもダメではないのですが、logwatchがアップデートされた時に上書きされて戻ってしまうので、カスタマイズ用を/etc配下に準備します。

# cp /usr/share/logwatch/scripts/services/http /etc/logwatch/scripts/services/http
logwatchレポートから408アクセスのIPアドレス表記を消す

現在のlogwatchのレポートレベルを見ると「Detail Level of Output: 5」とあるので「Attempts to use known hacks」がLevel 5の時に表示されないようにします。
「Attempts to use」の文字列を検索して付近の「if (($detail >4)」を「if (($detail >5)」に書き換えてしまいましょう。

# vi /etc/logwatch/scripts/services/http
 #  List attempted exploits
 #

-if (($detail >4) and $total_hack_count) {
+if (($detail >5) and $total_hack_count) {
    print "\nAttempts to use known hacks by ".(keys %hacks).
          " hosts were logged $total_hack_count time(s) from:\n";
    my $order = TotalCountOrder(%hacks);

「A total of XXX sites probed the server」の削除は、「sites probed the server」の記載がある行付近の「#if ($detail > 4)」を探してコメントアウト「#」の解除と数値を修正します。また2行下にある「#}」のコメントアウト削除も忘れないでください。

# vi /etc/logwatch/scripts/services/http
       print "\nA total of ".scalar(keys %ban_ip)." sites probed the server \n";
       $flag = 0;
    }
-   #if ($detail > 4) {
+   if ($detail > 5) {
       print "   $i\n";
-   #}
+   }
 }
 #
transferredのOtherをなるべく減らす

httpフィルタを眺めてみるとlogwatchは「# file type comparisons are case-insensitive」セクションででアクセスログのファイル種別を振り分けているようです。
中身を見てみるとWordPressには適していない条件設定なのがわかります。そこで自サイトのアクセスログを確認し、比較的アクセスの多い文字列を条件に追加してみました。

# vi /etc/logwatch/scripts/services/http
######################
# file type comparisons are case-insensitive
    ...
    $content_types =  $content_types.'|\.torrent';
    $content_types =  $content_types.'|\.css|\.js|\.cgi';
    $content_types =  $content_types.'|\.fla|\.swf|\.rdf';
    $content_types =  $content_types.'|\.class|\.jsp|\.jar|\.java';
+   $content_types =  $content_types.'|\/feed|\/atom|\/rss';
+   $content_types =  $content_types.'|\/archives/(..*)|\/page/\d{1,3}';
    $content_types =  $content_types.'|COPYRIGHT|README|FAQ|INSTALL|\.txt)';
 my $docs_types =     '(\.asc|\.bib|\.djvu|\.doc|\.dot|\.dtd|\.dvi|\.gnumeric|\.mcd|\.mso|\.pdf|\.pps|\.ppt|\.ps|\.rtf|\.sxi|\.tex|\.text|\.tm|\.xls|\.xml)';
 my $archive_types =  '(\.ace|\.bz2|\.cab|\.deb|\.dsc|\.ed2k|\.gz|\.hqx|\.md5|\.rar|\.rpm|\.sig|\.sign|\.tar|\.tbz2|\.tgz|\.vl2|\.z|\.zip|\.hdr)';

    $program_src =    $program_src.'|Makefile|Makefile_c|Makefile_f77)';
 my $images_types =   '(\.bin|\.cue|\.img|\.iso|\.run)';
 my $logs_types =     '(\.log|_log|-log|\.logs|\.out|\.wyniki)';
-my $fonts_types =    '(\.aft|\.ttf)';
+my $fonts_types =    '(\.aft|\.ttf|\.otf|\.woff|\.woff2|\.eot)';
 my $config_types =   '(\.cfg|\.conf|\.config|\.ini|\.properties)';
 my $xpcomext_types = '(\.xpt)';
 my $mozext_types =   '(\.xul)';
@@ -639,7 +641,7 @@
 #  List attempted exploits
 #

この行だけは解説が必要かと思います。

+   $content_types =  $content_types.'|\/archives/(..*)|\/page/\d{1,3}';

\/archives/(..*)」は本サイトの記事には「/archives/」が入っていることから正規表現「(..*)」を使ってワイルドカードを設定、「\/page/\d{1,3}」はリストページの2ページ以降に入る「/page/**(数値)」に対応するため、正規表現「\d{1,3}」で1~3桁の数値を指定しています。

カスタマイズ結果

/etc/logwatch/scripts/services/httpを修正したのでlogwatchを実行してみます。

# logwatch --print
--------------------- httpd Begin ------------------------

 439.78 MB transferred in 22864 responses  (1xx 0, 2xx 20172, 3xx 1886, 4xx 806, 5xx 0)
    10348 Images (347.75 MB),
       26 Documents (0.03 MB),
    10771 Content pages (53.31 MB),
        9 Redirects (0.00 MB),
      967 Fonts (38.56 MB),
      743 Other (0.13 MB)

 A total of 239 sites probed the server

 A total of 1 possible successful probes were detected (the following URLs
 contain strings that match one or more of a listing of strings that
 indicate a possible exploit):

    /archives/tag/\xe3\x81\xa9\xe3\x82\x89\xe3\x82\x84\xe3\x81\x8d HTTP Response 301

 Requests with error response codes
    400 Bad Request
       /: 1 Time(s)
       /x: 1 Time(s)
    401 Unauthorized

----------------------------------------------------------------------------------------

transferredの分類が以前よりは最適化されました。また408のIPアドレスが表示されなくなり長々としていたレポートがスッキリしています。表示される情報が少なくなったことで攻撃への気づきが遅れる可能性がありますが、「 A total of XXX sites probed the server」の行自体は残しているので、この件数を確認すれば408の原因がChromeなのかDDOS攻撃なのかの動向はつかめるはずです。

スポンサーリンク