閉じる

Tripwire(ファイル改竄検知システムの導入・設定)

[root@server1 ~]# dnf install -y epel-release ← EPELリポジトリをインストールします。

[root@server1 ~]# dnf -y install tripwire ← tripwireをインストールします。

[root@server1 ~]# tripwire-setup-keyfiles ← tripwirebの初期設定をします。


The Tripwire site and local passphrases are used to sign a variety of
files, such as the configuration, policy, and database files.
Passphrases should be at least 8 characters in length and contain both
letters and numbers.
See the Tripwire manual for more information.

Creating key files…
(When selecting a passphrase, keep in mind that good passphrases typically
have upper and lower case letters, digits and punctuation marks, and are
at least 8 characters in length.)
Enter the site keyfile passphrase: ← 任意のサイトパスフレーズを応答します。
Verify the site keyfile passphrase: ← 任意のサイトパスフレーズ確認
Generating key (this may take several minutes)…Key generation complete.
(When selecting a passphrase, keep in mind that good passphrases typically
have upper and lower case letters, digits and punctuation marks, and are
at least 8 characters in length.)
Enter the local keyfile passphrase: ← 任意のローカルパスフレーズを応答します。
Verify the local keyfile passphrase: ← 任意のローカルパスフレーズ確認
Generating key (this may take several minutes)…Key generation complete.

Signing configuration file…
Please enter your site passphrase: ← サイトパスフレーズを応答します。
Wrote configuration file: /etc/tripwire/tw.cfg
A clear-text version of the Tripwire configuration file:
/etc/tripwire/twcfg.txt
has been preserved for your inspection. It is recommended that you
move this file to a secure location and/or encrypt it in place (using a
tool such as GPG, for example) after you have examined it.

Signing policy file…
Please enter your site passphrase: ← サイトパスフレーズを応答します。
Wrote policy file: /etc/tripwire/tw.pol
A clear-text version of the Tripwire policy file:
/etc/tripwire/twpol.txt
has been preserved for your inspection. This implements a minimal
policy, intended only to test essential Tripwire functionality. You
should edit the policy file to describe your system, and then use
twadmin to generate a new signed copy of the Tripwire policy.
Once you have a satisfactory Tripwire policy file, you should move the
clear-text version to a secure location and/or encrypt it in place
(using a tool such as GPG, for example).
Now run “tripwire –init” to enter Database Initialization Mode. This
reads the policy file, generates a database based on its contents, and
then cryptographically signs the resulting database. Options can be
entered on the command line to specify which policy, configuration, and
key files are used to create the database. The filename for the
database can be specified as well. If no options are specified, the
default values from the current configuration file are used.

[root@server1 ~]# vi /etc/tripwire/twcfg.txt ← Tripwireの設定ファイル(テキスト版)を編集します。

LOOSEDIRECTORYCHECKING =true ← ファイル変更時に、所属ディレクトリの変更を通知しないようにします。
REPORTLEVEL =4 ← リポートレベルを変更します。

[root@server1 ~]# twadmin -m F -c /etc/tripwire/tw.cfg -S /etc/tripwire/site.key /etc/tripwire/twcfg.txt ← Tripwireの設定ファイル(暗号署名版)を作成します。
Please enter your site passphrase: ← サイトパスフレーズを応答します。
Wrote configuration file: /etc/tripwire/tw.cfg

[root@server1 ~]# rm -f /etc/tripwire/twcfg.txt ← Tripwireの設定ファイル(テキスト版)を削除します。

↓ Tripwire設定ファイル(テキスト版)を復元する場合
[root@server1 ~]# twadmin -m f -c /etc/tripwire/tw.cfg > /etc/tripwire/twcfg.txt

[root@server1 ~]# 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@server1 ~]# perl /etc/tripwire/twpolmake.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.new ← ポリシーファイルを最適化します。


[root@server1 ~]# echo ! "/var/lib/tripwire/hostname.twd ;" >> /etc/tripwire/twpol.txt.new ← Tripwireデータベース自体をチェック対象外にします。


[root@server1 ~]# echo ! "/tmp/tripwire.log ;" >> /etc/tripwire/twpol.txt.new ← Tripwireログをチェック対象外にします。※後述のTripwire定期自動実行スクリプトで使用します。

[root@server1 ~]# 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@server1 ~]# rm -f /etc/tripwire/twpol.txt* ← ポリシーファイル(テキスト版)を削除します。

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

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

[root@server1 ~]# dnf -y install tmpwatch ← tmpwatchをインストールします。


[root@server1 ~]# 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@server1 ~]# chmod 700 tripwire.sh ← Tripwire定期自動実行スクリプトへ実行権限を付加します。


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

[root@server1 ~]# twadmin -m p -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key > /etc/tripwire/twpol.txt ← ポリシーファイル(テキスト版)を作成します。


[root@server1 ~]# echo ‘!/var/log/sa ;’ >> /etc/tripwire/twpol.txt ← /var/log/saをチェック対象外にします。


[root@server1 ~]# echo ‘!/etc/sysconfig/iptables ;’ >> /etc/tripwire/twpol.txt ← /etc/sysconfig/iptablesをチェック対象外にします。


[root@server1 ~]# 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@server1 ~]# rm -f /etc/tripwire/twpol.txt /etc/tripwire/tw.pol.bak ← ポリシーファイル(テキスト版とバックアップ)を削除します。


[root@server1 ~]# rm -f /var/lib/tripwire/.twd ; tripwire -m i -s -c /etc/tripwire/tw.cfg ← Tripwireのデータベースを更新します。


Please enter your local passphrase: ← ローカルパスフレーズを応答します。

[root@server1 ~]# tripwire -m c -s -c /etc/tripwire/tw.cfg ← Tripwireチェックを実行します。

© 2025 Linuxで自宅サーバー(ホームサーバー)-構築・設定方法- | WordPress テーマ: CrestaProject の Annina Free