What is SQL Injection?

Programming for Search Engines 101. An area for avid PHP and .NET developers to chat about Programming techniques and how to make better use of search engines.

Moderator: Moderators

What is SQL Injection?

Postby jincy » Wed Nov 13, 2013 11:42 pm

SQL injection (or a SQL injection attack) occurs when a user provides SQL code as user input for a Web page, and the SQL code is then executed in the database.
see one example below:

SELECT * FROM Users WHERE Username='$_POST['Username’]' AND Password='$_POST['Password’]'

which is designed to show all records from the table "Users" for a username and password supplied by a user. Using a Web interface, when prompted for his username and password, a malicious user might enter:

1' or '1' = '1

1' or '1' = '1

resulting in the query:

SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1'

The hacker has effectively injected a whole OR condition into the authentication process. Worse, the condition '1' = '1' is always true, so this SQL query will always result in the authentication process being bypassed.

There are several PHP functions that can be used to avoid this vulnerability. See one example :
We can prevent SQL injection is by using parameterized queries. This means defining the SQL code that is to be executed with placeholders for parameter values, programmatically adding the parameter values, then executing the query. Doing this allows the server to create an execution plan for the query, which prevents any "injected" SQL from being executed.

Let’s use the same example, but I will define the SQL query with parameter placeholders:

$sql = "SELECT * FROM Users WHERE Username = ? and Password = ?";

Now, define an array that holds the parameter values:

$params = array($_POST['Username’], $_POST['Password’]);

When execute the query, we can pass the $params array as an argument:

$stmt = sqlsrv_query($conn, $sql, $params);

When sqlsrv_query is called, an execution plan is created on the server before the query is executed. The plan only allows our original query to be executed. Parameter values (even if they are injected SQL) won’t be executed because they are not part of the plan. So, if we submit a password like above example ('or 1=1--), it will be treated as user input, not SQL code. In other words, the query will look for a user with this password instead of executing unexpected SQL code.

Also I would like to share one Infographic regarding Injection Attack. :)

Image

see more @
jincy
 
Posts: 17
Joined: Tue Jun 21, 2011 11:29 pm
Tell us why you would like to become a WyseLabs Member:

Re: What is SQL Injection?

Postby sreelal » Thu Nov 14, 2013 4:04 am

Very informative share, Jincy. SQL injection is a most commonly used hacking method, especially on Wordpress powered sites. It is found that when compared to other popular CMS platforms like Joomla and Drupal, Worpress is an easy target for hackers. For WP users, there are many efficient Plugins like 'WP Total Security' to prevent SQL injection.
sreelal
 

Re: What is SQL Injection?

Postby beniston » Wed Aug 20, 2014 10:37 am

A must know info for all who work with sql....

I would like to add two more rules related with it:

[list=]Never trust a data entered by the end user
Also Never trust a data retrieved from the database (to be used in another query)[/list]

Both these data can harm the DB. :D
beniston
 
Posts: 502
Joined: Wed Nov 02, 2011 4:35 am
Location: Cochin
Tell us why you would like to become a WyseLabs Member:


Return to Programming

Who is online

Users browsing this forum: No registered users and 6 guests