SQL Injection Cheat Sheet PDF
SQL Injection Cheat Sheet PDF
Cheat Sheet
In this cheat sheet, you will learn:
• How do malicious hackers conduct SQL injection attacks
• How to fix your code that has SQL injection vulnerabilities
FOR DEVELOPERS
• How to avoid SQL injection vulnerabilities for the future
<?PHP
$userid = $_GET["userid"];
$query = "SELECT user FROM users WHERE userid = $userid;";
$result = pg_query($conn, $query);
?>
ATTACKER REQUEST:
$query = "SELECT user FROM users WHERE userid = 0; DELETE FROM users WHERE 1;";
As a result, if the current user (current database user) has suitable permissions, the entire users table is cleared.
• If the web application doesn’t return errors and the returned information is the EXAMPLE:
same for boolean-based payloads, the attacker sends a payload that includes a •
time delay command such as SLEEP, which delays the whole response
• The attacker draws conclusions from the length of response delays and repeats
the process as many times as necessary with different arguments
ht p:/ testphp.vulnweb.com/artist .php?%20artist=1-SLE P(3)
• This type of an SQL injection is often used to check whether any other SQL •
injections are possible
• This type of SQL injection may also, for example, be used to guess the content of a
database cell a character at a time by using different ASCII values in conjunction
with a time delay
• This type of SQL injection is possible only for some databases, for example, EXAMPLE:
Microsoft SQL Server and Oracle •
• The attacker includes a special database command in the payload – this
command causes a request to an external resource (controlled by the attacker)
• The attacker monitors for attempts to contact the external resource, for example, •
DNS lookups or HTTP request logs of the external resource
• If there is a request coming once the payload is executed, this confirms that the
SQL injection is possible
• The attacker accesses database information and can send it to the external resource
PHP EXAMPLE
Using PHP Data Objects (PDO):
JAVA EXAMPLE
int id = Integer.parseInt(id);
String query = "SELECT column_name FROM table_name WHERE id = ? order by column_name desc";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setInt(1,id);
ResultSet results = stmt.executeQuery();
STORED PROCEDURES
• Use only if your programming language does not support prepared statements
• To avoid SQL injections, you must use prepared statements in stored procedures
• Available only for database engines that support stored procedures but most modern engines support them
• The query is prepared and stored in the database engine
• The application calls the stored procedure and passes variables to it
MYSQL EXAMPLE
Creating the procedure:
CALL example("1");
MSSQL EXAMPLE
Creating the procedure:
EXEC database.dbo.example 1;
Manual penetration Attack Able to find even very Very time and
testing proxies complex and rare types resource intensive
Reports a lot of
Automatic code
SAST Can reach even code false positives and
analysis (white box
software that is not used directly does not prove that
scanning)
a vulnerability exists
Note: To improve detection, it is best to employ several methods at the same time.
However, if you cannot afford it, go for the most effective method first.
https://www.acunetix.com/blog/articles/blind-out-of-band-sql-injection-vulnerability-testing-added-acumonitor/
Acunetix: A detailed explanation of out-of-band SQL injections
https://www.acunetix.com/blog/articles/exploiting-sql-injection-example/
Acunetix: A practical example that shows how an SQL injection may lead to system compromise
https://www.acunetix.com/blog/articles/using-logs-to-investigate-a-web-application-attack/
Acunetix: A practical example that shows how to analyze logs to discover an SQL injection attack
https://www.acunetix.com/blog/articles/prevent-sql-injection-vulnerabilities-in-php-applications/
Acunetix: A detailed article about preventing SQL injections in PHP
https://www.acunetix.com/how-to-prevent-sql-injections-java/
Acunetix: An article about preventing SQL injections in Java
https://www.acunetix.com/blog/articles/blind-sql-injection/
Acunetix: An article about preventing blind SQL injections
http://pentestmonkey.net/category/cheat-sheet/sql-injection
Pentestmonkey: Detailed SQL injection cheat sheets for penetration testers
ht
Bobby tp
Tables: s:
The most //bo
comprehensible b
library b
of SQL y-
injection ta
defense ble
techniques s.
for many com
programming /
languages