首页 > 技术文章 > 在CentOS 7上使用Tripwire监控和检测修改的文件

jackyzm 2018-09-11 18:48 原文

在CentOS 7上使用Tripwire监控和检测修改的文件

Tripwire是一个免费的开源入侵检测系统(IDS)。 它是用于监视和警告系统上文件更改的安全工具。 Tripwire是一个功能强大的IDS,可以保护您的系统免受不必要的更改。 您可以使用它来监控您的系统文件,包括网站文件,因此当有不需要的文件更改时,Tripwire会检查您的系统,如果设置正确,可以通过电子邮件提醒您。

在本教程中,我们将向您展示如何使用CentOS 7系统上的Tripwire监控和检测系统文件中的任何更改。 我们将向您展示如何在CentOS 7上安装和配置Tripwire,如何生成Tripwire密钥文件,配置和添加tripwire策略,检查系统以及为tripwire和cron设置启用电子邮件通知。

我们将要做什么

  1. 在CentOS 7上安装Tripwire
  2. 为CentOS 7配置Tripwire策略
  3. 验证Tripwire配置
  4. 向Tripwire政策添加新规则
  5. 设置Tripwire电子邮件通知和Cron

先决条件

  • CentOS 7系统
  • 根特权

第1步 - 在CentOS 7上安装Tripwire

我们必须做的第一步是将Tripwire安装到系统上。 默认情况下,tripwire在CentOS 7存储库中可用。

登录到您的服务器并更新所有软件包。

ssh root@hakase-labs.co
sudo yum update -y

现在使用yum安装Tripwire。

yum -y install tripwire

安装完成后,我们需要生成新的密钥文件。

Tripwire使用2个关键文件。

  1. site-key:它用于保护Tripwire配置。 因此,除非我们再次生成配置,否则对tripwire配置所做的任何更改都不会生效,我们会提示您输入“site-key”密码。
  2. local-key:它用于验证tripwire二进制文件。 当我们想要更新tripwire系统数据库时,我们需要运行tripwire命令,并且会提示我们输入'local-key'的密码。

我们使用下面的命令生成新的tripwire密钥文件(站点和本地密钥)。

sudo tripwire-setup-keyfiles

该命令将生成两个密钥文件“site-key”和“local-key”,并且您将被要求输入每个密码。

输入您自己的“ 网站密钥 ”密码,然后按Enter键。

设置sitekey密码

输入您自己的“ 本地密钥 ”密码并再次按Enter键。

设置本地密钥

接下来,使用'site-key'签署tripwire配置。

输入您的“ 网站密钥 ”密码。

配置站点密钥

现在,为了签署Tripwire政策,请输入您的“ 本地密钥 ”密码。

签署tripwire政策

Tripwire已安装在CentOS 7上,新的tripwire配置和密钥位于'/ etc / tripwire'目录中。

第2步 - 为CentOS 7配置Tripwire策略

在我们第一步讨论的tripwire安装之后,我们需要初始化tripwire数据库并确保没有错误。

使用下面的tripwire命令初始化tripwire数据库。

sudo tripwire --init

您将被问到“local-key”密码短语,您可能会收到错误消息“no such directory”,如下所示。

在CentOS上配置tripwire策略

我们得到错误是因为系统没有在tripwire配置中已经定义的目录和文件。 为了解决这个错误,我们需要编辑tripwire配置'twpol.txt'并重新签署tripwire配置。

现在使用下面的命令从tripwire生成日志错误。

sudo sh -c "tripwire --check | grep Filename > no-directory.txt"

所有不存在于CentOS 7系统上的目录和文件都列在文件'mo-directory.txt'中

cat no-directory.txt

cat no-directory.txt

使用以下bash脚本编辑tripwire配置'twpol.txt' - 在终端上运行此脚本。

for f in $(grep "Filename:" no-directory.txt | cut -f2 -d:); do
sed -i "s|\($f\) |#\\1|g" /etc/tripwire/twpol.txt
done

毕竟,我们需要使用twadmin命令重新生成并重新签署tripwire配置,如下所示。

sudo twadmin -m P /etc/tripwire/twpol.txt

输入您的“网站密钥”密码。

重新初始化tripwire数据库,并确保没有错误。

sudo tripwire --init

重新初始化tripwire数据库,没有任何错误。

twadmin命令

第3步 - 验证Tripwire配置和检查系统

要验证tripwire配置,我们可以运行系统检查命令如下。

sudo tripwire --check

你应该得到类似于以下的结果。

验证Tripwire配置和检查系统

所以这意味着在我们的系统上没有发现错误和系统违规。

现在我们将尝试在根目录下添加一个新文件,并使用tripwire再次检查。

转到根目录并创建一个新文件'hakase-labs.txt'。

cd ~/
touch hakase-labs.txt

现在使用tripwire命令再次检查系统。

sudo tripwire --check

您将在系统中得到严重程度为100的新违规的结果,如下所示。

tripwire检查

在这个阶段,Tripwire为CentOS 7系统安装和配置。

第4步 - 将新规则添加到Tripwire策略

在这一步中,我们将向您展示如何将新规则添加到tripwire策略配置“twpol.txt”。

要执行这项工作,我们需要定义规则名称,严重程度,监视目录和文件类型。 在这一步中,我们将在'/ var / www /'目录下为我们的WordPress安装创建一个名为'Wordpress Data'的新规则,严重程度为'HIGH / SIG_HI',并且该目录中的所有文件都是关键的以及源代码不能更改)。

转到tripwire配置目录'/ etc / tripwire'并使用vim编辑配置文件'twpol.txt'。

cd /etc/tripwire/
vim twpol.txt

转到该行的末尾,并在那里粘贴以下WordPress规则。

# Ruleset for Wordpress
(
  rulename = "Wordpress Data",
  severity= $(SIG_HI)
)
{
        /var/www        -> $(SEC_CRIT);
}

保存并退出。

使用twadmin命令重新生成并重新签名配置,如下所示。

sudo twadmin -m P /etc/tripwire/twpol.txt

输入您的“网站密钥”密码。

现在我们需要再次重新生成tripwire数据库。

sudo tripwire --init

输入“本地密钥”密码。

新的规则集已添加并应用于Tripwire策略配置。

重新签署配置

使用下面的tripwire命令检查您的系统。

sudo tripwire --check

你应该得到结果说没有错误和违反。

使用tripwire检查违规行为

现在进入'/ var / www /'目录并在其中创建一个新文件。

cd /var/www/
touch hakase-labs.php

创建一个测试文件

再次使用tripwire进行系统检查。

sudo tripwire --check

并且您将在安全级别为高100的'/ var / www /'目录中得到说明系统违规的结果。

测试结果

新规则已添加并应用于Tripwire策略配置。

第5步 - 安装Tripwire电子邮件通知和Cron

在这一步中,我们将为特定tripwire规则集策略配置通知,并配置用于自动系统检查的cronjob。 我们会将任何违反'WordPress数据'规则的报告发送到电子邮件地址' myemail@gmail.com '。

对于电子邮件通知,tripwire在配置中提供了一个'emailto'功能。 默认情况下,tripwire使用Postfix或Sendmail通过电子邮件发送报告。

在配置电子邮件通知之前,请使用以下命令测试tripwire通知功能。

sudo tripwire --test --email email@gmail.com

检查你的电子邮件,你应该从你的服务器得到电子邮件报告如下。

Tripwire通过电子邮件报告

现在进入'/ etc / tripwire'目录并编辑'twpol.txt'配置。

cd /etc/tripwire/
vim twpol.txt

在'WordPress数据'规则中添加新行'emailto',如下所示。

# Ruleset for Wordpress
(
  rulename = "Wordpress Data",
  severity= $(SIG_HI),
  emailto = myemail@gmail.com
)
{
        /var/www        -> $(SEC_CRIT);
}

保存并退出。

使用twadmin命令重新生成并签署配置。

sudo twadmin -m P /etc/tripwire/twpol.txt

输入您的“网站密钥”密码。

并重新生成tripwire数据库。

sudo tripwire --init

输入您的tripwire'local-key'密码。

Tripwire电子邮件通知的配置已完成。

生成并签署配置

现在通过在'/ var / www /'目录中再次创建一个新文件来做一些测试。

cd /var/www/
touch hakase.txt

使用下面的命令再次检查您的系统。

sudo tripwire --check --email-report

注意:

  • --email-report:将系统报告发送到每个规则中定义的电子邮件地址。

检查你的电子邮件,你应该在电子邮件中得到如下结果。

电邮报告

Tripwire的电子邮件通知已启用并应用。

接下来,我们将使用cron setup启用自动Tripwire系统检查。 为此,请使用下面的crontab命令在root用户下创建一个新的cron脚本。

sudo crontab -e -u root

粘贴以下cron配置。

0 0 * * * tripwire --check --email-report

保存并退出。

注意:

  • - cron脚本将每天进行tripwire系统检查。

现在重新启动CentOS 7上的crond服务。

systemctl restart crond

设置tripwire cronjob

现在,您将每天收到tripwire报告通知到您的电子邮件。

Tripwire已经安装并配置用于CentOS 7系统。

 

安装和配置Tripwire,加强你的Linux系统安全

2014年09月05日 ⁄ 综合 ⁄ 共 12424字 ⁄ 字号 小 中 大 ⁄ 评论关闭

1、为什么要安装 tripwire
在安装完 Linux,做好设定后,建议你马上安装 tripwire 这套软件,它能把文件的特征,如对象大小、拥有者、群组、存取权限等建立成指纹数据库(fingerprints),并定期执行检查。当发现文件现况与指纹数据库不符合时,tripwire 会提出警告,告知你哪些项目与指纹数据库不符。

 

2、安装程序

说明:本文档使用的linux发行版本为RedHat Linux 7.3。其他的发行版本设置类似。

 

1.       安装套件:rpm -Uvh tripwire-版本号码.rpm

 

2.       切换工作目录到 /etc/tripwire,其中有两个配置文件:

 

§         twcfg.txt:可用来设定 tripwire 的工作环境,可依照你的习惯来调整,

 

§         twpol.txt:指定 tripwire 对哪些文件的哪些项目进行监控。

 

3.       预设的 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 =false

 

MAILNOVIOLATIONS       =true

 

EMAILREPORTLEVEL       =3

 

REPORTLEVEL            =3

 

MAILMETHOD             =SENDMAIL

 

SYSLOGREPORTING        =false

 

MAILPROGRAM            =/usr/sbin/sendmail -oi -t

 

§         DBFILE 为指纹数据库之文件名。

 

§         REPORTFILE 为检测报告档之档名。

 

4.       再来看看 twpol.txt,我们可以设定它来指定 tripwire 对哪些文件的 哪些项目进行监控。tripwire 可监控的项目可在 twpolicy 的 man page 中, 『property masks』一节内找到,如下所示: 

 

-     Ignore the following properties

 

    +     Record and check the following properties

 

a     Access timestamp

 

b     Number of blocks allocated

 

 c     Inode timestamp (create/modify)

 

 d     ID of device on which inode resides

 

       g     File owner's group ID

 

 i     Inode number

 

 l     File is increasing in size (a "growing file")

 

m     Modification timestamp

 

        n     Number of links (inode reference count)

 

           p     Permissions and file mode bits

 

            r     ID of device pointed to by inode

 

                  (valid only for device objects)

 

            s     File size

 

            t     File type

 

            u     File owner's user ID

 

            C     CRC-32 hash value

 

            H     Haval hash value

 

            M     MD5 hash value

 

S     SHA hash value

 

其中『+』与『-』容后说明。

 

5.       如何要求 tripwire 监控某些文件呢?Red Hat 所附的 twpol.txt 已把重要的配置文件与程序行入监控的范围,你可以找到如附 图内的这一段:(

 

 

  rulename = "Security Control",

 

  severity = $(SIG_HI)

 

)

 

{

 

 /etc/group                           -> $(SEC_CRIT) ;

 

 /etc/security                         -> $(SEC_CRIT) ;

 

}

 

这一段把 /etc/group 以及 /etc/security 这两个对象纳入 『Security Control』这一组,警戒程度为由 SIG_HI 这个变量定义, 值为 100(稍后会介绍)。而 tripwire 会监控 /etc/group 及 /etc/security 的哪些项目呢?则由 SEC_CRIT 这个变量来定义。

 

6.       从 twpol.txt 的前端往后浏览,可以找到如附图的这一段:

 

@@section FS

 

SEC_CRIT      = $(IgnoreNone)-SHa ;  # Critical files that cannot change

 

SEC_SUID      = $(IgnoreNone)-SHa ;  # Binaries with the SUID or SGID flags set

 

SEC_BIN       = $(ReadOnly) ;        # Binaries that should not change

 

SEC_CONFIG    = $(Dynamic) ;         # Config files that are changed infrequently but accessed often

 

SEC_LOG       = $(Growing) ;         # Files that grow, but that should never change ownership

 

SEC_INVARIANT = +tpug ;              # Directories that should never change permission or ownership

 

SIG_LOW       = 33 ;                 # Non-critical files that are of minimal security impact

 

SIG_MED       = 66 ;                 # Non-critical files that are of significant security impact

 

SIG_HI        = 100 ;                # Critical files that are significant points of vulnerability

 

你可以发现 SIG_HI 的值就如上一点所提的,为 100。 而 tripwire 会监控 /etc/group 的哪些项目是由 SEC_CRIT 所定义; 在此处你发现 SEC_CRIT 等于『$(IgnoreNone)-SHa』,究竟是哪些项目?

 

要解开这个谜,必须先找出 IgnoreNone 的变量究竟定义为何。 但找遍 twpol.txt 还是解不开,原来 IgnoreNone 跟下面的 ReadOnly, Dynamic, Growing 等均为 tripwire 预先定义好的变量, 在 twpolicy 的 man page 中『Variables』一节内可以找到如附图的内容:    

 

ReadOnly       ReadOnly is good for files that are widely                      available but are intended to be read-only.                      Value: +pinugtsdbmCM-rlacSH

 

Dynamic        Dynamic is good for monitoring user direc-                      tories and files that tend to be dynamic in                     behavior.  value: +pinugtd-srlbamcCMSH

 

Growing        The Growing variable is intended for files                      that should only get larger.  Value: +pinugtdl-srbamcCMSH

 

 Device         Device is good for devices or other files                      that Tripwire should not attempt to open.                      Value: +pugsdr-intlbamcCMSH

 

IgnoreAll      IgnoreAll tracks a file's presence or                      absence, but doesn't check any other prop-                      erties.  Value: -pinugtsdrlbamcCMSH

 

IgnoreNone     IgnoreNone turns on all properties and pro-vides a convenient starting point for                      defining your own property masks.                      (For example, mymask = $(IgnoreNone) -ar;)                      Value: +pinugtsdrbamcCMSH-l

 

从上面得知『IgnoreNone』的值为『+pinugtsdrbamcCMSH-l』, 其中『+』后所列的是要监控的项目,而『-』后所列的则为不监控的项目。那么『$(IgnoreNone)-SHa』呢?就是把 IgnoreNone 内原本列入监控的 SHa 项目改列为不监控。

 

你可以按照需求来修改这个文件。。

 

7.       接着在 在 /etc/tripwire 内执行 ./twinstall.sh。 执行过程中会要求你设定两个密码(pass phrase):

 

§         site pass phrase :加密 twpol.txt 及 twcfg.txt 时用。

 

§         local pass phrase:加密指纹数据库时用。

 

之后会再要你输入正确的 site pass phrase, 此时会对 twpol.txt 及 twcfg.txt 分别进行加密处理, 由原始文本文件产生 tw.pol 及 tw.cfg。过程如附图所示:[root@localhost tripwire]# ./twinstall.sh

 

 

 

----------------------------------------------

 

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 delete this file manually 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.

 

 

 

You have new mail in /var/spool/mail/root

 

在 twinstall.sh 执行完毕后,建议把 twpol.txt 及 twcfg.txt 这两个文本文件删除或移至别处。

 

8.       执行 tripwire -m i 来建立指纹数据库,它会要求你输入 local pass phase。

 

[root@localhost tripwire]# tripwire -m i

 

Please enter your local passphrase:

 

Parsing policy file: /etc/tripwire/tw.pol

 

Generating the database...

 

*** Processing Unix File System ***

 

### Warning: File system error.

 

### Filename: /proc/scsi

 

### No such file or directory

 

### Continuing...

 

### Warning: File system error.

 

### Filename: /usr/sbin/fixrmtab

 

### No such file or directory

 

### Continuing...

 

Wrote database file: /var/lib/tripwire/localhost.localdomain.twd

 

The database was successfully generated.

 

You have new mail in /var/spool/mail/root

 

9.       或许你怀疑 tripwire 真的能侦测出文件最细微的改变吗? 以下来做个实验,我们把 /etc/group 中第一行第二个字段的『x』改成『X』:

 

[root@localhost tripwire]#head –1 /etc/group

 

root:x:0:root

 

[root@localhost tripwire]#vi /etc/group

 

[root@localhost tripwire]#head –1 /etc/group

 

root:X:0:root

 

10.   接下来执行『tripwire -m c --interactive』进行检查,最后出现报告清单 (内定使用 vi),有:

 

§         『Rule Summary』:列出所有组别的检查结果。

 

===============================================================================

 

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

 

  User binaries                   66                0        0        0

 

  Tripwire Binaries               100               0        0        0

 

  Critical configuration files    100               0        0        0

 

  Libraries                       66                0        0        0

 

  Operating System Utilities      100               0        0        0

 

  Critical system boot files      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

 

  Shell Related Programs          100               0        0        0

 

  Critical Utility Sym-Links      100               0        0        0

 

  Shell Binaries                  100               0        0        0

 

  System boot changes             100               0        0        0

 

  OS executables and libraries    100               0        0        0

 

* Security Control                100               0        0        1

 

  Login Scripts                   100               0        0        0

 

  Root config files               100               0        0        0

 

Total objects scanned:  15675

 

Total violations found:  2

 

有两组有异动,一为 tripwire 的资料文件(新增),另一为 『System Control』这一组(/etc/group 属于这一组!)。

 

『Object Summary』:列出有异动的对象清单。

 

===============================================================================

 

Object Summary:

 

===============================================================================

 

-------------------------------------------------------------------------------

 

# Section: Unix File System

 

-------------------------------------------------------------------------------

 

-------------------------------------------------------------------------------

 

Rule Name: Tripwire Data Files (/var/lib/tripwire)

 

Severity Level: 100

 

-------------------------------------------------------------------------------

 

Remove the "x" from the adjacent box to prevent updating the database

 

with the new values for this object.

 

Added:

 

[x] "/var/lib/tripwire/localhost.localdomain.twd"

 

 

 

-------------------------------------------------------------------------------

 

Rule Name: Security Control (/etc/group)

 

Severity Level: 100

 

-------------------------------------------------------------------------------

 

Remove the "x" from the adjacent box to prevent updating the database

 

with the new values for this object.

 

 

 

Modified:

 

[x] "/etc/group"

 

 

 

===============================================================================

 

Object Detail:

 

===============================================================================

 

-------------------------------------------------------------------------------

 

  Section: Unix File System

 

-------------------------------------------------------------------------------

 

-------------------------------------------------------------------------------

 

Rule Name: Tripwire Data Files (/var/lib/tripwire)

 

/etc/group 被发现有异动了。如果要把指纹数据库内 /etc/group 的资料 更新为目前的状态,请保留 /etc/group 前方 [ ] 内的 x, 否则把它改为空格。

 

『Object Detail』:异动对象的详细信息,如哪些项目有异动等。===============================================================================

 

Object Summary:

 

===============================================================================

 

-------------------------------------------------------------------------------

 

# Section: Unix File System

 

-------------------------------------------------------------------------------

 

-------------------------------------------------------------------------------

 

Rule Name: Tripwire Data Files (/var/lib/tripwire)

 

Severity Level: 100

 

-------------------------------------------------------------------------------

 

Remove the "x" from the adjacent box to prevent updating the database

 

with the new values for this object.

 

 

 

Added:

 

[x] "/var/lib/tripwire/localhost.localdomain.twd"

 

-------------------------------------------------------------------------------

 

Rule Name: Security Control (/etc/group)

 

Severity Level: 100

 

-------------------------------------------------------------------------------

 

Remove the "x" from the adjacent box to prevent updating the database

 

with the new values for this object.

 

 

 

Modified:[x] "/etc/group"

 

虽然仅仅是把 x 改成 X,但有四个项目受到影响。其中最重要的是 MD5 的值。一个文件的 MD5 值只要跟原先的值不一样,就可以断定文件的内容 已被修改过。

 

§         tripwire 每日均会自动执行检查,并寄 e-mail 给 root 告知结果。 你也应定期执行『tripwire -m c --interactive』来更新指纹数据库。

参考

推荐阅读