Python實現(xiàn)的檢測網(wǎng)站掛馬程序
更新時間:2014年11月30日 20:38:28 投稿:mdxy-dxy
這篇文章主要介紹了Python實現(xiàn)的檢測網(wǎng)站掛馬程序,需要的朋友可以參考下
系統(tǒng)管理員通常從svn/git中檢索代碼,部署站點后通常首先會生成該站點所有文件的MD5值,如果上線后網(wǎng)站頁面內容被篡改(如掛馬)等,可以比對之前生成MD5值快速查找去那些文件被更改,為了使系統(tǒng)管理員第一時間發(fā)現(xiàn),可結合crontab或nagios等工具。
程序測試如下:
# python check_change.py
Usage: python check_change.py update /home/wwwroot
python check_change.py check /home/wwwroot
# python check_change.py update /data/www #生成站點的md5值
# echo ' ' > /data/www/sitemap.html #測試清空文件
# rm -rf /data/www/sitemap.xml #測試刪除文件
# python check_change.py check /data/www #查找那些文件被篡改
/data/www/sitemap.xml
/data/www/sitemap.html
代碼如下(check_change.py):
#!/usr/bin/env python
import os,sys,subprocess
def update(path):
f = open(file,'w')
for root,dirs,files in os.walk(path):
for name in files:
line = os.path.join(root, name)
(stdin,stderr) = subprocess.Popen(['md5sum',line],stdout=subprocess.PIPE).communicate()
f.write(stdin)
f.close()
def check(path):
f = open(file,'r')
for line in f:
check_ok = """echo '%s' | md5sum -c > /dev/null 2>&1""" % line
#print check_ok
if not subprocess.call(check_ok, shell = True) == 0:
abnormal = line.split()
print abnormal[1]
f.close()
def Usage():
print '''
Usage: python %s update /home/wwwroot
python %s check /home/wwwroot
''' % (sys.argv[0],sys.argv[0])
sys.exit()
if len(sys.argv) != 3:
Usage()
file = 'file.key'
model = sys.argv[1]
path = sys.argv[2]
if os.path.exists(path) == False:
print "\033[;31mThe directory or file does not exist\033[0m"
sys.exit()
elif model == 'update':
update(path)
elif model == 'check':
check(path)
else:
Usage()
相關文章
python爬蟲爬取bilibili網(wǎng)頁基本內容
這篇文章主要介紹了python爬蟲爬取bilibili網(wǎng)頁基本內容,用爬蟲爬取bilibili網(wǎng)站排行榜游戲類的所有名稱及鏈接,下面來看看具體的實現(xiàn)過程吧,需要的朋友可以參考一下2022-01-01
django實現(xiàn)HttpResponse返回json數(shù)據(jù)為中文
這篇文章主要介紹了django實現(xiàn)HttpResponse返回json數(shù)據(jù)為中文,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python使用Py2neo創(chuàng)建Neo4j的節(jié)點和關系
Neo4j是一款開源圖數(shù)據(jù)庫,使用Python語言訪問Neo4j可以使用Py2neo。本文介紹了使用Py2neo訪問Neo4j,批量創(chuàng)建節(jié)點和關系的方法2021-08-08

