forked from mo-xiaoxi/Attack_Defense_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.py
66 lines (59 loc) · 2.37 KB
/
watch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# encoding:utf-8
import sys
import pyinotify
import os
import time
def detect_waf(pathname):
try:
with open(pathname) as f:
content = f.read()
black_list = ["<?", "<%"]
black_list += ['eval', 'assert']
black_list += ['passthru', 'exec', 'system', 'shell_exec', 'popen', 'proc_open']
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']
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:
target_path = "webshells/%s.log" % (time.strftime('%Y-%m-%d-%H:%M:%S',time.localtime(time.time())))
print "[+] Detect webshell , moving from %s to %s" % (pathname, target_path)
os.rename(pathname, target_path)
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()