用Python和MD5實現網站掛馬檢測程式

來源:互聯網
上載者:User
一、程式測試
複製代碼 代碼如下:

# 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()

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.