系统管理员通常从svn/git中com.cn/tag/%e6%a3%80%e7%b4%a2" target="_blank">检索代码,部署站点后通常首先会生成该站点所有文件的MD5值,如果上线后网站页面内容被篡改(如挂马)等,可以比对之前生成MD5值快速查找去那些文件被更改,为了使系统管理员第一时间发现,可结合crontab或nagios等工具。
程序测试如下:
- #pythoncheck_change.py
- Usage:pythoncheck_change.py update /home/wwwroot
- pythoncheck_change.pycheck/home/wwwroot
- #pythoncheck_change.pyupdate/data/www#生成站点的md5值
- #echo''>/data/www/sitemap.html#测试清空文件
- #rm-rf/data/www/sitemap.xml#测试删除文件
- #pythoncheck_change.pycheck/data/www#查找那些文件被篡改
- /data/www/sitemap.xml
- /data/www/sitemap.html
代码如下(check_change.py):
- #!/usr/bin/envpython
- importos,sys,subprocess
- defupdate(path):
- f= open(file,'w')
- forroot,dirs,filesinos.walk(path):
- fornameinfiles:
- line= os.path.join(root,name)
- (stdin,stderr)=subprocess.Popen(['md5sum',line],stdout=subprocess.PIPE).communicate()
- f.write(stdin)
- f.close()
- defcheck(path):
- f= open(file,'r')
- forlineinf:
- check_ok= """echo'%s'|md5sum-c>/dev/null2>&1"""%line
- #printcheck_ok
- ifnotsubprocess.call(check_ok,shell=True)==0:
- abnormal=line.split()
- printabnormal[1]
- f.close()
- defUsage():
- print'''
- Usage:python%supdate/home/wwwroot
- python%scheck/home/wwwroot
- '''%(sys.argv[0],sys.argv[0])
- sys.exit()
- iflen(sys.argv)!=3:
- Usage()
- file='file.key'
- model= sys.argv[1]
- path= sys.argv[2]
- ifos.path.exists(path)==False:
- print"\033[;31mThedirectoryorfiledoesnotexist\033[0m"
- sys.exit()
- elifmodel=='update':
- update(path)
- elifmodel=='check':
- check(path)
- else:
- Usage()
发表评论