检测网站挂马程序(Python)

系统管理员通常从svn/gitcom.cn/tag/%e6%a3%80%e7%b4%a2" target="_blank">检索代码,部署站点后通常首先会生成该站点所有文件的MD5值,如果上线后网站页面内容被篡改(如挂马)等,可以比对之前生成MD5值快速查找去那些文件被更改,为了使系统管理员第一时间发现,可结合crontab或nagios等工具

程序测试如下:

  1. #pythoncheck_change.py
  2. Usage:pythoncheck_change.py update /home/wwwroot
  3. pythoncheck_change.pycheck/home/wwwroot
  4. #pythoncheck_change.pyupdate/data/www#生成站点的md5值
  5. #echo''>/data/www/sitemap.html#测试清空文件
  6. #rm-rf/data/www/sitemap.xml#测试删除文件
  7. #pythoncheck_change.pycheck/data/www#查找那些文件被篡改
  8. /data/www/sitemap.xml
  9. /data/www/sitemap.html

代码如下(check_change.py):

  1. #!/usr/bin/envpython
  2. importos,sys,subprocess
  3. defupdate(path):
  4. f= open(file,'w')
  5. forroot,dirs,filesinos.walk(path):
  6. fornameinfiles:
  7. line= os.path.join(root,name)
  8. (stdin,stderr)=subprocess.Popen(['md5sum',line],stdout=subprocess.PIPE).communicate()
  9. f.write(stdin)
  10. f.close()
  11. defcheck(path):
  12. f= open(file,'r')
  13. forlineinf:
  14. check_ok= """echo'%s'|md5sum-c>/dev/null2>&1"""%line
  15. #printcheck_ok
  16. ifnotsubprocess.call(check_ok,shell=True)==0:
  17. abnormal=line.split()
  18. printabnormal[1]
  19. f.close()
  20. defUsage():
  21. print'''
  22. Usage:python%supdate/home/wwwroot
  23. python%scheck/home/wwwroot
  24. '''%(sys.argv[0],sys.argv[0])
  25. sys.exit()
  26. iflen(sys.argv)!=3:
  27. Usage()
  28. file='file.key'
  29. model= sys.argv[1]
  30. path= sys.argv[2]
  31. ifos.path.exists(path)==False:
  32. print"\033[;31mThedirectoryorfiledoesnotexist\033[0m"
  33. sys.exit()
  34. elifmodel=='update':
  35. update(path)
  36. elifmodel=='check':
  37. check(path)
  38. else:
  39. Usage()