SQL Injection

SQL Discovery

Note: In some cases, we may have to use the URL encoded version of the payload. An example of this is when we put our payload directly in the URL 'i.e. HTTP GET request'.

Payload

URL Encoded

'

%27

"

%22

#

%23

;

%3B

)

%29

DBMS Identification Error Based

Different DBMSs return distinct error messages when they encounter issues. By triggering errors and examining the specific messages sent back by the database, you can often identify the type of DBMS the website is using.

DBMS
Example Error Message
Example Payload

MySQL

You have an error in your SQL syntax; ... near '' at line 1

'

PostgreSQL

ERROR: unterminated quoted string at or near "'"

'

PostgreSQL

ERROR: syntax error at or near "1"

1'

Microsoft SQL Server

Unclosed quotation mark after the character string ''.

'

Microsoft SQL Server

Incorrect syntax near ''.

'

Microsoft SQL Server

The conversion of the varchar value to data type int resulted in an out-of-range value.

1'

Oracle

ORA-00933: SQL command not properly ended

'

Oracle

ORA-01756: quoted string not properly terminated

'

Oracle

ORA-00923: FROM keyword not found where expected

1'

Methodology

  • Use a simple quote or time query to test the parameter.

    • Oracle Database require "from dual" to create a valid query

  • Different payloads to discover the SQL Type.

    • SQL Error based injection

      • Force error to determine if vulnerable.

    • SQL Blind injection

      • Boolean based

        • Pay attention for any modification within the website since it could be alter the behavior website or appears certain item

      • Conditional error

        • Force 500 status code error to determine a valid query.

      • SQL Time based Injection

        • Insert a sleep clause to determine if is vulnerable.

  • Use order by to match with the total number columns to of the affected query

    • Try different values on the number of columns for example (select 1,2,3) or (select null,null,null) or (select 'abc','def')

  • If the database is oracle you has to specify the from dual to make a valid query.

Visible error-based SQL injection

This attack involves using error messages returned from the database to gather information about its structure.

Payload:

Common Error Messages and Their Meanings:

  • "ERROR: argument of AND must be type boolean, not type integer" → You need to carefully analyze the error message on the fly to adapt the query. Sometimes, the application forces you to use a boolean to retrieve the values.

  • "Error: Unterminated string literal started at position 95 in ......" → This means you need to shorten the string value.

Forcing an SQL syntax error.

levering the error message to retrieved the username

Retrieving the administrator's password

Blind SQL Injections

Blind SQL Injection with Conditional Responses (Boolean-Based)

This attack relies on observing how the application conditionally responds when a specific query evaluates to True or False.

Methodology

  1. Identify the vulnerable parameter → Inject controlled inputs to determine if the parameter is susceptible to SQL Injection.

  2. Analyze the boolean response behavior → Observe how different conditions (True vs. False) modify the application's response.

  3. Adapt the script to the boolean response → Structure an automated approach based on the application's response pattern.

  4. Determine the length of the target string → Use length-based queries to retrieve the number of characters in the desired data.

  5. Extract the data character by character → Iteratively retrieve the content by leveraging the boolean response mechanism.

Automation Script

Blind SQL Injection with Conditional Errors

This technique forces the web application to trigger a 500 Internal Server Error to determine if a specific condition in the query is valid, allowing us to extract database information.

Methodology

  1. Identify the vulnerable parameter → Inject controlled inputs to determine if the parameter is susceptible to SQL Injection.

  2. Force an application error → Inject queries that cause the database to produce an error (500 status code).

  3. Adapt the script to conditional errors → Automate the extraction based on response status or application behavior.

  4. Determine the length of the target string → Use length-based queries to retrieve the number of characters in the desired data.

  5. Extract the data character by character → Iteratively retrieve the content by leveraging the response status mechanism.

Example Payloads

Injection Structure:

Extract password length:

Extract password value (character by character):

Automatic script

Blind SQL Injection time based

Identify Time Based

Extract password length

Extract password value

Automatic script

Blind SQL injection with out-of-band interaction

Blind SQL injection with out-of-band data exfiltration

Bypass WAF with burpsuite with Hackvector

Hackvertor is a powerful, versatile tool designed to supercharge your workflows by seamlessly converting, encoding, and transforming text or code.

WAF example response:

To obfuscate the payload Just highlight your input, right-click, then select Extensions > Hackvertor > Encode > dec_entities/hex_entities. finally, send the request.

Authentication bypass

Socket SQLmap

Useful Resources

Web Page to test SQL Queries

Port Portswigger

Miscellaneous

Mysql Concatenation: group_concat(0x0a,value)

Mysql Limitation value: limit 0,1

ExtractValue example

Last updated