GuardianUnivalle-Benito-Yucra 0.1.4__tar.gz → 0.1.6__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.6/GuardianUnivalle_Benito_Yucra/detectores/detector_sql.py +113 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra.egg-info/PKG-INFO +1 -1
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/PKG-INFO +1 -1
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/pyproject.toml +1 -1
- guardianunivalle_benito_yucra-0.1.4/GuardianUnivalle_Benito_Yucra/detectores/detector_sql.py +0 -89
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/__init__.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/auditoria/registro_auditoria.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/criptografia/cifrado_aead.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/criptografia/intercambio_claves.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/criptografia/kdf.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/detectores/detector_csrf.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/detectores/detector_dos.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/detectores/detector_keylogger.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/detectores/detector_xss.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/middleware_web/middleware_web.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/mitigacion/limitador_peticion.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/mitigacion/lista_bloqueo.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/puntuacion/puntuacion_amenaza.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra/utilidades.py +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra.egg-info/SOURCES.txt +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra.egg-info/dependency_links.txt +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra.egg-info/requires.txt +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/GuardianUnivalle_Benito_Yucra.egg-info/top_level.txt +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/LICENSE +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/README.md +0 -0
- {guardianunivalle_benito_yucra-0.1.4 → guardianunivalle_benito_yucra-0.1.6}/setup.cfg +0 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import json
|
|
3
|
+
import time
|
|
4
|
+
from typing import Tuple
|
|
5
|
+
from django.http import JsonResponse
|
|
6
|
+
from django.utils.deprecation import MiddlewareMixin
|
|
7
|
+
|
|
8
|
+
# ---------- Patrones de ataques SQL ----------
|
|
9
|
+
PATTERNS = [
|
|
10
|
+
(
|
|
11
|
+
re.compile(r"\bunion\b\s+(all\s+)?\bselect\b", re.I),
|
|
12
|
+
"Intento de UNION SELECT detectado",
|
|
13
|
+
),
|
|
14
|
+
(
|
|
15
|
+
re.compile(r"\bselect\b.*\bfrom\b.*\bwhere\b.*\b(or|and)\b.*=", re.I),
|
|
16
|
+
"Intento de SELECT con OR/AND detectado",
|
|
17
|
+
),
|
|
18
|
+
(
|
|
19
|
+
re.compile(r"\b(or|and)\s+\d+\s*=\s*\d+", re.I),
|
|
20
|
+
"Intento de OR/AND 1=1 detectado",
|
|
21
|
+
),
|
|
22
|
+
(
|
|
23
|
+
re.compile(r"\b(drop|truncate|delete|insert|update)\b", re.I),
|
|
24
|
+
"Intento de manipulación de tabla detectado",
|
|
25
|
+
),
|
|
26
|
+
(
|
|
27
|
+
re.compile(r"(--|#|;)", re.I),
|
|
28
|
+
"Comentario o terminador de sentencia sospechoso detectado",
|
|
29
|
+
),
|
|
30
|
+
(re.compile(r"exec\s*\(", re.I), "Intento de ejecución de procedimiento detectado"),
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
# ---------- Bloqueo temporal por IP ----------
|
|
34
|
+
TEMP_BLOCK = {} # {ip: timestamp}
|
|
35
|
+
BLOCK_DURATION = 30 # segundos
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# ---------- Helpers ----------
|
|
39
|
+
def extract_payload_text(request) -> str:
|
|
40
|
+
"""Extrae todo el contenido que podría contener inyecciones"""
|
|
41
|
+
parts = []
|
|
42
|
+
# Query params
|
|
43
|
+
if request.META.get("QUERY_STRING"):
|
|
44
|
+
parts.append(request.META.get("QUERY_STRING"))
|
|
45
|
+
# Body
|
|
46
|
+
try:
|
|
47
|
+
content_type = request.META.get("CONTENT_TYPE", "")
|
|
48
|
+
if "application/json" in content_type:
|
|
49
|
+
body_json = json.loads(request.body.decode("utf-8") or "{}")
|
|
50
|
+
parts.append(json.dumps(body_json))
|
|
51
|
+
else:
|
|
52
|
+
parts.append(request.body.decode("utf-8", errors="ignore"))
|
|
53
|
+
except Exception:
|
|
54
|
+
pass
|
|
55
|
+
# Headers
|
|
56
|
+
parts.append(request.META.get("HTTP_USER_AGENT", ""))
|
|
57
|
+
parts.append(request.META.get("HTTP_REFERER", ""))
|
|
58
|
+
return " ".join([p for p in parts if p])
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def detect_sqli_text(text: str) -> Tuple[bool, list]:
|
|
62
|
+
"""Detecta ataques SQL en el texto"""
|
|
63
|
+
matches = []
|
|
64
|
+
for patt, message in PATTERNS:
|
|
65
|
+
if patt.search(text):
|
|
66
|
+
matches.append(message)
|
|
67
|
+
return (len(matches) > 0, matches)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def get_client_ip(request):
|
|
71
|
+
"""Obtiene la IP del cliente"""
|
|
72
|
+
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
|
|
73
|
+
if x_forwarded_for:
|
|
74
|
+
ip = x_forwarded_for.split(",")[0]
|
|
75
|
+
else:
|
|
76
|
+
ip = request.META.get("REMOTE_ADDR")
|
|
77
|
+
return ip
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# ---------- Middleware ----------
|
|
81
|
+
class SQLIDefenseStrongMiddleware(MiddlewareMixin):
|
|
82
|
+
def process_request(self, request):
|
|
83
|
+
client_ip = get_client_ip(request)
|
|
84
|
+
|
|
85
|
+
# Revisa si la IP está temporalmente bloqueada
|
|
86
|
+
if client_ip in TEMP_BLOCK:
|
|
87
|
+
if time.time() - TEMP_BLOCK[client_ip] < BLOCK_DURATION:
|
|
88
|
+
return JsonResponse(
|
|
89
|
+
{
|
|
90
|
+
"detail": "Acceso temporalmente bloqueado por actividad sospechosa",
|
|
91
|
+
"alerts": ["IP bloqueada temporalmente"],
|
|
92
|
+
},
|
|
93
|
+
status=403,
|
|
94
|
+
)
|
|
95
|
+
else:
|
|
96
|
+
del TEMP_BLOCK[client_ip]
|
|
97
|
+
|
|
98
|
+
text = extract_payload_text(request)
|
|
99
|
+
if not text:
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
flagged, matches = detect_sqli_text(text)
|
|
103
|
+
if flagged:
|
|
104
|
+
# Bloquea temporalmente la IP
|
|
105
|
+
TEMP_BLOCK[client_ip] = time.time()
|
|
106
|
+
return JsonResponse(
|
|
107
|
+
{
|
|
108
|
+
"detail": "Request bloqueado: posible intento de inyección SQL detectado",
|
|
109
|
+
"alerts": matches,
|
|
110
|
+
},
|
|
111
|
+
status=403,
|
|
112
|
+
)
|
|
113
|
+
return None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: GuardianUnivalle-Benito-Yucra
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.6
|
|
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.6
|
|
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
|
|
@@ -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.6"
|
|
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" }
|
guardianunivalle_benito_yucra-0.1.4/GuardianUnivalle_Benito_Yucra/detectores/detector_sql.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
# middleware_sql_defense.py
|
|
2
|
-
import re
|
|
3
|
-
import json
|
|
4
|
-
from typing import Tuple
|
|
5
|
-
from django.http import JsonResponse
|
|
6
|
-
from django.utils.deprecation import MiddlewareMixin
|
|
7
|
-
|
|
8
|
-
# ---------- Patrones y normalización ----------
|
|
9
|
-
_literal_single = re.compile(r"'([^'\\]|\\.)*'")
|
|
10
|
-
_literal_double = re.compile(r'"([^"\\]|\\.)*"')
|
|
11
|
-
_comment_sql = re.compile(r"(--[^\n]*|/\*.*?\*/)", re.S)
|
|
12
|
-
|
|
13
|
-
PATTERNS = [
|
|
14
|
-
(re.compile(r"\bunion\b\s+(all\s+)?\bselect\b", re.I), 0.95),
|
|
15
|
-
(re.compile(r"(?<!\w)or(?=\s+1=1)", re.I), 0.99),
|
|
16
|
-
(re.compile(r"\b(select\b.*\bfrom\b.*\bwhere\b.*\b(or|and)\b.*=)", re.I), 0.7),
|
|
17
|
-
(re.compile(r"\b(drop|truncate|delete|insert|update)\b", re.I), 0.8),
|
|
18
|
-
(re.compile(r"(--|#|;)", re.I), 0.4),
|
|
19
|
-
]
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
# ---------- Helpers ----------
|
|
23
|
-
def normalize_text(s: str) -> str:
|
|
24
|
-
"""Quita literales y comentarios para reducir falsos positivos"""
|
|
25
|
-
if not s:
|
|
26
|
-
return ""
|
|
27
|
-
s = _comment_sql.sub(" ", s)
|
|
28
|
-
s = _literal_single.sub("''", s)
|
|
29
|
-
s = _literal_double.sub('""', s)
|
|
30
|
-
s = re.sub(r"\s+", " ", s).strip()
|
|
31
|
-
return s
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def extract_payload_text(request) -> str:
|
|
35
|
-
"""Extrae texto potencialmente peligroso del request"""
|
|
36
|
-
parts = []
|
|
37
|
-
try:
|
|
38
|
-
# query params
|
|
39
|
-
if request.META.get("QUERY_STRING"):
|
|
40
|
-
parts.append(request.META.get("QUERY_STRING"))
|
|
41
|
-
# body
|
|
42
|
-
content_type = request.META.get("CONTENT_TYPE", "")
|
|
43
|
-
if "application/json" in content_type:
|
|
44
|
-
try:
|
|
45
|
-
body_json = json.loads(request.body.decode("utf-8") or "{}")
|
|
46
|
-
parts.append(json.dumps(body_json))
|
|
47
|
-
except Exception:
|
|
48
|
-
parts.append((request.body or b"").decode("utf-8", errors="ignore"))
|
|
49
|
-
else:
|
|
50
|
-
try:
|
|
51
|
-
parts.append(request.body.decode("utf-8", errors="ignore"))
|
|
52
|
-
except Exception:
|
|
53
|
-
pass
|
|
54
|
-
# headers sospechosos
|
|
55
|
-
parts.append(request.META.get("HTTP_USER_AGENT", ""))
|
|
56
|
-
parts.append(request.META.get("HTTP_REFERER", ""))
|
|
57
|
-
except Exception:
|
|
58
|
-
pass
|
|
59
|
-
return " ".join([p for p in parts if p])
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def detect_sqli_text(text: str) -> Tuple[bool, list]:
|
|
63
|
-
"""Detecta patrones en un texto normalizado"""
|
|
64
|
-
q = normalize_text(text)
|
|
65
|
-
matches = []
|
|
66
|
-
for patt, sev in PATTERNS:
|
|
67
|
-
if patt.search(q):
|
|
68
|
-
matches.append((patt.pattern, float(sev)))
|
|
69
|
-
return (len(matches) > 0, matches)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
# ---------- Middleware ----------
|
|
73
|
-
class SQLIDefenseMiddleware(MiddlewareMixin):
|
|
74
|
-
def process_request(self, request):
|
|
75
|
-
text = extract_payload_text(request)
|
|
76
|
-
if not text:
|
|
77
|
-
return None
|
|
78
|
-
|
|
79
|
-
flagged, matches = detect_sqli_text(text)
|
|
80
|
-
if flagged:
|
|
81
|
-
# Bloqueo inmediato solo para pruebas
|
|
82
|
-
return JsonResponse(
|
|
83
|
-
{
|
|
84
|
-
"detail": "Request bloqueado: posible inyección SQL detectada",
|
|
85
|
-
"matches": matches,
|
|
86
|
-
},
|
|
87
|
-
status=403,
|
|
88
|
-
)
|
|
89
|
-
return None
|
|
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
|
|
File without changes
|