用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读取ini文件、操作mysql、发送邮件实例

我是闲的没事干,2014过的太浮夸了,博客也没写几篇,哎~~~ 用这篇来记录即将逝去的2014 python对各种数据库的各种操作满大街都是,不过,我还是喜欢我这种风格的,涉及到其它操作...

python获取本机mac地址和ip地址的方法

本文实例讲述了python获取本机mac地址和ip地址的方法。分享给大家供大家参考。具体如下: import sys, socket def getipaddrs(hostname)...

Python3.6实现带有简单界面的有道翻译小程序

本人使用的是Python3.6(32bit),在win10上运行的     代码如下: from tkinter import * import url...

解决Pycharm运行时找不到文件的问题

解决Pycharm运行时找不到文件的问题

这是解释器设置问题,在设置里找到interpreter 找到合适的目录就可以了。因为重装了python导致pycharm找不到路径了。 另外,pycharm找不到tensorflow包的...

Python验证码识别的方法

本文实例讲述了Python验证码识别的方法。分享给大家供大家参考。具体实现方法如下: #encoding=utf-8 import Image,ImageEnhance,ImageF...