思路
需要實現準備一份未受感染的源代碼和一份可能受感染的源代碼,然后運行以下腳本,就能找出到底哪些文件被掛馬了。
其中,主要是根據比對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:])
新聞熱點
疑難解答