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

yipeiwu_com6年前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实现SMTP邮件发送功能

一直想着给框架添加邮件发送功能、所以整理下python下邮件发送功能 首先python是支持邮件的发送、内置smtp库、支持发送纯文本、HTML及添加附件的邮件。之后是邮箱、像163、q...

python 按不同维度求和,最值,均值的实例

python 按不同维度求和,最值,均值的实例

当变量维数加大时很难想象是怎样按不同维度求和的,高清楚一个,其他的应该就很清楚了,什么都不说了,上例子,例子一看便明白….. a=range(27) a=np.array(a) a=...

python 实现二维列表转置

python 二维列表转置 def transpose(self, matrix): new_matrix = [] for i in range(len(matri...

将matplotlib绘图嵌入pyqt的方法示例

我的终极整理,供参考 # coding:utf-8 import matplotlib # 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends...

python dataframe NaN处理方式

将dataframe中的NaN替换成希望的值 import pandas as pd df1 = pd.DataFrame([{'col1':'a', 'col2':1}, {'co...