php - How to make this login unvulnerable -


this question has answer here:

they have told me log in vulnerable, dont know how make safe, im looking if whants me i'd appreciate it.

when add this: "1' or 1=1 limit 1#" password en login form enters.

here 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 usuarios usuario='$usuario' , 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';  ?>

there several known problems page:

  1. sql injection

    $sql= "select usuario, pass usuarios usuario='$usuario' , pass='$pass'"; 

    the problem use text processing insert in query. modify password , specify ' or 'a'='a', in case entrance. because 'a'='a'. furthermore allows me kinds of queries. '; drop table usuarios. better use prepared statements (or equivalent) don't enter parameters yourself. prepared statements escape parameters such 1 cannot inject sql these.

    you should use prepared statements; like:

    $sql= "select usuario, pass usuarios usuario=? , pass=?"; $stm = $bd->prepare($sql); $stm->execute(array($usuario,$pass)); $result = $stm->fetchall(); $contar = count($result); 
  2. unhashed passwords: here manual on password hashing. never store passwords itself. hacker has found weak spot in website , somehow has managed access database, lost. hacker can copy passwords , modify them. furthermore there lot of users use same password on applications making easier hack other websites.

  3. a third aspect found assignment in if statement:

    if ($_session['logueado'] = true) {     panel(); } elseif ($contar == 1) {     $_session['logueado'] = true; panel(); } else{     echo "el usuario o contraseña es incorrecto"; } 

    here assign true $_session variable. better way following:

    if($contar == 1) {     $_session['logueado'] = true; } if($_session['logueado']) {     panel(); } else{     echo "el usuario o contraseña es incorrecto"; } 

another aspect: cannot make page unvulnerable: chances great hackers find ways circumvent lot of protective measures eventually. security not yes-or-no. point must make hard hackers fail in bypassing security.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -