SQL injection (SQLi) (original) (raw)

What is a SQL injection (SQLi)?

A SQL injection (SQLi) 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 manipulates Structured Query Language 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 Microsoft SQL Server.

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 interprets and then executes 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 enables 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.

In-band SQLi

Also known as classic SQLi, in-band SQLi is when hackers use the same channel -- or band -- to launch database errors and to collect the results from an attack. In-band SQLi is most commonly achieved through two methods:

  1. Error-based injection techniques force the database to produce error messages that reveal information about the structure of the database.
  2. Union-based attacks use prepared statements that exploit the SQL union function, which combines the results of multiple queries into one result.

Inferential SQLi

Also known as blind SQLi, 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 occurring within the database. The server's response provides attackers with clues that they can use to adjust their attack strategy.

Inferential SQLi can be either Boolean-based or time-based. Boolean SQLi uses true or false statements to solicit a response, while time-based SQLi sets a designated response period.

Out-of-band SQLi

Out-of-band SQLi is when hackers take advantage of domain name system or Hypertext Transfer Protocol requests to retrieve data. Out-of-band SQLi is usually only performed when a web server is too slow or when in-band SQLi is not possible to execute.

three types of SQL injection attacks

Diagram showing the three types of SQL injection attacks

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:

Some database administrators believe that a stored procedure statement can often aid in the prevention of SQL injection attacks by restricting the types of statements that can be supplied to its parameters. However, this doesn't prevent all exploits, as there are numerous workarounds and intriguing statements that can still be provided to stored procedures.

The impact of SQL injection attacks for your business

SQL injection attacks can seriously harm a business or organization. The negative effects could include the following:

A few well-known companies that have experienced SQL injection attacks include Target, Yahoo, LinkedIn, Sony Pictures and 7-Eleven.

Examples of SQL injection attacks

There are numerous SQL injection vulnerabilities, attacks and strategies that can occur in a variety of settings.

The following are some common SQL injection examples:

The following are some examples of real-life SQL injection vulnerabilities:

History of SQL injections

SQL injection attacks have been around for over two decades since they were first documented by cybersecurity researcher and hacker Jeff Forristal in 1998. However, they didn't appear to get much attention until 2002.

SQL injections are considered one of the most common security exploits, as evidenced by their presence on the list of Open Web Application Security Project's 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.

These attacks have now become one of the most common and continuously rated top security exploits in database software history, with a succession of high-profile attacks on large organizations, such as TalkTalk, where an outdated database was the main factor behind the breach.

Successful SQL injection attacks enable attackers to modify database information, access sensitive data, perform administrative activities on the database and recover files from the database.

SQL injections and cross-site scripting are two types of application attacks that can bring an organization to its knees. Discover the five most prevalent application security threats, and learn how to protect against them.

This was last updated in April 2023

Continue Reading About SQL injection (SQLi)

Dig Deeper on Agile, DevOps and software development methodologies