在CentOS 7上使用Tripwire監控和檢測修改的文件

在CentOS 7上使用Tripwire監控和檢測修改的文件

Tripwire是一個免費的開源入侵檢測系統(IDS)。 它是用於監視和警告系統上文件更改的安全工具。 Tripwire是一個功能強大的IDS,能夠保護您的系統免受沒必要要的更改。 您可使用它來監控您的系統文件,包括網站文件,所以當有不須要的文件更改時,Tripwire會檢查您的系統,若是設置正確,能夠經過電子郵件提醒您。php

在本教程中,咱們將向您展現如何使用CentOS 7系統上的Tripwire監控和檢測系統文件中的任何更改。 咱們將向您展現如何在CentOS 7上安裝和配置Tripwire,如何生成Tripwire密鑰文件,配置和添加tripwire策略,檢查系統以及爲tripwire和cron設置啓用電子郵件通知。html

咱們將要作什麼

  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存儲庫中可用。前端

登陸到您的服務器並更新全部軟件包。node

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

如今使用yum安裝Tripwire。git

yum -y install tripwiregithub

安裝完成後,咱們須要生成新的密鑰文件。數據庫

Tripwire使用2個關鍵文件。swift

  1. site-key:它用於保護Tripwire配置。 所以,除非咱們再次生成配置,不然對tripwire配置所作的任何更改都不會生效,咱們會提示您輸入「site-key」密碼。
  2. local-key:它用於驗證tripwire二進制文件。 當咱們想要更新tripwire系統數據庫時,咱們須要運行tripwire命令,而且會提示咱們輸入'local-key'的密碼。

咱們使用下面的命令生成新的tripwire密鑰文件(站點和本地密鑰)。vim

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字 ⁄ 字號 小 中 大 ⁄ 評論關閉

一、爲何要安裝 tripwire
在安裝完 Linux,作好設定後,建議你立刻安裝 tripwire 這套軟件,它能把文件的特徵,如對象大小、擁有者、羣組、存取權限等創建成指紋數據庫(fingerprints),並按期執行檢查。當發現文件現況與指紋數據庫不符合時,tripwire 會提出警告,告知你哪些項目與指紋數據庫不符。

 

二、安裝程序

說明:本文檔使用的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』來更新指紋數據庫。

參考

相關文章
相關標籤/搜索