SQL Note

Define

SQL injection is web security vulnerability that allow an attacker to interfere with the queries that an application make it to databases.

This can allow an attacker to view data that not able to retrive including data belongings to other person, or other data that application can be access.

Detect SQLi

When a site appears to be vulnerable to SQL injection (SQLi) due to unusual server responses to SQLi-related inputs, the first step is to understand how to inject data into the query without disrupting it. This requires identifying the method to escape from the current context effectively.

'
"
`
')
")
`)
'))
"))
`))
OR 1=1
OR 1=2
OR '1'='1'  //true statement
OR '1'='2' //false statement

After confirming the site are vulnerable, try to put comment.

Retreive Hidden Data

https://insecure-website.com/products?category=Gifts

This website try to display product in different categories. The SQL query to retrieve details look like.

SELECT * FROM products WHERE category = 'Gifts' AND released = 1

The restriction released = 1 is being used to hide products that are not released. We could assume for unreleased products, released = 0.

The payload

Since the query doesnt implement any defense againts SQL injection, the attacker can craft a payload such as :

  • symbol represent space in url.

SQL injection vulnerability in WHERE clause allowing retrieval of hidden data

Given the website with search funtion.

https://URL/filter?category=Pets%27+OR+1=1--

https://URL/filter?category=Pets'+--

Subverting Application Logic

Application that have login and when user submit the application check the credentials by performing SQL query.

SELECT * FROM users WHERE username = 'whyme' AND password = 'superstrong'

If the query returns the details of a user, then the login is successful. Otherwise, it is rejected. In this case, an attacker can log in as any user without the need for a password. They can do this using the SQL comment sequence -- to remove the password check from the WHERE clause of the query. For example, submitting the username administrator'-- and a blank password results in the following query:

SELECT * FROM users WHERE username = 'administrator'--' AND password = ''

SQLi Logic

SQL injection UNION attacks

When an application is vulnerable to SQL injection, and the results of the query are returned within the application's responses, you can use the UNION keyword to retrieve data from other tables within the database. This is commonly known as a SQL injection UNION attack.

The UNION keyword enables you to execute one or more additional SELECT queries and append the results to the original query. For example: SELECT a, b FROM table1 UNION SELECT c, d FROM table2

This SQL query returns a single result set with two columns, containing values from columns a and b in table1 and columns c and d in table2.

1. Determine number of column

Increase number using ORDER BY 1 -- to find number of column

Can also use ' UNION SELECT NULL,NULL,NULL --

null represent number of column. can be increase

SQL injection UNION attack, determining the number of columns returned by the query

1. Finding column with usefull data

The interesting data that you want to retrieve is normally in string form. This means you need to find one or more columns in the original query results whose data type is, or is compatible with, string data.

Database Specific

Full Exploit

Multiple values ' UNION SELECT NULL,username || '~' || password FROM users--

Blind SQLi

This query is vulnerable to SQL injection, but the results from the query are not returned to the user. However, the application does behave differently depending on whether the query returns any data. If you submit a recognized TrackingId, the query returns data and you receive a "Welcome back" message in the response.

This behavior is enough to be able to exploit the blind SQL injection vulnerability. You can retrieve information by triggering different responses conditionally, depending on an injected condition.

?id=1 AND SELECT SUBSTR(table_name,1,1) FROM information_schema.tables = 'A'

Error-Based SQL Injection

Last updated