Lost Password?


Go Back   CodeCall Programming Forum > Web Development Forum > PHP Forum

PHP Forum Use this forum to discuss all aspects of PHP Development. PHP is a server-side, cross-platform, HTML embedded scripting language that lets you create dynamic web pages.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-15-2007, 03:50 PM
shibbythestoner's Avatar   
shibbythestoner shibbythestoner is offline
Programmer
 
Join Date: Jun 2007
Location: Scotland
Age: 17
Posts: 128
Rep Power: 6
shibbythestoner is on a distinguished road
Default Make a script vulnerable to SQL injection?

Hello.

I find myself in need of a way to show how some things can go wrong with php, databases, and the web. I decided there are few better ways than to demonstrate an SQL injection.
My problem is, however, that the injections don't appear to be working.
I've made sure that the variables passed through are pure input (employed stripslashes(), didn't use mysql_real_escape_string(), and so on). I've made various different attempts at injection, but so far the best result I get is an error about the following line:
Code:
if (mysql_num_rows($execute) == NULL){
Code:
mysql_num_rows(): supplied argument is not a valid MySQL result resource
Which is the error appearing on attempted injections.
Here is some of the code I have been using:
Code:
                                if (!$db){
                                    
                                    die("<font color='#F62817'><b>Error: Could not connect to database - ".mysql_error()."</b></font><p>\n");
                                    include("admin_loginform.php");
                                    mysql_close($db);
                                    
                                } //end of if
                                else {
                                    
                                    //Leaving $admin and $password open to SQL injection for demonstration purposes
                                    
                                    $query="SELECT *
                                        FROM admins
                                        WHERE username='$admin' AND password='$password'";
                                    $execute=mysql_query($query,$db);
                                    if (mysql_num_rows($execute) == NULL){
                                        
                                        echo "<font color='#F62817'><b>Error: Invalid username or password.</b></font><p>\n";
                                        include("admin_loginform.php");
                                        mysql_close($db);
                                        
                                    } //end of if
                                    else {
                                        
                                        echo "<font color='#5EFB6E'><b>Welcome to administration, $admin.</font></b><br>\n";
                                        echo "What do you want to do?<br>\n";
Can anyone tell me what I could do to make my script vulnerable? For once I want my script to be exploited and I find it doesn't work anyway! I've Googled a bit but I've either not looked in the right places or (oddly enough) there's not much material on how to MAKE a website more vulnerable.

Summary: I need my script to be exploitable via SQL injection. Any ideas please?
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-15-2007, 04:45 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,203
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

Everything looks good and vulnerable here. How are you calling your script? You should call it as:

http://domain/script.php?admin=OR 1&password=anything

The OR 1 will cause a clause in the execution of your SQL Query that always equals true.

Also, I'm not entirely sure that the above statement will actually return any rows. This would cause the mysql_num_rows not to return anything.

You could try doing:


http://domain/script.php?admin=admin&password=OR 1

Which would cause the SQL to return the row with the username of admin.

Sidewinder should be able to help more once he sees this. He is fairly good with security and PHP.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-15-2007, 07:21 PM
shibbythestoner's Avatar   
shibbythestoner shibbythestoner is offline
Programmer
 
Join Date: Jun 2007
Location: Scotland
Age: 17
Posts: 128
Rep Power: 6
shibbythestoner is on a distinguished road
Default

So you suggest the use of the GET method, as opposed to POST? I'll try that out...perhaps that's what I've been doing wrong all along. You may well be correct about mysql_num_rows() returning nothing; since that's where some problems appear to originate from.
Well thanks very much, I'll try that out.

Edit: I'm afraid those suggestions didn't work, the input seems to go through fine but I still get the invalid username/password error. Anything else I could try?
__________________

Last edited by shibbythestoner; 12-15-2007 at 07:35 PM. Reason: fail @ spelling.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-15-2007, 07:44 PM
shibbythestoner's Avatar   
shibbythestoner shibbythestoner is offline
Programmer
 
Join Date: Jun 2007
Location: Scotland
Age: 17
Posts: 128
Rep Power: 6
shibbythestoner is on a distinguished road
Default

Thanks to the "Similar Threads" feature I happened upon, I found the answer in v0id's post on Sidewinder's thread,
PHP: SQL Injections
Trying this:
Code:
' OR '1' = '1
works fine on my script. Thanks, guys!
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-15-2007, 07:53 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,433
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

It's really all about how crappy you write your code, when I wrote a my tutorial on SQL injections [PHP: SQL Injections], I couldn't for the life of me get a functional demo to work. However, you inspired me to give it another shot, and I managed to get it to work. I'm not really in the mood for explaining [tutorial does most of it] it so I'll just post my code and you can figure it out yourself.

Database: injectiontest
SQL Code:
  1. CREATE TABLE `users` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `username` varchar(200) NOT NULL,
  4.   `password` varchar(200) NOT NULL,
  5.   `creditcard` varchar(200) NOT NULL,
  6.   PRIMARY KEY  (`id`)
  7. );
  8.  
  9. INSERT INTO `users` (`id`, `username`, `password`, `creditcard`) VALUES
  10. (1, 'Sidewinder', 'monkey', '0123456789987654');

File: login.php
PHP Code:
<?php

if(get_magic_quotes_gpc()) {
   
$username stripslashes($_GET['username']);
   
$password stripslashes($_GET['password']);
}

$link = @mysql_connect('localhost'''''
    or die(
'Could not connect: ' mysql_error());

mysql_select_db('injectiontest'$link
    or die(
'Could not select database.');


$query mysql_query("SELECT * FROM `users` "
        
"WHERE `username` = '$username' "
        
"AND `password` = '$password'");
$row mysql_fetch_assoc($query);

if(
mysql_num_rows($query) == 1) {
    echo 
"Hello {$row['username']}!<br />";
    echo 
"Your creditcard number is: {$row['creditcard']}";
}

?>
Then if you point your browser to:
Quote:
login.php?username=anything&password=anything'%20O R%201='1
You will see:
Quote:
Hello Sidewinder!
Your creditcard number is: 0123456789987654
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 12-15-2007, 08:36 PM
shibbythestoner's Avatar   
shibbythestoner shibbythestoner is offline
Programmer
 
Join Date: Jun 2007
Location: Scotland
Age: 17
Posts: 128
Rep Power: 6
shibbythestoner is on a distinguished road
Default

Cool script Sidewinder!
Also, thanks for your credit card number.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-15-2007, 09:17 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,203
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

Nice work Sidewinder!
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-15-2007, 09:56 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,433
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

It feels weird being complemented for providing a script with poor standards, horrible security measures, and completely senseless code - but thanks
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VB 6.0: Tutorial, How to Make Glass2K!! TcM VB Tutorials 12 09-28-2008 12:53 PM
PHP: SQL Injections John Security Tutorials 11 08-18-2008 01:43 PM
VB 6.0: Tutorial, How to make a GIF in your application TcM VB Tutorials 17 07-01-2008 04:26 PM
How to make Dumplings ahsan16 The Lounge 2 01-11-2007 11:55 PM


All times are GMT -5. The time now is 07:10 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads