CentOS配置Tripwire笔记——数据完整性监测

之前两天,在虚拟机上装上了Tripwire,现在该对它进行一翻配置了,不然怎么用呀。本来就是要用来监测硬盘数据的。

 

一、配置Tripwire

修改文本格式配置文件:

vi /etc/tripwire/twcfg.txt

找到这一个行

LOOSEDIRECTORYCHECKING =false

将false的值变为true(不监测所属目录的数据完整性)

LOOSEDIRECTORYCHECKING =true

找到这一行

REPORTLEVEL =3

将3变为4(改变监测结果报告的等级)

REPORTLEVEL =4

从文本配置文件建立加密格式配置文件,中途需要输入“site keyfile”口令

twadmin --create-cfgfile -S /etc/tripwire/site.key /etc/tripwire/twcfg.txt

为不留安全隐患,删除文本格式的配置文件

rm -f /etc/tripwire/twcfg.txt

恢复文本格式的Tripwire配置文件,可通过执行以下命令:

twadmin --print-cfgfile > /etc/tripwire/twcfg.txt

 

二、Policy文件的配置

Tripwire的数据库是基于Policy文件建立的。但默认的Policy文件并没有有效的依照我们的需要建立数据完整性监测规则,所以这里通过一段Perl脚本来让数据监测实际满足于我们的需要。

建立用于建立Policy文件的Perl脚本

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) = /^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 (! -s $tpath) {
$_ = "$sharp#$tpath$cond" if ($ret == 0) ;
}
else {
$_ = "$sharp$tpath$cond" ;
}
}
print "$_n" ;
}
close(POL) ;

建立Policy文件并删除默认Policy文件

perl /etc/tripwire/twpolmake.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.out
rm -f /etc/tripwire/twpol.txt

将新建立的Policy文件的名改为默认Policy文件的文件名,并编辑该文件

mv /etc/tripwire/twpol.txt.out /etc/tripwire/twpol.txt
vi /etc/tripwire/twpol.txt

找到以下这一行

$(TWREPORT)                          -> $(Dynamic) (recurse=0) ;

在这行的下面添加一下语句(不对数据库进行监测)

!$(TWDB)/$(HOSTNAME).twd ;

从文本配置文件建立加密格式配置文件,中途需要输入“site keyfile”口令

twadmin --create-polfile -S /etc/tripwire/site.key /etc/tripwire/twpol.txt

为不留安全隐患,删除文本格式的配置文件

rm -f /etc/tripwire/twcfg.txt

 

三、建立数据库

执行一下语句,建立数据库

tripwire --init

 

四、建立运行脚本

执行一下语句,建立Tripwire运行脚本

#!/bin/bash

PATH=/usr/local/sbin:/usr/bin:/bin
SITEPASS=******** # Site Key Passphrase ← 将星号部分换为Site Keyfile的口令
LOCALPASS=******** # Local Key Passphrase ← 将星号部分换为Local Keyfile的口令
REPORTFILE=/usr/local/lib/tripwire/report/`hostname`-`date +%Y%m%d`.twr

# Run the Tripwire
tripwire --check -r "$REPORTFILE"| logger -t tripwire

# Mail the Tripwire Report to root
cd /etc/tripwire
REPORTPRINT=`mktemp`
twprint -m r -c tw.cfg -r "$REPORTFILE" -L `hostname`-local.key -t 4 > $REPORTPRINT
if [ -z "$(grep 'Total violations found:  0' $REPORTPRINT)" ]; then
cat $REPORTPRINT | mail -s "Tripwire(R) Integrity Check Report in `hostname`" root
fi
rm -f $REPORTPRINT

# Update the Policy File
cd /etc/tripwire
twadmin --print-polfile > twpol.txt
perl twpolmake.pl twpol.txt > twpol.txt.out
twadmin --create-polfile -S site.key -Q $SITEPASS twpol.txt.out | logger -t tripwire
rm -f twpol.*
# update the Database
rm -f /usr/local/lib/tripwire/`hostname`.twd
tripwire --init -P $LOCALPASS | logger -t tripwire

注意脚本内需要修改的内容。

赋予运行脚本文件可执行的权限

chmod 700 tripwire-check

运行一次脚本

./tripwire-check

由于增加了运行脚本本身,也被认作系统被作了改动,会发邮件通知root…查看邮箱回收到监测报告,再次运行一次脚本,由于两次连续运行,之间不太可能有文件变更,所以请确认不会发送E-mail给root

 

五、在服务器本地监测报告的浏览

监测报告所在目录的文件列表

ls -l /usr/local/lib/tripwire/report/

有如下文件:

-rw-r--r-- 1 root root 2630 Aug 22 22:17 localhost.localdomain-20110822.twr   <-比如想浏览此篇报告
-rw-r--r-- 1 root root 2766 Aug 22 22:17 localhost.localdomain-20110822.twr.bak

进入Tripwire配置文件所在目录

cd /etc/tripwire

将监测报告保存到名为tripwire-report的文件中

twprint -m r -c tw.cfg -r "/usr/local/lib/tripwire/report/sample.centospub.com-20060823.twr" -L sample.centospub.com-local.key -t 4 > tripwire-report

浏览监测报告

cat tripwire-report

删除监测报告

rm -f tripwire-report

 

六、让监测脚本每天自动运行

进入Tripwire运行脚本所在的root目录

cd

转移脚本到每天自动运行的目录中

mv tripwire-check /etc/cron.daily/

放在/etc/cron.daily下的脚本,会在每天4点02分自动被运行。这样通过数据完整性监测来监视系统文件的状况。如果增加、修改或删除的情况,将会给root发送邮件,并自动转送到初始环境设置中设置的转送邮箱中。

 

本文全篇操作参考CentOS攻略站,修正了一个查找位置的错误与一个脚本匹配错误,可能是因为tripwire版本不一致所导致的。

{ 发表评论? }

  1. an9

    技术大白路过. :twisted:

  2. 松鼠男

    博客备案,需要关闭一段时间,呵呵,大概一个月后再见哈。。。~~~

    • 松鼠男

      博客备案完成啦

        • 松鼠男

          @jiechic 没花钱,之前备案黑莓博客的时候在那边买了虚拟主机,然后又代备案的。呵呵。。现在这个还在美国,我在准备搬回来。。。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>