在本页

  • 先决条件
  • 入门
  • 安装 Tripwire
  • 配置 Tripwire
  • 验证 Tripwire IDS
  • 自动化 Tripwire 报告
  • 结论

Tripwire 是一个免费的开源 Linux 入侵检测系统。它用于检测和报告 Linux 上文件和目录的任何未经授权的更改。当文件/目录发生变化时,它还会通过电子邮件向您发送警报。Tripwire 的工作原理是将当前文件系统状态与已知的基线状态进行比较,如果检测到任何变化就会报告。

在本教程中,我们将向您展示如何在 Debian 10 上安装和配置 Tripwire。

先决条件

  • 一台运行 Debian 10 的服务器。
  • 服务器上已配置 root 密码。

入门

首先,通过运行以下命令将系统软件包更新到最新版本:

apt-get update -y

所有软件包更新完成后,您可以继续下一步。

安装 Tripwire

默认情况下,Tripwire 软件包在 Debian 10 默认存储库中可用。您可以使用以下命令安装它:

apt-get install tripwire -y

在安装过程中,系统会要求您选择电子邮件配置,如下所示:

如何在 Debian 10 上安装配置 Tripwire 入侵检测系统的教程

选择您想要的选项并按 ENTER 键。系统会要求您设置系统邮件名称,如下所示:

如何在 Debian 10 上安装配置 Tripwire 入侵检测系统的教程

提供您的系统邮件名称并按 ENTER 键。系统会要求您创建站点密钥口令,如下所示:

如何在 Debian 10 上安装配置 Tripwire 入侵检测系统的教程

选择 Yes 并按 ENTER 键。系统会要求您重建 Tripwire 配置文件,如下所示:

如何在 Debian 10 上安装配置 Tripwire 入侵检测系统的教程

选择 Yes 并按 ENTER 键。系统会要求您重建 Tripwire policy 文件,如下所示:

如何在 Debian 10 上安装配置 Tripwire 入侵检测系统的教程

选择 Yes 并按 ENTER 键。系统会要求您提供站点密钥口令,如下所示:

如何在 Debian 10 上安装配置 Tripwire 入侵检测系统的教程

提供您的密码并按 ENTER 键。系统会要求您设置本地密钥口令,如下所示:

如何在 Debian 10 上安装配置 Tripwire 入侵检测系统的教程

提供您的密码并按 ENTER 键。Tripwire 安装完成后,您应该会看到以下屏幕:

如何在 Debian 10 上安装配置 Tripwire 入侵检测系统的教程

单击 Ok 按钮完成安装。

配置 Tripwire

接下来,您需要生成 Tripwire 密钥并初始化数据库。首先,使用以下命令将目录切换到 Tripwire 并列出所有密钥和文件:

cd /etc/tripwire/
ls

您应该看到以下输出:

debian10-local.key  site.key  tw.cfg  twcfg.txt  tw.pol  twpol.txt

接下来,编辑 Tripwire 配置文件并将 REPORTLEVEL 设置为 4

nano /etc/tripwire/twcfg.txt

更改以下行:

REPORTLEVEL   =4

完成后保存并关闭文件。

接下来,使用以下命令生成新的配置文件:

twadmin -m F -c tw.cfg -S site.key twcfg.txt

系统会要求您提供站点口令,如下所示:

Please enter your site passphrase: 
Wrote configuration file: /etc/tripwire/tw.cfg

接下来,创建一个 twpolmake.pl 文件来优化 Tripwire policy。

nano twpolmake.pl

添加以下行:

#!/usr/bin/perl
$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 twpolmake.pl twpol.txt > twpol.txt.new 
twadmin -m P -c tw.cfg -p tw.pol -S site.key twpol.txt.new

您应该看到以下输出:

Please enter your site passphrase: 
Wrote policy file: /etc/tripwire/tw.pol

接下来,使用以下命令创建 Tripwire 数据库:

tripwire -m i -s -c tw.cfg

您应该看到以下输出:

Please enter your local passphrase: 
### Warning: File system error.
### Filename: /var/lib/tripwire/debian10.twd
### No such file or directory
### Continuing...

您还可以使用以下命令显示生成的数据库:

twprint -m d -d /var/lib/tripwire/debian10.twd

您应该看到以下输出:

Open Source Tripwire(R) 2.4.3.7 Database

Database generated by:        root
Database generated on:        Sun 09 May 2021 08:39:18 AM UTC
Database last updated on:     Never

===============================================================================
Database Summary: 
===============================================================================

Host name:                    debian10
Host IP address:              45.58.38.142
Host ID:                      None
Policy file used:             /etc/tripwire/tw.pol
Configuration file used:      /etc/tripwire/tw.cfg
Database file used:           /var/lib/tripwire/debian10.twd
Command line used:            tripwire -m i -s -c tw.cfg 

===============================================================================
Object Summary: 
===============================================================================

-------------------------------------------------------------------------------
# Section: Unix File System
-------------------------------------------------------------------------------

如果要更新 Tripwire 数据库,请运行以下命令:

tripwire --update --accept-all

您应该得到以下输出:

### Error: File could not be opened.
### Filename: /var/lib/tripwire/report/debian10-20210509-084141.twr
### No such file or directory
### Exiting...

现在,使用以下命令测试 Tripwire:

tripwire -m c -s -c /etc/tripwire/tw.cfg

您应该看到以下输出:

Open Source Tripwire(R) 2.4.3.7 Integrity Check Report

Report generated by:          root
Report created on:            Sun 09 May 2021 08:42:15 AM UTC
Database last updated on:     Never

===============================================================================
Report Summary:
===============================================================================

Host name:                    debian10
Host IP address:              45.58.38.142
Host ID:                      None
Policy file used:             /etc/tripwire/tw.pol
Configuration file used:      /etc/tripwire/tw.cfg
Database file used:           /var/lib/tripwire/debian10.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 
  ---------                       --------------    -----    -------  -------- 
  Other binaries                  66                0        0        0        
  Tripwire Binaries               100               0        0        0        
  Other libraries                 66                0        0        0        
  Root file-system executables    100               0        0        0        
* Tripwire Data Files             100               1        0        0        
  System boot changes             100               0        0        0        
  Root file-system libraries      100               0        0        0        
  (/lib)
  Critical system boot files      100               0        0        0        
* Other configuration files       66                0        0        1        
  (/etc)
  Boot Scripts                    100               0        0        0        
  Security Control                66                0        0        0        
  Root config files               100               0        0        0        
  Devices & Kernel information    100               0        0        0        
  (/dev)
  Invariant Directories           66                0        0        0        

Total objects scanned:  27975
Total violations found:  2

===============================================================================
Object Summary: 
===============================================================================

-------------------------------------------------------------------------------
# Section: Unix File System
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
Rule Name: Tripwire Data Files (/var/lib/tripwire/debian10.twd)
Severity Level: 100
-------------------------------------------------------------------------------

默认情况下,Tripwire 报告文件位于 /var/lib/tripwire/report/:

ls /var/lib/tripwire/report/

输出:

debian10-20210509-084215.twr

您可以使用以下命令查看此报告:

twprint -m r -t 4 -r /var/lib/tripwire/report/debian10-20210509-084215.twr

验证 Tripwire IDS

此时,Tripwire 已安装并配置完成。现在,是时候检查 Tripwire 是否正常工作了。

首先,使用以下命令在系统中创建一些文件:

touch fil1 file2 file3 file4 file5

现在,运行 Tripwire 以检查 Tripwire 是否检测到这些文件:

tripwire --check --interactive

您应该在以下输出中看到新创建的文件:

Open Source Tripwire(R) 2.4.3.7 Integrity Check Report

Report generated by:          root
Report created on:            Sun 09 May 2021 08:46:36 AM UTC
Database last updated on:     Never

===============================================================================
Report Summary:
===============================================================================

Host name:                    debian10
Host IP address:              45.58.38.142
Host ID:                      None
Policy file used:             /etc/tripwire/tw.pol
Configuration file used:      /etc/tripwire/tw.cfg
Database file used:           /var/lib/tripwire/debian10.twd
Command line used:            tripwire --check --interactive

===============================================================================
-------------------------------------------------------------------------------
Rule Name: Other configuration files (/etc)
Severity Level: 66
-------------------------------------------------------------------------------

Remove the "x" from the adjacent box to prevent updating the database
with the new values for this object.

Modified:
[x] "/etc/tripwire"

-------------------------------------------------------------------------------
Rule Name: Root config files (/root)
Severity Level: 100
-------------------------------------------------------------------------------

Remove the "x" from the adjacent box to prevent updating the database
with the new values for this object.

Added:
[x] "/root/file4"
[x] "/root/file3"
[x] "/root/fil1"
[x] "/root/file2"
[x] "/root/file5"

Modified:
[x] "/root"

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

您稍后也可以使用以下命令查看生成的报告:

twprint --print-report --twrfile /var/lib/tripwire/report/debian10-20210509-084636.twr

自动化 Tripwire 报告

您还可以设置 cron 作业,在特定时间运行 Tripwire。您可以使用以下命令来完成:

crontab -e

添加以下行:

00 06 * * * /usr/sbin/tripwire --check

完成后保存并关闭文件。

上述文件将在每天早上 06:00 运行一次 Tripwire。您可以在 /var/lib/tripwire/report/ 查看生成的报告。

结论

恭喜!您已在 Debian 10 上成功安装并配置了 Tripwire IDS。我希望这能帮助您检查系统中哪些文件或目录被修改。