wasAsked.

What is a SQL injection attack and how do you prevent it?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · SQL, Authentication & Security

Community answers

Posted by wasAsked editorial collection

SQL injection is an attack where malicious input changes the structure of a SQL query, allowing an attacker to read, modify or delete data, bypass authentication, or in some cases execute commands on the database server. It remains one of the most serious web vulnerabilities (listed under Injection in the OWASP Top 10). How it happens Vulnerable code builds SQL by concatenating user input: var sql = "SELECT * FROM Users WHERE Email = '" + email + "' AND Password = '" + password + "'"; If an attacker enters this as the email: ' OR 1=1 -- the query becomes: SELECT * FROM Users WHERE Email = '' OR 1=1 --' AND Password = '...' 1=1 is always true, and -- comments out the rest, so the attacker logs in without a password. Other attack forms - UNION-based: ' UNION SELECT username, password_hash FROM Users -- to read other tables. - Destructive: '; DROP TABLE Orders; -- where stacked queries are allowed. - Blind SQL injection: inferring data through true/false responses or time delays (WAITFOR DELAY, pg_sleep). - Second-order injection: malicious data stored safely, then later used unsafely in another query. How to prevent it 1. Parameterized queries (the primary defence) The query structure and the data are sent separately, so input is always treated as a value, never as SQL. ADO.NET: using var cmd = new SqlCommand("SELECT Id, Name FROM Users WHERE Email = @email", connection); cmd.Parameters.Add("@email", SqlDbType.NVarChar, 256).Value = email; Dapper: var user = await conn.QuerySingleOrDefaultAsync<User>("SELECT * FROM Users WHERE Email = @Email", new { Email = email }); Java (JDBC): PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE email = ?"); ps.setString(1, email); 2. Use ORMs safely - Entity Framework Core LINQ queries are parameterized automatically. - With raw SQL in EF Core, use FromSql or FromSqlInterpolated with interpolation (converted to parameters), never FromSqlRaw with concatenated input. - JPA: use named parameters in JPQL, never string concatenation. 3. Validate and allow-list input - Validate types, lengths and formats. - For things that cannot be parameterized (column names for sorting, table names), map user choices to an allow-list of known values. 4. Least privilege - The application's database account should have only the permissions it needs (no db_owner or DROP rights). This limits the damage of any successful attack. 5. Stored procedures — only when they use parameters internally Stored procedures that build dynamic SQL with concatenation are still vulnerable; use sp_executesql with parameters. 6. Defence in depth - Do not display detailed database errors to users. - Use a web application firewall, security testing (SAST, DAST tools such as OWASP ZAP) and code reviews. - Monitor for suspicious queries and failed login spikes. Escaping input manually is error-prone and not recommended as the main defence. Interview summary: "SQL injection happens when user input is concatenated into SQL. The fix is parameterized queries or safe ORM usage, plus input validation, allow-lists for identifiers, least-privilege database accounts and hiding detailed errors."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics