eliminando sql injection
DESCRIPTION
Conferencia que di para OWASP Guatemala sobre SQL injection en PHP y como eliminar ese problema de seguridad el cual es el número uno del Top10 según OWASP.TRANSCRIPT
![Page 1: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/1.jpg)
Copyright © The OWASP FoundationPermission is granted to copy, distribute and/or modify this document under the terms of the OWASP License.
The OWASP Foundation
OWASP
Eliminando SQL injection
Stuardo -StR- RodríguezWeb developerLa [email protected]
19/08/2011
![Page 2: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/2.jpg)
2OWASP
¿Quién soy?
Stuardo RodríguezPromotor del software libre desde 1998Desarollador web en PHP desde 2000Fundador comunidad PHPGT /in/stuardo http://www.google.com/profiles/stuardo.strActualmente trabajando para La Maphpia
![Page 3: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/3.jpg)
3OWASP
Eliminando SQL injection
Agenda3 categorías de posible inseguridad¿Qué es SQL injection?Como solucionar problemas de SQL injectionComo ELIMINAR problemas de SQL injectionIr un paso mas allá.
![Page 4: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/4.jpg)
4OWASP
3 categorías de posible inseguridad
Datos de entradaDatos de salidaEntorno
sistemaentrada salida
![Page 5: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/5.jpg)
5OWASP
3 categorías de posible inseguridad
Datos de entradaDatos de salidaEntorno
sistemaentrada salida
![Page 6: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/6.jpg)
6OWASP
¿Qué es SQL injection?
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = “INSERT INTO users (username, password, email) VALUES (
'{$_POST['username']}',
'{$_POST['pass']}',
'{$_POST['email']}'
)”;
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]');
DROP TABLE users;
-- ');
![Page 7: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/7.jpg)
7OWASP
¿Qué es SQL injection?
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = “INSERT INTO users (username, password, email) VALUES (
'{$_POST['username']}',
'{$_POST['pass']}',
'{$_POST['email']}'
)”;
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]');
DROP TABLE users;
-- ');
![Page 8: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/8.jpg)
8OWASP
¿Qué es SQL injection?
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = “INSERT INTO users (username, password, email) VALUES (
'{$_POST['username']}',
'{$_POST['pass']}',
'{$_POST['email']}'
)”;
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]');
DROP TABLE users;
-- ');
![Page 9: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/9.jpg)
9OWASP
¿Qué es SQL injection?
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = “INSERT INTO users (username, password, email) VALUES (
'{$_POST['username']}',
'{$_POST['pass']}',
'{$_POST['email']}'
)”;
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]');
DROP TABLE users;
-- ');
![Page 10: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/10.jpg)
10OWASP
Como solucionar problemas de SQL injection
Método automáticomagic_quotes
Existe desde PHP2 Creado para “ayudar” a programadores novatos a
escribir código “seguro” Escapeaba con \ cualquier ',”,\ o caracter NULL.
![Page 11: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/11.jpg)
11OWASP
Como solucionar problemas de SQL injection
Método automáticomagic_quotes
No todos los datos de entrada son para ser insertados en SQL.
No todos los datos a ser insertados vienen en un campo del formulario.
magic_quotes usa addslashes() que no es seguro para escapar texto unicode.
No todas las bases de datos usan \ para escapear '
![Page 12: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/12.jpg)
12OWASP
Como solucionar problemas de SQL injection
Método automáticomagic_quotes
No todos los datos de entrada son para ser insertados en SQL.
No todos los datos a ser insertados vienen en un campo del formulario.
magic_quotes usa addslashes() que no es seguro para escapar texto unicode.
No todas las bases de datos usan \ para escapear ' GENERA IGNORANCIA!
![Page 13: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/13.jpg)
13OWASP
Como solucionar problemas de SQL injection
Método automáticofilter_input()filter_var()filter_input_array()filter_var_array()
![Page 14: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/14.jpg)
14OWASP
Como solucionar problemas de SQL injection
Método automáticofilter_input()filter_var()filter_input_array()filter_var_array()
SIGEN GENERANDO IGNORANCIA!
![Page 15: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/15.jpg)
15OWASP
Como solucionar problemas de SQL injection
Método manualif (strpos($_POST['email'], ';'))
throw new Exception('Invalid input');
if (strpos($_POST['email'], '\''))
throw new Exception('Invalid input');
if (strpos($_POST['email'], '”'))
throw new Exception('Invalid input');
if (preg_match('/from|select|delete|insert|where|drop|show|#|--|/ui',$_POST['email']))
throw new Exception('Invalid input');
![Page 16: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/16.jpg)
16OWASP
Como solucionar problemas de SQL injection
Método manualmysql_escape_string()mysql_real_escape_string()
![Page 17: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/17.jpg)
17OWASP
Como solucionar problemas de SQL injection
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = “INSERT INTO users (username, password, email) VALUES (
'{$_POST['username']}',
'{$_POST['pass']}',
'{$_POST['email']}'
)”;
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]');
DROP TABLE users;
-- ');
![Page 18: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/18.jpg)
18OWASP
Como solucionar problemas de SQL injection
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = sprintf(“INSERT INTO users (username, password, email) VALUES ('%s', '%s', '%s')”,
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string($_POST['pass']),
mysql_real_escape_string($_POST['email'])
)”;
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]\');
DROP TABLE users;
-- ');
![Page 19: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/19.jpg)
19OWASP
Como ELIMINAR problemas de SQL injection
PDO...
![Page 20: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/20.jpg)
20OWASP
Como ELIMINAR problemas de SQL injection
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = sprintf(“INSERT INTO users (username, password, email) VALUES ('%s', '%s', '%s')”,
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string($_POST['pass']),
mysql_real_escape_string($_POST['email'])
)”;
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]\');
DROP TABLE users;
-- ');
![Page 21: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/21.jpg)
21OWASP
Como ELIMINAR problemas de SQL injection
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = $db->prepare('INSERT INTO users
(username, password, email)
VALUES (:username, :pass, :email)');
$sql->exec($_POST);
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]\');
DROP TABLE users;
-- ');
![Page 22: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/22.jpg)
22OWASP
Como ELIMINAR problemas de SQL injection
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = $db->prepare('INSERT INTO users
(username, password, email)
VALUES (:username, :pass, :email)');
$sql->exec($_POST);
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]\');
DROP TABLE users;
-- ');
![Page 23: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/23.jpg)
23OWASP
Como ELIMINAR problemas de SQL injection
PDOMódulo PECL para PHP 5.0Nace de DB y MDB de PEARPor default en PHP 5.1
![Page 24: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/24.jpg)
24OWASP
Como ELIMINAR problemas de SQL injection
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$db = new PDO('mysql:host=localhost;
dbname=example', $user, $password);
$sql = $db->prepare('INSERT INTO users
(username, password, email)
VALUES (:username, :pass, :email)');
$sql->exec($_POST);
![Page 25: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/25.jpg)
25OWASP
Ir un paso mas allá.
PDO es una capa de abstracción de acceso a datos
Usar una capa de abstracción de datosDoctrine
![Page 26: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/26.jpg)
26OWASP
Ir un paso mas allá.
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$sql = $db->prepare('INSERT INTO users
(username, password, email)
VALUES (:username, :pass, :email)');
$sql->exec($_POST);
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]\');
DROP TABLE users;
-- ');
![Page 27: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/27.jpg)
27OWASP
Ir un paso mas allá.
$_POST['email'] = “[email protected]');
DROP TABLE users;
-- ”;
$user = new User();
$user->insert($_POST);
INSERT INTO USERS (username, password, email)
VALUES ('str', 'password', '[email protected]\');
DROP TABLE users;
-- ');
![Page 28: Eliminando SQL injection](https://reader034.vdocument.in/reader034/viewer/2022052621/557ba389d8b42a29258b4642/html5/thumbnails/28.jpg)
28OWASP
Eliminando SQL injection
GRACIASStuardo -StR- Rodrí[email protected] /in/stuardo http://www.google.com/profiles/stuardo.str