SQL Injection Image

SQL Injection

SQL Injection occurs when user input is improperly sanitized allowing attackers to inject malicious SQL statements into a query. This can happen through web forms, URL parameters, or any input field that interacts with a database.

Example Scenario

Imagine a login form where users enter their username and password. The backend query might look something like this:

      
        SELECT * FROM users WHERE username = 'input' AND password = 'input';
      
    

If the input fields are not properly sanitized, attackers can manipulate this query to gain unauthorized access.


OR-Based SQL Injection

The OR-based SQL Injection involves using the OR operator to manipulate the query logic. This can trick the system into granting access without proper authentication.

How it works:

Let's say an attacker inputs the following in the username field:

      
        admin' OR '1'='1
      
    
The query sent to the database would look like this:
      
        SELECT * FROM users WHERE username = 'admin' OR '1'='1' AND password = 'input';
      
    
Here's what happens: Why it's dangerous:

The attacker can bypass authentication and access protected areas of the application, potentially exposing sensitive information.


Comment-Based SQL Injection

The Comment-Based SQL Injection uses SQL comments to ignore parts of the query. This can bypass certain security checks and expose data.

How it works:

If an attacker enters the following into the username field:

      admin' --
    
The query becomes:
      
        SELECT * FROM users WHERE username = 'admin' --' AND password = 'input';
      
    
Here's what happens: Why it's dangerous:

This allows attackers to log in as any user if the username is valid, without needing the correct password.


09-09-2024 by azedev

This blog post is intended for educational purposes only.