SQL Injection

What is SQL Injection

SQL Injection is a type of cybersecurity vulnerability where an attacker manipulates a SQL query by injecting malicious SQL code into user inputs (e.g., login forms, search boxes) that are not properly sanitized. This allows the attacker to bypass authentication, access, modify, or delete database data, or even execute administrative operations on the database. SQL injection exploits occur when applications fail to validate or escape user-supplied input, making it possible to alter the intended behavior of SQL queries.

Video explaination

Types of SQL Injection

Types of SQL Injection

SQL Injection during code review

Below you can find examples of unsafe code leading to SQL Injection.

If you want to correct the sanatize the input and do not use string concatenation.

Exploiting SQL Injection

Fuzz String

Most of the time SQL Injections differ from the regular ones. Do not paste payloads thoughtlessly - match them to your specific case.

STACKED QUERIES


UNION Keyword

  • It has to contain same amount of columns as the original query.

  • The data types need to be compatible between each column.

  • You can try to cast string as int e.g. in PostgreSQL

Determine number of columns with order by (increase the number until it fails):

Sample final query:

If application does not return the output from given command it can me data type mismatch try to change the place of executed command.

Dumping other tables example


IN KEYWORD

Useful SQL commands

Manual Code Execution

Common locations: ('/var/www/, /var/www/html, /var/www/htdocs, /usr/local/apache2/htdocs, /usr/local/www/data, /var/apache2/htdocs, /var/www/nginx-default, /srv/www/htdocs, /usr/local/var/www')

INTO OUTFILE

In some cases you can spawn the webshell using SQL Injection.

Last updated