bLessFLA Posted May 29, 2015 Report Share Posted May 29, 2015 1. Cum sa evitam SQL Injection (SQLi)De obicei acesta este folosit in linkuri de genul: site.tld/script.php?id=1 , adaugand dupa 1 o continuare a comanezii SQL.De exemplu:Code: (Select All)site.com/script.php?id=1Acesta in cod arata cam asa:Code: (Select All)SELECT camp1,camp2 FROM tabel WHERE id=’1?Insa, putem adauga ceva acelui id, ceea ce va continua comanda noastra SQL:Code: (Select All)site.com/script.php?id=1’OR+id%3D’3?Asta, in codul SQL va arata asa:Code: (Select All)SELECT camp1,camp2 FROM tabel WHERE id=’1? OR id=’3?Bineinteles, acest exemplu nu este daunator, dar daca “hackerul” foloseste DROP sau DELETE, poate iesi urat.Cum se pot securiza acestea ? Simplu ! Aplicam stringului pe care il introducem in baza de date o functie, mysql_real_escape_string(), care inlocuieste toate caracterele care ar putea avea vreun efect asupra comenzii SQL.De exemplu:script.phpCode: (Select All)$id = $_GET[‘id’];$id = mysql_real_escape_string($id);$query = “SELECT camp1,camp2 FROM tabel WHERE id='”. $id .”‘”;Cel mai bine e sa luam toate datele in functie de ID (adica sa nu avem urluri gen useri.php?user=bogdan, ci useri.php?iduser=1) deoarece ID-uri, trebuie sa fie numere, lucru care se poate verifica foarte usor. Deci, datele le vom selecta dupa un anumit ID, care o sa fie numeric.Astfel, scriptul devine simplu:Code:$id = $_GET[‘id’];if(!is_numeric($id)){echo ‘ID-ul nu est numeric. Incercare de hacking ?? Politia a fost anuntata';}else{//este indicat sa verificati intai daca acel ID se afla in baza de date. folositi mysql_num_rows, iar daca rezultatul este 0, id-ul nu exista in baza de date//ceva de genul:$query = mysql_query(“SELECT camp1,camp2 FROM tabel WHERE id='”. $id .”‘”);if(mysql_num_rows($query)==0) { echo ‘ID-ul nu exista in baza de date. Anunt avocatul'; }else{//totul e OK, id-ul e validat si exista in BD}}In principiu, pentru a valida un GET folositi urmatoarele 3 functii, in functie de caz:mysql_real_escape_string() – sau alternativa: addslashes()is_numeric()mysql_num_rows() Quote Link to comment Share on other sites More sharing options...
Nytro Posted May 29, 2015 Report Share Posted May 29, 2015 Incomplet. $id = $_GET[‘id’];$id = mysql_real_escape_string($id);$query = "SELECT camp1,camp2 FROM tabel WHERE id=". $id;Vulnerabil cat casa.PS: Exista prepared statements aka metoda eficienta de protejare. Quote Link to comment Share on other sites More sharing options...
sarmulita Posted May 29, 2015 Report Share Posted May 29, 2015 Sau un script mai complex care face asta automat " " WAF. Quote Link to comment Share on other sites More sharing options...
Reckon.. Posted May 29, 2015 Report Share Posted May 29, 2015 (edited) Exista PDO. Restul e futere si deprecated.$query = $db->prepare("SELECT finger FROM hand WHERE fingerID = 3");$query->execute();$row = $query->fetch();echo $row[0]; Edited May 29, 2015 by Reckon.. Quote Link to comment Share on other sites More sharing options...