SQL injection (SQLi)
SQL injection is a security vulnerability in which an user/attacker somehow become able to inject their queries with existing sql queries.
- Prerequisite
- Techniques of sqli
- Where to find sqli
- How to detect sqli
- Impact if successful sqli
- Filter Evasion Techniques
- Mitigation
- Best practices
- SQLi automation tools
- Plateforms to learn sqli
Contents :
- Structured Query Language (SQL)
- Basics of request/response and structured databases
Prerequisite :
- In-band -
- Out-of-band
- Inferential -
- Boolean
- Time based
- Second order sqli
Techniques of sqli :
- Search functionality
- Insecure functions (in source codes)
- In url parameters
- Login functionality
- Filteration (Like: category)
- Request header
Where to find sqli :
- Try to put a quote ‘ (check each type of quotes ) with all the possible places , where might the data got sent with the SQL query.If any kind of server error (5***) occurs then there should be a sqli possible.
- Inject an query that made a significance changes in response like - Response time,Error/log in response
How to detect sqli :
Impact if successful sqli :
- URL Encoding (%sign followed by their ASCII value in hexadecimal)
- Hexadecimal Encoding
- Unicode Encoding
- Using Numerical Values (OR 1=1)
- Using SQL Comments (--)
- Using CONCAT() Function (CONCAT(0x61, 0x64, 0x6d, 0x69, 0x6e))
If quotes are not allowed -
- Comments to Replace Spaces (SELECT/**//*FROM/**/users)
- Tab or Newline Characters (SELECT\t*\tFROM\tusers)
- Alternate Characters (%09/%0A/%0C/%0D/%A0 in plave of %20)
If spaces are not allowed -
- Change case (SElEcT)
- Add comment (SE/**/LECT)
SELECT not allowed -
- Use && , ||
AND, OR not allowd
- Use CHAR() function => (CHAR(0x61,0x64,0x6D,0x69,0x6E))
- Use CONCAT() funciton => (CONCAT('a','d','m','i','n'))
Specific word not allowd
Filter Evasion Techniques -
Mitigation :
Prepared Statements (With Parameterized Queries) - In a prepared query, the first thing a developer writes is the SQL query, and then any user inputs are added as parameters afterwards. Writing prepared statements ensures the SQL code structure doesn't change and the database can distinguish between the query and the data. As a benefit, it also makes your code look much cleaner and easier to read.
Input Validation - Input validation can go a long way to protecting what gets put into an SQL query. Employing an allow list can restrict input to only certain strings, or a string replacement method in the programming language can filter the characters you wish to allow or disallow.
Escaping User Input - Allowing user input containing characters such as ' " $ \ can cause SQL Queries to break or, even worse, as we've learnt, open them up for injection attacks. Escaping user input is the method of prepending a backslash (\) to these characters, which then causes them to be parsed just as a regular string and not a special character.
Best practices -
- Parameterised Queries and Prepared Statements
- Input Validation and Sanitisation
- Least Privilege Principle
- Stored Procedures
- Regular Security Audits and Code Reviews
- Exploiting Database-Specific Features (execute system command etc)
- Leveraging Error Messages
- Bypassing WAF and Filters
- Database Fingerprinting
- Pivoting with SQL Injection
For Secure Coders -
For Pentesters -
- SQLMap
- SQLNinja
- JSQL Injection
- BBQSQL