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

最終更新日: 2014.02.19

<<トップページ <<新着情報 <<サイト内検索 <<CentOSで自宅サーバー構築 <<Scientific Linuxで自宅サーバー構築

■概要

クラッカーによるLinuxサーバーのファイルの改竄を検知するシステムを導入する。
ここでは、ファイル改竄検知システムにホスト型IDS(IDS=Intrusion Detection System)のTripwireを導入する。
Tripwireは、導入時のファイル状態をデータベース化し、そのデータベースとファイルの現状を比較することにより、ファイルの追加/変更/削除を検知するため、Linuxインストール後の初期の段階で導入しておくのが望ましい。


■Tripwireインストール

[root@fedora ~]# wget http://jaist.dl.sourceforge.net/sourceforge/tripwire/tripwire-2.4.1.2-src.tar.bz2 ← tripwireダウンロード

※最新版のURLはダウンロードページで確認すること

[root@fedora ~]# tar jxvf tripwire-2.4.1.2-src.tar.bz2 ← tripwire展開

[root@fedora ~]# cd tripwire-2.4.1.2-src ← tripwire展開先ディレクトリへ移動

[root@fedora tripwire-2.4.1.2-src]# ./configure --prefix=/usr/local/tripwire sysconfdir=/etc/tripwire && make && make install
 ← tripwireインストール

Press ENTER to view the License Agreement. ← 空ENTER

SPACEキー押下でライセンス表示を流す

Please type "accept" to indicate your acceptance of this
license agreement. [do not accept] accept ← accept応答

Continue with installation? [y/n] y ← y応答

Enter the site keyfile passphrase: ← 任意のサイトパスフレーズ応答
Verify the site keyfile passphrase: ← 任意のサイトパスフレーズ応答(確認)

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

Creating signed configuration file...
Please enter your site passphrase: ← サイトパスフレーズ応答

Creating signed policy file...
Please enter your site passphrase: ← サイトパスフレーズ応答

The installation succeeded.

[root@fedora tripwire-2.4.1.2-src]# cd ← tripwire展開先ディレクトリを抜ける

[root@fedora ~]# rm -rf tripwire-2.4.1.2-src ← tripwire展開先ディレクトリを削除

[root@fedora ~]# rm -f tripwire-2.4.1.2-src.tar.bz2 ← tripwireを削除

[root@fedora ~]# echo PATH=$PATH:/usr/local/tripwire/sbin >> .bashrc ; source .bashrc
 ← tripwire実行ファイル格納ディレクトリへパスを通す

■Tripwire設定

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

REPORTLEVEL   =4 ← リポートレベルを変更する

[root@fedora ~]# 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@fedora ~]# rm -f /etc/tripwire/twcfg.txt ← Tripwire設定ファイル(テキスト版)削除

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

(2)ポリシーファイル設定
Tripwireは、ポリシーファイルを元に作成したデータベースと、現在のファイル状態を比較することにより、ファイルが変更されたかどうかを検知するが、デフォルトのポリシーファイルでは存在しないファイルのチェックが有効になっていたり、逆に存在するファイルのチェックが無効になっていたりするため、存在しないファイルのチェックを無効に、存在するファイルのチェックを有効にするPerlスクリプトを作成して、ポリシーファイルを最適化する。
[root@fedora ~]# 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@fedora ~]# perl /etc/tripwire/twpolmake.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.new ← ポリシーファイル最適化

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

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

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

■Tripwire確認

[root@fedora ~]# tripwire -m c -s -c /etc/tripwire/tw.cfg ← Tripwireチェック実行
Tripwire(R) 2.4.0 Integrity Check Report

Report generated by:          root
Report created on:            Fri Mar 31 20:06:47 2006
Database last updated on:     Never

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

Host name:                    fedora.fedorasrv.com
Host IP address:              127.0.0.1
Host ID:                      None
Policy file used:             /etc/tripwire/tw.pol
Configuration file used:      /etc/tripwire/tw.cfg
Database file used:           /usr/local/tripwire/lib/tripwire/fedora.fedorasrv.com.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
  ---------                       --------------    -----    -------  --------
  Tripwire Data Files             0                 0        0        0
  Monitor Filesystems             0                 0        0        0
  OS Binaries and Libraries       0                 0        0        0
  Tripwire Binaries               0                 0        0        0
  User Binaries and Libraries     0                 0        0        0
  Temporary Directories           0                 0        0        0
  RPM Checksum Files              0                 0        0        0
  System Boot Changes             0                 0        0        0
  Global Configuration Files      0                 0        0        0
  OS Boot Files and Mount Points  0                 0        0        0
  OS Devices and Misc Directories 0                 0        0        0
  Root Directory and Files        0                 0        0        0

Total objects scanned:  55650
Total violations found:  0

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

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

No violations.

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

No Errors

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

Tripwire 2.4 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.

[root@fedora ~]# echo test > test.txt ← 試しにテストファイルを作成する

[root@fedora ~]# tripwire -m c -s -c /etc/tripwire/tw.cfg ← Tripwireチェック再実行
Tripwire(R) 2.4.0 Integrity Check Report

Report generated by:          root
Report created on:            Fri Mar 31 20:21:28 2006
Database last updated on:     Never

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

Host name:                    fedora.fedorasrv.com
Host IP address:              127.0.0.1
Host ID:                      None
Policy file used:             /etc/tripwire/tw.pol
Configuration file used:      /etc/tripwire/tw.cfg
Database file used:           /usr/local/tripwire/lib/tripwire/fedora.fedorasrv.com.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
  ---------                       --------------    -----    -------  --------
  Tripwire Data Files             0                 0        0        0
  Monitor Filesystems             0                 0        0        0
  OS Binaries and Libraries       0                 0        0        0
  Tripwire Binaries               0                 0        0        0
  User Binaries and Libraries     0                 0        0        0
  Temporary Directories           0                 0        0        0
  RPM Checksum Files              0                 0        0        0
  System Boot Changes             0                 0        0        0
  Global Configuration Files      0                 0        0        0
  OS Boot Files and Mount Points  0                 0        0        0
  OS Devices and Misc Directories 0                 0        0        0
* Root Directory and Files        0                 1        0        0

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

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

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

-------------------------------------------------------------------------------
Rule Name: Root Directory and Files (/root)
Severity Level: 0
-------------------------------------------------------------------------------

Added:
"/root/test.txt" ← /root/test.txtの追加を検知した

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

No Errors

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

Tripwire 2.4 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.

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

※過去のチェック結果を参照する場合
[root@fedora ~]# twprint -m r -c /etc/tripwire/tw.cfg -r /usr/local/tripwire/lib/tripwire/report/fedora.fedorasrv.com-20060331-202128.twr

■Tripwire定期自動実行設定

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

PATH=/usr/sbin:/usr/bin:/bin:/usr/local/tripwire/sbin

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

cd /etc/tripwire

# Tripwireチェック実行
tripwire -m c -s -c tw.cfg|mail -s "Tripwire(R) Integrity Check Report in `hostname`" root

# ポリシーファイル最新化
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 /usr/local/tripwire/lib/tripwire/*.twd*
tripwire -m i -s -c tw.cfg -P $LOCALPASS

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

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


■関連コンテンツ




▲このページのトップへ戻る

プライバシーポリシー