Categories: SQL Injection Attacks

How to Test for SQL Injection Vulnerabilities

UNDERSTANDING OWASP TOP 10 SERIES and how to test SQL injection vulnerabilities

Open Source Web Application Consortium releases its list of top 10 web vulnerabilities, every three years. The second time in a row, Injection flaws like SQL and LAPD, have managed to keep their top position secure and have emerged as the worst web vulnerability. Injection flaws, such as SQL injection, OS, and LDAP injection occur when untrusted data is sent to an interpreter as part of a command or query. The attacker’s hostile data can trick the interpreter into executing unintended commands or accessing data without proper authorization.

Threat Agents Application Specific Consider anyone who can send untrusted data to the system, including external users, internal users, and administrators.
Attack Vectors Exploitability EASY Attacker sends simple text-based attacks that exploit the syntax of the targeted interpreter. Almost any source of data can be an injection vector, including internal sources
Security Weakness Prevalence COMMON Injection flaws occur when an application sends untrusted data to an interpreter. Injection flaws are very prevalent, particularly in legacy code, often found in:
  • SQL queries
  • LDAP queries
  • XPath queries
  • OS commands
  • Program arguments, etc.

Injection flaws are easy to discover when examining the code, but more difficult via testing. Scanners and fuzzers can help attackers find them.

Detectability AVERAGE
Technical Impacts Impact of SEVERE The injection can result in data loss or corruption, lack of accountability, or denial of access. The injection can sometimes lead to complete host takeover.
Business Impacts Application / Business Specific Consider the business value of the affected data and the platform running the interpreter. All data could be stolen, modified, or deleted. Could your reputation be harmed?
Source: OWASP

The following example will explain an injection attack scenario:

For www.vulnerable-bank.com, imagine a typical username and password login page. Let’s take an attacking example and how it works scenario.

Normal Scenario: username = author and password = author123

SQL Query:

SELECT * FROM AUTH_TABLE WHERE user =‘author’ and passw = ‘author123’

Attacking Scenario: username = author and password = ‘ or ‘1’=’1

SQL Query:

SELECT * FROM AUTH_TABLE WHERE user =‘author’ and passw = ‘ ‘ or ‘1’=’1’

The above line says, password equals to null or 1=1 (which is universal truth), so the attacker modifies logical AND to logical OR.

The best way to find out if an application is vulnerable to injection is to verify that all use of interpreters clearly separates untrusted data from the command or query. For SQL calls, this means using bind variables in all prepared statements and stored procedures and avoiding dynamic queries.

Checking the code is a fast and accurate way to see if the application uses interpreters safely. Code analysis tools can help a security analyst find the use of interpreters and trace the data flow through the application. Manual penetration testers can confirm these issues by crafting exploits that confirm the vulnerability.

Automated dynamic scanning which exercises the application may provide insight into whether some exploitable injection problems exist. Scanners cannot always reach interpreters and can have difficulty detecting whether an attack was successful.

How to prevent injections like SQL:

Preventing injection requires keeping untrusted data separate from commands and queries.

  1. The preferred option is to use a safe API that avoids the use of the interpreter entirely or provides a parameterized interface. Beware of APIs, such as stored procedures, that appear parameterized, but may still allow injection under the hood.
  2. If a parameterized API is not available, you should carefully escape special characters using the specific escape syntax for that interpreter. OWASP’s ESAPI has some of these escaping routines.
  3. Positive or “whitelist” input validation with appropriate canonicalization also helps protect against SQL injection but is not a complete defense as many applications require special characters in their input. OWASP’s ESAPI has an extensible library of white list input validation routines.

Here are some recommended best practices to prevent injection exploits:

  1. Make white box testing and secure code review an integral part of the development cycle.
  2. Manual penetration testing should be part of the QA cycle.
  3. Automated along with periodic manual checks should be part of the operations process after deployment to keep up with new vulnerabilities and the automated scanning can help perform basic security sanity checks. It is very common to have injection issues introduced even with minor code changes as such changes may not be subjected to a fully-fledged security review process in the development cycle.
  4. In situations where fixing the code of detected vulnerabilities cannot be done in a timely manner, a WAF can be used for virtual patching after deployment

Venkatesh Sundar

Venky is an Application Security technologist who built the new age Web application Scanner and Cloud WAF - AppTrana at Indusface as a Founding CTO. Currently, he spends his time on driving Product Roadmap, Customer Success, Growth, and technology adoption for US businesses.

This post was last modified on January 2, 2024 17:31

Share
Venkatesh Sundar
Published by
Venkatesh Sundar

Recent Posts

Indusface Recognized as a 2024 Gartner® Peer Insights™ Customers’ Choice for Cloud WAAP

Indusface has once again been recognized as a Gartner® Peer Insights™ Customers' Choice for Cloud… Read More

7 days ago

Top 15 DDoS Protection Best Practices

Protect your business from DDoS attacks with multi-layered DDoS defense, proactive threat modeling, rate limiting,… Read More

1 week ago

Managed WAF: A Must-Have to Stop Website Attacks

A Managed WAF is a comprehensive cybersecurity service offered by specialized providers to oversee, optimize,… Read More

2 weeks ago