SQL Injection

  • SQL injection (SQLi) is a web security vulnerability that allows an attacker to interfere with the queries that an application makes to its database.

  • Attackers can exploit SQLi to access and read sensitive information from the database, such as user credentials, personal data, and financial records.

  • In certain scenarios, SQLi can be leveraged to gain shell access to the server, enabling the attacker to execute arbitrary commands, escalate privileges, and potentially take full control of the server.

  • It can occur in different parts of query:

    1. UPDATE: Within value or WHERE clause

    2. INSERT: Within values

    3. SELECT: Within table name, column name, and ORDER BY clause.

Detection

  • The single quote character ' is often used in SQLi attacks. Look for errors or anomalies triggered by it.

  • Look for differences in application responses when injecting some SQL-specific syntax.

  • Boolean conditions such as OR 1=1 and OR 1=2, which help identify successful injections based on different responses.

  • Payloads designed to trigger time delays when executed within a SQL query, such as SLEEP() or WAITFOR DELAY. Monitor any delay in the application response.

  • Out-of-band (OAST) payloads trigger a network interaction when executed within a SQL query. Monitor for any external interactions, such as DNS requests.

# Basic payload that always returns true
' OR '1'='1
" OR "1"="1
' OR '1'='1' #
" OR "1"="1" #
# Injection with a comment to ignore the rest of the query
' OR 1=1--
" OR 1=1--

Exploitation

1. Retrieving Hidden Data

SQL injections can be used to reveal hidden or sensitive data that would otherwise not be visible to the user.

2. Subverting Application Logic

SQL injections can also bypass authentication and authorization mechanisms, giving attackers control over the application logic.

3. UNION Injection

Union-based SQL injection allows attackers to combine the results of multiple queries, which can be used to extract data from different tables.

4. Database Enumeration

5. Reading Files

  • In MySQL, the DB user must have the FILE privilege to load a file's content into a table and then dump data from that table and read it.

  • If you have FILE privilege, you can use this query to load file:

6. Writing Files

  • Modern DBMSes disable file-write by default and require certain privileges for DataBase Administrators to write files.

  • To be able to write files to the back-end server using a MySQL database, we require three things:

  1. User with FILE privilege enabled

  2. MySQL global secure_file_priv variable not enabled

  3. Write access to the location we want to write to on the server

  • Writing data into files:

  • Writing Web Shell:

Using SQLMap For Exploitation

  • Where usage of OR payloads is a must (e.g., in case of login pages), we may have to raise the risk level ourselves because OR payloads are dangerous in a default run.

Last updated