Forums Gallery Movies File Hosting Classifieds Jokes Free Hosting Free Blogs

Go Back   BizHat Forums > Computer Forum > PHP Programming

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-11-2008, 02:30 AM
Administrator
Site Admin
 
Join Date: Sep 2006
Location: Pune
Posts: 3,632
Default SQL injection

SQL injection is a technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is in fact an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another.

To protect against SQL injection, user input must not directly be embedded in SQL statements. Instead, user input must be escaped or filtered or parameterized statements must be used.
Reply With Quote
  #2 (permalink)  
Old 12-18-2008, 10:55 AM
Administrator
Site Admin
 
Join Date: Sep 2006
Location: Pune
Posts: 3,632
Default Simple case of SQL injection

During data base querry, you might ask the user for a user ID and password, then check for the user by passing the database a query and checking the result.

Code:
SELECT * FROM users WHERE name='$username' AND pass='$password';

However, if the user who's logging in is devious, he may enter the following as his password:


Code:
' OR '1'='1

This results in the query being sent to the database as:


Code:
SELECT * FROM users WHERE name='known_user' AND pass='' OR '1'='1';

This will return the username without validating the password -- the malicious user has gained entry to your application as a user of his choice.


To alleviate this problem, you need to escape dangerous characters from the user-submitted values, most particularly the single quotes ('). The simplest way to do this is to use PHP's addslashes() function.


Code:
$username = addslashes($_POST["username"]); 
$password = addslashes($_POST["password"]);
Reply With Quote
  #3 (permalink)  
Old 12-18-2008, 10:57 AM
Administrator
Site Admin
 
Join Date: Sep 2006
Location: Pune
Posts: 3,632
Default magic_quotes_gpc

Set the magic_quotes_gpc variable php.ini to Off, will automatically apply addslashes to all values submitted via GET, POST or Cookies.

Code:
if (get_magic_quotes_gpc()){ 
  $_GET = array_map('stripslashes', $_GET); 
  $_POST = array_map('stripslashes', $_POST); 
  $_COOKIE = array_map('stripslashes', $_COOKIE); 
}
Reply With Quote
Reply

Bookmarks

Tags
sql injection

Thread Tools
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



All times are GMT. The time now is 10:24 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0