之前工作需要,為nagios寫的url篡改告警shell腳本。若url頁面變動則告警。若為正常修改,則需手動重新生成hash檔案,适用于變動不頻繁而又較關鍵url。根據經驗,若頁面插入其他統計流量,顯示時間等代碼,則無法很好經行url修改監控。
功能核心是經行url比較。利用curl 下載下傳頁面,然後計算hash值,并儲存以備下次比較,此部分思想由jokechoo提供,腳本實作:tqh。 不同于防篡改,此處是實時url比較,告警。
而防篡改技術,一般由2組伺服器提供web服務,一組對外服務,此組伺服器無修改權限,一旦檔案被修改就會被另一組背景伺服器同步回去,以此實作防篡改。想要安全就要付出不少代價。
插件1. inithash.sh 計算原始hash值并儲存 。(注意修改生成hash檔案的權限,或改為nagios使用者生成)
插件2. check_hash nagios的檢測插件
PS:插件内使用的是中文,注意編碼格式utf-8,可先手工調試是否顯示中文。若nagios主程式本身無中文支援可能顯示為亂碼。
代碼如下,需要可下載下傳附件。
一、插件1 inithash.sh
1.
2. #!/bin/bash
3. # version 1.0 beta,2011.01.17
4. # used for add/update webpage hash
5. hash_lib=/tmp/test/hashfile
6. url_list=/tmp/test/urlfile #模式1 直接修改url檔案位址
7. log_file=/tmp/test/updatehash.log
8.
9. #######@@ #注釋掉上面url_list,開啟此處@@之間内容,變為互動模式。另一check腳本會生成update.list 友善update
10. #if [ $# -eq 1 -a -f "$1" ]
11. #then
12. # url_list="$1"
13. # echo $url_list
14. #else
15. # echo "ERROR: plz input 1 urlfile"
16. # exit
17. #fi
18. ########@@
19. ###以下注釋内容暫無意義。
20. #[ ! -e $url_list ] && echo "not exist urlfile" && exit
21. ###tmp_dir=/tmp/test
22. ###web_tmp=/tmp/test/tmpfile
23. ###cd $tmp_dir || echo "no tmp_dir,plz create first" && exit
24.
25. while read LINE
26. do
27. curl -s -A topsec $LINE > tmpfileabc
28. #URL名可能含/,無法直接作為檔案名,hash表需針對URL作修改
29. #if [ ! $? -eq 0 ] #URL頁面無法下載下傳,測試中
30. #then
31. #echo "this page cant download"
32. #fi
33. hashtmp=`sha1sum tmpfileabc` #生成目前頁面HASH
34. greptmp=`grep $LINE $hash_lib` #檢測該URL是否已hash
35. if [ $? -eq 0 ]
36. then
37. echo "The site $LINE has checked before, update the hash value."
38. sed -i "s#$greptmp#$hashtmp#" $hash_lib #用#做分隔,確定頁面url中無#号
39. sed -i s#tmpfileabc#"$LINE"# $hash_lib #将tmpfileabc替換為正确url
40. echo `date +%Y%m%d_%R` "$LINE hash has updated" >> $log_file
41. else
42. echo "The site $LINE is a new check, add the hash value."
43. echo $hashtmp>> $hash_lib
44. sed -i "s#tmpfileabc#$LINE#" $hash_lib
45.
46. fi
47. rm -f tmpfileabc
48.
49. done < $url_list
50.
二、 插件2. check_url
1.
2.
#!/bin/bash
3. #version 1.0 ,2011.01.18
4. #nagios check http plug. check whether webpage has modified . url list is from file
5. #後續不同參數不同站點等
6.
7. url_list=/tmp/test/urlfile
8. hash_lib=/tmp/test/hashfile
9. tmp_dir=/tmp/test
10. log_file=/tmp/test/hashcheck.log
11. need_update=/tmp/test/needupHASH.list
12. cd $tmp_dir
13.
14.
15. flag_a=0
16. flag_c=0
17.
18. while read URL
19. do
20.
21. curl -s -A topsec "$URL" > webtmpfile
22. current_hash=`sha1sum webtmpfile | awk '{print$1}'`
23.
24. #####################check url hash
25. if `grep $URL $hash_lib>greptmpfile` #測試URL是否已經HASH處理過
26. then
27. if grep -q $current_hash greptmpfile #比對HASH值
28. then
29. flag_a=0 #未變動
30. else
31. flag_a=1 #變動
32. echo `date +%Y%m%d_%R` "$URL web changed" >> $log_file
33. echo "$URL" >>$need_update
34. #儲存到需更新url檔案中,或者直接從l_file檔案中提提取
35. fi
36. else
37. flag_c=3 #HASH值未被儲存過
38. echo `date +%Y%m%d_%R` "$URL hash should be create" >>$log_file
39. echo "$URL" >>$need_update
40. fi
41.
42. done < $url_list
43. rm -f greptmpfile
44. rm -f webtmpfile
45. ######################nagios checkstat
46. if [ $flag_c -eq 3 ]
47. then
48. if [ $flag_a -eq 1 ]
49. then
50. echo "網頁改動,且檢測到新URL" #檢測到網頁改動,且含有新URL
51. exit 3
52. else
53. echo "檢測到新URL" #正常,但有新URL未做比對。 後續手動/自動添加
54. exit 1
55. fi
56.
else
57. if [ $flag_a -eq 0 ]
58. then
59. echo "一切正常" #頁面未被篡改
60. exit 0
61. else
62. echo "網頁内容改動" #改動
63. exit 2
64. fi
65.
66. fi
67.