What is SQL injection?
What is it?
SQL injection is a code injection technique where untrusted input alters the logic of a SQL query. Attackers exploit input validation or query construction flaws to change what the database executes.
Why does it matter?
It can expose or modify sensitive data, bypass login checks, and escalate access if the database account has broad permissions. In many real incidents, it leads to direct data exposure or account takeover.
Example
A login form sends username='admin' OR '1'='1' to a query such as SELECT * FROM users WHERE username = '...' AND password = '...'. If unsafely constructed, the condition may always evaluate to true.
How to detect it
Use application security testing, SQLi scanners, and code review to look for string concatenation in database queries. Monitor for anomalous database error patterns, query anomalies, and unexpected results from user-controlled inputs.
How to prevent it
Use parameterized queries, prepared statements, and ORM APIs that bind values instead of concatenating SQL text. Validate and constrain user input, enforce least-privilege database accounts, and log suspicious query behavior.
Best practices
- Use parameterized queries and prepared statements
- Separate database accounts by role and privilege
- Validate input before it reaches the database
- Review error handling so details do not leak internals