-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweakGrepper.py
More file actions
executable file
·70 lines (59 loc) · 2.35 KB
/
weakGrepper.py
File metadata and controls
executable file
·70 lines (59 loc) · 2.35 KB
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
67
68
69
70
#!/usr/bin/env python3
import os
import re
from termcolor import colored
import constants
import sys
def searchAll(regs, data):
matches = []
for r in regs:
if type(r) is str:
r = r.encode()
mo = re.findall(r, data, re.IGNORECASE)
else:
# it was case-sensitive
mo = re.findall(r, data)
if len(mo) > 0:
for m in mo:
m = m.decode()
if m not in matches:
matches.append(m)
if len(matches) > 0:
return matches
def searchWeak(key, dir):
print(colored('Searching for {}...'.format(key), 'green'))
for (dirpath, dirnames, filenames) in os.walk(dir, followlinks=True):
for names in filenames:
path = '{}/{}'.format(dirpath, names)
if os.path.isfile(path):
try:
with open(path, 'rb') as f:
data = f.read()
# break when one family has matched for this file
for patterFamily in constants.PATTERNS[key]:
res = searchAll(patterFamily[1], data)
if res is not None:
for m in res:
m = "\t{} : Found pattern '{}'".format(path, m)
sev = patterFamily[0]
if sev == constants.severity.INFO:
print(colored(m, 'green'))
elif sev == constants.severity.WARNING:
print(colored(m, 'yellow'))
elif sev == constants.severity.CRITIC:
print(colored(m, 'red'))
else:
print(m)
break
except PermissionError:
print("Skipping, {} : Permission denied".format(path))
if __name__ == "__main__":
if len(sys.argv) != 2:
print('Usage : {} <dir>'.format(sys.argv[0]))
sys.exit(0)
dir = sys.argv[1]
if not os.path.isdir(dir):
print(colored("The directory specified doesn't exist", 'red'))
sys.exit(0)
for key in constants.PATTERNS:
searchWeak(key, dir)