SQL Injection
Description
Injection flaws, such as SQL, NoSQL, OS, and LDAP injection, occur when untrusted data is sent to an interpreter as part of a command or query. The attacker’s hostile data can trick the interpreter into executing unintended commands or accessing data without proper authorization.
Resources
****
Explanations
What is SQL injection? SQL injection is an attack in which malicious SQL statements are injected into a SQL database. SQL injection is easy to avoid, but still happens often! If successful, we can read sensitive databases, extract information, modify databases, and potentially even get a shell.
Common SQL Verbs SQL statements begin with verbs. Let's take a look at a few common verbs: → SELECT - Retrieves data from table → INSERT - Adds data to a table → DELETE - Removes data from a data → UPDATE - Modifies data in a table → DROP - Delete a table (dangerous !) → UNION - Combines data from multiple queries → WHERE - Filters records based on specific condition → AND/OR/NOT - Filter records based on multiple conditions → ORDER BY - Sorts records in ascending/descending order
Examples Users is the table.

SELECT * FROM USERS; will display the whole table SELECT UserID, UserName FROM Users; will display all the column UserID and UserName SELECT * FROM Users WHERE Country='RU'; will display the entire line Natasha (blackwidow) SELECT * FROM Users WHERE Country='US' AND UserName='Frank'; will display the entire line Frank (punisher)
Special Characters ' and " are string delimiters -- /* # and -- - are comment delimiters * and % are wildcards ; ends sql statement = + > < () follow programmic logic
SQL Basics
Types of SQLI
UNION - Data Based
Union based SQL injection allows an attacker to extract information from the database by extending the results returned by the original query. The Union operator can only be used if the original/new queries have the same structure (number and data type of columns).
ERRORS - Error Based
Error based injections are exploited through triggering errors in the database when invalid inputs are passed to it. The error messages can be used to return the full query results, or gain information on how to restructure the query for further exploitation.
TRUE/FALSE - Boolean Based
Boolean-based SQL injection is a technique which relies on sending an SQL query to the database. This injection technique forces the application to return a different result, depending on the query. Depending on the boolean result (TRUE or FALSE), the content within the HTTP response will change, or remain the same. The result allows an attacker to judge whether the payload used returns true or false, even though no data from the database are recovered.
TIME - Time Based
Time-based techniques are often used to achieve tests when there is no other way to retrieve information from the database server. This kind of attack injects a SQL segment which contains specific DBMS function or heavy query that generates a time delay. Depending on the time it takes to get the server response, it is possible to deduct some information. As you can guess, this type of inference approach is particularly useful for blind and deep blind SQL injection attacks.
Second order SQLI
In second-order SQL injection (also known as stored SQL injection), the application takes user input from an HTTP request and stores it for future use. This is usually done by placing the input into a database, but no vulnerability arises at the point where the data is stored. Later, when handling a different HTTP request, the application retrieves the stored data and incorporates it into an SQL query in an unsafe way.

SQL MAP
Mitigations
Secure code
Input validation
Parametrized queries
Stored procédures
Escaping
Host Based WAF (example Wordfence for WordPress)
Edge Sided WAF (example Cloudflare Firewall)
Last updated
Was this helpful?