-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsecurity.py
More file actions
304 lines (244 loc) · 9.09 KB
/
Copy pathsecurity.py
File metadata and controls
304 lines (244 loc) · 9.09 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
Sistema de segurança para o BNVD
Proteção contra SQL injection, XSS e outras vulnerabilidades
"""
import re
import html
import logging
from typing import Optional, Any, Dict, List
from markupsafe import escape
class SecurityManager:
"""Gerenciador de segurança para validação e sanitização de entrada"""
# Padrões de CVE válidos
CVE_PATTERN = re.compile(r'^CVE-\d{4}-\d{4,}$')
# Padrões suspeitos de SQL injection
SQL_INJECTION_PATTERNS = [
r'(\bor\b|\band\b)\s+\d+\s*=\s*\d+',
r'union\s+select',
r'insert\s+into',
r'delete\s+from',
r'drop\s+table',
r'update\s+set',
r'create\s+table',
r'alter\s+table',
r'exec\s*\(',
r'execute\s*\(',
r'sp_\w+',
r'xp_\w+',
r'--\s*$',
r'/\*.*\*/',
r';\s*(drop|delete|insert|update|create|alter)',
r'(\'|\")(\s*;\s*)*(drop|delete|insert|update|create|alter)',
r'(\bor\b|\band\b)\s+(\'|\")?\w*(\'|\")?\s*=\s*(\'|\")?\w*(\'|\")?'
]
# Padrões suspeitos de XSS
XSS_PATTERNS = [
r'<script[^>]*>.*?</script>',
r'javascript:',
r'on\w+\s*=',
r'<iframe[^>]*>',
r'<object[^>]*>',
r'<embed[^>]*>',
r'<form[^>]*>',
r'<meta[^>]*>',
r'<link[^>]*>',
r'<style[^>]*>.*?</style>',
r'<base[^>]*>',
r'vbscript:',
r'data:text/html',
r'expression\s*\(',
r'url\s*\(',
r'@import'
]
def __init__(self):
"""Inicializa o gerenciador de segurança"""
self.sql_regex = [re.compile(pattern, re.IGNORECASE) for pattern in self.SQL_INJECTION_PATTERNS]
self.xss_regex = [re.compile(pattern, re.IGNORECASE) for pattern in self.XSS_PATTERNS]
def validate_cve_id(self, cve_id: str) -> bool:
"""
Valida se o CVE ID está no formato correto
Args:
cve_id: ID da vulnerabilidade
Returns:
True se válido, False caso contrário
"""
if not cve_id or not isinstance(cve_id, str):
return False
return bool(self.CVE_PATTERN.match(cve_id.upper()))
def sanitize_input(self, text: str, max_length: int = 1000) -> str:
"""
Sanitiza entrada do usuário para prevenir XSS
Args:
text: Texto a ser sanitizado
max_length: Tamanho máximo permitido
Returns:
Texto sanitizado
"""
if not text or not isinstance(text, str):
return ""
# Truncar se muito longo
if len(text) > max_length:
text = text[:max_length]
# Escapar HTML para prevenir XSS
text = html.escape(text, quote=True)
# Remover caracteres de controle
text = ''.join(char for char in text if ord(char) >= 32 or char in '\n\r\t')
return text.strip()
def detect_sql_injection(self, text: str) -> bool:
"""
Detecta possíveis tentativas de SQL injection
Args:
text: Texto a ser analisado
Returns:
True se suspeito, False caso contrário
"""
if not text or not isinstance(text, str):
return False
text_lower = text.lower()
# Verificar padrões suspeitos
for regex in self.sql_regex:
if regex.search(text_lower):
logging.warning(f"Possível SQL injection detectado: {text}")
return True
return False
def detect_xss(self, text: str) -> bool:
"""
Detecta possíveis tentativas de XSS
Args:
text: Texto a ser analisado
Returns:
True se suspeito, False caso contrário
"""
if not text or not isinstance(text, str):
return False
# Verificar padrões suspeitos
for regex in self.xss_regex:
if regex.search(text):
logging.warning(f"Possível XSS detectado: {text}")
return True
return False
def validate_year(self, year: Any) -> Optional[int]:
"""
Valida e converte ano para inteiro
Args:
year: Ano a ser validado
Returns:
Ano válido ou None
"""
if not year:
return None
try:
year_int = int(year)
if 1999 <= year_int <= 2030: # Range razoável para CVEs
return year_int
except (ValueError, TypeError):
pass
return None
def validate_page_number(self, page: Any) -> int:
"""
Valida número da página
Args:
page: Número da página
Returns:
Número da página válido (mínimo 1)
"""
try:
page_int = int(page)
return max(1, page_int)
except (ValueError, TypeError):
return 1
def validate_per_page(self, per_page: Any, max_limit: int = 100) -> int:
"""
Valida número de resultados por página
Args:
per_page: Número de resultados por página
max_limit: Limite máximo permitido
Returns:
Número válido de resultados por página
"""
try:
per_page_int = int(per_page)
return min(max(1, per_page_int), max_limit)
except (ValueError, TypeError):
return 20
def validate_severity(self, severity: str) -> Optional[str]:
"""
Valida severidade CVSS
Args:
severity: Severidade a ser validada
Returns:
Severidade válida ou None
"""
if not severity or not isinstance(severity, str):
return None
valid_severities = ['LOW', 'MEDIUM', 'HIGH', 'CRITICAL']
severity_upper = severity.upper()
if severity_upper in valid_severities:
return severity_upper
return None
def validate_search_query(self, query: str, max_length: int = 200) -> Optional[str]:
"""
Valida query de busca
Args:
query: Query de busca
max_length: Tamanho máximo
Returns:
Query sanitizada ou None se suspeita
"""
if not query or not isinstance(query, str):
return None
# Detectar ataques
if self.detect_sql_injection(query) or self.detect_xss(query):
return None
# Sanitizar
sanitized = self.sanitize_input(query, max_length)
# Verificar se não está vazia após sanitização
if not sanitized:
return None
return sanitized
def secure_request_params(self, request_args: Dict[str, Any]) -> Dict[str, Any]:
"""
Sanitiza todos os parâmetros de uma requisição
Args:
request_args: Dicionário com parâmetros da requisição
Returns:
Dicionário com parâmetros sanitizados
"""
secure_params = {}
for key, value in request_args.items():
if not isinstance(key, str) or not value:
continue
# Sanitizar key
safe_key = self.sanitize_input(key, 50)
if not safe_key:
continue
# Processar diferentes tipos de valores
if key.lower() == 'cve_id':
if self.validate_cve_id(str(value)):
secure_params[safe_key] = str(value).upper()
elif key.lower() == 'year':
valid_year = self.validate_year(value)
if valid_year:
secure_params[safe_key] = valid_year
elif key.lower() == 'page':
secure_params[safe_key] = self.validate_page_number(value)
elif key.lower() == 'per_page':
secure_params[safe_key] = self.validate_per_page(value)
elif key.lower() == 'severity':
valid_severity = self.validate_severity(str(value))
if valid_severity:
secure_params[safe_key] = valid_severity
elif key.lower() in ['search', 'keyword', 'query', 'vendor']:
safe_value = self.validate_search_query(str(value))
if safe_value:
secure_params[safe_key] = safe_value
elif key.lower() == 'include_pt':
secure_params[safe_key] = str(value).lower() == 'true'
else:
# Para outros parâmetros, aplicar sanitização básica
safe_value = self.sanitize_input(str(value), 100)
if safe_value:
secure_params[safe_key] = safe_value
return secure_params
# Instância global do gerenciador de segurança
security_manager = SecurityManager()