企業Shell面試題14:開發腳本入侵檢測與報警案例
面試及實戰考試題:監控web站點目錄(/var/html/www)下所有文件是否被惡意篡改(文件內容被改了),如果有就打印改動的文件名(發郵件),定時任務每3分鐘執行一次。
1.1問題分析
1)首先要說明的是,思考過程的積累比實際代碼開發的能力積累更重要。
2)什麽是惡意篡改,只要是未經過許可的改動都是篡改。
3)文件內容被改動了會有如下特征。
◎ 大小可能會變化
◎ 修改時間會變化
◎ 文件內容會變化,利用md5sum指紋校驗
◎ 增加或刪除文件,比對每次檢測前後的文件數量。
1.2參考解答
本題主要采用md5sum的方法來實現。
第一步,在企業網站發布代碼之後,即對所有網站數據建立初始指紋庫和文件庫,這個步驟很重要,沒有基礎的指紋庫,無法進行入侵檢測。
以/var/html/www作為站點目錄為例。
1)建立測試數據:
[[email protected] scripts]# mkdir /var/html/www -p #<==創建站點目錄。
[[email protected] scripts]# cp -a /etc/a* /var/html/www #<==復制少量測試數據。
[[email protected] scripts]# cp -a /etc/b* /var/html/www #<==復制少量測試數據。
[[email protected] scripts]# ls /var/html/www #<==檢查。
abrt acpi adjtime aliases aliases.db alsa alternatives anacrontab asound.conf at.deny audisp audit bash_completion.d bashrc blkid
2)建立初始的文件指紋庫:
[[email protected] scripts]# find /var/html/www -type f|xargs md5sum >/opt/zhiwen.db.ori
<==建立文件內容指紋庫。
[[email protected] scripts]# tail /opt/zhiwen.db.ori
68b329da9893e34099c7d8ad5cb9c940 /var/html/www/at.deny
e5d91bca71662d7c09bc7fc731ad3222 /var/html/www/adjtime
8241db83d5edf01c71734e41e383e205 /var/html/www/anacrontab
c23a47aca3ec55122b8871c5a61494b5 /var/html/www/abrt/abrt-action-save-package-data.conf
9cd848af905b767fa410070b265a70c7 /var/html/www/abrt/gpg_keys
b6bcc3a178b9442d30d88444a9311769 /var/html/www/abrt/abrt.conf
441645d0e419c1f593694ca014817ee1 /var/html/www/abrt/plugins/CCpp.conf
1ecf30990ac5948a8e3bd7b8c1cd944f /var/html/www/abrt/plugins/python.conf
1e4aded98bb1ff08094c8dfb09d33192 /var/html/www/abrt/plugins/oops.conf
b2a676d524cb2d46eccc00baadfbfe29 /var/html/www/aliases.db
3)建立初始的文件庫:
[[email protected] scripts]# find /var/html/www -type f>/opt/wenjian.db.ori
#<==建立文件數量和名字庫。
[[email protected] scripts]# tail /opt/wenjian.db.ori
/var/html/www/at.deny
/var/html/www/adjtime
/var/html/www/anacrontab
/var/html/www/abrt/abrt-action-save-package-data.conf
/var/html/www/abrt/gpg_keys
/var/html/www/abrt/abrt.conf
/var/html/www/abrt/plugins/CCpp.conf
/var/html/www/abrt/plugins/python.conf
/var/html/www/abrt/plugins/oops.conf
/var/html/www/aliases.db
第二步,檢測文件內容和文件數量變化。
1)檢測文件內容變化:
[[email protected] scripts]# echo oldboy>>/var/html/www/audisp/plugins.d/af_unix.conf #<==篡改文件。
[[email protected] scripts]# export #<==調整字符集。
[[email protected] scripts]# md5sum -c --quiet/opt/zhiwen.db.ori
#<==檢查所有文件的內容是否變化。
/var/html/www/audisp/plugins.d/af_unix.conf: FAILED #<==變化的會被打印出來。
md5sum: WARNING: 1 of 29 computed checksums did NOTmatch #<==綜合提示。
2)檢測文件數量變化:
[[email protected] scripts]# echo oldgirl.txt>/var/html/www/test.txt
#<==模擬增加新文件。
[[email protected] scripts]# md5sum -c --quiet/opt/zhiwen.db.ori
#<==利用指紋庫無法檢測新增文件。
/var/html/www/audisp/plugins.d/af_unix.conf: FAILED
md5sum: WARNING: 1 of 29 computed checksums did NOT match
[[email protected] scripts]# find /var/html/www -type f>/opt/wenjian.db_curr.ori
#<==獲取檢測前的所有文件數量及文件名。
[[email protected] scripts]# diff /opt/wenjian.db* #<==采用diff命令比較。
20d19
< /var/html/www/test.txt #<==test.txt就是新增的,怎麽樣,還可以吧。
第三步,開發檢查指紋識別腳本。
首先,人工做如下操作:
[[email protected] scripts]# find /var/html/www -type f |xargs md5sum>/opt/zhiwen.db.ori
[[email protected] scripts]# find /var/html/www -type f>/opt/wenjian.db.ori
腳本檢測會以上述兩個命令獲取的結果為原始的正確依據,如下
[[email protected] scripts]# cat 30-14.sh
#!/bin/bash
RETVAL=0 #<==狀態初始化。
export #<==調整字符集。
CHECK_DIR=/var/html/www #<==定義要監測得站點目錄。
[ -e $CHECK_DIR ] || exit 1 #<==如果目錄不存在則退出腳本。
ZhiWenDbOri="/opt/zhiwen.db.ori" #<==定義原始指紋庫路徑。
FileCountDbOri="/opt/wenjian.db.ori" #<==定義原始文件庫路徑。
ErrLog="/opt/err.log" #<==定義檢測後的內容日誌。
[ -e $ZhiWenDbOri ] || exit 2 #<==如果原始指紋庫不存在則退出腳本。
[ -e $FileCountDbOri ] || exit 3 #<==如果原始文件庫不存在則退出腳本。
#judge file contet
echo "[[email protected] scripts]# md5sum -c --quit/opt/zhiwen.db.ori">$ErrLog #<==打印檢測命令。
md5sum -c --quiet /opt/wenjian.db.ori &>>$ErrLog #<==實際執行檢測命令。
RETVAL=$? #<==收集返回值。
#com file count
find $CHECK_DIR -type f >/opt/wenjian.db_curr.ori #<==實際執行檢測命令,獲取最新文件數量等。
echo "[[email protected] scripts]# diff /opt/wenjian.db*"&>>$ErrLog #<==打印檢測命令。
diff /opt/wenjian.db* &>>$ErrLog #<==實際執行檢測命令,比對文件數量及文件名變化情況。
if [ $RETVAL -ne 0 -o `diff /opt/wenjian.db*|wc -l`-ne 0 ]
#<==如果返回值不為0,或者比對結果行數不為0,則進入判斷。
then
mail -s "`uname -n`$(date +%F) err" [email protected] <$ErrLog
else
echo "Sites dir isok"|mail -s"`uname -n` $(date +%F) is ok" [email protected]
fi
mail發送相關配置內容
[[email protected] scripts]# cat /etc/mail.rc
# For Linux and BSD, this should be set. #<==配置文件的最後修改mail內容。
set bsdcompat
set [email protected] smtp=smtp.163.com
set smtp-auth-user=15537920814smtp-auth-password=l123456 smtp-auth=login
然後利用定時任務檢查,命令如下:
[[email protected] scripts]# crontab -l|tail -2
# ids monitor site dir and file change by oldboy at20170511
*/3 * * * * /bin/sh /server/scripts/30-10.sh>/dev/null 2>&1
現在來思考一下,在企業中一般什麽文件需要做指紋驗證呢?
系統命令、用戶文件、配置文件、啟動文件等重要文件,都要監控起來,另外,在實際工作中應對所有的用戶操作做日誌審計,讓所有人的所有操作無處遁形,起到威懾和監督的作用,從而減少被當作“黑鍋俠”的風險。
本文出自 “為人民服務” 博客,請務必保留此出處http://junhun.blog.51cto.com/12852949/1924731
企業Shell面試題14:開發腳本入侵檢測與報警案例