Tripwireで改ざんを検知しよう!
自分の知らないところで勝手に設定ファイルが編集されていたり、アクセス権が書き換えられていたりするのは、それはそれは大変なことです。何より、自分の知らないところでのことなので、気づくことが出来ません。
ここでは、Tripwireの導入手順を掲載しています。
Tripwireとは…?
設定ファイルなどが勝手に編集されていないかを確認し、報告をしてくれるファイル改ざん検知システムです。
前の設定状態をデータベースとして保存しておき、そのデータベースと今の状態を比較し、ユーザの意図しない変更がないかを探します。
ただし、このTripwireでは検出しか出来ません。仮に改ざんが検知された場合、修復などは自分で行う必要があります。
[root@co ~]# yum --enablerepo=epel -y install tripwire
Loaded plugins: downloadonly, fastestmirror, security
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* epel: ftp.iij.ad.jp
* extras: ftp.iij.ad.jp
* updates: ftp.iij.ad.jp
base | 1.1 kB 00:00
epel | 3.6 kB 000:00
extras | 2.1 kB 000:00
updates | 1.9 kB 000:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package tripwire.x86_64 0:2.4.1.1-1.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
tripwire x86_64 2.4.1.1-1.el5 epel 1.8 M
Transaction Summary
================================================================================
Install 1 Package(s)
Upgrade 0 Package(s)
Total download size: 1.8 M
Downloading Packages:
tripwire-2.4.1.1-1.el5.x86_64.rpm | 1.8 MB 00:07
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : tripwire 1/1
Installed:
tripwire.x86_64 0:2.4.1.1-1.el5
Complete!
サイトパスフレーズとローカルパスフレーズの設定
Tripwireを実行するにあたり、サイトパスフレーズとローカルパスフレーズの2つを設定する必要があります。
この2つは自由に設定することが出来ますが、rootや一般ユーザのパスワードと違うものをそれぞれ指定すると、よりセキュリティを高めることが出来ます。
[root@co ~]# 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を実行するための設定を行います。
[root@co ~]# vi /etc/tripwire/twcfg.txt
LOOSEDIRECTORYCHECKING =false
↓
LOOSEDIRECTORYCHECKING =true // ディレクトリへファイルが追加・削除された際に、ディレクトリのレポートを報告しない
EMAILREPORTLEVEL =3
↓
EMAILREPORTLEVEL =4 // メールでのレポートの詳細レベル
REPORTLEVEL =3
↓
REPORTLEVEL =4 // レポートの詳細レベル
続いて、設定ファイルにサイトパスフレーズを使って暗号署名を行います。
[root@co ~]# 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
暗号署名を行う前のファイルはいらないので削除してしまいましょう。
[root@co ~]# rm -f /etc/tripwire/twcfg.txt
ポリシーファイルの作成
Tripwireを実行する上でのルールを決めておきます。ここで決めたルールに従ってデータベースを作り、そのデータベースと現在の状態を比較して改ざんをチェックします。
歪 HomePage様のスクリプトを使わせて頂きます。
[root@co ~]# vi /etc/tripwire/tripwire_pol.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 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) = /^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@co ~]# perl /etc/tripwire/tripwire_pol.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.new
最後に、ポリシーファイルに暗号署名を行い、署名前のファイルは削除します。
[root@co ~]# 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@co ~]# tripwire -m i -s -c /etc/tripwire/tw.cfg
Please enter your local passphrase:[ローカルパスフレーズ]
Tripwireの実行
実際に動かしてみたいと思います。
この作業も時間がかかるので、せんべいと一緒にあつーいお茶でも飲みながら待ちましょう。
[root@co ~]# tripwire -m c -s -c /etc/tripwire/tw.cfg
チェックが終わると、以下のようなレポートが表示されます。
Tripwire(R) 2.4.1 Integrity Check Report
Report generated by: root
Report created on: 2013年06月12日 21時28分20秒
Database last updated on: Never
===============================================================================
Report Summary:
===============================================================================
Host name: co.kimamabi.biz
Host IP address: 192.168.11.11
Host ID: None
Policy file used: /etc/tripwire/tw.pol
Configuration file used: /etc/tripwire/tw.cfg
Database file used: /var/lib/tripwire/co.kimamabi.biz.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
--------- -------------- ----- ------- --------
Invariant Directories 66 0 0 0
Temporary directories 33 0 0 0
* Tripwire Data Files 100 1 0 0
Critical devices 100 0 0 0
(/proc/kcore)
User binaries 66 0 0 0
Tripwire Binaries 100 0 0 0
Libraries 66 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)
Shell Related Programs 100 0 0 0
Operating System Utilities 100 0 0 0
Critical Utility Sym-Links 100 0 0 0
Shell Binaries 100 0 0 0
Critical system boot files 100 0 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
Total objects scanned: 14671
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/co.kimamabi.biz.twd"
===============================================================================
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.
Addedは追加、Removedは削除、Modifiedは変更や更新をしたファイルやディレクトリを指します。
No Errorsと表示されていれば、とりあえずはOKです。
[root@co ~]# vi /etc/cron.daily/tripwire
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin
# パスフレーズ設定
SITEPASS=[サイトパスフレーズ] # サイトキーファイルパスフレーズ
LOCALPASS=[ローカルパスフレーズ] # ローカルキーファイルパスフレーズ
MAILTO=root # チェック結果をメールで送る際の送信先
cd /etc/tripwire
# Tripwireチェック実行
tripwire -m c -s -c tw.cfg|mail $MAILTO -s "Tripwire(R) Integrity Check Report in `hostname`"
# ポリシーファイル最新化
twadmin -m p -c tw.cfg -p tw.pol -S site.key > twpol.txt
perl tripwire_pol.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@co ~]# chmod +x /etc/cron.daily/tripwire
また、自動実行の妨げになるので、cron.dailyのtripwire-checkを無効化しておきます。
[root@co ~]# chmod -x /etc/cron.daily/tripwire-check
[root@co ~]# ls -l /etc/cron.daily/tripwire-check
-rw-r--r-- 1 root root 315 12月 17 2008 /etc/cron.daily/tripwire-check
これにて、Tripwireの導入は一通り終了です。次はrootkit対策としてchkrootkitを導入したいと思います。
参考にしたサイト様
CentOSで自宅サーバー構築 - ファイル改竄検知システム導入(Tripwire)
歪 HomePage - 自宅サーバの構築 - Redhat8.0 : Tripwire
今回新しく登場したコマンド
- perl
[広告]

トップページ
Rocky Linux 8
CentOS 7
Scientific Linux 6
CentOS 5
○準備
○VMware Player
○Hyper-V(Win Proのみ)
○導入
○セキュリティ対策
○Dynamic DNS
○NTPサーバ
○データベース
○WEBサーバ
○FTPサーバ
○メールサーバ
○DNSサーバ
○ファイルサーバ
○ブログシステム
○その他
ブログ
[広告]