-
Notifications
You must be signed in to change notification settings - Fork 12
/
static_scan.py
66 lines (52 loc) · 1.97 KB
/
static_scan.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/python
import fnmatch
import os
import glob
import re
class StaticPhpScanner:
def __init__(self, folder=None, recursive=False):
""" Constructor
"""
self.folder = folder
self.recursive = recursive
self.patterns = ['\$_(?P<method>(GET|POST|COOKIE))\[(\'|")(?P<var>[a-z0-9-_:]+)(\'|")\]',
'\$_(?P<method>SERVER)\[(\'|")(?P<var>(HTTP_REFERER|HTTP_USER_AGENT))(\'|")\]']
def searchFiles(self, folder, recursive):
""" Search php files in folder
"""
files = []
if recursive:
for root, dirnames, filenames in os.walk(folder):
for filename in fnmatch.filter(filenames, '*.php'):
files.append(os.path.join(root, filename))
else:
files = glob.glob("%s/*.php" % folder)
return files
def parsePages(self, files):
""" Find entrypoint in files
"""
result = {}
compiledPatterns = []
for pattern in self.patterns:
regexp = re.compile(pattern, re.IGNORECASE)
compiledPatterns.append(regexp)
for file in files:
result[file] = []
with open(file, 'r') as lines:
line_number = 0
for line in lines:
line_number = line_number + 1
for pattern in compiledPatterns:
for m in pattern.finditer(line):
found = m.groupdict()
result[file].append({'line': line_number,
'method': found['method'],
'var': found['var']})
if len(result[file]) == 0:
result.pop(file, None)
return result
def scan(self, output):
""" Begin file scanning
"""
files = self.searchFiles(self.folder, self.recursive)
return self.parsePages(files)