What is SQL Injection?
SQL injection is a security vulnerability where attackers inject malicious SQL code, preventable with parameterized queries.
SQL Injection is a security vulnerability where attackers inject malicious SQL code into your database queries. It's like someone sneaking a fake order into your restaurant's order system — they can access data they shouldn't or even delete everything.
How It Works:
- Your app builds SQL queries using user input
- Attacker provides malicious input (like
'; DROP TABLE users; --) - Your app executes the malicious SQL
- Attacker can read, modify, or delete data
Example:
# VULNERABLE CODE
query = "SELECT * FROM users WHERE name = '" + user_input + "'"
# If user_input = "'; DROP TABLE users; --"
# Query becomes: SELECT * FROM users WHERE name = ''; DROP TABLE users; --'
How to Prevent:
- Parameterized queries: Use placeholders, never concatenate
- Input validation: Validate and sanitize all inputs
- Least privilege: Database users should have minimal permissions
- ORM: Use Object-Relational Mapping tools that handle this
Impact:
- Data theft: Steal sensitive information
- Data loss: Delete or corrupt data
- Unauthorized access: Bypass authentication
- System compromise: Gain control of the database
FAQ
Is SQL injection still a problem?
Yes! It's still one of the most common vulnerabilities. Always use parameterized queries.
Do ORMs prevent SQL injection?
Yes, if used correctly. ORMs use parameterized queries automatically. But you can still write vulnerable code with ORMs if you're not careful.