【IDS】Tripwireを入れてファイル差分の検知内容をChatworkに通知してみる(Ubuntu 22.0.4)
こんにちは。
部下からの信頼度ゼロトラスト
システムソリューション部のかわいです。
あけましておめでとうございます。
今年も変わらずがんばりたいと思います。
今回はホスト型IDSのOSS「Tripwire」を使って、ファイル改ざんがあった場合の検知内容を自動でChatwork通知してみたいと思います。
サーバー内のあまり触らないファイルやディレクトリの変更にはなかなか気づきにくいので、意外と使えそうです。本格的に運用するのは大変ですが、導入自体はそんなに難しくないです。
Tripwireとは
Tripwireは、事前定義したディレクトリ配下のファイルを監視して、その結果を確認したり通知できるIDS(侵入検知)、というかファイル改ざん検知ツールです。
Tripwire Inc.社の製品で、無償のOSS版と有償版が存在します。今回はOSS版を使用します(IPS機能はありません)。
man page:https://linux.die.net/man/8/tripwire
インストール
■検証環境
1 2 3 | Ubuntu 24.04 LTS Tripwire 2.4.3.7.0 Python 3.12.3 # 通知に使います |
■インストールと初期設定
1 2 | $ sudo apt update $ sudo apt install tripwire |
インストールはこれだけ。
インストール中にプロンプトがいくつか表示されるので、適切な設定を選択し進みます。
※今回は完全にlocalhost環境を想定しています。
▼ 用途を選択します。今回はLocal Onlyにしましたが、サイトを外部公開している環境であればInternet Siteを選びます。
▼ 通知用のメールアドレス用ドメイン。特にない場合はexample.comなどで大丈夫です。今回は使いません。
▼ 安全のため、Tripwire用にキーペアを作成します。デフォルトのYesを選択。
(詳しくはtwfiles(5)に記載があります:https://linux.die.net/man/5/twfiles)
▼ 初期構成のため、Yes
▼ パスフレーズを入力(メモ推奨)。確認用と2回入力します。
▼ インストール完了
ポリシーファイル設定に進みます。
ポリシー設定
「twpol.txt」がポリシーファイルなので、これを編集します。
念のため事前にバックアップを取得します。
1 2 | $ sudo cp /etc/tripwire/twpol .txt /etc/tripwire/twpol_org .txt $ sudo nano /etc/tripwire/twpol .txt |
検知内容が長くなってしまうので、今回は「/var/www/html以下のみ」を対象にしたいと思います。
※本記事と同じにする場合は以下内容をコピペしてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # Global Variable Definitions @@section GLOBAL TWBIN = /usr/sbin ; TWETC = /etc/tripwire ; TWVAR = /var/lib/tripwire ; # File System Definitions @@section FS # Tripwire Binaries ( rulename = "Tripwire Binaries" , severity = 100 ) { $(TWBIN) /siggen -> $(ReadOnly) ; $(TWBIN) /tripwire -> $(ReadOnly) ; $(TWBIN) /twadmin -> $(ReadOnly) ; $(TWBIN) /twprint -> $(ReadOnly) ; } # Apache Document Root ( rulename = "Apache Document Root" , severity = 100 ) { /var/www/html/ -> $(IgnoreNone)-SHa; } # End of Policy File |
ポリシーの編集が完了したら、ポリシー適用と初期化を行います。
パスフレーズには前述の初期設定プロンプトで設定したものを入力します。
以下のように出力されればOKです。
1 2 3 4 5 6 7 8 9 10 11 | $ sudo twadmin --create-polfile /etc/tripwire/twpol .txt Please enter your site passphrase: Wrote policy file : /etc/tripwire/tw .pol $ sudo tripwire --init Please enter your local passphrase: Parsing policy file : /etc/tripwire/tw .pol Generating the database... *** Processing Unix File System *** Wrote database file : /var/lib/tripwire/hamchan .twd The database was successfully generated. |
これで基本設定は完了です。
試しにチェックをかけてみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | $ sudo tripwire --check Parsing policy file : /etc/tripwire/tw .pol *** Processing Unix File System *** Performing integrity check... Wrote report file : /var/lib/tripwire/report/hamcha-20241025-135438 .twr Open Source Tripwire(R) 2.4.3.7 Integrity Check Report Report generated by: root Report created on: Fri 25 Oct 2024 01:54:38 PM JST Database last updated on: Never =============================================================================== Report Summary: =============================================================================== Host name: hamchan Host IP address: 127.0.1.1 Host ID: None Policy file used: /etc/tripwire/tw .pol Configuration file used: /etc/tripwire/tw .cfg Database file used: /var/lib/tripwire/hamchan .twd Command line used: tripwire --check =============================================================================== Rule Summary: =============================================================================== ------------------------------------------------------------------------------- Section: Unix File System ------------------------------------------------------------------------------- Rule Name Severity Level Added Removed Modified --------- -------------- ----- ------- -------- Tripwire Binaries 100 0 0 0 Apache Document Root 100 0 0 0 ( /var/www/html ) Total objects scanned: 7 Total violations found: 0 =============================================================================== Object Summary: =============================================================================== ------------------------------------------------------------------------------- # Section: Unix File System ------------------------------------------------------------------------------- No violations. =============================================================================== Error Report: =============================================================================== No Errors ------------------------------------------------------------------------------- *** End of report *** Open Source Tripwire 2.4 Portions copyright 2000-2018 Tripwire, Inc. Tripwire is a registered trademark of Tripwire, Inc. This software comes with ABSOLUTELY NO WARRANTY; for details use --version. This is free software which may be redistributed or modified only under certain conditions; see COPYING for details. All rights reserved. Integrity check complete. |
スキャンされたオブジェクト数などをまとめたレポートが出力されました。
次は、ファイル差分を検知させてみます。
差分の検知
手始めに、以下HTMLファイルに「test」と記述し、編集してみます。
1 | sudo nano /var/www/html/test .html |
再度スキャンをかけると、「Modified」箇所に変更の加わったディレクトリやファイルについてのサマリが表示されました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # 抜粋 =============================================================================== Rule Summary: =============================================================================== ------------------------------------------------------------------------------- Section: Unix File System ------------------------------------------------------------------------------- Rule Name Severity Level Added Removed Modified --------- -------------- ----- ------- -------- Tripwire Binaries 100 0 0 0 * Apache Document Root 100 0 0 2 ( /var/www/html ) Total objects scanned: 7 Total violations found: 2 =============================================================================== Object Summary: =============================================================================== ------------------------------------------------------------------------------- # Section: Unix File System ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Rule Name: Apache Document Root ( /var/www/html ) Severity Level: 100 ------------------------------------------------------------------------------- Modified: "/var/www/html" "/var/www/html/test.html" |
今回は「/var/www/html」以下で「test.html」に変更が加わった、ことが分かります。
ちなみにファイルの所有者やパーミッションが変更された場合でも検知対象になります。
次に、新規ファイルを作成してみます。
1 | sudo touch /var/www/html/ransom .sh |
再度スキャンをかけます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | ------------------------------------------------------------------------------- Section: Unix File System ------------------------------------------------------------------------------- Rule Name Severity Level Added Removed Modified --------- -------------- ----- ------- -------- Tripwire Binaries 100 0 0 0 * Apache Document Root 100 1 0 2 ( /var/www/html ) Total objects scanned: 8 Total violations found: 3 =============================================================================== Object Summary: =============================================================================== ------------------------------------------------------------------------------- # Section: Unix File System ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Rule Name: Apache Document Root ( /var/www/html ) Severity Level: 100 ------------------------------------------------------------------------------- Added: "/var/www/html/ransom.sh" Modified: "/var/www/html" "/var/www/html/test.html" |
「Added」箇所に1とあるので、「/var/www/html/ransom.sh」が作成されたことが分かります。
レポートを確認すれば、意図しないファイルが置かれたことが一目瞭然ですね。便利。
定期実行とレポート通知
Tripwireにはメール通知機能がありますが、今回はChatworkに通知する方法を紹介したいと思います。
まずはcronジョブを仕込みます。
通常のcheckだとレポートがバイナリで出力されるので、テキストファイルで任意のパスに保存させます。
実行間隔は環境や用途に応じてお好みで。
1 2 3 4 5 6 7 8 | sudo crontab -e 0 0 * * * /usr/sbin/tripwire --check > /home/hamchan/ $( date +\%Y\%m\%d)_report.txt # ※出力時に日付(yyyymmdd)を付与する処理を加えています。 $ echo $( date ) Fri Oct 25 02:47:26 PM JST 2024 $ echo $( date +\%Y\%m\%d) 20241025 |
次に、Chatwork通知用のPythonコードを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import requests import subprocess import os from glob import glob # Chatwork APIの設定 API_TOKEN = 'xxxxxxxx' # ChatworkのAPIトークン ROOM_ID = 'xxxxxxxx' # 送信先グループのID HEADERS = { 'X-ChatworkToken' : API_TOKEN, } # 最新のTripwireレポートファイルのパスを返す関数 def get_latest_tripwire_report(): report_files = glob( '/home/hamchan/*_report.txt' ) if report_files: return max (report_files, key = os.path.getmtime) # 最新のファイルを取得 return None # ファイルを読み込ませて、テキストとして返す関数 def get_report_content(report_path): with open (report_path, 'r' , encoding = 'utf-8' ) as file : return file .read() # ファイルの内容をテキストとして返す # Chatworkにファイル名とパスを送信 def send_chatwork_message(message): payload = { 'body' : message} response = requests.post(ENDPOINT, headers = HEADERS, data = payload) if response.status_code = = 200 : print ( 'Chatworkに通知が送信されました。' ) else : print (f '通知に失敗しました: {response.status_code}, {response.text}' ) if __name__ = = '__main__' : latest_report_path = get_latest_tripwire_report() if latest_report_path: report_content = get_report_content(latest_report_path) # レポート内容を取得 send_chatwork_message( f "[info][title]Tripwireのレポートが以下に保存されました[/title]\n" f "{latest_report_path}[/info]\n\n" f "\n{report_content}" ) else : send_chatwork_message( "最新のTripwireレポートが見つかりませんでした。" ) |
cronジョブに仕込みます。
試しにTripwireのレポート出力後、2分後に通知を行なうようにしました。
1 2 | sudo crontab -e 2 0 * * * /usr/bin/python3 /home/hamchan/tripwire_test/chatwork_notification .py |
▼ 通知内容
こんなんでました。
これで不審なファイルが置かれていても気付くことができますね。何も入れないよりは格段にセキュリティ向上に繋がると思います。
ぜひお試しあれ。
完