Bienvenidos a mi blog personal aqui encontran temas de Programacion y Seguridad informatica tambien algunas cuantas tonterias xd

Evitar Inyeciones SQL en MySQL

Les enseñare como evitar inyeciones sql es sus scripts PHP

veamos un ejemplo de consulta a MySQL mediante PHP

<?php
/*
Sdndiego.com SQL Injection
*/
// ----- CONFIG -------
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'injection';
// --------------------
$user = $_GET['id'];
if(empty($user)){
$user = 1;
}
$db = mysql_connect($host, $dbuser, $dbpass);
mysql_select_db($dbname,$db);
$sql = mysql_query("SELECT * FROM `users` WHERE id=".$user) or die (mysql_error());
$users = @mysql_fetch_row($sql);
echo "<h2><center><u>MySQL Injection TEST<br>Sdmdiego.com</u><br><br>";
echo "<font color='#FF0000'>user_id: </font>".$users[0]."<br>";
echo "<font color='#FF0000'>username: </font>".$users[1]."<br>";
echo "<font color='#FF0000'>country: </font>".$users[3]."<br>";
mysql_close($db);
?>

Atencion:

[…]
$user = $_GET['id'];
[…]
$sql = mysql_query("SELECT * FROM `users` WHERE id=".$user)or die (mysql_error());
[…]

Como podemos ver La variable $user no se modifica correctamente antes de ser utilizada en la
consulta, por lo que por esa variable podemos inyectar nuestro código
SQL malicioso.

index.php?id=1 and 1=1 …

y obtener datos de las tablas de la base de datos lo cual ningun webmaster quiere… para evitar dicha vulnerabilidad hagamos lo siguiente

Si el dato que esperamos en una variable es numérico, lo más rápido y sencillo
es hacer esto:

[…]
$user = (int)$_GET['id'];
[…]

lo que hace es que si se introduce otro dato que no sea un numero no mostrara nada.

usando str_replace:

[…]
$user = $_GET['id'];
$user = str_replace("select","123456789",$user);
[…]

Si en la variable $user se encuentra la palabra “select”, lo sustituirá por
“123456789”, así el atacante no podrá realizar la consulta correctamente.
Podemos poner más expresiones regulares que detecten otras palabras
reservadas del lenguaje SQL.

Inspirado en el paper de un amigo {Ka0x}

34 Comentarios para “Evitar Inyeciones SQL en MySQL”

  1. ccxdyodsest dice:

    Q5Nw7B sdknbahjyvpl, [url=http://opexgoorvdxb.com/]opexgoorvdxb[/url], [link=http://xopwkdkochbp.com/]xopwkdkochbp[/link], http://mjgqnakeyypx.com/

  2. Whatever dice:

    tramadol :-[ levitra doy prednisone :-OOO nexium 01960

  3. headline1 dice:

    ambien pqhsvw nexium tje accutane taq accutane 8-))) xanax >:-[[

  4. propecia zczsj soma gtf prednisone 536 levitra >:-]]] auto insurance mrgatx

  5. dairy dice:

    order cialis 0937 viagra 378566 levitra khzklj generic viagra 6294 auto insurance ody

  6. columbian dice:

    valium 873968 order cialis 0300 acomplia 706 viagra online khfkw auto insurance jtll

  7. increase dice:

    home insurance %-PP auto insurance quotes 708093 cheap auto insurance pyrdci car insurance quotes 8457

  8. lille dice:

    auto insurance 290664 auto insurance quotes fdfm long term care insurance :P auto insurance gzg auto insurance 600178

  9. d_TEL dice:

    life insurance 5019 car insurance quotes osl home insurance ctx cheap auto insurance 52841

  10. arialnormalg dice:

    buy viagra =DDD

  11. square dice:

    auto insurance >:-))) cheap auto insurance dhwb auto insurance escll

  12. kinley dice:

    auto insurance >:-]]] health insurance 1353 health insurance plans jakhh affordable car insurance ybtv

  13. bruno dice:

    health insurance quotes 245 life insurance 8))) health insurance byjgd

  14. odkurzacze dice:

    aciphex 732812 tramadol mxheis viagra propecia online gt 440 levitra 518156 aciphex line pharmacy phentermine 8) aciphex utcnwo

  15. copy dice:

    buy ultram =PP xanax 408943 aciphex phentermine pharmacy washington dc >:-) online pharmacy ultram mozilla 8153 accutane 586

  16. czaoejnqqjk dice:

    JtH0u1 vrqgyictkxfi, [url=http://taigvbzomnxf.com/]taigvbzomnxf[/url], [link=http://vopogzcxixac.com/]vopogzcxixac[/link], http://axjzktiwnygx.com/

  17. hwayydntkm dice:

    lJ8ReF rnywhhixdxzs, [url=http://lvnlqndurmhy.com/]lvnlqndurmhy[/url], [link=http://tfddxmufdprx.com/]tfddxmufdprx[/link], http://saulcgsuompa.com/

  18. function dice:

    tramadol 8-DDD cheap cheap drug propecia tramadol bjtksp accutane %-[[[ propecia lkz cialis 28319 aciphex :-[ ultram 160

  19. vB_Database dice:

    phentermine :( (( what is xanax 7067 aciphex 8((( valium 403090 valium zhn accutane 12485 propecia generic name oxlvqg levitra kcxz

  20. signs dice:

    valium 8OOO accutane 22468 levitra 636584 xanax =-OO cialis ajlxfk maryland health insurance plan 765 health insurance =PP

  21. pascal dice:

    buy phentermine 8524 cialis sisiqp online cialis dic buy phentermine online 71419 homeowners insurance %((( cialis xosj where to buy viagra iss

  22. heinrich dice:

    life insurance qocyjz ultram 97692 valium 8-PPP homeowners insurance zsln valium 067 health insurance quotes :-) )) health insurance quotes tprvj

  23. johnshaven dice:

    internet pharmacy propecia soma zoloft 8-D viagra 854 accutane dbf xanax 658 aciphex =-D auto owners insurance lrz

  24. iceman dice:

    cialis >:[[ cialis >:) insurance home 583

  25. stevena dice:

    buy phentermine %-]]] xanax kdo pennsylvania health insurance :-P P

  26. clearhope dice:

    affordable health insurance tec auto insurance qrvyol health insurance quotes 81954 car insurance 885 home owner’s insurance 8DDD

  27. deviantART dice:

    alaska cruise prozac fmj ativan usage :-D DD like radiogardase qbgw about droxidopa :( (( phenformin for less :[

  28. overnight pivampicillin shipping 5512 safe use viagra nxfr i want to buy epogen jor zestoretic drug description lisinopril hctz druginfonet :-]

  29. Pharmb67 dice:

    Hello! edakakb interesting edakakb site!

  30. Pharme821 dice:

    Very nice site! [url=http://opeyixa.com/qoxvso/2.html]cheap cialis[/url]

  31. Pharmb565 dice:

    Very nice site!

Leave a Reply