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

ファイルの改竄を検知する為に「Tripwire」を導入する。

「Tripwire」のインストール

「Tripwire」のインストール

Gentoo ~ # emerge tripwire

セットアップ

セットアップスクリプトを実行し、サイトパスフレーズとローカルパスフレーズを設定する。

Gentoo ~ # /etc/tripwire/twinstall.sh <= セットアップスクリプトの実行

----------------------------------------------
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 delete this file manually 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.
「Tripwire」の設定

テキスト版の設定ファイルを編集する。

Gentoo ~ # vi /etc/tripwire/twcfg.txt <= 設定ファイルの編集
REPORTFILE             =/var/lib/tripwire/report/$(HOSTNAME)-$(DATE).twr
↓
REPORTFILE             =/var/lib/tripwire/report/$(HOSTNAME).twr
 <= レポートファイル名を変更

LOOSEDIRECTORYCHECKING =false
↓
LOOSEDIRECTORYCHECKING =true <= ファイル変更検知時に所属ディレクトリまで変更検知しない

MAILNOVIOLATIONS       =true
↓
MAILNOVIOLATIONS       =false <= ファイル変更検知時のみメールを送信する

EMAILREPORTLEVEL       =3
↓
EMAILREPORTLEVEL       =4 <= ファイル変更チェック結果レポートレベルの変更

REPORTLEVEL            =3
↓
REPORTLEVEL            =4 <= ファイル変更チェック結果レポートレベルの変更

設定ファイルの暗号化

Gentoo ~ # twadmin --create-cfgfile -S /etc/tripwire/site.key /etc/tripwire/twcfg.txt
 <= 暗号化した設定ファイルの作成(テキスト版=>暗号署名版)
Please enter your site passphrase: <= サイトパスフレーズ入力
Wrote configuration file: /etc/tripwire/tw.cfg

Gentoo ~ # rm -f /etc/tripwire/twcfg.txt <= テキスト版の設定ファイルの削除


・暗号化されたファイルから設定ファイル(テキスト版)を作成する場合
Gentoo ~ # twadmin --print-cfgfile > /etc/tripwire/twcfg.txt
ポリシーファイルの設定

ポリシーファイルの作成

デフォルトのポリシーファイルでは存在しないファイルのチェックが有効であったり、 存在するファイルのチェックが無効になっている。
そこで、和泉宏明氏の作成された Perlスクリプトを利用し、 ポリシーファイルを作成する。

Gentoo ~ # wget http://www.aritia.org/hizumi/linux/tripwire_pol.pl.txt <= スクリプトファイルの取得
Gentoo ~ # mv tripwire_pol.pl.txt /etc/tripwire/tripwire_pol.pl <= スクリプトファイルを移動
Gentoo ~ # chmod 700 /etc/tripwire/tripwire_pol.pl <= スクリプトファイルに実行権限付加
Gentoo ~ # cat /etc/tripwire/tripwire_pol.pl <= Perlスクリプトの確認
#!/usr/bin/perl
# Tripwire Policy File customize tool for Redhat 8.0
# ----------------------------------------------------------------
# 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 tripwire_pol.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) = /^HOSTNAMEs*=\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 (! -e $tpath) {
            $_ = "$sharp#$tpath$cond" if ($ret == 0) ;
        }
        else {
            $_ = "$sharp$tpath$cond" ;
        }
    }
    print "$_\n" ;
}
close(POL) ;


Gentoo ~ # /etc/tripwire/tripwire_pol.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.tmp
 <= デフォルトのポリシーファイルを基にポリシーファイル作成

Gentoo ~ # rm -f /etc/tripwire/twpol.txt <= デフォルトのポリシーファイルを削除

Gentoo ~ # mv /etc/tripwire/twpol.txt.tmp /etc/tripwire/twpol.txt
 <= 作成したポリシーファイルをデフォルトのポリシーファイル名にリネーム

ポリシーファイルの設定

全てのファイル変更チェックの結果をroot宛にメールを送信するようにする。

Gentoo ~ # vi /etc/tripwire/twpol.txt <= 設定ファイルの作成
( emailto = root ) <= 追加<= 追加
# Tripwire Binaries


{
     /bin                              -> $(SEC_BIN) ;
     /lib                              -> $(SEC_BIN) ;
}
} <= 追加

ポリシーファイルの暗号化

Gentoo ~ # twadmin --create-polfile -S /etc/tripwire/site.key /etc/tripwire/twpol.txt
 <= 暗号化したポリシーファイルの作成(テキスト版=>暗号署名版)
Please enter your site passphrase: <= サイトパスフレーズ入力
Wrote policy file: /etc/tripwire/tw.pol

Gentoo ~ # rm -f /etc/tripwire/twpol.txt <= ポリシーファイル(テキスト版)を削除

・暗号化されたファイルからポリシーファイル(テキスト版)を作成する場合
Gentoo ~ # twadmin --print-polfile > /etc/tripwire/twpol.txt
データベースの作成

ポリシーファイルよりデータベースを作成する。

Gentoo ~ # 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/Gentoo.twd
The database was successfully generated.
Tripwireチェック
Gentoo ~ # tripwire --check <= ファイル変更チェック
Parsing policy file: /etc/tripwire/tw.pol
*** Processing Unix File System ***
Performing integrity check...
Wrote report file: /var/lib/tripwire/report/Gentoo.twr


Tripwire(R) 2.3.0 Integrity Check Report

Report generated by:          root
Report created on:            Thu Jun  1 20:48:07 2006
Database last updated on:     Never

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

Host name:                    Gentoo
Host IP address:              Unknown IP
Host ID:                      None
Policy file used:             /etc/tripwire/tw.pol
Configuration file used:      /etc/tripwire/tw.cfg
Database file used:           /var/lib/tripwire/Gentoo.twd
Command line used:            tripwire --check

===============================================================================
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
  Application Information Programs
                                  100               0        0        0
  (/sbin/genksyms)
  Critical Utility Sym-Links      100               0        0        0
  Critical system boot files      100               0        0        0
* Tripwire Data Files             100               1        0        0 <= Tripwire Data Filesに1件の追加を検出
  Security Control                100               0        0        0
  System boot changes             100               0        0        0
  OS executables and libraries    100               0        0        0
  Login Scripts                   100               0        0        0
  Critical configuration files    100               0        0        0
  Shell Binaries                  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

Total objects scanned:  13873
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/Gentoo.twd" <= /var/lib/tripwire/Gentoo.twdの追加を検出
 (Tripwireのデータベースの為、問題無し)

===============================================================================
Error Report:
===============================================================================

No Errors

-------------------------------------------------------------------------------
*** End of report ***

Tripwire 2.3 Portions copyright 2000 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.
「Tripwire」の自動実行

/etc/cron.daily/tripwire.cron ファイルが自動的作成され、cronで実行されるが、「Tripwire」が実行されるだけである。
そこで、中身を以下のように書き換え、ポリシーファイルとデータベースも最新化するようにする。

Gentoo ~ # vi /etc/cron.daily/tripwire.cron <= 設定ファイルの編集
#!/bin/sh

# 設定
RM=/usr/bin/rm
TRIPWIRE=/usr/sbin/tripwire
TWADMIN=/usr/sbin/twadmin
TRIPWIRE_POL=/etc/tripwire/tripwire_pol.pl
LOG=/var/log/tripwire.log
LOCALPASS=ローカルパスフレーズ <= ローカルパスフレーズを入力
SITEPASS=サイトパスフレーズ <= サイトパスフレーズを入力

HOST_NAME=`/usr/bin/uname -n`

if [ ! -e /var/lib/tripwire/${HOST_NAME}.twd ] ; then
        echo "****    Error: Tripwire database for ${HOST_NAME} not found.    ****"
        echo "**** Run "/etc/tripwire/twinstall.sh" and/or "tripwire --init". ****"
else
        # Tripwire実行
        test -f /etc/tripwire/tw.cfg &&  $TRIPWIRE --check --email-report > $LOG
        # ポリシーファイル最新化
        $TWADMIN --print-polfile > /etc/tripwire/twpol.txt
        $TRIPWIRE_POL /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.tmp
        $TWADMIN --create-polfile -S /etc/tripwire/site.key -Q $SITEPASS /etc/tripwire/twpol.txt.tmp >> $LOG
        $RM -f /etc/tripwire/twpol.*

        # データベース最新化
        $RM -f /var/lib/tripwire/${HOST_NAME}.twd
        $TRIPWIRE --init -P $LOCALPASS >> $LOG
fi
▲ページのトップへ