系统管理员通常从svn/git中检索代码,部署站点后通常首先会生成该站点所有文件的MD5值,如果上线后网站页面内容被篡改(如挂马)等,可以比对之前生成MD5值快速查找去那些文件被更改,为了使系统管理员第一时间发现,可结合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/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/null21"""%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() Fri Oct 25 11:02:44 CST 2013

闽公网安备 35020602001684号