A SQL injection is a technique that attackers use to gain unauthorized access to a web application database by adding a string of malicious code to a database query.
A SQL injection (SQLi) manipulates SQL code to provide access to protected resources, such as sensitive data, or execute malicious SQL statements. When executed correctly, a SQL injection can expose intellectual property, customer data or the administrative credentials of a private business.
SQL injection attacks can be used to target any application that uses a SQL database, with websites being the most common prey. Common SQL databases include MySQL, Oracle and SQL Server.
SQL injections are considered one of the most common security exploits, as evidenced by their presence on the list of OWASP top 10 threats to web application security. The risk of SQLi exploits and the damage they can cause have both grown with the availability of automated tools for executing SQL injections. In the past, the likelihood of an enterprise being targeted with a SQL injection was somewhat limited because attackers had to carry out these exploits manually.
How does a SQL injection attack work?
A SQL query is a request for some action to be performed on an application database. Queries can also be used to run operating system commands. Each query includes a set of parameters that ensure only desired records are returned when a user runs the query. During a SQL injection, attackers exploit this by injecting malicious code into the query's input form.
The first step of a SQL injection attack is to study how the targeted database functions. This is done by submitting a variety of random values into the query to observe how the server responds.
Attackers then use what they've learned about the database to craft a query the server will interpret and then execute as a SQL command. For example, a database may store information about customers who have made a purchase with customer ID numbers. Instead of searching for a specific customer ID, an attacker may insert "CustomerID = 1000 OR 1=1" into the input field. Since the statement 1=1 is always true, the SQL query would return all available customer IDs and any corresponding data. This allows the attacker to circumvent authentication and gain administrator-level access.
In addition to returning unauthorized information, SQL attacks can be written to delete an entire database, bypass the need for credentials, remove records or add unwanted data.
How many types of SQL injection attacks are there?
There are a few different types of SQL injection attacks.
Also known as a classic SQLi, an in-band SQLi is when hackers use the same channel (or band) to launch database errors and to collect the results from an attack. An in-band SQLi is most commonly achieved through two methods: error-based and Union-based attacks.
- Error-based injection techniques force the database to produce error messages that reveal information about the structure of the database.
- Union-based attacks use prepared statements that exploit the SQL Union function, which combines the results of multiple queries into one result.
Also known as a blind SQL injection, an inferential SQLi is when hackers send data payloads to a database server to observe its response and behavior without being able to see what is actually occurring within the database. The server's response provides the attacker with clues that they can use to adjust their attack strategy.
An inferential SQLi can be either Boolean or time-based. A Boolean SQLi uses true or false statements to solicit a response, while a time-based SQLi sets a designated response period.
An out-of-band SQLi is when hackers take advantage of domain name system or HTTP requests to retrieve data. An out-of-band SQLi is usually only performed when a web server is too slow or when an in-band SQLi is not possible to execute.
How can a SQL injection attack be detected and prevented?
If a SQL injection attack is successfully carried out, it could cause extensive damage by exposing sensitive data and damaging customer trust. That's why it is important to detect this type of attack in a timely manner.
Web application firewalls (WAFs) are the most common tool used to filter out SQLi attacks. WAFs are based on a library of updated attack signatures and can be configured to flag malicious SQL queries in web applications.
To prevent a SQL injection attack from occurring, businesses can follow these practices:
1. Train employees on prevention methods.
It's important that IT teams -- including DevOps pros, system administrators and software development teams -- receive proper security training to understand how SQLi attacks happen and how they can be prevented in web applications.
2. Don't trust user input.
Any user input provided in a SQL query increases the likelihood for a successful SQL injection. The best way to mitigate this type of risk is to put security measures around user input.
3. Use an allowlist instead of a blocklist.
Validating and filtering user input via an allowlist, as opposed to a blocklist, is recommended because cybercriminals can usually bypass a blocklist.
4. Perform routing updates and use the newest version of applications.
One of the most common SQL injection vulnerabilities is outdated software. Not only is older technology unlikely to have built-in SQLi protection, but unpatched software is also often easier to manipulate. This includes programming languages, too. Older languages and syntax are more vulnerable. For example, use PDO as a substitute for older MySQL.
5. Use validated prevention methods.
Query strings written from scratch offer insufficient protection against a SQLi attack. The best way to protect web applications is through input validation, prepared statements and parameterized queries.
6. Perform regular security scans
Regularly scanning web applications will catch and remedy potential vulnerabilities before they do serious damage.