入侵檢測系統 IDS Intrusion Detection Systems
入侵防護系統 IPS Intrusion Prevention System
入侵前
(入侵后檢測,事后入侵檢測) tripwire
(入侵中檢測,實時入侵檢測) snort
因特網
|
|
路由
|
|
防火牆
|
|
入侵檢測系統
|
|
內部服務器
tripwire
你下載一個軟件,會有md5值或數據簽名等方法來讓你驗證這個軟件是否被人惡意修改過.這也是一種完整性檢測。
你的操作系統里有很多重要的文件(如/etc/passwd,/etc/shadow,各種命令等),也不能被人惡意修改,如果你懷疑有人入侵了你的操作系統,如何檢測出哪些文件被修改過內容或屬性等?
tripwire軟件就可以幫你解決。
完整性檢測
md5或者rpm等都可以對單個文件做完整性檢測
rpm -Vf
缺點1:只針對安裝的rpm包里的文件做完整性檢測,無法對自建的文件做檢測
缺點2:只能檢測單個文件,你需要寫腳本循環去檢測多個文件
# rpm -Vf /etc/php.ini --這個文件是rpm版的php的配置文件,第一次使用這個命令,沒有任何顯示,表示此文件沒有做過任何的改變
# rpm -Vf /etc/php.ini --對這個文件做微小的改變后,再執行此命令就會有信息了
S.5....T. c /etc/php.ini
S file Size differs
M Mode differs (includes permissions and file type)
5 digest (formerly MD5 sum) differs
D Device major/minor number mismatch
L readLink(2) path mismatch
U User ownership differs
G Group ownership differs
T mTime differs
P caPabilities differ
1,安裝
# yum install tripwire --需要epel源
# ls /etc/tripwire/
twcfg.txt --定義tripwire各個相關文件的路徑
twpol.txt --定義檢測的對象文件,及違規時采取的行為
# cat /etc/tripwire/twcfg.txt --查看此文件,有以下相關的文件定義
/var/lib/tripwire/$(hostname).twd --用於存放生成的快照
/var/lib/tripwire/report/ --存放檢測的報告
tripwire為了自身的安全,設定了兩個密碼文件
/etc/tripwire/site.key --用於保護策略文件和配置文件
/etc/tripwire/$(HOSTNAME)-local.key --用於保護數據庫和分析報告
# ls /etc/tripwire/ --做完后,就會多產生幾個配置文件了
li.cluter.com-local.key site.key tw.cfg twcfg.txt tw.pol twpol.txt
2,建立兩個密碼文件
# tripwire-setup-keyfiles
Enter the site keyfile passphrase:
Verify the site keyfile passphrase: --輸入你設定的site密碼兩次
Generating key (this may take several minutes)...Key generation complete.
Enter the local keyfile passphrase:
Verify the local keyfile passphrase: --輸入你設定的local密碼兩次
Generating key (this may take several minutes)...Key generation complete.
Signing configuration file... --簽名配置文件
Please enter your site passphrase: --輸入剛設定好的site密碼
Wrote configuration file: /etc/tripwire/tw.cfg
Signing policy file... --簽名策略文件
Please enter your site passphrase: --輸入剛設定好的site密碼
Wrote policy file: /etc/tripwire/tw.pol
3,修改默認配置文件
rpm版
# man twpolicy --可以查看策略幫助
# - ignore the following properties
# + check the following properties
#
# a access timestamp (mutually exclusive with +CMSH)
# b number of blocks allocated
# c inode creation/modification timestamp
# d ID of device on which inode resides
# g group id of owner
# i inode number
# l growing files (logfiles for example)
# m modification timestamp
# n number of links
# p permission and file mode bits
# r ID of device pointed to by inode (valid only for device objects)
# s file size
# t file type
# u user id of owner
#
# C CRC-32 hash
# H HAVAL hash
# M MD5 hash
# S SHA hash
Device = +pugsdr-intlbamcCMSH ;
Dynamic = +pinugtd-srlbamcCMSH ;
Growing = +pinugtdl-srbamcCMSH ;
IgnoreAll = -pinugtsdrlbamcCMSH ;
IgnoreNone = +pinugtsdrbamcCMSH-l ;
ReadOnly = +pinugtsdbmCM-rlacSH ;
Temporary = +pugt ;
===============================================================
--以twpol.txt下面這一段為例來說明它這個默認策略文件定義的意思
(
rulename = "Tripwire Binaries",
severity = $(SIG_HI)
)
{
$(TWBIN)/siggen -> $(SEC_BIN) ;
$(TWBIN)/tripwire -> $(SEC_BIN) ;
$(TWBIN)/twadmin -> $(SEC_BIN) ;
$(TWBIN)/twprint -> $(SEC_BIN) ; --這四個文件都要被以SEC_BIN的檢測等級來檢測
}
--然后又找到有這句變量定義
SEC_BIN = $(ReadOnly) ;
--又有下面這句的定義(rpm版里要man twpolicy去找,source版里有)
ReadOnly = +pinugtsdbmCM-rlacSH ;
-----------------------------------------------------------------------------------------------------------
現在/etc/tripwire/twpol.txt已經默認定義了非常多的文件,但現在這里有一個問題:它默認定義的文件或命令,你的系統上不一定有,所以你要注釋掉它。它默認注釋的文件或命令,你的系統上卻有,所以你要去掉注釋.
比如:
下面這一句,它默認是定義的,但我系統上沒有,所以需要前面加#號來注釋
/sbin/busybox -> $(SEC_CRIT) ;
下面這一句,默認是注釋的,但我系統上有,所以我想去掉前面的注釋
#/sbin/lvchange -> $(SEC_CRIT) ;
為了實現這兩個需求,我們需要使用腳本來處理默認的policy文件
# cp 筆記目錄下/arch/TRANtwpol.sh /etc/tripwire/
# cd /etc/tripwire/
# cp twpol.txt twpol.txt.bak --因為這個腳本是用sed直接操作修改這個文件,所以可以先備份一下
# sh TRANtwpol.sh twpol.txt --處理完后的twpol.txt文件就是實現了上面兩個需求的文件
這樣處理並不能增加配置,也就是說你如果有需要添加的自定義需求,就只能手動再添加;下面我就添加一段做個示例,在配置文件最后加下如下一段
(
rulename = "notes",
severity = $(SIG_HI)
)
{
/share/ -> $(SEC_INVARIANT) (recurse = 0) ;
/share/20170522/ -> $(SEC_CRIT);
!/share/20170522/program/ ;
}
--這個策略的意思是/share/目錄本身(不遞歸到下級子目錄),只檢查$(SEC_INVARIANT);/share/20170522/及其遞歸下級所有文件和子目錄都檢查$(SEC_CRIT);/share/20170522/program/及其遞歸下級所有都不檢查任何屬性
4,把修改好的文件,定入policy文件
# twadmin -m P twpol.txt --將修改完畢的文件,編碼寫入policy file
Please enter your site passphrase:
Wrote policy file: /etc/tripwire/tw.pol
# file /etc/tripwire/tw.pol --把你修改好的策略都寫入了這個數據文件里
/etc/tripwire/tw.pol: data
5,初始化
# tripwire --init --通過上一步的數據文件來初始化數據庫文件(就相當於是對你所有的配置做了數字簽名)
Please enter your local passphrase:
Parsing policy file: /etc/tripwire/tw.pol
Generating the database...
*** Processing Unix File System *** --在這里時間較長,幾分鍾左右
Wrote database file: /var/lib/tripwire/li.cluter.com.twd --數據庫文件路徑
The database was successfully generated.
6,懷疑被入侵后進行手動檢測,或者用crontab周期性檢測
# tripwire --check --對所有定義的文件進行一次檢測,速度較慢
Wrote report file: /var/lib/tripwire/report/li.cluster.com-20160930-101619.twr --檢測完的報告,時間格式為系統的年月日-時分秒
# tripwire --check --rule-name notes --指定只檢測的定義的規則名
查看歷史報告,可以看到更詳細的信息
# twprint --print-report --twrfile /var/lib/tripwire/report/li.cluster.com-20160930-101619.twr
一般為了安全性,在寫入數據庫之后,把明文的twpol.txt文件給刪除或者備份到其它介質
# rm /etc/tripwire/twpol.txt -rf
7,策略如何更新?
a),導出正在使用的策略
# twadmin -m p >/etc/tripwire/twpol.txt
b),按照需求對其進行修改
c),再導進去
# twadmin -m P /etc/tripwire/twpol.txt
d),重新生成數據文件
#tripwire --init