GuardianUnivalle-Benito-Yucra 0.1.27__py3-none-any.whl → 0.1.29__py3-none-any.whl
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/detectores/detector_xss.py +131 -13
- {guardianunivalle_benito_yucra-0.1.27.dist-info → guardianunivalle_benito_yucra-0.1.29.dist-info}/METADATA +1 -1
- {guardianunivalle_benito_yucra-0.1.27.dist-info → guardianunivalle_benito_yucra-0.1.29.dist-info}/RECORD +6 -6
- {guardianunivalle_benito_yucra-0.1.27.dist-info → guardianunivalle_benito_yucra-0.1.29.dist-info}/WHEEL +0 -0
- {guardianunivalle_benito_yucra-0.1.27.dist-info → guardianunivalle_benito_yucra-0.1.29.dist-info}/licenses/LICENSE +0 -0
- {guardianunivalle_benito_yucra-0.1.27.dist-info → guardianunivalle_benito_yucra-0.1.29.dist-info}/top_level.txt +0 -0
|
@@ -1,15 +1,133 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
import re
|
|
5
|
+
from typing import List, Tuple
|
|
6
|
+
from django.conf import settings
|
|
7
|
+
from django.utils.deprecation import MiddlewareMixin
|
|
8
|
+
|
|
9
|
+
# Logger
|
|
10
|
+
logger = logging.getLogger("xssdefense")
|
|
11
|
+
logger.setLevel(logging.INFO)
|
|
12
|
+
if not logger.handlers:
|
|
13
|
+
handler = logging.StreamHandler()
|
|
14
|
+
handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
|
|
15
|
+
logger.addHandler(handler)
|
|
16
|
+
|
|
17
|
+
# Intentar usar bleach si está disponible
|
|
18
|
+
try:
|
|
19
|
+
import bleach
|
|
20
|
+
|
|
21
|
+
_BLEACH_AVAILABLE = True
|
|
22
|
+
except Exception:
|
|
23
|
+
_BLEACH_AVAILABLE = False
|
|
24
|
+
|
|
25
|
+
# Patrones de detección XSS
|
|
26
|
+
XSS_PATTERNS: List[Tuple[re.Pattern, str]] = [
|
|
27
|
+
(re.compile(r"<\s*script\b", re.I), "Etiqueta <script>"),
|
|
28
|
+
(re.compile(r"on\w+\s*=", re.I), "Atributo evento (on*)"),
|
|
29
|
+
(re.compile(r"javascript:\s*", re.I), "URI javascript:"),
|
|
30
|
+
(re.compile(r"<\s*iframe\b", re.I), "Etiqueta <iframe>"),
|
|
31
|
+
(re.compile(r"<\s*embed\b", re.I), "Etiqueta <embed>"),
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# Funciones auxiliares
|
|
36
|
+
def detect_xss_text(text: str) -> Tuple[bool, List[str]]:
|
|
37
|
+
matches: List[str] = []
|
|
38
|
+
if not text:
|
|
39
|
+
return False, matches
|
|
40
|
+
for patt, message in XSS_PATTERNS:
|
|
41
|
+
if patt.search(text):
|
|
42
|
+
matches.append(message)
|
|
43
|
+
return len(matches) > 0, matches
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def sanitize_input_basic(text: str) -> str:
|
|
47
|
+
if text is None:
|
|
48
|
+
return text
|
|
49
|
+
if _BLEACH_AVAILABLE:
|
|
50
|
+
return bleach.clean(text, tags=[], attributes={}, protocols=[], strip=True)
|
|
51
|
+
replacements = [
|
|
52
|
+
("&", "&"),
|
|
53
|
+
("<", "<"),
|
|
54
|
+
(">", ">"),
|
|
55
|
+
('"', """),
|
|
56
|
+
("'", "'"),
|
|
57
|
+
("/", "/"),
|
|
58
|
+
]
|
|
59
|
+
result = text
|
|
60
|
+
for old, new in replacements:
|
|
61
|
+
result = result.replace(old, new)
|
|
62
|
+
return result
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def extract_payload_text(request) -> str:
|
|
66
|
+
parts: List[str] = []
|
|
67
|
+
try:
|
|
68
|
+
ct = request.META.get("CONTENT_TYPE", "")
|
|
69
|
+
if "application/json" in ct:
|
|
70
|
+
parts.append(
|
|
71
|
+
json.dumps(
|
|
72
|
+
json.loads(request.body.decode("utf-8") or "{}"), ensure_ascii=False
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
else:
|
|
76
|
+
parts.append(request.body.decode("utf-8", errors="ignore"))
|
|
77
|
+
except Exception:
|
|
78
|
+
pass
|
|
79
|
+
qs = request.META.get("QUERY_STRING", "")
|
|
80
|
+
if qs:
|
|
81
|
+
parts.append(qs)
|
|
82
|
+
parts.append(request.META.get("HTTP_USER_AGENT", ""))
|
|
83
|
+
parts.append(request.META.get("HTTP_REFERER", ""))
|
|
84
|
+
return " ".join([p for p in parts if p])
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# Middleware XSS
|
|
88
|
+
class XSSDefenseMiddleware(MiddlewareMixin):
|
|
89
|
+
"""
|
|
90
|
+
Middleware Django que detecta XSS en IPs no confiables.
|
|
91
|
+
Solo marca el ataque en request.sql_attack_info para que
|
|
92
|
+
AuditoriaMiddleware lo registre y bloquee.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def process_request(self, request):
|
|
96
|
+
trusted_ips: List[str] = getattr(settings, "XSS_DEFENSE_TRUSTED_IPS", [])
|
|
97
|
+
ip = request.META.get("REMOTE_ADDR", "")
|
|
98
|
+
if ip in trusted_ips:
|
|
99
|
+
return None # IP confiable → no analizar
|
|
100
|
+
|
|
101
|
+
excluded_paths: List[str] = getattr(settings, "XSS_DEFENSE_EXCLUDED_PATHS", [])
|
|
102
|
+
if any(request.path.startswith(p) for p in excluded_paths):
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
payload = extract_payload_text(request)
|
|
106
|
+
if not payload:
|
|
107
|
+
return None
|
|
108
|
+
|
|
109
|
+
flagged, matches = detect_xss_text(payload)
|
|
110
|
+
if not flagged:
|
|
111
|
+
return None
|
|
112
|
+
|
|
113
|
+
logger.warning(
|
|
114
|
+
"XSS detectado desde IP %s: %s ; payload truncated: %.200s",
|
|
115
|
+
ip,
|
|
116
|
+
matches,
|
|
117
|
+
payload,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
# Solo marcamos el ataque, no bloqueamos aquí
|
|
121
|
+
request.sql_attack_info = {
|
|
122
|
+
"ip": ip,
|
|
123
|
+
"tipos": ["XSS"],
|
|
124
|
+
"descripcion": matches,
|
|
125
|
+
"payload": payload,
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return None
|
|
129
|
+
|
|
130
|
+
|
|
13
131
|
"""
|
|
14
132
|
Algoritmos relacionados:
|
|
15
133
|
*Guardar entradas sospechosas con AES-GCM para confidencialidad y autenticidad.
|
|
@@ -18,4 +136,4 @@ S_xss = w_xss * detecciones_xss
|
|
|
18
136
|
S_xss = 0.3 * 2
|
|
19
137
|
donde w_xss es peso asignado a XSS y detecciones_xss es la cantidad de patrones detectados.
|
|
20
138
|
|
|
21
|
-
"""
|
|
139
|
+
"""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: GuardianUnivalle-Benito-Yucra
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.29
|
|
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
|
|
@@ -8,13 +8,13 @@ GuardianUnivalle_Benito_Yucra/detectores/detector_csrf.py,sha256=EAYfLkHuxGC5rXS
|
|
|
8
8
|
GuardianUnivalle_Benito_Yucra/detectores/detector_dos.py,sha256=lMWmCw6nccCEnek53nVjpoBCeiBqLdrSXxqRuI7VP2I,696
|
|
9
9
|
GuardianUnivalle_Benito_Yucra/detectores/detector_keylogger.py,sha256=rEDG-Q_R56OsG2ypfHVBK7erolYjdvATnAxB3yvPXts,729
|
|
10
10
|
GuardianUnivalle_Benito_Yucra/detectores/detector_sql.py,sha256=b7pd4-CTH0FqW5-dwA6RA38Dls0bSBXKSLlmnKbLnbA,2982
|
|
11
|
-
GuardianUnivalle_Benito_Yucra/detectores/detector_xss.py,sha256=
|
|
11
|
+
GuardianUnivalle_Benito_Yucra/detectores/detector_xss.py,sha256=S2vJUbdrh4oG7-1plBK7Emu2eG5ERWHBFWZe-e5OTgo,4201
|
|
12
12
|
GuardianUnivalle_Benito_Yucra/middleware_web/middleware_web.py,sha256=23pLLYqliUoMrIC6ZEwz3hKXeDjWfHSm9vYPWGmDDik,495
|
|
13
13
|
GuardianUnivalle_Benito_Yucra/mitigacion/limitador_peticion.py,sha256=ipMOebYhql-6mSyHs0ddYXOcXq9w8P_IXLlpiIqGncw,246
|
|
14
14
|
GuardianUnivalle_Benito_Yucra/mitigacion/lista_bloqueo.py,sha256=6AYWII4mrmwCLHCvGTyoBxR4Oasr4raSHpFbVjqn7d8,193
|
|
15
15
|
GuardianUnivalle_Benito_Yucra/puntuacion/puntuacion_amenaza.py,sha256=Wx5XfcII4oweLvZsTBEJ7kUc9pMpP5-36RfI5C5KJXo,561
|
|
16
|
-
guardianunivalle_benito_yucra-0.1.
|
|
17
|
-
guardianunivalle_benito_yucra-0.1.
|
|
18
|
-
guardianunivalle_benito_yucra-0.1.
|
|
19
|
-
guardianunivalle_benito_yucra-0.1.
|
|
20
|
-
guardianunivalle_benito_yucra-0.1.
|
|
16
|
+
guardianunivalle_benito_yucra-0.1.29.dist-info/licenses/LICENSE,sha256=5e4IdL542v1E8Ft0A24GZjrxZeTsVK7XrS3mZEUhPtM,37
|
|
17
|
+
guardianunivalle_benito_yucra-0.1.29.dist-info/METADATA,sha256=E6PCNE91cwAxPFWHDpeLdEnsGHKfvvH8TRDQ3fNIFj4,1893
|
|
18
|
+
guardianunivalle_benito_yucra-0.1.29.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
19
|
+
guardianunivalle_benito_yucra-0.1.29.dist-info/top_level.txt,sha256=HTWfZM64WAV_QYr5cnXnLuabQt92dvlxqlR3pCwpbDQ,30
|
|
20
|
+
guardianunivalle_benito_yucra-0.1.29.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|