天天看點

Shell程式設計---監控網站目錄檔案是否被惡意篡改(md5sum指紋)

題目要求:使用shell腳本監控網站目錄(/var/html/www)中的檔案是否被篡改,如果有就列印出改動的檔案名并用郵件告警。

分析:

概念:

什麽是惡意篡改?隻要是未進過許可改動的都是惡意篡改。

檔案被篡改了,會有特征:

a.檔案大小可能會發生變化;

b.檔案的修改時間會發生變化(檔案測試符ot、nt等);

c.檔案内容會發生變化(md5sum指紋);

d.檔案目錄中的檔案被删除,或者檔案目錄中有其他檔案增加;

注意:

需要檔案資訊記錄庫(變化前 VS 變化後,這個是衡量對比的标準)

實際應用:

1.代碼釋出方案:大公司或規範的公司,不會時刻上傳代碼,每天1-2次為宜。

2.伺服器上的項目目錄是否被黑客入侵進行惡意篡改。

解答:

指令行測試:
[[email protected] ~]# cd /var/www/html
[[email protected] html]# pwd
/var/www/html
[[email protected] html]# touch wenben1.txt
[[email protected] html]# cat wenben1.txt 
[[email protected] html]# md5sum wenben1.txt 
d41d8cd98f00b204e9800998ecf8427e  wenben1.txt
[[email protected] html]# echo "addContent to the file" >> wenben1.txt 
[[email protected] html]# cat wenben1.txt 
addContent to the file
[[email protected] html]# md5sum wenben1.txt 
009c4ee40b070fa48fb47c503642c0f1  wenben1.txt

[[email protected] html]# find /var/www/html/ -type f | xargs md5sum
009c4ee40b070fa48fb47c503642c0f1  /var/www/html/wenben1.txt
d41d8cd98f00b204e9800998ecf8427e  /var/www/html/wenben2.txt
[[email protected] html]# find /var/www/html/ -type f | xargs md5sum > /home/yuki/infoDB/md5sumBefore.log
[[email protected] html]# cat /home/yuki/infoDB/md5sumBefore.log
009c4ee40b070fa48fb47c503642c0f1  /var/www/html/wenben1.txt
d41d8cd98f00b204e9800998ecf8427e  /var/www/html/wenben2.txt

[[email protected] html]# md5sum -c /home/yuki/infoDB/md5sumBefore.log 
/var/www/html/wenben1.txt: OK
/var/www/html/wenben2.txt: OK
[[email protected] html]# md5sum -c /home/yuki/infoDB/md5sumBefore |egrep -i "fail"

#測試:修改檔案中的内容(增加内容/減少内容/修改内容)
[[email protected] html]# echo "now we add sth to the file with name wenben2.txt" >> wenben2.txt 
[[email protected] html]# md5sum -c /home/yuki/infoDB/md5sumBefore.log |egrep -i "fail"
md5sum: WARNING: 1 of 2 computed checksums did NOT match
/var/www/html/wenben2.txt: FAILED

#測試:删除某個檔案
[[email protected] html]# rm -rf wenben1.txt 
[[email protected] html]# md5sum -c /home/yuki/infoDB/md5sumBefore.log
md5sum: /var/www/html/wenben1.txt: No such file or directory
/var/www/html/wenben1.txt: FAILED open or read
/var/www/html/wenben2.txt: FAILED
md5sum: WARNING: 1 of 2 listed files could not be read
md5sum: WARNING: 1 of 1 computed checksum did NOT match

#測試:建立剛剛删除的檔案
[[email protected] html]# touch wenben1.txt
[[email protected] html]# md5sum -c /home/yuki/infoDB/md5sumBefore.log 
/var/www/html/wenben1.txt: FAILED
/var/www/html/wenben2.txt: FAILED
md5sum: WARNING: 2 of 2 computed checksums did NOT match
#注意:向剛剛建立的檔案恢複删除之前的内容,md5sum又和之前的一樣了。
[[email protected] html]# echo "addContent to the file" >> wenben1.txt 
[[email protected] html]# md5sum -c /home/yuki/infoDB/md5sumBefore.log 
/var/www/html/wenben1.txt: OK
/var/www/html/wenben2.txt: FAILED
md5sum: WARNING: 1 of 2 computed checksums did NOT match

#測試:建立新的檔案(新的檔案代表着它的md5sum指紋從未被記錄到檔案記錄資訊庫中去)
[[email protected] html]# touch newFresh.log
[[email protected] html]# md5sum -c /home/yuki/infoDB/md5sumBefore.log 
/var/www/html/wenben1.txt: OK
/var/www/html/wenben2.txt: FAILED
md5sum: WARNING: 1 of 2 computed checksums did NOT match

#注意:我發現md5sum從未被記錄的新檔案是無法被監控對比到是否被篡改了。
#但是,我們可以提前将這個目錄的檔案數量、資訊都記錄下來然後再将現在的檔案數量和之前的進行對比就可以什麽新檔案被建立了。

[[email protected] html]# ll
total 8
-rw-r--r--. 1 root root  0 Nov 14 14:01 newFresh.log
-rw-r--r--. 1 root root 23 Nov 14 13:58 wenben1.txt
-rw-r--r--. 1 root root 49 Nov 14 11:45 wenben2.txt
[[email protected] html]# find /var/www/html/ -type f | wc -l
3

#建立前記錄
[[email protected] html]# find /var/www/html/ -type f > /home/yuki/infoDB/fileInfoBefore.log

#建立新檔案
[[email protected] html]# touch wenben3.log
[[email protected] html]# ll
total 8
-rw-r--r--. 1 root root  0 Nov 14 14:01 newFresh.log
-rw-r--r--. 1 root root 23 Nov 14 13:58 wenben1.txt
-rw-r--r--. 1 root root 49 Nov 14 11:45 wenben2.txt
-rw-r--r--. 1 root root  0 Nov 14 14:09 wenben3.log
#建立後再記錄
[[email protected] html]# find /var/www/html/ -type f > /home/yuki/infoDB/fileInfoAfter.log
#比較兩個記錄檔案的不同
[[email protected] html]# diff /home/yuki/infoDB/fileInfoBefore.log  /home/yuki/infoDB/fileInfoAfter.log
diff: home/yuki/infoDB/fileInfoAfter.log: No such file or directory
           

腳本編寫:

腳本1:篡改之前做檔案資訊記錄庫,(變化前 VS 變化後,這個是衡量對比的标準)
#!/bin/sh
source /etc/profile

#define variable
#定義一個需要監控的站點目錄
siteDir="/var/www/html"
#定義一個檔案資訊庫目錄
path="/home/yuki/infoDB"
if [ !d ${path} ];
	then
		mkdir -p ${path}
fi
#做好的檔案md5sum指紋記錄檔案
md5sumBeforeDB="${path}/md5sumBefore.log"
if [ -f ${md5sumBeforeDB} ];
	then 
		touch ${md5sumBeforeDB}
fi
echo `find ${siteDir} -type f | xargs > ${path}/md5sumBefore.log`

#篡改之前記錄的需要監控目錄下的檔案數量、資訊記錄
fileInfoBefore="${path}/fileInfoBefore.log"
if [ -f ${fileInfoBefore} ];
	then 
		touch ${fileInfoBefore}
fi
echo `find ${siteDir} -type f > ${path}/fileInfoBefore.log`

           
腳本2:網站目錄(/var/html/www)中的檔案是否被篡改,如果有就列印出改動的檔案名并用郵件告警。
#!/bin/sh
source /etc/profile

#define variable

#定義一個郵件位址
[email protected]

#定義需要監控的站點目錄
siteDir="/var/www/html"

#定義檔案資訊庫目錄
path="/home/yuki/infoDB"
if [ !d ${path} ];
	then
		mkdir -p ${path}
fi

#下面這個是之前就做好的檔案md5sum指紋記錄檔案
md5sumBeforeDB="${path}/md5sumBefore.log"
fileInfoBefore="${path}/fileInfoBefore.log"

if [ ! -f ${md5sumBeforeDB}  -o ! -f ${fileInfoBefore}  ];
	then 
		echo "檔案資訊庫的建立不完全,無法參照對比"
		exit 1
fi
fileNumBefore=`cat ${fileInfoBefore}|wc -l`

---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------

#篡改之後記錄的需要監控目錄下的檔案數量、資訊記錄
fileInfoAfter="${path}/fileInfoAfter.log"
if [ -f ${fileInfoAfter} ];
	then 
		touch ${fileInfoAfter}
fi
echo `find ${siteDir} -type f > ${path}/fileInfoBefore.log`
fileNumAfter=`cat ${fileInfoAfter}|wc -l`

#再繼續定義一個記錄篡改檔案的篡改資訊檔案,友善後面郵件告警。
recordAllInfoLog="${path}/recordAllInfo.log"
if [ ! -f ${recordAllInfoLog} ];
	then
		touch ${recordAllInfoLog}
fi

#篡改标志可以通過兩個方面進行比較:
#2>/dev/null 将錯誤資訊追加空裝置
changeNumFlag=`md5sum -c ${md5sumBeforeDB}  2 > /dev/null  |egrep -i "failed" | wc -l`
if [ ${changeNumFlag} -ne 0 ] || [ ${fileNumAfter} -ne ${fileNumBefore} ]
	then	
		echo `md5sum -c ${md5sumBeforeDB}  2 > /dev/null |egrep -i "failed"` > ${recordAllInfoLog}
		diff ${fileInfoAfter} ${fileInfoBefore} >> ${recordAllInfoLog}
		happenTime= `date "+%Y-%m-%d %H:%M:%S"`
		mail -s "the directory of the site on ${happenTime}  ${email}" < ${recordAllInfoLog}

fi

           

繼續閱讀