SQL Injection

SQL injection is an attack in which someone enters specially crafted text into a website or app so it gets run as a database command, letting them read, change or delete data they shouldn't be able to reach.

IntermediateCategory: Website, application and API security

Also called: SQLi, SQL injection attack, injection attack

What is SQL injection?

Most web apps store data in a database and use SQL commands to read and write it. If an app builds those commands by pasting in whatever a user typed, an attacker can type SQL instead of, say, a username, and the database will run it. The result can be stolen customer records, bypassed logins or deleted data.

How does SQL injection work?

A login form might build the query `SELECT * FROM users WHERE name = '` + input + `'`. If someone enters `' OR '1'='1`, the condition becomes always true and the query returns every user. More advanced attacks extract entire databases or, in some setups, run commands on the server.

Example

In 2023 the Cl0p ransomware gang exploited a SQL injection flaw in the MOVEit Transfer file-sharing product. CISA and the FBI described how the group used it to install a web shell and steal data from organizations around the world, affecting thousands of businesses and millions of people.

How to prevent SQL injection

CISA and the FBI's Secure by Design alert points out that SQL injection has been well understood for decades and urges software makers to eliminate it entirely. OWASP's guidance:

  • Use parameterized queries (prepared statements) everywhere, so input is always treated as data, never as code.
  • Validate input against expected formats.
  • Give the app's database account the least privilege it needs.
  • Keep third-party software patched, since many SQL injection attacks hit off-the-shelf products.
  • A web application firewall can block common attempts, but it's a backstop, not a fix.

Related terms

Sources

  1. Secure by Design Alert: Eliminating SQL Injection Vulnerabilities in Software (CISA and FBI)
  2. #StopRansomware: CL0P Ransomware Gang Exploits CVE-2023-34362 MOVEit Vulnerability (AA23-158A) (CISA and FBI)
  3. SQL Injection Prevention Cheat Sheet (OWASP)

Drafted with AI assistance, then edited and fact-checked by a person. How we use AI.