Python实现的检测网站挂马程序

yipeiwu_com6年前Python基础

系统管理员通常从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/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实现Adapter模式实例代码

python实现Adapter模式实例代码

本文研究的主要是python实现Adapter模式的相关内容,具体实现代码如下。 Adapter模式有两种实现方式一种是类方式。 #理解 #就是电源适配器的原理吧,将本来不兼容的接...

python 判断三个数字中的最大值实例代码

python 判断三个数字中的最大值,具体代码如下所示: #判断三个数中最大值 n1= int(input('please enter the firest number:')) n...

Django 登陆验证码和中间件的实现

Django 登陆验证码和中间件的实现

验证码 在用户注册、登陆页面为了防止暴力请求,可以加入验证码。如果验证码错误,则不需要继续处理,可以减轻服务器的压力 使用验证码也是一种有效防止 csrf 的方法 def veri...

Python定义二叉树及4种遍历方法实例详解

Python定义二叉树及4种遍历方法实例详解

本文实例讲述了Python定义二叉树及4种遍历方法。分享给大家供大家参考,具体如下: Python & BinaryTree 1. BinaryTree (二叉树) 二叉树是有限个元素的...

Python模块WSGI使用详解

WSGI(Web Server Gateway Interface):Web服务网关接口,是Python中定义的服务器程序和应用程序之间的接口。 Web程序开发中,一般分为服务器程序和...