๐ What Is It?
Injection flaws 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. The root cause is always the same: user input concatenated into a command that an interpreter then executes.
A05
OWASP Rank
33
Mapped CWEs
Interpreter
Target
โ ๏ธ Common Attack Vectors
- SQL Injection โ manipulate database queries
- NoSQL Injection โ exploit document stores
- OS / Command Injection โ run system commands
- LDAP Injection โ alter directory queries
- XPath Injection โ manipulate XML queries
- Expression Language (EL) / template injection
๐ด Attack Flow
1. Submits hostile input (e.g. admin' --)
โ
2. App concatenates input into a SQL query
โ
3. Injected syntax alters query logic
โ
4. Interpreter executes unintended command
โ
5. BREACH: auth bypass / data exfiltration!
โ
2. App concatenates input into a SQL query
โ
3. Injected syntax alters query logic
โ
4. Interpreter executes unintended command
โ
5. BREACH: auth bypass / data exfiltration!
โ Vulnerable Code
# WRONG: input concatenated into the query string
query = f"SELECT * FROM users WHERE id = {user_id}"
# Escaping is not enough -- it can be bypassed
escaped = input.replace("'", "''")
query = f"SELECT * FROM users WHERE name = '{escaped}'"
โ Secure Code
# RIGHT: parameterized query, input stays data
cursor.execute(
"SELECT * FROM users WHERE id = ?",
(user_id,)
)
cursor.execute(
"SELECT * FROM users WHERE name = ?",
(input,)
)
โ Prevention Checklist
- Use parameterized queries always
- Never concatenate user input into commands
- Validate input; whitelist acceptable patterns
- Apply least privilege to the database user
- Escape output with context-appropriate encoding
- Use ORMs properly; avoid raw queries
- Prefer static SQL; avoid dynamic queries
- Avoid dynamic SQL inside stored procedures
- Do not rely on escaping alone (defense in depth)
- Perform regular security testing
๐ Real-World & Pitfalls
Heartland Payment Systems (2008): a SQL injection flaw led to ~130M credit-card numbers stolen and roughly $140M in settlements.
Common pitfall: Trusting input validation or escaping alone. Escaping is error-prone and bypassable โ parameterized queries keep user input as data, not executable syntax. Use both as defense in depth.
Common pitfall: Trusting input validation or escaping alone. Escaping is error-prone and bypassable โ parameterized queries keep user input as data, not executable syntax. Use both as defense in depth.
๐ Tools & Takeaway
sqlmap
Burp Suite
OWASP ZAP
NoSQLMap
Nikto
semgrep
Key Takeaway: Never concatenate untrusted input into commands or queries โ always use parameterized queries, with least-privilege access and input validation as defense in depth.