forked from AnkitSinghGTHB/hybrid-recommender
-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathscan.py
More file actions
38 lines (35 loc) · 1.35 KB
/
Copy pathscan.py
File metadata and controls
38 lines (35 loc) · 1.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
import os
import pathlib
import re
PATTERNS = {
'eval/exec': r'\b(eval|exec)\s*\(',
'subprocess': r'\b(subprocess\.|os\.system|popen)',
'sql_injection': r'(execute|query)\s*\([^,]*%[^,]*\)|(execute|query)\s*\([^,]*\+[^,]*\)|(execute|query)\s*\([^\)]*f".*\{.*\}',
'pickle': r'\bpickle\.loads?\(',
'yaml': r'\byaml\.load\(',
'path_traversal': r'open\s*\(\s*f["\'].*\{',
'open_redirect': r'(RedirectResponse|redirect)\s*\(\s*[a-zA-Z_]',
'ssrf': r'requests\.(get|post|put|delete|request)\s*\(\s*[a-zA-Z_]',
'csrf': r'csrf',
'jwt_secret': r'jwt\.encode',
'cors': r'CORSMiddleware',
'xxe': r'xml\.etree'
}
def scan_dir(d):
for root, _, files in os.walk(d):
for f in files:
if not f.endswith('.py'):
continue
path = os.path.join(root, f)
try:
with open(path, 'r', encoding='utf-8') as file:
content = file.readlines()
for i, line in enumerate(content):
for name, pat in PATTERNS.items():
if re.search(pat, line, re.IGNORECASE):
print(f"{path}:{i+1}:{name} -> {line.strip()}")
except Exception as e:
pass
if __name__ == '__main__':
repo_root = pathlib.Path(__file__).parent
scan_dir(str(repo_root))