import os
import sys
import time
import hashlib
from multiprocessing.dummy import Pool as ThreadPool
def File_Search(filename):
for lists in os.listdir(filename):
path = os.path.join(filename, lists)
if os.path.isfile(path):
File_Hash(path)
if os.path.isdir(path):
File_Search(path)
return True
if __name__=='__main__':
start = time.clock()
if File_Search('/usr/bin') and File_Search('/usr/sbin') and File_Search('/bin') and File_Search('/var/www/htdocs'):
end = time.clock()
print '文件MD5哈希计算完毕,共运行时长:' + str(end - start)
import os
import hashlib
import sys
from multiprocessing.dummy import Pool as ThreadPool
import smtplib
from email.mime.text import MIMEText
import json
import string
hash_list = []
result = []
def File_Search(filename):
for lists in os.listdir(filename):
path = os.path.join(filename, lists)
if os.path.isfile(path):
File_Hash(path)
if os.path.isdir(path):
File_Search(path)
return True
def Hash_Comp():
fp = open('/var/www/sec/file_monitor.txt').readlines()
for line in hash_list:
if line not in fp: #File is created、updated、changed
result.append(str(line).strip()+"\n")
for line1 in fp: #File is deleted
if line1 not in hash_list:
result.append(str(line).strip()+"\n")
if __name__=='__main__':
if File_Search('/usr/bin') and File_Search('/usr/sbin') and File_Search('/bin') and File_Search('/var/www/htdocs'):
if Hash_Comp():
if result:
Content = ''.join(result)
Send_Mail(Content)
else:
sys.exit(0)