SQL Injection Security PHP

This is a discussion on "SQL Injection Security PHP" within the Databases section. This forum, and the thread "SQL Injection Security PHP are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jul 12th, 2007, 03:24
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
SQL Injection Security PHP

Hi,

I was wondering if the following code is suitable to check user input:

Input_check.php


Code: Select all
 
<?php
function sql_quote( $value ) 
{ 
include 'connectdb_techs.php';
if( get_magic_quotes_gpc() ) 
 { 
     $value = stripslashes( $value ); 
 } 
 
if( function_exists( "mysql_real_escape_string" ) ) 
 { 
      $value = mysql_real_escape_string( $value ); 
 } 
 else 
 { 
     $value = addslashes( $value ); 
 }
 
return $value; 
include 'closedb.php';
}
?>

is called from:

Code: Select all
 
<?php
session_start();
header("Cache-control: private");
include 'input_check.php';
$user_name = $_POST['user_name'];
$password = $_POST['password'];
$_SESSION['user_name'] = sql_quote($user_name);
$_SESSION['password'] =  sql_quote($password);
$a = $_POST['a'];
if ($a == 'Login')
 {
 header( 'Location: http://***/login2.php' ) ;
 }
 
if ($a == 'Register')
 {
 header( 'Location: http://***/register_page_2.php' ) ;
 }
?>
Thanks,

Nathan.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jul 12th, 2007, 10:54
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Injection Security PHP

Yes, that should work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jul 12th, 2007, 11:33
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Injection Security PHP

Cool, that takes care of malicious input.. I've read there's also issues of a users session being hijacked and you should re-issue a session ID after login.. is this the case?? Any other security issues I should be aware off??

Thanks,

Nathan.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jul 12th, 2007, 12:08
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Injection Security PHP

Session hijacking is when someone finds out the session of another user. The sessionid is normally stored in a cookie, so if someone steals this cookie and sets it as their own, the website will think the person is the user that the cookie was stolen from.
Cookies are normally stolen via XSS vulnerabilities in your site, so make sure all user input is filtered to remove HTML characters.
Another thing you can do to protect against XSS is bind sessions to IP addresses. If you do this, if someone steals a cookie and uses it, their IP will not match the one associated with the cookie, and so they should not be granted access.
Another related security problem is CSRF, although this is not as common. This is when an XSS hole is exploited to cause the user to perform an action that the user didn't choose to do.

More serious security holes include LFI and RFI.
Local File Inclusion is when an include() is exploited, but can only be used to access local files. But, if users are allowed to upload files (e.g. an avatar), this can be a very serious problem, as they could potentially execute code.
Remote File Inclusion is probably the worse coding vulnerability you could get. It is when a user is able to include whatever page they like on your website.
e.g. include($page);
If page hasn't been defined beforehand, someone could set $page as, e.g. http://www.site.com/c99.txt
This would include the code at c99.txt into your php code.
By doing this, someone can have full access to the files on your site.

If you want me to explain any of them more, just ask.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jul 13th, 2007, 13:44
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Injection Security PHP

crikey, ok... do need to think about that. I give users the ability to upload images, but i check the extensions first.. and only display in an img tag.. is that safe??

also I use this code to start a page:

Code: Select all
session_start();
header("Cache-control: private");
does this send a cookie to the users machine, or is it all server side??


Other simpler stuff to sort out!!

I'm retrieving data from mySQL to a textbox for editing. I can't get it to work when using the mysql_real_escape_string function.. it keeps either printing slashes or displaying text without line breaks. Is this an important function for security??

Thanks,

Nathan.

Last edited by nate2099; Jul 13th, 2007 at 13:46. Reason: remove weird linky link
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jul 13th, 2007, 14:43
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Injection Security PHP

Quote:
I give users the ability to upload images, but i check the extensions first.. and only display in an img tag.. is that safe??
If you check extensions it should be fine, although there are always problems with image CSRF. Also, check to make sure uploaded images don't allow any tags, to prevent some persistant XSS.

The problem is not how they are displayed, but how they are uploaded to the server. E.g. If a user is allowed to upload "badfile.php", it will appear on the page as a broken image, but then if a user directly visits that broken file, they will be visiting badfile.php and the server treats it as a typical php file.

Quote:
does this send a cookie to the users machine, or is it all server side??
No cookies are sent here.
See here or look up php.net for more information about sending/processing cookies.

session_start() starts the session. (No surprises there )
The header sends some HTTP Headers.
There aren't any problems with this, but if the headers are displayed anywhere, e.g. if you display the user agent, then you must remember to filter the strings, as headers can be spoofed using flash for malicious purposes.

Quote:
it keeps either printing slashes
stripslashes() just before putting the data where the text goes. Then use mysql_real_escape_string() when updating or inserting back into the database.

Quote:
displaying text without line breaks
str_replace() \n with <br>.

Quote:
Is this an important function for security??
It does the same as adding slashes to \x00, \n, \r, \, ', " and \x1a; it is important for security.
See here for more information about the function.

Does this help?

Last edited by balaclave; Jul 13th, 2007 at 14:49.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jul 14th, 2007, 09:45
SuperMember

SuperMember
Join Date: Apr 2007
Location: Sydney
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Injection Security PHP

Ok the printing from sql now works.. however..

I just wrote a simple html page, saved it to my desktop, renamed it something.jpg, went to my website, created new user, uploaded something.jpg as my user image, viewed source to see where it was stored, opened new browser, typed url/filename.jpg and the code executed... bad right!!
how do I make sure that a file named something.jpg is actually an image file and not script before it's uploaded??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jul 14th, 2007, 13:58
Up'n'Coming Member
Join Date: Sep 2006
Location: UK
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Injection Security PHP

Quote:
I just wrote a simple html page, saved it to my desktop, renamed it something.jpg, went to my website, created new user, uploaded something.jpg as my user image, viewed source to see where it was stored, opened new browser, typed url/filename.jpg and the code executed... bad right!!
how do I make sure that a file named something.jpg is actually an image file and not script before it's uploaded??
After the image is uploaded, check if it's a valid image file. If it is, keep it, if not, destroy it.
Try this:
PHP: Select all

$image "nameoftheimage.jpg";
// name of the image file person is trying to upload
   
if (!getimagesize($image)){ 
      echo(
"not a valid image"); 
      
unlink($image);
   } 
   else { 
      
// save image 
      
echo("Uploaded"); 
   } 
This method still isn't very safe though.
If you combine this with extension checking, the ability to execute server side scripts (e.g. PHP) is stopped, so long as .htaccess doesn't allow image formats to be interpreted as PHP or similar.

This is quite a good article (PDF) and Google has quite a lot of answers.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
injection security, php, sql

Thread Tools

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
question about database injection sudhakararaog PHP Forum 0 Mar 17th, 2008 01:00
SQL injection prevention AdRock PHP Forum 3 Sep 6th, 2007 13:55
Devilsown water injection rocket468 Free Web Site Critique 9 Jan 21st, 2007 18:13
ohol-injection.com rocket468 Free Web Site Critique 2 Oct 27th, 2006 00:03


All times are GMT. The time now is 23:56.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42