Jump to content
boco_bc

web security, kill sql inject

Recommended Posts

Salut, sunt un junior(ma rog....tind sa cred asta ) php/mysql si din dorinta a asimila mai multe cunostinte referitor la securitatea unui web am facut acest post.

Am vazut ca sunt multe challenge-uri cu SQL inject si presupun ca daca cineva stie cum sa faca un inject , stie si cum sa previna,nu? :) deci puteti sa luati postul acesta si ca un challange sql protect :)

Acest script adauga in table "unu" , userul si passwordul prin metoda POST iar mai jos se face selectare de useri in functie de pagina. Pe pagina apar cate 3 useri, pagina fiind selectata prin metoda GET.

Tema ar fi: tu cum te protejezi de sql inject? tu cum opresti ca scriptul sa iti ia comanda ?page=1' order by x-- ? ce sa folosesti pe langa mysql_real_escape_string ? tu ce ai face scriptului de mai jos sa fie nu fie "hack-uit" ?

Avem: id,user,pass

si codul:


<?php

$conn=mysql_connect("localhost","root","");
$db=mysql_select_db("ex2",$conn);

if(isset($_POST['submit']))
{
$user=$_POST['user'];
$password=$_POST['password'];

$sql="INSERT INTO unu(id,user,pass)VALUES(0,'$user','$password')";
if(mysql_query($sql)){echo "Datele s-au adaugat cu succes!";}else{ echo "tEroare";}
}
?>
<form action="1.php" method="post">
<span>USER</span><input type="text" name="user"><br>
<span>PASSWORD</span><input type="password" name="password"><br>
<input type="submit" name="submit" value="go"><br>
</form>
<?php
$users_number=mysql_query("SELECT id from unu");
$users_number=mysql_num_rows($users_number);
$per_page=3;
$get_page=(isset($_GET['page']))?$_GET['page']:1;
$pages=$users_number/$per_page;
$pages=ceil($pages);
$page=($get_page-1)*$per_page;
$sel=mysql_query("SELECT * from unu LIMIT $page,$per_page");
while($rand=mysql_fetch_array($sel)){echo $rand['user'],'<br>';}

for($x=1;$x<=$pages;$x++)
{echo '<a href="1.php?page='.$x.'"> '.$x.' </a>';}

?>

Link to comment
Share on other sites

Eu folosesc preg_replace-uri. Utilizatorul sa bage ce vreau eu nu ce vrea el.

EDIT: Si mai folosesc uneori (int) din lene pentru variabilele care nu au voie sa contina altceva in afara de numere.


<?php
$id = (int)$_GET["id"];
echo $id;
?>

Edited by eusimplu
Link to comment
Share on other sites

Speram sa intre mai multi :D scriptul e pur informativ,nu il folosesc la ceva anume , poate fi folositor si celor care sunt la inceput de drum :D.

Eu as face ceva de genul asta! Criticile sunt binevenite !


<?php

$conn=mysql_connect("localhost","root","");
$db=mysql_select_db("ex2",$conn);

if(isset($_POST['submit']))
{
$user=mysql_real_escape_string($_POST['user']);
$password=mysql_real_escape_string($_POST['password']);

$problema=FALSE;
$mesaj_eroare=array();
if(preg_match('/[\'\"!@#$><%^&*)(;:-]/',$user) || preg_match('/[\'\"!@#$><%^&*)(;:-]/',$password)){$problema=TRUE; $mesaj_eroare[]="Caractere neacceptate";}
if(strpos($user,' ')>0 ||strpos($password,' ')>0){$problema=TRUE; $mesaj_eroare[]="Spatiu gol la user si password";}
if(empty($user) || empty($password)) {$problema=TRUE;$mesaj_eroare[]="User sau password nu este completat";}
$password=md5($password);
if(empty($problema))
{
$sql="INSERT INTO unu(id,user,pass)VALUES(0,'$user','$password')";
if(mysql_query($sql)){echo "Datele s-au adaugat cu succes!";}else{ echo "tEroare";}
}
foreach($mesaj_eroare AS $me)
{echo '<b>'.$me.'</b><br>';}
}
?>
<form action="1.php" method="post">
<span>USER</span><input type="text" name="user"><br>
<span>PASSWORD</span><input type="password" name="password"><br>
<input type="submit" name="submit" value="go"><br>
</form>
<?php
$users_number=mysql_query("SELECT id from unu");
$users_number=mysql_num_rows($users_number);

$per_page=3;
$get_page=(isset($_GET['page']))?$_GET['page']:1;
$chars=array("!","@","#","$","%","^","&","*",")","(","_","-","'","\"","]","[",":",";",".",",",">","<");
foreach($chars as $ch):
$get_page=str_replace($ch,"",$get_page);
endforeach;
$page=($get_page-1)*$per_page;
$page=intval($page);

$pages=$users_number/$per_page;
$pages=ceil($pages);

if($get_page==0){die();}
if($get_page>$pages){die();}
if($get_page==NULL){die();}

$sel=mysql_query("SELECT * from unu LIMIT $page,$per_page");
while($rand=mysql_fetch_array($sel)){echo $rand['user'],'<br>';}

for($x=1;$x<=$pages;$x++)
{echo '<a href="1.php?page='.$x.'"> '.$x.' </a>';}

?>


Link to comment
Share on other sites

Ok, eu vad lucrurile in felul urmator.

Pentru a te proteja de sql inject trebuie sa filtrezi ORICE valoare care ajunge la mysql_query , fie insert,select,delete sau update.

Trebuie sa stergi toate caracterele periculoase( ', ; , -,etc). Dupa o gramada de balarii citite despre securitate cred ca abea acuma mi se formeaza o idee in cap despre "cum sa te previi sql inject" :) (sau nu?we'll see :D ) .

Referitor la base64, sincer e prima data cand aud de base64 :D , m-am documentat si am ajuns la concluzia urmatoare:

ca atunci cand bagi in baza de date criptat cu base64, sunt toate bune si frumoase, intra sub forma criptata,nicio problema pana aici, DAR cand "scoti" (select) din baza de date si decriptezi( sa presupunem ca asta e un cod de te "bubuie" )

<script>window.alert('BOOM')</script>

ajungi sa fi bubuit la output . Asa ca..... as filtra output-ul cu "htmlentities" sa-mi apara codul motamo(CRED: ca aici am blocat xss-ul) si pentru input as bloca sau sterge tot ce contine < si > pentru a nu putea baga nimic dubios :D.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...