GuardianUnivalle-Benito-Yucra 0.1.62__tar.gz → 0.1.64__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of GuardianUnivalle-Benito-Yucra might be problematic. Click here for more details.
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/detectores/detector_csrf.py +47 -44
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/detectores/detector_dos.py +1 -1
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra.egg-info/PKG-INFO +1 -1
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/PKG-INFO +1 -1
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/pyproject.toml +1 -1
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/__init__.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/auditoria/registro_auditoria.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/criptografia/cifrado_aead.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/criptografia/intercambio_claves.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/criptografia/kdf.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/detectores/detector_keylogger.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/detectores/detector_sql.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/detectores/detector_xss.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/middleware_web/middleware_web.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/mitigacion/limitador_peticion.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/mitigacion/lista_bloqueo.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/puntuacion/puntuacion_amenaza.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra/utilidades.py +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra.egg-info/SOURCES.txt +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra.egg-info/dependency_links.txt +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra.egg-info/requires.txt +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/GuardianUnivalle_Benito_Yucra.egg-info/top_level.txt +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/LICENSE +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/README.md +0 -0
- {guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/setup.cfg +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# CSRF defense (
|
|
1
|
+
# CSRF defense (versión reforzada)
|
|
2
2
|
from __future__ import annotations
|
|
3
3
|
import secrets
|
|
4
4
|
import logging
|
|
@@ -21,23 +21,24 @@ CSRF_HEADER_NAMES = ("HTTP_X_CSRFTOKEN", "HTTP_X_CSRF_TOKEN")
|
|
|
21
21
|
CSRF_COOKIE_NAME = getattr(settings, "CSRF_COOKIE_NAME", "csrftoken")
|
|
22
22
|
POST_FIELD_NAME = "csrfmiddlewaretoken"
|
|
23
23
|
|
|
24
|
-
#
|
|
25
|
-
# porque muchas APIs legítimas usan JSON.
|
|
24
|
+
# Patrón de Content-Type sospechoso
|
|
26
25
|
SUSPICIOUS_CT_PATTERNS = [
|
|
27
26
|
re.compile(r"text/plain", re.I),
|
|
28
27
|
re.compile(r"application/x-www-form-urlencoded", re.I),
|
|
29
28
|
re.compile(r"multipart/form-data", re.I),
|
|
30
29
|
]
|
|
31
30
|
|
|
32
|
-
#
|
|
31
|
+
# Parámetros sensibles típicos de CSRF
|
|
32
|
+
SENSITIVE_PARAMS = [
|
|
33
|
+
"password", "csrfmiddlewaretoken", "token", "amount", "transfer", "delete", "update"
|
|
34
|
+
]
|
|
35
|
+
|
|
33
36
|
CSRF_DEFENSE_MIN_SIGNALS = getattr(settings, "CSRF_DEFENSE_MIN_SIGNALS", 1)
|
|
34
|
-
# Opción para excluir rutas de API que manejan JSON (cambia según tu proyecto)
|
|
35
37
|
CSRF_DEFENSE_EXCLUDED_API_PREFIXES = getattr(settings, "CSRF_DEFENSE_EXCLUDED_API_PREFIXES", ["/api/"])
|
|
36
38
|
|
|
37
39
|
def get_client_ip(request):
|
|
38
40
|
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
|
|
39
41
|
if x_forwarded_for:
|
|
40
|
-
# toma la primera IP real
|
|
41
42
|
ips = [ip.strip() for ip in x_forwarded_for.split(",") if ip.strip()]
|
|
42
43
|
if ips:
|
|
43
44
|
return ips[0]
|
|
@@ -63,25 +64,22 @@ def origin_matches_host(request) -> bool:
|
|
|
63
64
|
referer = request.META.get("HTTP_REFERER", "")
|
|
64
65
|
origin_host = host_from_header(origin)
|
|
65
66
|
referer_host = host_from_header(referer)
|
|
66
|
-
#
|
|
67
|
+
# Bloquear obvious javascript: referers
|
|
67
68
|
if any(re.search(r"(javascript:|<script|data:text/html)", h or "", re.I) for h in [origin, referer]):
|
|
68
69
|
return False
|
|
69
70
|
if origin_host and origin_host == host:
|
|
70
71
|
return True
|
|
71
72
|
if referer_host and referer_host == host:
|
|
72
73
|
return True
|
|
73
|
-
# si no hay origin ni referer, lo consideramos neutral (no marcar)
|
|
74
74
|
if not origin and not referer:
|
|
75
75
|
return True
|
|
76
76
|
return False
|
|
77
77
|
|
|
78
78
|
def has_csrf_token(request) -> bool:
|
|
79
|
-
# busca header, cookie o campo form
|
|
80
79
|
for h in CSRF_HEADER_NAMES:
|
|
81
80
|
if request.META.get(h):
|
|
82
81
|
return True
|
|
83
|
-
|
|
84
|
-
if cookie_val:
|
|
82
|
+
if request.COOKIES.get(CSRF_COOKIE_NAME):
|
|
85
83
|
return True
|
|
86
84
|
try:
|
|
87
85
|
if request.method == "POST" and hasattr(request, "POST"):
|
|
@@ -106,12 +104,25 @@ def extract_payload_text(request) -> str:
|
|
|
106
104
|
parts.append(request.META.get("HTTP_REFERER", ""))
|
|
107
105
|
return " ".join([p for p in parts if p])
|
|
108
106
|
|
|
107
|
+
def extract_parameters(request) -> List[str]:
|
|
108
|
+
params = []
|
|
109
|
+
if hasattr(request, "POST"):
|
|
110
|
+
params.extend(request.POST.keys())
|
|
111
|
+
if hasattr(request, "GET"):
|
|
112
|
+
params.extend(request.GET.keys())
|
|
113
|
+
try:
|
|
114
|
+
if request.body and "application/json" in (request.META.get("CONTENT_TYPE") or ""):
|
|
115
|
+
data = json.loads(request.body)
|
|
116
|
+
params.extend(data.keys())
|
|
117
|
+
except Exception:
|
|
118
|
+
pass
|
|
119
|
+
return params
|
|
120
|
+
|
|
109
121
|
class CSRFDefenseMiddleware(MiddlewareMixin):
|
|
110
122
|
def process_request(self, request):
|
|
111
|
-
#
|
|
123
|
+
# Excluir APIs JSON si se configuró así
|
|
112
124
|
for prefix in CSRF_DEFENSE_EXCLUDED_API_PREFIXES:
|
|
113
125
|
if request.path.startswith(prefix):
|
|
114
|
-
# debug log opcional
|
|
115
126
|
logger.debug(f"[CSRFDefense] Skip analysis for API prefix {prefix} path {request.path}")
|
|
116
127
|
return None
|
|
117
128
|
|
|
@@ -130,6 +141,7 @@ class CSRFDefenseMiddleware(MiddlewareMixin):
|
|
|
130
141
|
|
|
131
142
|
descripcion: List[str] = []
|
|
132
143
|
payload = extract_payload_text(request)
|
|
144
|
+
params = extract_parameters(request)
|
|
133
145
|
|
|
134
146
|
# 1) Falta token CSRF
|
|
135
147
|
if not has_csrf_token(request):
|
|
@@ -139,24 +151,33 @@ class CSRFDefenseMiddleware(MiddlewareMixin):
|
|
|
139
151
|
if not origin_matches_host(request):
|
|
140
152
|
descripcion.append("Origin/Referer no coinciden con Host (posible cross-site)")
|
|
141
153
|
|
|
142
|
-
# 3) Content-Type sospechoso
|
|
154
|
+
# 3) Content-Type sospechoso
|
|
143
155
|
content_type = (request.META.get("CONTENT_TYPE") or "")
|
|
144
156
|
for patt in SUSPICIOUS_CT_PATTERNS:
|
|
145
157
|
if patt.search(content_type):
|
|
146
158
|
descripcion.append(f"Content-Type sospechoso: {content_type}")
|
|
147
159
|
break
|
|
148
160
|
|
|
149
|
-
# 4) Referer ausente y sin
|
|
161
|
+
# 4) Referer ausente y sin token CSRF
|
|
150
162
|
referer = request.META.get("HTTP_REFERER", "")
|
|
151
163
|
if not referer and not any(request.META.get(h) for h in CSRF_HEADER_NAMES):
|
|
152
164
|
descripcion.append("Referer ausente y sin X-CSRFToken")
|
|
153
165
|
|
|
154
|
-
#
|
|
166
|
+
# 5) Parámetros sensibles en GET/JSON
|
|
167
|
+
for p in params:
|
|
168
|
+
if p.lower() in SENSITIVE_PARAMS and method == "GET":
|
|
169
|
+
descripcion.append(f"Parámetro sensible '{p}' enviado en GET (posible CSRF)")
|
|
170
|
+
|
|
171
|
+
# 6) JSON sospechoso desde dominio externo
|
|
172
|
+
if "application/json" in content_type:
|
|
173
|
+
origin = request.META.get("HTTP_ORIGIN") or ""
|
|
174
|
+
if origin and host_from_header(origin) != (request.META.get("HTTP_HOST") or "").split(":")[0]:
|
|
175
|
+
descripcion.append("JSON POST desde origen externo (posible CSRF)")
|
|
176
|
+
|
|
177
|
+
# Señales >= umbral => marcar
|
|
155
178
|
if descripcion and len(descripcion) >= CSRF_DEFENSE_MIN_SIGNALS:
|
|
156
179
|
w_csrf = getattr(settings, "CSRF_DEFENSE_WEIGHT", 0.2)
|
|
157
|
-
|
|
158
|
-
s_csrf = w_csrf * intentos_csrf
|
|
159
|
-
|
|
180
|
+
s_csrf = w_csrf * len(descripcion)
|
|
160
181
|
request.csrf_attack_info = {
|
|
161
182
|
"ip": client_ip,
|
|
162
183
|
"tipos": ["CSRF"],
|
|
@@ -164,40 +185,22 @@ class CSRFDefenseMiddleware(MiddlewareMixin):
|
|
|
164
185
|
"payload": payload,
|
|
165
186
|
"score": s_csrf,
|
|
166
187
|
}
|
|
167
|
-
|
|
168
188
|
logger.warning(
|
|
169
189
|
"CSRF detectado desde IP %s: %s ; path=%s ; Content-Type=%s ; score=%.2f",
|
|
170
190
|
client_ip, descripcion, request.path, content_type, s_csrf
|
|
171
191
|
)
|
|
172
192
|
else:
|
|
173
|
-
# debug útil: saber por qué NO se marcó
|
|
174
193
|
if descripcion:
|
|
175
194
|
logger.debug(f"[CSRFDefense] low-signals ({len(descripcion)}) not marking: {descripcion}")
|
|
176
195
|
|
|
177
196
|
return None
|
|
178
197
|
|
|
179
198
|
"""
|
|
180
|
-
CSRF Defense Middleware
|
|
181
|
-
|
|
182
|
-
Detecta
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
* Integración con detección XSS/SQL Injection mediante registro unificado.
|
|
188
|
-
|
|
189
|
-
Fórmula de amenaza:
|
|
190
|
-
S_csrf = w_csrf * intentos_csrf
|
|
191
|
-
S_csrf = 0.2 * 1
|
|
192
|
-
"""
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
"""
|
|
196
|
-
Algoritmos relacionados:
|
|
197
|
-
*Uso de secreto aleatorio criptográfico.
|
|
198
|
-
*Opcionalmente derivación con PBKDF2 / Argon2 para reforzar token.
|
|
199
|
-
Contribución a fórmula de amenaza S:
|
|
200
|
-
S_csrf = w_csrf * intentos_csrf
|
|
201
|
-
S_csrf = 0.2 * 1
|
|
202
|
-
donde w_csrf es peso asignado a CSRF y intentos_csrf es la cantidad de intentos detectados.
|
|
199
|
+
CSRF Defense Middleware - Reforzado
|
|
200
|
+
===================================
|
|
201
|
+
- Detecta múltiples categorías de CSRF: clásico, login, logout, password change, file/action, JSON API.
|
|
202
|
+
- Escanea payloads POST, GET y JSON.
|
|
203
|
+
- Detecta parámetros sensibles enviados en GET o JSON desde origen externo.
|
|
204
|
+
- Scoring configurable y logging detallado.
|
|
205
|
+
- Fácil integración con auditoría XSS/SQLi.
|
|
203
206
|
"""
|
|
@@ -59,7 +59,7 @@ def fetch_and_parse_blacklists() -> Set[str]:
|
|
|
59
59
|
|
|
60
60
|
for url in IP_BLACKLIST_SOURCES:
|
|
61
61
|
try:
|
|
62
|
-
response = requests.get(url, headers=SCRAPING_HEADERS, timeout=
|
|
62
|
+
response = requests.get(url, headers=SCRAPING_HEADERS, timeout=40)
|
|
63
63
|
response.raise_for_status()
|
|
64
64
|
|
|
65
65
|
found_ips = ip_pattern.findall(response.text)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: GuardianUnivalle-Benito-Yucra
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.64
|
|
4
4
|
Summary: Middleware y detectores de seguridad (SQLi, XSS, CSRF, DoS, Keylogger) para Django/Flask
|
|
5
5
|
Author-email: Andres Benito Calle Yucra <benitoandrescalle035@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: GuardianUnivalle-Benito-Yucra
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.64
|
|
4
4
|
Summary: Middleware y detectores de seguridad (SQLi, XSS, CSRF, DoS, Keylogger) para Django/Flask
|
|
5
5
|
Author-email: Andres Benito Calle Yucra <benitoandrescalle035@gmail.com>
|
|
6
6
|
License: MIT
|
{guardianunivalle_benito_yucra-0.1.62 → guardianunivalle_benito_yucra-0.1.64}/pyproject.toml
RENAMED
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "GuardianUnivalle-Benito-Yucra" # usar mayúsculas consistente
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.64"
|
|
8
8
|
description = "Middleware y detectores de seguridad (SQLi, XSS, CSRF, DoS, Keylogger) para Django/Flask"
|
|
9
9
|
authors = [
|
|
10
10
|
{ name = "Andres Benito Calle Yucra", email = "benitoandrescalle035@gmail.com" }
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|