From 0322a4bdbcd60e82ee3bef0e66a578f1a2d9908b Mon Sep 17 00:00:00 2001 From: PlexicusAI Date: Sat, 21 Mar 2026 01:01:07 +0000 Subject: [PATCH] SQL Injection --- index.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index 5600588..bf249f6 100644 --- a/index.php +++ b/index.php @@ -16,13 +16,23 @@ if ($conn->connect_error) { // Vulnerabilidad de SQL Injection // El siguiente código es vulnerable a SQL Injection ya que el input del usuario se concatena directamente en la consulta SQL sin validación o sanitización. if(isset($_GET['id'])) { - $id = $_GET['id']; // Input del usuario tomado directamente desde la URL - $sql = "SELECT * FROM usuarios WHERE id = $id"; // Vulnerable a SQL Injection - $result = $conn->query($sql); + // Analysis: Fix SQL Injection by using a prepared statement with parameter binding. + // This prevents user-controlled input from altering the query structure. + $id = intval($_GET['id']); // Input del usuario tomado directamente desde la URL (cast to int) + // Use prepared statement to prevent SQL injection + $stmt = $conn->prepare("SELECT * FROM usuarios WHERE id = ?"); + if ($stmt) { + $stmt->bind_param("i", $id); + $stmt->execute(); + $result = $stmt->get_result(); + } else { + // If prepare fails, preserve behavior by setting $result to false + $result = false; + } if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { - echo "id: " . $row["id"]. " - Nombre: " . $row["nombre"]. "
"; + } } else { echo "0 resultados";