用Python和MD5实现网站挂马检测程序

yipeiwu_com5年前Python基础

一、程序测试

复制代码 代码如下:
# 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实现K折交叉验证法的方法步骤

学习器在测试集上的误差我们通常称作“泛化误差”。要想得到“泛化误差”首先得将数据集划分为训练集和测试集。那么怎么划分呢?常用的方法有两种,k折交叉验证法和自助法。介绍这两种方法的资料有很...

Python判断中文字符串是否相等的实例

Python判断中文字符串是否相等的实例

Python判断两个相等的中文字符串为false,将两个待比较的字符串都把unicode编码设为‘utf-8'也不能解决问题,具体原因如下: 1.首先查看待比较两个字符串的编码格式 ,使...

python中sort和sorted排序的实例方法

Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列。 1)排序基础 简单的升序排序是非常容易的。只需要...

Python3 利用requests 库进行post携带账号密码请求数据的方法

如下所示: import urllib,json,requests url = 'http://127.0.0.1:8000/account/login' headers = {}...

python回溯法实现数组全排列输出实例分析

本文实例讲述了python回溯法实现数组全排列输出的方法。分享给大家供大家参考。具体分析如下: 全排列解释:从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同...