SQL injection is a code injection 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. This allows alteration of, for example, a hyperlink, which would then cause a false positive query result from the database and grant you access.
SQL Injection can be broken up into 3 classes:
Assume that you have a website with this existing hyperlink:
http://[sitedomain]/products/products.asp?productid=123
This is a common format, and clicking it would take you to the product
the ProductID = 123. From a SQL standpoint, this would could look like:
SELECT ProductName, ProductDescription
FROM Products
WHERE ProductNumber = 123
Since the product id is actually visible in the link, it is not very
safe, and is open to alteration and injection. Append or 1=1 to
the link, to make it:
http://[sitedomain]/products/products.asp?productid=123 or 1=1
Now from a SQL standpoint this becomes:
SELECT ProductName, Product Description
FROM Products
WHERE ProductNumber = 123 OR 1=1
Since 1 = 1 is always true, this grants you access to the database,
and displays all Product Names and Descriptions.
Some other examples of SQL injection through the inbound use of a
hyperlink are:
http://www.mydomain.com/products/products.asp?productid=123; DROP TABLE
Products
http://www.mydomain.com/products/products.asp?productid=123 UNION SELECT
user-name, password FROM USERS
The first will drop the products table, while the second while return
two tables, the productID, and the union joined table with a list of
user names and passwords.
SQL injection can also be used to grant login access onto a website, or online database GUI. In order to do this, you use true statements to bypass security, or in some cases by using the administrative rights account. When logging onto a site, or server, the user name and password you put in is compared to an encrypted list in order to determine what type of access, if any, you have. By using a true statement, such "or 1=1" will cause the database to believe you input proper credentials.
http://mysite.com/listauthordetails.aspx?SSN=172-32-9999As in prior example, you could add a drop, or other command, to alter the database.
$SQL = "SELECT * FROM users where username = "mysql_real_escape_string($POST['user'])";When someone tries to access the database using a command like OR 1 their query would return \' OR 1\', because your query was created to have a defined escape string.