[Security Lesson] Find the Vulnerability and Exploit :: 2

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 and Exploit :: 2

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

As usually, please enclose your answers in the spoiler BBCode
Code: Select all
[spoiler]your answers[/spoiler]


In this lesson, explain the various vulnerabilities found in this code, next, create a test-case that can be used to find out how an attacker would exploit those vulnerabilities.
Once a sufficient number of answers are given, I will break down the responses and the code.
Login action page:
Code: Select all
<?php

$user 
= $_POST['username'];
$pass = $_POST['password'];

$select_admin = mysql_query("SELECT * FROM cms_admin");

while ($dati_admin = mysql_fetch_array($select_admin))
{
    $username = $dati_admin['username'];
    $password = $dati_admin['password'];
}

if ($user == $username && $pass == $password)
{
    setcookie("login", "OK", time() + $logintime);


admin/delete_page.php
Code: Select all
$admin = ($_COOKIE['login'] == 'OK') ? true false;

if (
$admin)
    $id = $_GET['id'];

    $delete = mysql_query("DELETE FROM cms_content WHERE id='$id'");

    if ($delete)
    {
        echo "" . _DELETE_PAGE_SUCCESS . "";
    }
    else
    
{
        echo "" . _DELETE_PAGE_ERROR . "";
    }
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 and Exploit :: 2

Postby Obsidian » 18 Feb 2009, 14:00

Spoiler:
Code: Select all
setcookie("login""OK"time() + $logintime); 


It's quite easy to alter/add cookies if you know how...so using setcookie() can't really be trusted. Login information ought to be stored in a separate table, one that is protected from any and all outside access.
うるさいうるさいうるさい!

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 and Exploit :: 2

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

@sTraTo, good!
Now can you explain how it can be exploited? (If not, don?t worry about it, it will be explained in fine detail when we receive a good number of answers).
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 and Exploit :: 2

Postby Mr_Bond » 18 Feb 2009, 15:24

Spoiler:
Code: Select all
$admin = ($_COOKIE['login'] == 'OK') ? true : false; 


That is only checking that the cookie 'login' has the value of 'OK' and if it does, it assumes they are an administrator. Someone can easily create a cookie called 'login' with 'OK' as it's value and they can then gain access and delete whatever is being stored in the 'cms_content' table.

As sTraTo said, it ought to be stored in a database table where you can validate the users credentials, e.g: check to make sure it is the same IP, browser string, session id etc. against the ones being provided by the user.
User avatar
Mr_Bond    
Lieutenant
Lieutenant
 
Posts: 246
Joined: 14 Feb 2008, 14:45
Location: localhost
Favorite Team: Chicago Bears
Gender: Male
phpBB Knowledge: 7

Re: [Security Lesson] Find the Vulnerability and Exploit :: 2

Postby Obsidian » 19 Feb 2009, 12:09

Highway of Life wrote:@sTraTo, good!
Now can you explain how it can be exploited? (If not, don?t worry about it, it will be explained in fine detail when we receive a good number of answers).


Spoiler:
False cookie, CSRF, Cross-site-scripting, etc.


Edit: /me slaps self for forgetting the spoiler tag
うるさいうるさいうるさい!

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 and Exploit :: 2

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

Right, but can you provide a working exploit? :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 and Exploit :: 2

Postby Obsidian » 20 Feb 2009, 10:18

Hmmm...

Spoiler:
Assuming you'd created the false cookie or whatever, and got into the admin page...

Code: Select all
if ($admin)
    $id = $_GET['id'];

    $delete = mysql_query("DELETE FROM cms_content WHERE id='$id'"); 

I see a possibility for an SQL inject via $_GET superglobal array. :)
うるさいうるさいうるさい!

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 and Exploit :: 2

Postby chaoskreator » 01 Mar 2009, 14:30

Spoiler:
There is a possibility of a SQL injection with the
Code: Select all
$id = $_GET['id'];


[ 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 and Exploit :: 2

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

Spoiler:
Technically, the problem isn't until you get to here:

Code: Select all
$admin = ($_COOKIE['login'] == 'OK') ? true : false;

if ($admin)
    $id = $_GET['id'];


as that's when the code actually gets abused.

Also, there's a syntax error. Did anybody catch it?

Code: Select all
$admin = ($_COOKIE['login'] == 'OK') ? true : false;

if ($admin)
    $id = $_GET['id'];

    $delete = mysql_query("DELETE FROM cms_content WHERE id='$id'");

    if ($delete)
    {
        echo "" . _DELETE_PAGE_SUCCESS . "";
    }
    else
    {
        echo "" . _DELETE_PAGE_ERROR . "";
    }
}
Techie-Micheal    
STG Development
STG Development
 
Posts: 63
Joined: 26 Oct 2007, 21:35
Gender: Male
phpBB Knowledge: 10

Re: [Security Lesson] Find the Vulnerability and Exploit :: 2

Postby topdown » 15 Mar 2009, 00:52

Spoiler:
:rotfl: That's funny, 5 posts and leave it to a pro[ Techie-Micheal ] to spot a simple syntax error { :P
I assume David knew it was there so I didn't count his posts. He likes leaving little things like that.
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

Next

Return to Security Class

Who is online

Users browsing this forum: No registered users and 1 guest