Tripwire(トリップワイヤ)は、主にLinuxで活用されている、ファイルの変更・改竄検知をしてくれるソフトウェアです。
無償で公開されているオープンソース版と、Tripwire社が販売している商用版があります。
IDS(Intrusion Detection System)とは不正侵入検知システムのことである。ネットワーク型IDS(NIDS)とホスト型IDS(HIDS)がある
Tripwireはこれ。
ファイル改竄検知システムにホスト型IDS(IDS=Intrusion Detection System)
IPSとは不正侵入予防システムのこと。不正と判断したパケットを遮断する
目次
前準備
既にインストールされている場合はアンインストールしておきます。
# yum remove tripwire
その他tripwireのごみファイルが存在しないか確認し、存在していたらrmコマンドで消しておきます。
# find /* | grep tripwire
Tripwireのインストール
今回は、rpm版をyumでインストールします。(もちろんソースからインストールも可能です。)
ソースからインストールする場合
任意のディレクトリで、ソースファイルを取得します。
# wget http://jaist.dl.sourceforge.net/sourceforge/tripwire/tripwire-2.4.2.2-src.tar.bz2
最新版はこちらより確認して落としてください。
https://sourceforge.net/projects/tripwire/files/tripwire-src/
ソースを落としたら展開して以下のようにインストールします。
# tar jxvf tripwire-2.4.2.2-src.tar.bz2
# cd tripwire-2.4.2.2-src
# ./configure --prefix=/usr/local/tripwire --sysconfdir=/etc/tripwire && make && make install
以下略
yumでインストールする場合
# yum --enablerepo=epel install tripwire
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* epel: ftp.jaist.ac.jp
* extras: ftp.iij.ad.jp
* remi-safe: mirrors.tuna.tsinghua.edu.cn
* updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ tripwire.x86_64 0:2.4.3.1-10.el7 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
==================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==================================================================================================================================================================
インストール中:
tripwire x86_64 2.4.3.1-10.el7 epel 1.0 M
トランザクションの要約
==================================================================================================================================================================
インストール 1 パッケージ
総ダウンロード容量: 1.0 M
インストール容量: 4.2 M
Is this ok [y/d/N]: y
Downloading packages:
tripwire-2.4.3.1-10.el7.x86_64.rpm | 1.0 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : tripwire-2.4.3.1-10.el7.x86_64 1/1
検証中 : tripwire-2.4.3.1-10.el7.x86_64 1/1
インストール:
tripwire.x86_64 0:2.4.3.1-10.el7
完了しました!
Tripwireの初期設定
インストールが完了したら、Tripwireの初期設定を行います。
★★の箇所で任意のサイトパスフレーズ・ローカルパスフレーズを入力します。
tripwire-setup-keyfiles
----------------------------------------------
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.
Tripwireの初期設定その2
以下のディレクトリに設定ファイルが生成されていますので、
そちらに設定を記載していきます。
# vi /etc/tripwire/twcfg.txt
ROOT =/usr/sbin
POLFILE =/etc/tripwire/tw.pol
DBFILE =/var/lib/tripwire/$(HOSTNAME).twd
REPORTFILE =/var/lib/tripwire/report/$(HOSTNAME)-$(DATE).twr
SITEKEYFILE =/etc/tripwire/site.key
LOCALKEYFILE =/etc/tripwire/$(HOSTNAME)-local.key
EDITOR =/bin/vi
LATEPROMPTING =false
LOOSEDIRECTORYCHECKING =true
MAILNOVIOLATIONS =true
EMAILREPORTLEVEL =3
REPORTLEVEL =4
MAILMETHOD =SENDMAIL
SYSLOGREPORTING =false
MAILPROGRAM =/usr/sbin/sendmail -oi -t
LOOSEDIRECTORYCHECKING=true は変更があった場合に、ファイルとディレクトリの2重検知を纏めます。
REPORTLEVEL は(0~4)が指定できます。数字の大きい方がより詳細レポート。
tripwireはレポートレベルを上げるとCPU・メモリを結構食います。
逆にレベルを下げると、必要な情報が取得できない場合がありますのでチェック内容は
用途に合わせて設定する必要があります。
一昔前にはtripwireが1時間くらいかかってた時期もあったそうですが、
昨今はマシンスペックの向上により、(対象量や用途にもよりますが)1分以内で終わることが多いので、
レポートレベルを上げても夜中にやってしまえば大丈夫。
Tripwire設定ファイル(暗号署名版)を作成
ここがtripwireの少し分かりにくい部分ですが、先ほど用いたサイトパスフレーズを使って、ファイルを暗号化します。
暗号化コマンドを打ち込むとサイトパスフレーズを聞かれますので、
入力するとファイルが生成されます。
# twadmin -m F -c /etc/tripwire/tw.cfg -S /etc/tripwire/site.key /etc/tripwire/twcfg.txt
Please enter your site passphrase:
Wrote configuration file: /etc/tripwire/tw.cfg
暗号署名版が出来たら、元のテキスト版は削除しておきます。(セキュリティ確保のため)
# rm -f /etc/tripwire/twcfg.txt
もしテキスト版の設定ファイルを復活させる場合は、以下のコマンド復活します。
# twadmin --print-cfgfile > /etc/tripwire/twcfg.txt
ポリシーファイルの設定
Tripwireでは、ポリシーファイルをもとに生成したDBと現在のファイルを比較して、改竄検知を行いますが、
初期設定のポリシーファイルでは存在しないファイルのチェックが有効になっていたり、
存在するファイルのチェックが無効になっていたりする為、最適化するためのPerlスクリプトを作成して対処します。
下記のPerlスクリプトは以下のサイトで紹介されているものです。
http://centossrv.com/tripwire.shtml
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 () {
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) ;
ポリシーファイルの最適化
perl /etc/tripwire/twpolmake.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.new
最適化済ポリシーファイルを元にポリシーファイル(暗号署名版)作成
再度、サイトパスフレーズの入力を求められます。
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
ポリシーファイル(テキスト版)削除
# rm -f /etc/tripwire/twpol.txt*
データベース作成
# tripwire -m i -s -c /etc/tripwire/tw.cfg
ローカルパスフレーズの入力を求められます。
データベースの作成には少し時間が掛かります。
これで設定は一通り完了です。
Tripwireの動作確認
以下のコマンドでTripwireを実行してみます。
# tripwire -m c -s -c /etc/tripwire/tw.cfg
Open Source Tripwire(R) 2.4.3.1 Integrity Check Report
Report generated by: root
Report created on: 2016年10月18日 22時56分06秒
Database last updated on: Never
===============================================================================
Report Summary:
===============================================================================
Host name: hogehoge.com
Host IP address: 103.3.188.123
Host ID: None
Policy file used: /etc/tripwire/tw.pol
Configuration file used: /etc/tripwire/tw.cfg
Database file used: /var/lib/tripwire/hogehoge.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
--------- -------------- ----- ------- --------
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/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
Critical configuration files 100 0 0 0
Security Control 100 0 0 0
Login Scripts 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: 40811
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/hogehoge.com.twd"
===============================================================================
Error Report:
===============================================================================
No Errors
-------------------------------------------------------------------------------
*** End of report ***
Open Source 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.
私の環境では、およそ40秒ほどで動作が完了しました。
初回の確認だと、Tripwire Data Files で1ファイルのAddとなります。
Cronによる定期自動実行設定
こちらも先述のサイトにて紹介されているスクリプトです。
# vi tripwire.sh
#!/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
後はスクリプトに実行権限を付与し、任意の時間に起動するように仕込めば作業完了です。
以下の設定は毎日午前4時に起動する設定です。
# chmod 700 tripwire.sh
# echo "0 4 * * * root /root/tripwire.sh" > /etc/cron.d/tripwire