SQL Injection

What's SQL injection all about, what forms does it take, and how can we stop it?

Прамежкавы

Кібербяспека


SQL injection is a type of cyber attack where an attacker injects malicious SQL (Structured Query Language) code into input fields or data entry points of a web application, with the intention of manipulating the application’s SQL query to perform unauthorized actions on the underlying database. This form of attack can lead to unauthorized access, data manipulation, or even data deletion within the affected database.


Forms of SQL Injection:

Classic SQL Injection:

Attackers inject malicious SQL code into input fields directly, manipulating the logic of the SQL query.

Input: ' OR '1'='1'; --

Query:

SELECT * FROM users WHERE username = '' OR '1'='1'; --



Time-Based Blind SQL Injection:

The attacker exploits database delay responses to infer information about the database by introducing time delays.

Input: ' OR IF(1=1, SLEEP(5), 0); --

Query:

SELECT * FROM users WHERE username = '' OR IF(1=1, SLEEP(5), 0); --



Union-Based SQL Injection:

Attackers use the UNION SQL operator to combine the results of the original query with those of a second query controlled by the attacker.

Input: ' UNION SELECT null, username, password FROM users; --

Query:

SELECT * FROM articles WHERE id = '' UNION SELECT null, username, password FROM users; --



Prevention Techniques:

query = "SELECT * FROM users WHERE username = ? AND password = ?"

cursor.execute(query, (input_username, input_password))