SQL Injection (SQLi) is a code injection technique that attacks data-driven applications by inserting malicious SQL statements into an execution entry point. It's one of the most common web hacking techniques.
Types of SQL Injection
- Classic SQLi: UNION-based attacks that combine results from multiple tables
- Error-based SQLi: Extracting information from database error messages
- Blind SQLi: Inferring data from true/false questions sent to the database
- Time-based SQLi: Using time delays to infer database structure
Example Attack
query = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "'";
# Malicious input
username: admin'--
password: [anything]
# Resulting query
SELECT * FROM users WHERE username = 'admin'--' AND password = '[anything]'
Prevention: Use parameterized queries, stored procedures, ORM frameworks, input validation, and principle of least privilege for database accounts.