国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實例分享:快速查找出被掛馬的文件

2019-11-25 18:23:37
字體:
來源:轉載
供稿:網友

思路

需要實現準備一份未受感染的源代碼和一份可能受感染的源代碼,然后運行以下腳本,就能找出到底哪些文件被掛馬了。

其中,主要是根據比對2份文件的md5值來過濾可能被掛馬的文件(確切的說應該是被修改過的文件)

Python腳本

復制代碼 代碼如下:

__author__ = 'Flying'
#coding:utf-8
#Date:2014.6.5
#檢測修改過的文件
import os,sys,hashlib,datetime
global_DirOld = ""
global_DirNew = ""
global_FilesList = []
#輸入要比對的文件路徑
def InputDirPath():
    global global_DirOld,global_DirNew
    global_DirOld = unicode(raw_input("請輸入備份文件所在目錄:"),"utf-8")
    while not os.path.exists(global_DirOld):
        print  u"指定的路徑不存在,請重新輸入"
        global_DirOld = unicode(raw_input("請輸入備份文件所在目錄:"),"utf-8")
    global_DirNew = unicode(raw_input("請輸入要檢測文件的目錄:"),"utf-8")
    while not os.path.exists(global_DirNew):
        print  u"指定的路徑不存在,請重新輸入"
        global_DirNew = unicode(raw_input("請輸入要檢測文件的目錄:"),"utf-8")

#將數據保存到文件中
def SaveToFile(filePath,content):
    try:
        f = open(filePath,"a+")
        f.write(content.encode("utf-8") + "/n")
        f.close()
    except Exception,ex:
        print "Error:" + str(ex)

#計算文件的MD5值
def CalcMD5(filepath):
    try:
        #以二進制的形式打開
        with open(filepath,'rb') as f:
            md5obj = hashlib.md5()
            md5obj.update(f.read())
            hash = md5obj.hexdigest()
            return hash
    except Exception,ex:
        print "Error:" + str(ex)
        return None

#遍歷目錄下的所有文件
def GetAllSubFiles():
    global global_FilesList
    for dir in os.walk(global_DirNew):
        for file in dir[2]:
            filePath = dir[0] + os.sep + file
            global_FilesList.append(filePath[len(global_DirNew)+1:])

#列出新增文件和變動的文件
def ListChangedFiles():
    global global_DirOld,global_DirNew,global_FilesList
    print u"變動或新增的文件:"
    for file in global_FilesList:
        filePathOld = global_DirOld + os.sep + file
        filePathNew = global_DirNew + os.sep + file
        if not os.path.exists(filePathOld) or CalcMD5(filePathOld)!=CalcMD5(filePathNew):
            content = "[" + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+ "]" + filePathNew
            print content
            SaveToFile("ChangedFiles.txt",content)

if __name__=="__main__":
    InputDirPath()
    GetAllSubFiles()
    ListChangedFiles()

腳本執行結果


 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通江县| 五指山市| 剑阁县| 金寨县| 图木舒克市| 抚宁县| 长沙市| 道真| 台南县| 东乌珠穆沁旗| 九龙城区| 台中县| 浪卡子县| 巴彦淖尔市| 开远市| 洛宁县| 东乌珠穆沁旗| 康保县| 巫山县| 内黄县| 云南省| 融水| 安丘市| 都匀市| 剑川县| 十堰市| 中山市| 嘉黎县| 安康市| 广河县| 井陉县| 东港市| 平利县| 鄱阳县| 精河县| 二连浩特市| 时尚| 哈巴河县| 淮南市| 静安区| 建平县|