Blog | G5 Cyber Security

SQL Injection: Bypassing Escape Functions

TL;DR

This guide shows how to bypass a simple SQL escape function using different techniques. It’s for educational purposes only – never use these methods on systems you don’t have permission to test!

Understanding the Problem

Many web applications try to prevent SQL injection by escaping special characters in user input before including it in a query. However, these escape functions aren’t always perfect and can be bypassed.

Scenario

Let’s assume an application uses a function like this (PHP example) to escape single quotes:

function escapeString($string) {
  return str_replace("'", "''", $string);
}

This replaces a single quote (‘) with two single quotes (”). While it prevents basic injection, it’s vulnerable to clever bypasses.

Bypass Techniques

  1. Double Encoding: If the application decodes user input multiple times, you can encode characters repeatedly.
  • Character Set Exploitation: Different character sets represent characters differently. You can try using alternative representations of special characters.
  • Concatenation: Break up your injection string into smaller parts and concatenate them within the query.
  • Using Comments: Insert comments to terminate the original query and start a new one.
  • Hex Encoding: Some applications might decode hex-encoded strings.
  • Case Sensitivity Exploits: Some databases are case-insensitive. Try using mixed-case keywords like SeLeCt.
  • Practical Example

    Let’s say the vulnerable application uses the following query:

    SELECT * FROM products WHERE name = '$name'

    And the escape function is the simple one shown earlier. Here are some potential bypasses:

    1. Using Comments:
  • Concatenation:
  • Important Considerations

    Disclaimer

    This information is for educational purposes only. Attempting to exploit vulnerabilities without permission is illegal and unethical. Always obtain explicit consent before testing any system.

    Exit mobile version