0

They have told me this log in is vulnerable, but i dont know how to make it safe, im looking for help if anyone whants to help me I'd really appreciate it.

When i add this: "1' OR 1=1 LIMIT 1#" to the password en the login form it enters.

Here is the code:

<?php
session_start();
include 'inc/header.php';
include 'panel_funciones.php';

$usuario = $_POST["nombre"];
$pass = $_POST["pass"];
try {
 $bd = new PDO("mysql:host=localhost;dbname=b9_16267033_1","b9_16267033","12346");
 $bd->query("SET NAMES 'utf8'");
} catch (Exception $e){
 echo "No se ha podido conectar";
 exit;
}
try{
        $sql= "SELECT usuario, pass FROM usuarios WHERE usuario='$usuario' and pass='$pass'";
}catch(Exception $e){
 echo "Error en consulta";
 exit;
}
$iniciosesion = $bd->query($sql);
$result = $iniciosesion->fetchAll();
$contar = count($result);

// AQUI COMIENZA COMPROBACIÓN
if ($_SESSION['Logueado'] = TRUE) {
 panel();
}
elseif ($contar == 1) {
 $_SESSION['Logueado'] = TRUE;
panel();
}
else{
 echo "El usuario o contraseña es incorrecto";
}
include 'inc/footer.php';
?>

1 Answers1

1

There are several well known problems with this page:

  1. SQL injection

    $sql= "SELECT usuario, pass FROM usuarios WHERE usuario='$usuario' and pass='$pass'";
    

    The problem is that you use text processing to insert something in the query. Now say I modify my password and specify it as ' or 'a'='a', in that case I get entrance. Because 'a'='a'. Furthermore it allows me to do all kinds of queries. Like '; DROP TABLE usuarios. You better always use prepared statements (or something equivalent) where you don't enter the parameters yourself. The prepared statements will escape the parameters such that one cannot inject SQL into these.

    You should use prepared statements; something like:

    $sql= "SELECT usuario, pass FROM usuarios WHERE usuario=? and pass=?";
    $stm = $bd->prepare($sql);
    $stm->execute(array($usuario,$pass));
    $result = $stm->fetchAll();
    $contar = count($result);
    
  2. Unhashed passwords: here is a manual on password hashing. Never store passwords itself. Say a hacker has found a weak spot in your website and somehow has managed access to your database, then you are lost. The hacker can copy all the passwords and easily modify them. Furthermore there are a lot of users that use the same password on all applications making it even easier to hack into other websites.

  3. A third aspect you found yourself is the assignment in the if statement:

    if ($_SESSION['Logueado'] = TRUE) {
        panel();
    } elseif ($contar == 1) {
        $_SESSION['Logueado'] = TRUE;
    panel();
    } else{
        echo "El usuario o contraseña es incorrecto";
    }
    

    Here you assign TRUE to your $_SESSION variable. A better way is the following:

    if($contar == 1) {
        $_SESSION['Logueado'] = TRUE;
    }
    if($_SESSION['Logueado']) {
        panel();
    } else{
        echo "El usuario o contraseña es incorrecto";
    }
    

Another aspect: you cannot make a page unvulnerable: chances are great that hackers will find ways to circumvent a lot of protective measures eventually. Security is not a yes-or-no. The point is that you must make it that much hard that most hackers will fail in bypassing security.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555