利用 PyInotify 來監(jiān)控 Web 目錄 WebShell


源碼 : https://github.com/seb-m/pyinotify
文檔 : http://seb.dbzteam.org/pyinotify/
參考腳本 : https://github.com/WangYihang/Attack_Defense_Framework/blob/master/watch.py


#!/usr/bin/env python
# encoding:utf-8

import sys
import pyinotify
import os

def detect_waf(pathname):
    try:
        with open(pathname) as f:
            content = f.read()
            # tags
            black_list = ["<?", "<%"]
            # code execute
            black_list += ['eval', 'assert']
            # command execute
            black_list += ['passthru', 'exec', 'system', 'shell_exec', 'popen', 'proc_open']
            # read file
            black_list += ['hightlight_file', 'show_source', 'php_strip_whitespace', 'file_get_contents', 'readfile', 'file', 'fopen', 'fread', 'include', 'include_once', 'require', 'require_once', 'fread', 'fgets', 'fpassthru', 'fgetcsv', 'fgetss', 'fscanf', 'parse_ini_file']
            # read directory
            black_list += ['glob', 'opendir', 'dir', 'readdir', 'scandir']
            FLAG = False
            for black in black_list:
                if black in content:
                    print "[!] Dangerous php script! (%s)" % (black)
                    print "[*] Content : "
                    print content.rstrip("\n")
                    FLAG = True
                    break
            if FLAG:
                os.remove(pathname)
    except Exception as e:
        print "[-] %s" % (str(e))

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        if event.dir:
            print "Create Directory : %s" % (event.pathname)
        else:
            print "Create File : %s" % (event.pathname)

    def process_IN_DELETE(self, event):
        if event.dir:
            print "Delete Directory : %s" % (event.pathname)
        else:
            print "Delete File : %s" % (event.pathname)

    def process_IN_CLOSE_WRITE(self, event):
        if event.dir:
            print "Close Writable Directory : %s" % (event.pathname)
        else:
            print "Close Writable File : %s" % (event.pathname)
            detect_waf(event.pathname)

def main():
    if len(sys.argv) != 2:
        print "Usage : "
        print "\tpython %s [PATH]" % (sys.argv[0])
        exit(1)
    path = sys.argv[1]
    wm = pyinotify.WatchManager()
    wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True)
    eh = EventHandler()
    notifier = pyinotify.Notifier(wm, eh)
    notifier.loop()

if __name__ == "__main__":
    main()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 太長了,還是轉(zhuǎn)載吧...今天在看博客的時候,無意中發(fā)現(xiàn)了@Trinea在GitHub上的一個項(xiàng)目Android開源...
    龐哈哈哈12138閱讀 20,398評論 3 283
  • 每個人對于小時候的特殊情感,一定都有一個特別的載體可以寄托。 好友Lily和我說,她現(xiàn)在想起小時候,總是會首先想起...
    良子小姐的簡書ID閱讀 580評論 0 0
  • 你走向海水,你說 你上班去了 用刻滿星星的語言,說 一個眼巴巴看著自己的男人 一個糟踐日子,又被日子粘著的人 海水...
    彭先生10閱讀 395評論 0 2
  • 文/劉彩霞 (十二) “跟誰生氣呀?誰讓你當(dāng)頭了。欲戴王冠,必承其重。有招自己想唄?!?一個...
    彩霞漫天閱讀 227評論 0 0
  • 前情回顧 時光定格在2000年的9月1日,大學(xué)新生報到的日子。 南京郵電大學(xué)的校園,一改假期的寧靜,迎來了許多新面...
    挪威陽光閱讀 421評論 0 2

友情鏈接更多精彩內(nèi)容