[Security Lesson] Find the Vulnerability :: Lesson 1

Learn about Security for code and servers. Learn how to secure your site and your code. Learn about hacking prevention, finding and identifying exploits, and recognising vulnerabilities. Plus, Weekly Security tips and Tutorials.
Forum rules
Post questions related to security, analyse and learn about vulnerabilities and exploits within code to protect yourself against hackers.

[Security Lesson] Find the Vulnerability :: Lesson 1

Postby Highway of Life » 17 Feb 2009, 23:25

Please note that there may be more than one vulnerability within this code.
Post your results in the
Code: Select all
[spoiler]your answers[/spoiler]
spoiler BBCodes.
You may post additional information such as what you believe this coder could have done to secure his code.

Once there has been a fair number of results, I will break the code down as well as the correct answers and will provide a security lesson based on the vulnerabilities within this cdoe.
Code: Select all
<?php
if (!$categoria $_POST['categoria'])
{
    
mensaje("campo_txt""Agregar Categoria""../agregar_categoria.php""""""");
}

$sql_gemelo " SELECT * FROM  tbl_categorias WHERE col_titulo = '" $_POST['categoria'] . "' ";
$rs mysql_query($sql_gemelo);
$num mysql_num_rows($rs);

if (
$num !== 0)
{
    
mensaje("gemelo""Agregar Categor&iacute;a""../agregar_categoria.php""la categor&iacute;a""""");
}

chdir("../..");
$dir_categoria $categoria;
$base "img/espanol/producto/$dir_categoria";

if (!@
mkdir("$base"))
{
    die(
"NueCat.-.ERROR MKDIR $categoria");
}

$consulta "INSERT INTO tbl_categorias VALUES('','" $categoria "')";

if (!@
mysql_query($consulta))
{
    die(
"NueCat.-.ERROR MYSQL " mysql_error());
}

$url "location:../agregar_subcategoria.php?NOMBRE_CATEGORIA=$categoria&RUTA=$base";
header($url);
?>
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10


Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby Obsidian » 18 Feb 2009, 13:57

Spoiler:
I see a possibility for an SQL inject VIA $_POST superglobal array.

Code: Select all
$sql_gemelo " SELECT * FROM  tbl_categorias WHERE col_titulo = '" $_POST['categoria'] . "' "


Not even the built-in MySQL inject protection is used. O_o
うるさいうるさいうるさい!

StopForumSpam Spam Reporting Database
Giving xrumer and friends a great big "screw you" since 2007.
User avatar
Obsidian    
Supporter
Supporter
 
Posts: 2250
Joined: 04 Mar 2008, 23:35
Gender: Male
phpBB Knowledge: 10

Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby Highway of Life » 18 Feb 2009, 14:26

That?s one. But there is more than one vulnerability in that code. ;)
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby Obsidian » 19 Feb 2009, 12:11

This is on a related note...what the heck is mensaje() for? :blink:
うるさいうるさいうるさい!

StopForumSpam Spam Reporting Database
Giving xrumer and friends a great big "screw you" since 2007.
User avatar
Obsidian    
Supporter
Supporter
 
Posts: 2250
Joined: 04 Mar 2008, 23:35
Gender: Male
phpBB Knowledge: 10

Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby Highway of Life » 19 Feb 2009, 21:49

It?s coded in Spanish. :D
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby Obsidian » 20 Feb 2009, 10:25

Hmm...then...

Spoiler:
I'd say the mkdir and chdir lines are possibles for having vulnerabilities. Dunno why though..
うるさいうるさいうるさい!

StopForumSpam Spam Reporting Database
Giving xrumer and friends a great big "screw you" since 2007.
User avatar
Obsidian    
Supporter
Supporter
 
Posts: 2250
Joined: 04 Mar 2008, 23:35
Gender: Male
phpBB Knowledge: 10

Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby chaoskreator » 01 Mar 2009, 14:20

"Mensaje" means "message", I believe...

Spoiler:
The author of the code failed to use any kind of cleansing before inserting into the database. Twice.

And isn't there an issue with the the chdir()?


[ Post made via Mobile Device ] Image
User avatar
chaoskreator    
Commander
Commander
 
Posts: 716
Joined: 02 Feb 2009, 22:05
Location: NC
Gender: Male
phpBB Knowledge: 7

Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby topdown » 14 Mar 2009, 21:16

Spoiler:
Couldn't I essentially use this from any location
Code: Select all
$url = "location:../agregar_subcategoria.php?NOMBRE_CATEGORIA=$categoria&RUTA=$base"; 

Which I believe is header injection through the vars
and dump any file into a directory of my desire (basically) img/espanol/producto/whatever_I_want/ :P
Code: Select all
    $dir_categoria = $categoria;
    $base = "img/espanol/producto/$dir_categoria"; 

and run it against there server, SQL to their database, what ever I want.

Another SQL injection here
Code: Select all
$consulta = "INSERT INTO tbl_categorias VALUES('','" . $categoria . "')"; 

which is using $_POST['categoria']
Something could probably be made usable out of the empty value in that query also.
Do not PM me for Support unless I give permission in a post......PM's only help one, posts help everyone !
User avatar
topdown    
STG Styles Leader
STG Styles Leader
 
Posts: 3021
Joined: 01 Oct 2007, 22:56
Location: Handyman's harddrive
Favorite Team: STG Teams
Gender: Male
phpBB Knowledge: 9

Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby Obsidian » 16 Mar 2009, 13:38

Spoiler:
topdown wrote:Couldn't I essentially use this from any location
Code: Select all
$url = "location:../agregar_subcategoria.php?NOMBRE_CATEGORIA=$categoria&RUTA=$base"; 

Which I believe is header injection through the vars
and dump any file into a directory of my desire (basically) img/espanol/producto/whatever_I_want/ :P


Uhm, I'm not so sure you can do too much via the header() command that can cause server abuse, but it's still not smart to let it be abused. Second opinion? :scratch:
うるさいうるさいうるさい!

StopForumSpam Spam Reporting Database
Giving xrumer and friends a great big "screw you" since 2007.
User avatar
Obsidian    
Supporter
Supporter
 
Posts: 2250
Joined: 04 Mar 2008, 23:35
Gender: Male
phpBB Knowledge: 10

Re: [Security Lesson] Find the Vulnerability :: Lesson 1

Postby Techie-Micheal » 16 Mar 2009, 18:45

sTraTo wrote:
Spoiler:
topdown wrote:Couldn't I essentially use this from any location
Code: Select all
$url = "location:../agregar_subcategoria.php?NOMBRE_CATEGORIA=$categoria&RUTA=$base"; 

Which I believe is header injection through the vars
and dump any file into a directory of my desire (basically) img/espanol/producto/whatever_I_want/ :P


Uhm, I'm not so sure you can do too much via the header() command that can cause server abuse, but it's still not smart to let it be abused. Second opinion? :scratch:


Spoiler:
I'm not sure what David has in mind for this, but there are header injections that can be done to abuse things like SMTP. As for the redirect, you can do even this: img/espano/producto/../../<insert path here>. That's probably what was meant above, but anywho. :)
Techie-Micheal    
STG Development
STG Development
 
Posts: 63
Joined: 26 Oct 2007, 21:35
Gender: Male
phpBB Knowledge: 10

Next

Return to Security Class

Who is online

Users browsing this forum: Magpie Crawler and 1 guest