What Is SQL Injection and How Do You Prevent It?

What Is SQL Injection and How Do You Prevent It?

SQL injection is a vulnerability that arises when an application inserts data received from the user directly into a database query. By typing specially crafted input into a form field or the address bar, the attacker changes the meaning of the query and gains access to information in the database. Known for more than twenty years, this vulnerability is still among the causes of major data leaks today.

How does it work?

Imagine a product page running a query like this:

SELECT * FROM products WHERE id = ' + idFromUser + '

When the user types 15, the query works as expected. But because the input is inserted directly into the query, if the attacker types SQL code instead of a number, that code becomes part of the query too. The attacker can then:

  • Read data from other tables, such as usernames and password hashes,
  • Bypass the password check on the login screen,
  • In some cases modify or delete data,
  • Depending on the database configuration, run commands on the server.

The vulnerability can be exploited even if the application shows no error messages. In "blind" SQL injection, the attacker extracts the data character by character from small differences in the page's behaviour or response time. With automated tools, this takes minutes.

Why does it still exist?

Modern software frameworks provide safe query methods by default. The vulnerability still appears in these situations:

  • Old code written years ago that is still running
  • Queries assembled piece by piece by hand in sorting, filtering and reporting screens
  • "Quick fixes" that bypass the framework's safe methods
  • Dynamic queries built inside stored procedures
  • Off-the-shelf plugins and theme components (see WordPress security)

How do you prevent it?

1. Use parameterised queries

This is the permanent fix. With parameterised queries (prepared statements), the structure of the query and the user data are sent to the database separately. Whatever the user types is treated as data and never executed as code.

SELECT * FROM products WHERE id = ?

2. Use ORMs correctly

ORM libraries use parameterised queries automatically, but most also allow you to write "raw queries". Make sure you are not concatenating user data in these escape hatches.

3. Use an allow list where parameters are not possible

Values such as table names, column names and sort direction cannot be sent as parameters. If you take them from the user, accept only one of a set of predefined values.

4. Limit database privileges

The database account the application uses should access only the tables it needs, with only the operations it needs. Even if a vulnerability is found, its impact stays limited.

5. Hide error messages

Detailed database errors should not be shown to users, only written to logs. This is not a fix on its own, but it makes the attacker's job harder.

6. Do not rely on a web application firewall

WAFs can block known attack patterns, but they can often be bypassed with carefully crafted input. A WAF is an extra layer; it is no substitute for fixing the code.

SQL injection and other types of injection

The same logic applies wherever user data turns into a command or a query: operating system commands, LDAP queries, NoSQL databases and template engines. In the OWASP Top 10, these vulnerabilities are grouped under "Injection". See What is the OWASP Top 10?

Is it in your application?

Automated scanners can find simple SQL injections, but they often miss vulnerabilities behind authentication, in complex workflows and in APIs. In a web application penetration test, we test every input point of your application by hand and report the vulnerabilities we find with evidence and code-level remediation advice.

2DR

Cyber Security Team

Articles by the 2DR team, working in penetration testing, incident response and personal digital security.

LinkedIn profile