Time based blind SQL injection

By
Manieendar Mohan
Published on
14 Oct 2024
6 min read
Vulnerability

Time-based SQL injection is a type of inferential injection or blind injection attack.

Inferential injection attack is a type of attack in which no data is transferred between the attacker and the database, and the attacker won’t be able to get results as easily as in an in-band injection attack.

This is why it is also called a blind injection attack. An attacker can reconstruct and make a new database structure inside the database.

In a time-based attack, an attacker sends an SQL command to the server with code to force a delay in the execution of the queries.

The response time indicates whether the result of the query is true or false. Depending on the response, the attacker will execute another query.

Because the attacker has to enumerate each character by character, this is usually a slow intrusion technique, especially for large databases.

Example

In MySQL, the function SLEEP can be used, this function is only available since MySQL 5

https://example.beaglesecurity/. com/report.php?id=23 order by 5--+
https://example.beaglesecurity/. com/report.php?id=23 union select 1,2,3,4,5 --

When an attacker tries to use these functions in the query and if he is successful in slowing the response, it proves SQL injection is possible and the server is using MySQL as a database.

This can lead the attacker to inject a more complex payload.

https://example.beaglesecurity/. com/report.php?id=23 order by 5--+
https://example.beaglesecurity/. com/report.php?id=23 union select 1,2,3,4,5 --

Likewise in SQL Server, the functions WAIT FOR DELAY can be used to suspend the execution for the specified amount of time or WAIT FOR TIME can be used to suspend the execution of the query and continue it when system time is equal to the parameter.

What are the impacts of time-based blind SQL injection?

Time-based blind SQL injection is particularly challenging to detect because it doesn’t rely on standard error messages or visible changes in the application’s behavior.

Here are some potential impacts of time-based blind SQL injection:

1. Data exfiltration

Attackers can use time delays to extract sensitive information from a database.

By injecting malicious queries and observing the time it takes for the application to respond, they can infer whether certain conditions are true or false, allowing them to gradually retrieve data.

2. Unauthorized access

Successful exploitation of Time-Based Blind SQL Injection may lead to unauthorized access to a database or other critical systems.

Attackers can manipulate the injected queries to bypass authentication mechanisms and gain unauthorized control.

3. Data manipulation

In addition to extracting data, attackers can use Time-Based Blind SQL Injection to modify or delete existing data in the database.

This can have severe consequences for the integrity of the data and the proper functioning of the affected application.

4. Application disruption

Time-Based Blind SQL Injection attacks can be used to disrupt the normal operation of an application.

By injecting queries that cause time delays, an attacker can slow down or temporarily disable certain functionalities, leading to a denial of service.

5. Reconnaissance and enumeration

Attackers can use Time-Based Blind SQL Injection as a means of reconnaissance to gather information about the structure and content of the database.

This information can be valuable for planning further attacks or identifying additional vulnerabilities.

6. Compromising user credentials

If the application stores user credentials in a database, successful exploitation of Time-Based Blind SQL Injection can allow attackers to retrieve and compromise user login credentials.

7. Business impact

A successful Time-Based Blind SQL Injection attack can have significant business impact, including reputational damage, financial losses, and legal consequences.

Breaches of sensitive data can lead to a loss of customer trust and legal liabilities.

How can you prevent time based blind SQL injection?

Preventing and mitigating Time-Based Blind SQL Injection involves a combination of secure coding practices, input validation, and using prepared statements or parameterized queries.

Here are some strategies to help prevent or mitigate Time-Based Blind SQL Injection:

1. Input validation

Implement strict input validation on both client and server sides. Validate and sanitize all user inputs to ensure they conform to expected formats and lengths.

This helps prevent the injection of malicious SQL code.

2. Use parameterized queries

Instead of dynamically constructing SQL queries by concatenating user inputs, use parameterized queries or prepared statements provided by your programming language or framework.

Parameterized queries separate the SQL code from the user input, making it much harder for attackers to inject malicious code.

3. Least privilege principle

Ensure that database accounts used by your application have the least privilege necessary. Avoid using accounts with excessive permissions.

For example, a web application should only have permissions to execute specific stored procedures or queries required for its functionality.

4. Database firewalls

Implement database firewalls or intrusion detection/prevention systems that can detect and block malicious SQL injection attempts. These systems can analyze the SQL traffic and identify patterns indicative of an attack.

5. Error handling

Customize error messages to reveal as little information as possible to potential attackers.

Generic error messages can help prevent attackers from gaining insights into the database structure and query execution.

6. Time-based execution thresholds

Set time-based execution thresholds for database queries.

If a query takes longer than the predefined threshold, consider logging the incident and taking appropriate actions, such as blocking the IP address making the request.

7. Regular security audits

Conduct regular security audits and penetration testing on your application. Identify and address potential vulnerabilities before they can be exploited.

Automated tools and manual testing can be employed to check for SQL injection vulnerabilities.

8. Web Application Firewalls (WAFs)

Deploy a Web Application Firewall that can analyze and filter HTTP traffic between a web application and the internet. A WAF can help block SQL injection attempts and other malicious activities.

9. Update and patch

Keep your web application framework, database management system, and any third-party libraries up to date with the latest security patches. Many security vulnerabilities are addressed in updates.

10. Educate developers

Train and educate developers on secure coding practices, including the risks associated with SQL injection. Encourage the use of secure coding guidelines and conduct regular security awareness training.

By implementing a combination of these measures, you can significantly reduce the risk of Time-based Bbind SQL injection and enhance the overall security posture of your web applications.

Automated human-like penetration testing for your web apps & APIs
Teams using Beagle Security are set up in minutes, embrace release-based CI/CD security testing and save up to 65% with timely remediation of vulnerabilities. Sign up for a free account to see what it can do for you.

Written by
Manieendar Mohan
Manieendar Mohan
Cyber Security Lead Engineer
Experience the Beagle Security platform
Unlock one full penetration test and all Advanced plan features free for 10 days
Find surface-level website security issues in under a minute
Free website security assessment
Experience the power of automated penetration testing & contextual reporting.