Tripwire(ファイル改竄検知システム)

自宅サーバー構築メモ

Tripwireインストール

[root@almalinux ~]# dnf -y install s-nail ※ EPELリポジトリインストール

[root@almalinux ~]# dnf -y install tripwire ※ tripwireインストール

[root@almalinux ~]# tripwire-setup-keyfiles ※ tripwire初期設定
Enter the site keyfile passphrase: ※ 任意のサイトパスフレーズ応答
Verify the site keyfile passphrase: ※ 任意のサイトパスフレーズ応答(確認)

Enter the local keyfile passphrase: ※ 任意のローカルパスフレーズ応答
Verify the local keyfile passphrase: ※ 任意のローカルパスフレーズ応答(確認)

Please enter your site passphrase: ※ サイトパスフレーズ応答

Please enter your site passphrase: ※ サイトパスフレーズ応答

Tripwire設定

1,Tripwire設定

[root@almalinux ~]# vi /etc/tripwire/twcfg.txt ※ Tripwire設定ファイル(テキスト版)編集
LOOSEDIRECTORYCHECKING =false
  ↓
LOOSEDIRECTORYCHECKING =true ※ ファイル変更時に所属ディレクトリの変更を通知しないようにする

REPORTLEVEL   =3
  ↓
REPORTLEVEL   =4 ※ リポートレベルを変更する

2,ポリシーファイル設定
  存在しないファイルのチェックを無効に、存在するファイルのチェックを有効にするPerlスクリプトを作成して、ポリシーファイルを最適化する。

[root@almalinux ~]# vi /etc/tripwire/twpolmake.pl ※ ポリシーファイル最適化スクリプト作成
#!/usr/bin/perl
# Tripwire Policy File customize tool
# ----------------------------------------------------------------
# Copyright (C) 2003 Hiroaki Izumi
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# ----------------------------------------------------------------
# Usage:
#    perl twpolmake.pl {Pol file}
# ----------------------------------------------------------------
#
$POLFILE=$ARGV[0];

open(POL,"$POLFILE") or die "open error: $POLFILE" ;
my($myhost,$thost) ;
my($sharp,$tpath,$cond) ;
my($INRULE) = 0 ;

while (<POL>) {
    chomp;
    if (($thost) = /^HOSTNAME\s*=\s*(.*)\s*;/) {
        $myhost = `hostname` ; chomp($myhost) ;
        if ($thost ne $myhost) {
            $_="HOSTNAME=\"$myhost\";" ;
        }
    }
    elsif ( /^{/ ) {
        $INRULE=1 ;
    }
    elsif ( /^}/ ) {
        $INRULE=0 ;
    }
    elsif ($INRULE == 1 and ($sharp,$tpath,$cond) = /^(\s*\#?\s*)(\/\S+)\b(\s+->\s+.+)$/) {
        $ret = ($sharp =~ s/\#//g) ;
        if ($tpath eq '/sbin/e2fsadm' ) {
            $cond =~ s/;\s+(tune2fs.*)$/; \#$1/ ;
        }
        if (! -s $tpath) {
            $_ = "$sharp#$tpath$cond" if ($ret == 0) ;
        }
        else {
            $_ = "$sharp$tpath$cond" ;
        }
    }
    print "$_\n" ;
}
close(POL) ;

[root@almalinux ~]# perl /etc/tripwire/twpolmake.pl /etc/tripwire/twpol.txt >/etc/tripwire/twpol.txt.new  ※ ポリシーファイル最適化
[root@almalinux ~]# echo ! "/var/lib/tripwire/`hostname`.twd ;" >> /etc/tripwire/twpol.txt.new ※Tripwireデータベース自体をチェック対象外にする 
[root@almalinux ~]# echo ! "/tmp/tripwire.log ;" >> /etc/tripwire/twpol.txt.new ※ Tripwireログをチェック対象外にする※後述のTripwire定期自動実行スクリプトで使用

[root@almalinux ~]# twadmin -m P -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key /etc/tripwire/twpol.txt.new ※ 最適化済ポリシーファイルを元にポリシーファイル(暗号署名版)作成
Please enter your site passphrase:  ※ サイトパスフレーズ応答
Wrote policy file: /etc/tripwire/tw.pol
[root@almalinux ~]# rm -f /etc/tripwire/twpol.txt* ※ ポリシーファイル(テキスト版)削除

※ポリシーファイル(テキスト版)を復元する場合
[root@almalinux ~]# twadmin -m p -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key > /etc/tripwire/twpol.txt

3,データベース作成

[root@almalinux ~]# tripwire -m i -s -c /etc/tripwire/tw.cfg ※ Tripwireデータベース作成
Please enter your local passphrase:  ※ ローカルパスフレーズ応答

Tripwire確認

[root@almalinux ~]# tripwire -m c -s -c /etc/tripwire/tw.cfg ※ Tripwireチェック実行
Open Source Tripwire(R) 2.4.3.7 Integrity Check Report

Report generated by:          root
Report created on:            2026年05月26日 10時40分18秒
Database last updated on:     Never

===============================================================================
Report Summary:
===============================================================================

Host name:                    almalinux
Host IP address:              192.168.10.150
Host ID:                      None
Policy file used:             /etc/tripwire/tw.pol
Configuration file used:      /etc/tripwire/tw.cfg
Database file used:           /var/lib/tripwire/almalinux.twd
Command line used:            tripwire -m c -s -c /etc/tripwire/tw.cfg 

===============================================================================
Rule Summary: 
===============================================================================

-------------------------------------------------------------------------------
  Section: Unix File System
-------------------------------------------------------------------------------

  Rule Name                       Severity Level    Added    Removed  Modified 
  ---------                       --------------    -----    -------  -------- 
  User binaries                   66                0        0        0        
  Tripwire Binaries               100               0        0        0        
  Libraries                       66                0        0        0        
  Operating System Utilities      100               0        0        0        
  File System and Disk Administraton Programs
                                  100               0        0        0        
  Kernel Administration Programs  100               0        0        0        
  Networking Programs             100               0        0        0        
  System Administration Programs  100               0        0        0        
  Hardware and Device Control Programs
                                  100               0        0        0        
  System Information Programs     100               0        0        0        
  (/sbin/runlevel)
  Application Information Programs
                                  100               0        0        0        
  (/sbin/rtmon)
  Critical Utility Sym-Links      100               0        0        0        
  Shell Binaries                  100               0        0        0        
  Critical system boot files      100               0        0        0        
* Tripwire Data Files             100               1        0        0        
  System boot changes             100               0        0        0        
  OS executables and libraries    100               0        0        0        
  Security Control                100               0        0        0        
  Login Scripts                   100               0        0        0        
  Critical configuration files    100               0        0        0        
  Root config files               100               0        0        0        
  Invariant Directories           66                0        0        0        
  Temporary directories           33                0        0        0        
  Critical devices                100               0        0        0        
  (/proc/kcore)

Total objects scanned:  38977
Total violations found:  1

===============================================================================
Object Summary: 
===============================================================================

-------------------------------------------------------------------------------
# Section: Unix File System
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
Rule Name: Tripwire Data Files (/var/lib/tripwire)
Severity Level: 100
-------------------------------------------------------------------------------

Added:
"/var/lib/tripwire/almalinux.twd.bak"

===============================================================================
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.

[root@almalinux ~]# echo test > test.txt ※ テストファイルを作成

[root@almalinux ~]# tripwire -m c -s -c /etc/tripwire/tw.cfg ※ Tripwireチェック再実行
Open Source Tripwire(R) 2.4.3.7 Integrity Check Report

(途中層略)
 
  Security Control                100               0        0        0        
  Login Scripts                   100               0        0        0        
  Critical configuration files    100               0        0        0        
* Root config files               100               1        0        0 ← 1件のファイル追加を検知した        
  Invariant Directories           66                0        0        0        
  Temporary directories           33                0        0        0        
  Critical devices                100               0        0        0        
  (/proc/kcore)

Total objects scanned:  38978
Total violations found:  1 ← 1件の変更を検知し

===============================================================================
Object Summary: 
===============================================================================

(途中層略)

----------------------------------------------------------------------------
Rule Name: Root config files (/root)
Severity Level: 100
-------------------------------------------------------------------------------

Added:
"/root/test.txt" ← 追加されたファイル

[root@almalinux ~]# twprint -m r --report-level 1 -c /etc/tripwire/tw.cfg -r /var/lib/tripwire/report/almalinux-20260526-104317.twr  ※ チェック結果サマリ参照
Note: Report is not encrypted.
Added:  "/root/test.txt"  ※ /root/test.txtの追加を検知した

[root@almalinux ~]# rm -f test.txt ※ テストファイル削除(後始末)

Tripwire定期自動実行設定

[root@almalinux ~]# dnf -y install tmpwatch ※ tmpwatchインストール

[root@almalinux ~]# vi tripwire.sh ※ Tripwire定期自動実行スクリプト作成
#!/bin/bash

# 既存のTripwire定期自動実行設定削除
rm -f /etc/cron.daily/tripwire-check

# パスフレーズ設定
LOCALPASS=xxxxxxxx # ローカルパスフレーズ
SITEPASS=xxxxxxxx  # サイトパスフレーズ

TRIPWIRE=/usr/sbin/tripwire
TWADMIN=/usr/sbin/twadmin
TWPRINT=/usr/sbin/twprint
cd /etc/tripwire

# Tripwireチェック実行
# ※ファイル変更を検知した場合のみroot宛にサマリをメールする
rm -f /var/lib/tripwire/report/`hostname`-`date +%Y%m%d`-*.twr
${TRIPWIRE} -m c -s -c tw.cfg > /tmp/tripwire.log
if [ $(grep "Total violations found" /tmp/tripwire.log | awk '{print $4}') -ne 0 ]; then
    ${TWPRINT} -m r --report-level 1 -c tw.cfg -r /var/lib/tripwire/report/`hostname`-`date +%Y%m%d`-*.twr | \
    mail -s "Tripwire Integrity Check Report from `hostname`" root
fi

# Tripwireチェック実行結果(過去分)削除
# ※過去90日分保管
tmpwatch -m 2160 /var/lib/tripwire/report

# ポリシーファイル最新化
${TWADMIN} -m p -c tw.cfg -p tw.pol -S site.key > twpol.txt
perl twpolmake.pl twpol.txt > twpol.txt.new
${TWADMIN} -m P -c tw.cfg -p tw.pol -S site.key -Q $SITEPASS twpol.txt.new > /dev/null
rm -f twpol.txt* *.bak

# データベース最新化
rm -f /var/lib/tripwire/*.twd*
${TRIPWIRE} -m i -s -c tw.cfg -P $LOCALPASS

[root@almalinux ~]# chmod 700 tripwire.sh ※ Tripwire定期自動実行スクリプトへ実行権限付加

[root@almalinux ~]# echo "0 3 * * * root /root/tripwire.sh" > /etc/cron.d/tripwire ※ Tripwire定期自動実行設定追加※毎日3:00にTripwire定期実行スクリプトを実行する

ポリシーファイルメンテナンス

あらかじめ変更がわかっているディレクトリやファイルについては検知されないようにする。

[root@almalinux ~]# twadmin -m p -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key > /etc/tripwire/twpol.txt ※ ポリシーファイル(テキスト版)作成
[root@almalinux ~]# echo '!/var/log/sa ;' >> /etc/tripwire/twpol.txt ※ 例としてディレクトリ/var/log/saをチェック対象外にする

[root@almalinux ~]# echo '!/etc/sysconfig/iptables ;' >> /etc/tripwire/twpol.txt ※ 例としてファイル/etc/sysconfig/iptablesをチェック対象外にする

[root@almalinux ~]# twadmin -m P -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key /etc/tripwire/twpol.txt ※ 最適化済ポリシーファイルを元にポリシーファイル(暗号署名版)作成
Please enter your site passphrase:  ※ サイトパスフレーズ応答
Wrote policy file: /etc/tripwire/tw.pol

[root@almalinux ~]# rm -f /etc/tripwire/twpol.txt /etc/tripwire/tw.pol.bak ※ ポリシーファイル(テキスト版&バックアップ)削除

[root@almalinux ~]# rm -f /var/lib/tripwire/*.twd* ; tripwire -m i -s -c /etc/tripwire/tw.cfg ※Tripwireデータベース更新 
Please enter your local passphrase:  ※ ローカルパスフレーズ応答

コメント