GuardianUnivalle-Benito-Yucra 0.1.9__py3-none-any.whl → 0.1.11__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.

@@ -1,3 +1,4 @@
1
+ # detector_sql.py
1
2
  import re
2
3
  import json
3
4
  import time
@@ -11,10 +12,11 @@ from django.core.signing import TimestampSigner, BadSignature, SignatureExpired
11
12
  # ---------- Configuración de logging ----------
12
13
  logger = logging.getLogger("sqlidefense")
13
14
  logger.setLevel(logging.INFO)
14
- handler = logging.StreamHandler() # Por consola; en producción puede ser FileHandler
15
- formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
16
- handler.setFormatter(formatter)
17
- logger.addHandler(handler)
15
+ if not logger.handlers:
16
+ handler = logging.StreamHandler() # en prod usa FileHandler/RotatingFileHandler
17
+ formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
18
+ handler.setFormatter(formatter)
19
+ logger.addHandler(handler)
18
20
 
19
21
  # ---------- Patrones de ataques SQL ----------
20
22
  PATTERNS = [
@@ -43,13 +45,14 @@ PATTERNS = [
43
45
 
44
46
  # ---------- Bloqueo temporal por IP ----------
45
47
  TEMP_BLOCK = {} # {ip: timestamp}
46
- BLOCK_DURATION = 30 # segundos
48
+ BLOCK_DURATION = getattr(settings, "SQL_DEFENSE_BLOCK_TTL", 30) # segundos
47
49
 
48
- # ---------- Configuración para bypass ----------
49
- BYPASS_MAX_AGE = getattr(settings, "SQLI_BYPASS_MAX_AGE", 30) # segundos
50
+ # ---------- Excepciones y bypass ----------
51
+ EXEMPT_PATHS = getattr(settings, "SQLI_EXEMPT_PATHS", ["/api/login/"])
50
52
  BYPASS_HEADER = "HTTP_X_SQLI_BYPASS"
53
+ BYPASS_MAX_AGE = getattr(settings, "SQLI_BYPASS_MAX_AGE", 30) # segundos (muy corto)
51
54
 
52
- # --- JWT soporte (si simplejwt está instalado) ---
55
+ # JWT support (optional)
53
56
  try:
54
57
  from rest_framework_simplejwt.backends import TokenBackend
55
58
 
@@ -60,25 +63,24 @@ except Exception:
60
63
 
61
64
  # ---------- Helpers ----------
62
65
  def extract_payload_text(request) -> str:
63
- """Extrae todo el contenido que podría contener inyecciones"""
64
66
  parts = []
65
-
66
- # Query params
67
- if request.META.get("QUERY_STRING"):
68
- parts.append(request.META.get("QUERY_STRING"))
69
-
70
- # Body
67
+ content_type = request.META.get("CONTENT_TYPE", "")
71
68
  try:
72
- content_type = request.META.get("CONTENT_TYPE", "")
73
69
  if "application/json" in content_type:
74
70
  body_json = json.loads(request.body.decode("utf-8") or "{}")
71
+ # 🔒 quitar campos sensibles antes de loguear/analizar
72
+ for key in getattr(settings, "SQL_DEFENSE_SENSITIVE_KEYS", []):
73
+ if key in body_json:
74
+ body_json[key] = "***"
75
75
  parts.append(json.dumps(body_json))
76
76
  else:
77
77
  parts.append(request.body.decode("utf-8", errors="ignore"))
78
78
  except Exception:
79
79
  pass
80
80
 
81
- # Headers
81
+ if request.META.get("QUERY_STRING"):
82
+ parts.append(request.META.get("QUERY_STRING"))
83
+
82
84
  parts.append(request.META.get("HTTP_USER_AGENT", ""))
83
85
  parts.append(request.META.get("HTTP_REFERER", ""))
84
86
 
@@ -86,7 +88,6 @@ def extract_payload_text(request) -> str:
86
88
 
87
89
 
88
90
  def detect_sqli_text(text: str) -> Tuple[bool, list]:
89
- """Detecta ataques SQL en el texto"""
90
91
  matches = []
91
92
  for patt, message in PATTERNS:
92
93
  if patt.search(text):
@@ -95,17 +96,13 @@ def detect_sqli_text(text: str) -> Tuple[bool, list]:
95
96
 
96
97
 
97
98
  def get_client_ip(request):
98
- """Obtiene la IP del cliente"""
99
- x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
100
- if x_forwarded_for:
101
- ip = x_forwarded_for.split(",")[0].strip()
102
- else:
103
- ip = request.META.get("REMOTE_ADDR", "0.0.0.0")
104
- return ip
99
+ xff = request.META.get("HTTP_X_FORWARDED_FOR")
100
+ if xff:
101
+ return xff.split(",")[0].strip()
102
+ return request.META.get("REMOTE_ADDR", "0.0.0.0")
105
103
 
106
104
 
107
105
  def is_valid_jwt(request) -> bool:
108
- """Valida si la petición trae un JWT válido en Authorization: Bearer <token>"""
109
106
  if not SIMPLEJWT_AVAILABLE:
110
107
  return False
111
108
  auth = request.META.get("HTTP_AUTHORIZATION", "")
@@ -126,7 +123,6 @@ def is_valid_jwt(request) -> bool:
126
123
 
127
124
 
128
125
  def is_valid_signed_bypass(request) -> bool:
129
- """Valida si la petición trae un token firmado válido y no expirado"""
130
126
  signed = request.META.get(BYPASS_HEADER, "")
131
127
  if not signed:
132
128
  return False
@@ -145,43 +141,40 @@ def is_valid_signed_bypass(request) -> bool:
145
141
  # ---------- Middleware ----------
146
142
  class SQLIDefenseStrongMiddleware(MiddlewareMixin):
147
143
  def process_request(self, request):
144
+ path = request.path or ""
148
145
  client_ip = get_client_ip(request)
149
146
 
150
- # Revisa si la IP está temporalmente bloqueada
147
+ # 1) Rutas exentas
148
+ EXEMPT_PATHS = getattr(settings, "SQLI_EXEMPT_PATHS", [])
149
+ for p in EXEMPT_PATHS:
150
+ if path.startswith(p):
151
+ return None
152
+
153
+ # 2) IP bloqueada temporalmente
151
154
  if client_ip in TEMP_BLOCK:
152
155
  if time.time() - TEMP_BLOCK[client_ip] < BLOCK_DURATION:
153
156
  logger.warning(f"IP bloqueada temporalmente: {client_ip}")
154
157
  return JsonResponse(
155
- {
156
- "detail": "Acceso temporalmente bloqueado por actividad sospechosa",
157
- "alerts": ["IP bloqueada temporalmente"],
158
- },
159
- status=403,
158
+ {"detail": "Acceso temporalmente bloqueado"}, status=403
160
159
  )
161
160
  else:
162
161
  del TEMP_BLOCK[client_ip]
163
162
 
164
- # --- Nuevo: bypass por JWT válido o token firmado válido ---
165
- if is_valid_jwt(request):
166
- return None
167
- if is_valid_signed_bypass(request):
168
- return None
169
-
170
- # --- Detección de SQLi ---
163
+ # 3) Extraer payload
171
164
  text = extract_payload_text(request)
172
165
  if not text:
173
166
  return None
174
167
 
168
+ # 4) Detectar SQLi
175
169
  flagged, matches = detect_sqli_text(text)
176
170
  if flagged:
177
- # Bloquea temporalmente la IP
178
171
  TEMP_BLOCK[client_ip] = time.time()
179
172
  logger.warning(
180
173
  f"Intento de ataque detectado desde IP: {client_ip}, detalles: {matches}, payload: {text}"
181
174
  )
182
175
  return JsonResponse(
183
176
  {
184
- "detail": "Request bloqueado: posible intento de inyección SQL detectado",
177
+ "detail": "Request bloqueado: posible inyección SQL",
185
178
  "alerts": matches,
186
179
  },
187
180
  status=403,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: GuardianUnivalle-Benito-Yucra
3
- Version: 0.1.9
3
+ Version: 0.1.11
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
@@ -7,14 +7,14 @@ GuardianUnivalle_Benito_Yucra/criptografia/kdf.py,sha256=_sbepEY1qHEKga0ExrX2WRg
7
7
  GuardianUnivalle_Benito_Yucra/detectores/detector_csrf.py,sha256=EAYfLkHuxGC5rXSu4mZJ4yZDCbwBpTX8xZWGKz7i5wA,692
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
- GuardianUnivalle_Benito_Yucra/detectores/detector_sql.py,sha256=BG2XaxgvQK5c6X5Mdz8g4dthuSvC8NMMiDt03MknoJQ,6346
10
+ GuardianUnivalle_Benito_Yucra/detectores/detector_sql.py,sha256=jyFZYi_VjEF6jvDuUmDOGo5D5IgTzDtprfyaAO-jypU,6095
11
11
  GuardianUnivalle_Benito_Yucra/detectores/detector_xss.py,sha256=66V_xuxNOZEwluvWOT4-6pk5MJ3zWE1IwcVkBl7MZSg,719
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.9.dist-info/licenses/LICENSE,sha256=5e4IdL542v1E8Ft0A24GZjrxZeTsVK7XrS3mZEUhPtM,37
17
- guardianunivalle_benito_yucra-0.1.9.dist-info/METADATA,sha256=PxiYZRLFPPf_PRvcBC_PmfPt-IYkGAMQSUqB0df89_g,1892
18
- guardianunivalle_benito_yucra-0.1.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
- guardianunivalle_benito_yucra-0.1.9.dist-info/top_level.txt,sha256=HTWfZM64WAV_QYr5cnXnLuabQt92dvlxqlR3pCwpbDQ,30
20
- guardianunivalle_benito_yucra-0.1.9.dist-info/RECORD,,
16
+ guardianunivalle_benito_yucra-0.1.11.dist-info/licenses/LICENSE,sha256=5e4IdL542v1E8Ft0A24GZjrxZeTsVK7XrS3mZEUhPtM,37
17
+ guardianunivalle_benito_yucra-0.1.11.dist-info/METADATA,sha256=yvOtVZ2eIA8Ztxo2_FjVU97mGetiT8KEr1QQ1iDHExA,1893
18
+ guardianunivalle_benito_yucra-0.1.11.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
+ guardianunivalle_benito_yucra-0.1.11.dist-info/top_level.txt,sha256=HTWfZM64WAV_QYr5cnXnLuabQt92dvlxqlR3pCwpbDQ,30
20
+ guardianunivalle_benito_yucra-0.1.11.dist-info/RECORD,,