仕事であるWEBサーバにファイル改竄検知システムである「Tripwire」のインストールおよび設定を依頼されました。
私自身、自宅サーバを立てたときに「OSインストール直後に導入すべきシステム」と聞いていたので導入しようと思ってはいたもののそのまま放置した状態となっていました。
これを機会に自宅サーバへ「Tripwire」インストールして、そこで得られた経験を依頼されたWEBサーバ設定へフィードバックさせようと思い、自宅サーバへの「Tripwire」の導入を実施しました。(かなり手遅れ間はありますが・・・)
今回もCentOSで自宅サーバー構築様のエントリーを参照させていただきました。
環境
CentOS5.5 64bit版
前準備
mkpasswdコマンドであらかじめ「ローカルパスプレーズ」と「サイトパスプレーズ」を生成しておくこと
サイト・ローカルのパスフレーズは8文字以上。今回は25文字)
Tripwireインストール
★Tripwireダウンロード [root@localhost ~]# wget http://jaist.dl.sourceforge.net/sourceforge/tripwire/tripwire-2.4.2-src.tar.bz2 ★ダウンロードしたファイルの解凍 [root@localhost ~]# tar jxvf tripwire-2.4.2-src.tar.bz2 ★解凍したディレクトリへ移動 [root@localhost ~]# cd tripwire-2.4.2-src ★インストール開始 [root@localhost ~]# ./configure --prefix=/usr/local/tripwire sysconfdir=/etc/tripwire && make && make install Press ENTER to view the License Agreement. ←空Enter Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. --続ける--(14%) SPACEキー押下でライセンス表示を流す ★accept と入力 Please type "accept" to indicate your acceptance of this license agreement. [do not accept]accept ★y を選択 Continue with installation? [y/n] y ---------------------------------------------- Creating directories... /usr/local/tripwire/sbin: already exists /etc/tripwire: created /usr/local/tripwire/lib/tripwire/report: already exists /usr/local/tripwire/lib/tripwire: already exists /etc/tripwire: already exists /etc/tripwire: already exists /usr/local/tripwire/man: already exists /usr/local/tripwire/doc/tripwire: already exists ---------------------------------------------- Copying files... /usr/local/tripwire/doc/tripwire/COPYING: file already exists /usr/local/tripwire/doc/tripwire/TRADEMARK: file already exists /usr/local/tripwire/doc/tripwire/policyguide.txt: file already exists /etc/tripwire/twpol-Linux.txt: copied ---------------------------------------------- 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.) ★サイトパスプレーズ入力【例:yh3pmczOubphtbrl[5bnziTgr】 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.) ★ローカルパスプレーズ入力【例:.qi9aoexrcGfukdt2Vrlhesrg】 Enter the local keyfile passphrase: ★再度ローカルパスプレーズ入力 Verify the local keyfile passphrase: Generating key (this may take several minutes)...Key generation complete. ---------------------------------------------- Generating Tripwire configuration file... ---------------------------------------------- Creating signed configuration file... ★先に決めたサイトパスプレーズ入力【例:yh3pmczOubphtbrl[5bnziTgr】 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. ---------------------------------------------- Customizing default policy file... ---------------------------------------------- Creating signed policy file... ★先に決めたサイトパスプレーズ入力【例:yh3pmczOubphtbrl[5bnziTgr】 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. ---------------------------------------------- The installation succeeded. Please refer to for release information and to the printed user documentation for further instructions on using Tripwire 2.4 Open Source. make[3]: ディレクトリ `/root/tripwire-2.4.2-src' から出ます make[2]: ディレクトリ `/root/tripwire-2.4.2-src' から出ます make[1]: ディレクトリ `/root/tripwire-2.4.2-src' から出ます ★tripwire展開先ディレクトリから抜ける [root@localhost tripwire-2.4.2-src]# cd ★tripwire-2.4.2-srcディレクトリを削除 [root@localhost ~]# rm -rf tripwire-2.4.2-src ★ダウンロードファイルを削除 [root@localhost ~]# rm -f tripwire-2.4.2-src.tar.bz2 ★tripwireディレクトリへパスを通す [root@localhost ~]# echo PATH=$PATH:/usr/local/tripwire/sbin >> .bashrc ; source .bashrc
Tripwire設定
★Tripwire設定ファイルの作成 [root@localhost ~]# vi /etc/tripwire/twcfg.txt ROOT =/usr/local/tripwire/sbin POLFILE =/etc/tripwire/tw.pol DBFILE =/usr/local/tripwire/lib/tripwire/$(HOSTNAME).twd REPORTFILE =/usr/local/tripwire/lib/tripwire/report/$(HOSTNAME)-$(DATE).twr SITEKEYFILE =/etc/tripwire/site.key LOCALKEYFILE =/etc/tripwire/localhost.localdomain-local.key EDITOR =/bin/vi LATEPROMPTING =false LOOSEDIRECTORYCHECKING =false MAILNOVIOLATIONS =true EMAILREPORTLEVEL =3 REPORTLEVEL =3 MAILMETHOD =SENDMAIL SYSLOGREPORTING =false MAILPROGRAM =/usr/sbin/sendmail -oi -t ■変更箇所 ★ファイル変更時に所属ディレクトリの変更を通知しないようにする LOOSEDIRECTORYCHECKING =true ★リポートレベルを変更する REPORTLEVEL =4 ★Tripwire設定ファイル(暗号署名版)作成 [root@localhost ~]# twadmin -m F -c /etc/tripwire/tw.cfg -S /etc/tripwire/site.key /etc/tripwire/twcfg.txt ★先に決めたサイトパスプレーズ入力【例:yh3pmczOubphtbrl[5bnziTgr】 Please enter your site passphrase: Wrote configuration file: /etc/tripwire/tw.cfg ★Tripwire設定ファイル(テキスト版)削除 [root@localhost ~]# rm -f /etc/tripwire/twcfg.txt 【Tripwire設定ファイル(テキスト版)を復元する場合】 [root@localhost ~]# twadmin -m f -c /etc/tripwire/tw.cfg > /etc/tripwire/twcfg.txt ※復元を確認してみる [root@localhost ~]# ls -l /etc/tripwire/ 合計 52 -rw-r----- 1 root root 931 12月 12 23:39 localhost.localdomain-local.key -rw-r----- 1 root root 931 12月 12 23:39 site.key -rw-r----- 1 root root 4586 12月 13 00:13 tw.cfg -rw-r----- 1 root root 4586 12月 13 00:13 tw.cfg.bak -rw-r----- 1 root root 4159 12月 12 23:43 tw.pol -rw-r--r-- 1 root root 540 12月 13 00:18 twcfg.txt -rw-r----- 1 root root 13748 12月 12 23:41 twpol.txt [root@localhost ~]# ※twcfg.txtが復元されていることを確認できたので再度削除...
ポリシーファイル設定
CentOSで自宅サーバー構築様のポリシースクリプトを利用させていただきます。
★ポリシーファイル最適化スクリプト作成 [root@localhost ~]# 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) ; ★ポリシーファイル最適化 [root@localhost ~]# perl /etc/tripwire/twpolmake.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.new ★最適化済ポリシーファイルを元にポリシーファイル(暗号署名版)作成 [root@localhost ~]# twadmin -m P -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key /etc/tripwire/twpol.txt.new ★先に決めたサイトパスプレーズ入力【例:yh3pmczOubphtbrl[5bnziTgr】 Please enter your site passphrase: Wrote policy file: /etc/tripwire/tw.pol
データベース作成
[root@localhost ~]# tripwire -m i -s -c /etc/tripwire/tw.cfg ★ローカルパスプレーズ入力【例:.qi9aoexrcGfukdt2Vrlhesrg】 Please enter your local passphrase: ### Warning: File system error. ### Filename: /home/lost+found ### (中略) ### Continuing... [root@localhost ~]#
Tripwire確認
Tripwireを実行してみる
[root@localhost ~]# tripwire -m c -s -c /etc/tripwire/tw.cfg Open Source Tripwire(R) 2.4.1 Integrity Check Report Report generated by: root Report created on: 2010年12月13日 00時53分07秒 Database last updated on: Never =============================================================================== Report Summary: =============================================================================== Host name: localhost.localdomain 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/localhost.localdomain.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 1 0 0 * Monitor Filesystems 0 0 0 6 User Binaries and Libraries 0 0 0 0 Tripwire Binaries 0 0 0 0 OS Binaries and Libraries 0 0 0 0 Temporary Directories 0 0 0 0 Global Configuration Files 0 0 0 0 System Boot Changes 0 0 0 0 RPM Checksum Files 0 0 0 0 OS Devices and Misc Directories 0 0 0 0 OS Boot Files and Mount Points 0 0 0 0 * Root Directory and Files 0 0 0 1 Total objects scanned: 323025 Total violations found: 8 ← 8件の変更を検知した =============================================================================== Object Summary: =============================================================================== ------------------------------------------------------------------------------- # Section: Unix File System ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Rule Name: Tripwire Data Files (/usr/local/tripwire/lib/tripwire) Severity Level: 0 ------------------------------------------------------------------------------- ★ファイルの追加を検知 Added: "/usr/local/tripwire/lib/tripwire/localhost.localdomain.twd" ------------------------------------------------------------------------------- Rule Name: Monitor Filesystems (/var) Severity Level: 0 ------------------------------------------------------------------------------- Modified: "/var/lib/mysql/abeko/wp_options.MYD" "/var/lib/mysql/abeko/wp_options.MYI" "/var/lib/mysql/abeko/wp_postmeta.MYD" "/var/lib/mysql/abeko/wp_postmeta.MYI" "/var/spool/postfix/public/pickup" "/var/spool/postfix/public/qmgr" ------------------------------------------------------------------------------- Rule Name: Root Directory and Files (/root) Severity Level: 0 ------------------------------------------------------------------------------- Modified: "/root" =============================================================================== Error Report: =============================================================================== ------------------------------------------------------------------------------- Section: Unix File System ------------------------------------------------------------------------------- 1. File system error. (中略) ------------------------------------------------------------------------------- *** 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.
Tripwire定期自動実行設定
#★Tripwire定期自動実行スクリプト作成 [root@localhost ~]# vi tripwire.sh #!/bin/bash PATH=/usr/sbin:/usr/bin:/bin:/usr/local/tripwire/sbin # パスフレーズ設定 # 先に設定したローカルパスプレーズ入力【例:.qi9aoexrcGfukdt2Vrlhesrg】 LOCALPASS=.qi9aoexrcGfukdt2Vrlhesrg # 先に設定したサイトパスプレーズ入力【例:yh3pmczOubphtbrl[5bnziTgr】 SITEPASS=yh3pmczOubphtbrl[5bnziTgr 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 ★Tripwire定期自動実行スクリプトへ実行権限付加 [root@localhost ~]# chmod 700 tripwire.sh ★Tripwire定期自動実行設定追加(毎日3:00にTripwire定期実行スクリプトを実行) [root@localhost ~]# echo "0 3 * * * root /root/tripwire.sh" > /etc/cron.d/tripwire
メールの確認
3:00に設定したTripwire定期実行スクリプトが時間通り実行され03:02にrootに指定したメールアドレスに
送信されていました。
タイトルは「Tripwire(R) Integrity Check Report in localhost.localdomain」
今回送られてきたメールの内容はこんな感じ
Open Source Tripwire(R) 2.4.1 Integrity Check Report Report generated by: root Report created on: Mon Dec 13 03:00:01 2010 Database last updated on: Never =============================================================================== Report Summary: =============================================================================== Host name: localhost.localdomain 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/localhost.localdomain.twd Command line used: tripwire -m c -s -c tw.cfg =============================================================================== Rule Summary: =============================================================================== ------------------------------------------------------------------------------- Section: Unix File System ------------------------------------------------------------------------------- Rule Name Severity Level Added Removed Modified --------- -------------- ----- ------- -------- * Tripwire Data Files 0 1 0 0 * Monitor Filesystems 0 11 0 14 User Binaries and Libraries 0 0 0 0 Tripwire Binaries 0 0 0 0 OS Binaries and Libraries 0 0 0 0 * Temporary Directories 0 3 0 0 * Global Configuration Files 0 1 0 3 System Boot Changes 0 0 0 0 RPM Checksum Files 0 0 0 0 OS Devices and Misc Directories 0 0 0 0 OS Boot Files and Mount Points 0 0 0 0 * Root Directory and Files 0 1 0 1 Total objects scanned: 323041 Total violations found: 35 =============================================================================== Object Summary: =============================================================================== ------------------------------------------------------------------------------- # Section: Unix File System ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Rule Name: Tripwire Data Files (/usr/local/tripwire/lib/tripwire) Severity Level: 0 ------------------------------------------------------------------------------- Added: "/usr/local/tripwire/lib/tripwire/localhost.localdomain.twd" ------------------------------------------------------------------------------- Rule Name: Global Configuration Files (/etc) Severity Level: 0 ------------------------------------------------------------------------------- Added: "/etc/cron.d/tripwire" Modified: "/etc/cron.d" "/etc/postfix/prng_exch" "/etc/tripwire" ------------------------------------------------------------------------------- Rule Name: Monitor Filesystems (/var) Severity Level: 0 ------------------------------------------------------------------------------- Added: "/var/spool/postfix/maildrop/EBEE8DB8206" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-16.gif" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-16-180x300.gif" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-11.ktai.jpg" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-16.ktai.gif" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-12.ktai.jpg" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-14.ktai.jpg" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-16.ktai.png" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-01.ktai.jpg" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-15.ktai.jpg" "/var/www/html/wordpress/wp-content/uploads/2010/12/20101210-13.ktai.jpg" Modified: "/var/lib/mysql/abeko/wp_options.MYD" "/var/lib/mysql/abeko/wp_options.MYI" "/var/lib/mysql/abeko/wp_postmeta.MYD" "/var/lib/mysql/abeko/wp_postmeta.MYI" "/var/lib/mysql/ks/mt_log.MYD" "/var/lib/mysql/ks/mt_log.MYI" "/var/lib/mysql/ks/mt_session.MYD" "/var/lib/mysql/ks/mt_session.MYI" "/var/lib/ntp" "/var/lib/ntp/drift" "/var/spool/postfix/maildrop" "/var/spool/postfix/public/pickup" "/var/spool/postfix/public/qmgr" "/var/www/html/wordpress/wp-content/uploads/2010/12" ------------------------------------------------------------------------------- Rule Name: Root Directory and Files (/root) Severity Level: 0 ------------------------------------------------------------------------------- Added: "/root/tripwire.sh" Modified: "/root" ------------------------------------------------------------------------------- Rule Name: Temporary Directories (/tmp) Severity Level: 0 ------------------------------------------------------------------------------- Added: "/tmp/mod_ktai_image/f/6/6/f6661bdd4712f58d8d1c186333a29998/180x0_sz102400_q0_copy0_op0_bg.img" "/tmp/mod_ktai_image/7/6/0/760491e5f0daee33214fc0f25c306427/180x0_sz102400_q0_copy0_op0_bg.img" "/tmp/mod_ktai_image/d/c/1/dc1c2bf52d7a971a6b53a3bb2cd20a21/180x0_sz102400_q0_copy0_op0_bg.img" =============================================================================== Error Report: =============================================================================== ------------------------------------------------------------------------------- Section: Unix File System ------------------------------------------------------------------------------- 1. File system error. Filename: /usr/local/doc No such file or directory 2. File system error. Filename: /usr/local/man No such file or directory 3. File system error. Filename: /usr/local/sysinfo No such file or directory 4. File system error. Filename: /usr/X11R6/lib No such file or directory 5. File system error. Filename: /etc/mail/statistics No such file or directory 6. File system error. Filename: /var/lost+found No such file or directory 7. File system error. Filename: /cdrom No such file or directory 8. File system error. Filename: /floppy No such file or directory 9. File system error. Filename: /initrd No such file or directory 10. File system error. Filename: /home/lost+found No such file or directory ------------------------------------------------------------------------------- *** 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.
Total violations found: 35 とあるので35件の変更があったようです。rootでログインして色々いじくったからかしら?
Added: を見てみるとどうやら画像の追加を検知したようですね(ブログへの画像アップを検知した?)
とりあえずスクリプトが実行され、結果がメールで送信されてきているのでインストール及び各種設定はうまく行っているようです。Tripwire設定ファイルの設定値の意味を調べつつ内容を調整しながら様子を見たいと思います。