現場(環境): CentOS 6.9 套件:tripwire
原因:
今天系統如果真的被入侵了,
沒有人能夠保證系統被動了哪裡都知道,
所以我們需要一個系統被動了哪裡,我們有機會知道的套件。
雖然不能保證系統安然無恙,但我們總得知倒是哪裡被隨意修改了。
處理方式:
tripwire的方法是用MD5創建唯一值,
只要檔案不更動,MD5數值就不會變動。
1. 安裝tripwire
下載source檔。
http://sourceforge.net/projects/tripwire/files/
#tar -jxvf tripwire-2.4.2.2-src.tar.bz2
#cd tripwire-2.4.2.2-src
編譯前要記得先安裝編譯套件。
#yum install glibc-headers
#yum install gcc-c++
#./configure --prefix=/opt/tripwire
#make
#make install
在make install的時候,
他會要求你閱讀license並確認是否要同意
這邊要輸入accept
是否確認
輸入要加密的密碼 (Global)
輸入要加密的密碼 (Local)
輸入剛才設定的密碼後,就完成安裝。
完成安裝後,說一下文件位置(可參閱參考網址)
A.這是主要的設定檔
/opt/tripwire/etc/twcfg.txt
B.定義檢測對象(規則)
以及發生問題該怎麼動作的設定檔。
/opt/tripwire/etc/twpol.txt
C.資料庫檔案,放我們的快照
/opt/tripwire/lib/tripwire/$(HOSTNAME).twd
D. 為了避免被竄改,Tripwise自己有加密跟簽名的兩把金鑰。
Site key: 保護規則文件,只要是相同規則的主機,都可以用同一把key
(但是建議還是每一台都有自己的比較好)
/opt/tripwire/etc/site.key
Local Key: 保護資料庫檔案及分析報告
/opt/tripwire/etc/[$HOSTNAME]-local.key
2. 開始設定tripwise
#vim /opt/tripwire/etc/twcfg.txt
修改設定檔內容:
監測所屬目錄的完整性。
報告等級。
=!===============================
LOOSEDIRECTORYCHECKING =true
REPORTLEVEL =4
=!===============================
建立加密格式(做編碼):
# /opt/tripwire/sbin/twadmin --create-cfgfile -S /opt/tripwire/sbin/site.key twcfg.txt
=!==================================================
Please enter your site passphrase:
Wrote configuration file: /opt/tripwire/etc/tw.cfg
=!==================================================
3. 初始化資料庫
#/opt/tripwire/sbin/tripwire --init
=!==================================================
初始化完會有下列訊息
### Continuing…
Wrote database file: /opt/tripwire/lib/tripwire/[$hostname].twd
The database was successfully generated.
=!==================================================
* 當你有更新設定檔twpol.txt的時候,要做資料庫更新
#/opt/tripwire/sbin/tripwire --update-policy --secure-mode low /opt/tripwire/etc/twpol.txt
=!==================================================
Parsing policy file: /opt/tripwire/etc/twpol.txt
Please enter your local passphrase:
Please enter your site passphrase:
======== Policy Update: Processing section Unix File System.
======== Step 1: Gathering information for the new policy.
The object: "/misc" is on a different file system...ignoring.
The object: "/net" is on a different file system...ignoring.
The object: "/sys" is on a different file system...ignoring.
### An object has been changed since the database was last updated.
### Object name: Conflicting properties for object
### /var/spool/postfix/public/qmgr
### > Modify Time
### Continuing…
...
======== Step 2: Updating the database with new objects.
======== Step 3: Pruning unneeded objects from the database.
Wrote policy file: /opt/tripwire/etc/tw.pol
Wrote database file: /opt/tripwire/lib/tripwire/tkbweb.twd
=!==================================================
4. 檢查系統文件是不是有被異動
#/opt/tripwire/sbin/tripwire --check --interactive
檢測完結果會直接顯示再畫面上,
一開始有ERROR是因為,
沒有去依系統環境建立規則,
所以出現路徑找不到的狀況。
轉換成報告
因為所有的報告內容都會已twr格式方式儲存。
(儲存在/opt/tripwire/lib/tripwire/report )
我們要用twprint指令做報告的轉換。
# /opt/tripwire/sbin/twprint --print-report --twrfile /opt/tripwire/lib/tripwire/report/[$hostname]-20170926-170329.twr > /tmp/20170926.txt
之後再去看轉出的檔案
# cat tmp/20170926.txt
或者不加 [ > ]做導出,直接看也可以。
#/opt/tripwire/sbin/twprint --print-report --twrfile /opt/tripwire/lib/tripwire/report/[$hostname]-20170926-170329.twr
基本上安裝跟設定就是這樣而已,
這邊的重點會在於規則及路徑上的設定。
大家再依自己的需求去做調整吧。
備註:
建議刪除tripwise內 的所有.txt 設定檔路徑,
這樣才不會被有心人士知道你在監控什麼檔案內容。
#rm -rf /opt/tripwire/etc/*.txt
當你有需要的時候在用twadmin把設定檔匯出來修改就好。
#twadmin --print-polfile > /root/twpol.txt
#twadmin --print-cfgfile > /root/twcfg.txt
要修改監控路徑的在twpol.txt找到下面路徑,然後自己添加。
相關設定值就要查一下官方網站了。
#IgnoreNone 值表示應檢查此檔案的所有屬性
我加入我想監控的路徑,並要求全部監測。
然後再次更新twpol就好。
#/opt/tripwire/sbin/tripwire --update-policy --secure-mode low /opt/tripwire/etc/twpol.txt
但是會發現錯誤
### Error: Rule already defined as a start or stop point of another rule.
### /opt: Line number 341
### Exiting...
原因是/opt已經有做另外的規則了,
當規則重複就會出現這個問題。
修改完,再試一次就成功了
# /opt/tripwire/sbin/tripwire --update-policy --secure-mode low /opt/tripwire/etc/twpol.txt
Parsing policy file: /opt/tripwire/etc/twpol.txt
Please enter your local passphrase:
這次是改用readonly檢查,看/opt下的檔案有沒有被異動過。
/opt -> $(ReadOnly) ;
這邊可以看到,有一支tw.pol.bak的檔案,之前是不存在的。
大致上可以這樣使用。
(本篇完)
參考網址:
http://www.cnblogs.com/yuanermen/archive/2012/10/16/2726441.html
http://crypto.nknu.edu.tw/netsec/using-Tripwire.htm
https://blog.longwin.com.tw/2004/03/tripwire%E5%AE%89%E8%A3%9D/