Skip to content

Commit 0fbaa8c

Browse files
authored
fix dockerized mysql function
1 parent 49a25c3 commit 0fbaa8c

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

usyd/scripts-library/dvwa-pentest-lab.sh

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ ubuntu_install_mysql_docker_vulnerable() {
299299
local DVWA_DB_NAME="dvwa"
300300
local DVWA_DB_USER="dvwa"
301301
local DVWA_DB_PASS="pass"
302-
local DB_HOST="localhost" # DVWA will connect to localhost, which is mapped to the container
302+
local DB_HOST="127.0.0.1" # DVWA will connect to localhost, which is mapped to the container
303303

304304
print_warn "This will stop and remove existing MySQL/MariaDB packages and data, and then install Docker."
305305
print_warn "It will then deploy a MySQL ${VULN_MYSQL_VERSION} Docker container."
@@ -340,6 +340,14 @@ ubuntu_install_mysql_docker_vulnerable() {
340340
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
341341
systemctl enable docker --now
342342
print_success "Docker installed and started."
343+
TARGET_USER=$(logname 2>/dev/null || echo "$SUDO_USER")
344+
if [ -z "$TARGET_USER" ]; then
345+
print_error "Could not determine target user. Exiting."
346+
exit 1
347+
fi
348+
USER_HOME=$(eval echo "~${TARGET_USER}")
349+
sudo usermod -aG docker "$TARGET_USER"
350+
print_warn "User '$TARGET_USER' added to 'docker' group. Please LOG OUT and LOG BACK IN (or reboot) for this change to take full effect."
343351

344352
# 3. Pull and Run MySQL Docker Container
345353
# Detect system architecture
@@ -361,13 +369,20 @@ ubuntu_install_mysql_docker_vulnerable() {
361369
print_info "Starting MySQL ${VULN_MYSQL_VERSION} Docker container '${CONTAINER_NAME}'.."
362370
# Map container port 3306 to host port 3306, relying on native DB being purged.
363371
# Environment variables handle initial root password, DVWA database, and DVWA user creation.
364-
docker run --name "${CONTAINER_NAME}" \
365-
-e MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD}" \
366-
-e MYSQL_DATABASE="${DVWA_DB_NAME}" \
367-
-e MYSQL_USER="${DVWA_DB_USER}" \
368-
-e MYSQL_PASSWORD="${DVWA_DB_PASS}" \
369-
-p 3306:3306 \
370-
-d "${MYSQL_IMAGE}:${VULN_MYSQL_VERSION}" || { print_error "Failed to start MySQL Docker container. Check 'docker logs ${CONTAINER_NAME}' for details."; exit 1; }
372+
# docker run --name "${CONTAINER_NAME}" \
373+
# -e MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD}" \
374+
# -e MYSQL_DATABASE="${DVWA_DB_NAME}" \
375+
# -e MYSQL_USER="${DVWA_DB_USER}" \
376+
# -e MYSQL_PASSWORD="${DVWA_DB_PASS}" \
377+
# -p 3306:3306 \
378+
# -d "${MYSQL_IMAGE}:${VULN_MYSQL_VERSION}"
379+
docker run --name "${CONTAINER_NAME}" \
380+
--network host \
381+
-e MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD}" \
382+
-e MYSQL_DATABASE="${DVWA_DB_NAME}" \
383+
-e MYSQL_USER="${DVWA_DB_USER}" \
384+
-e MYSQL_PASSWORD="${DVWA_DB_PASS}" \
385+
-d "${MYSQL_IMAGE}:${VULN_MYSQL_VERSION}" || { print_error "Failed to start MySQL Docker container. Check 'docker logs ${CONTAINER_NAME}' for details."; exit 1; }
371386

372387
print_info "Waiting for MySQL container to become healthy (up to 90 seconds).."
373388
local RETRIES=18 # 18 * 5 seconds = 90 seconds
@@ -392,21 +407,35 @@ ubuntu_install_mysql_docker_vulnerable() {
392407
exit 1
393408
fi
394409
sed -i 's/\r//g' "$CONFIG_FILE" # Remove Windows-style carriage returns
395-
sed -i "/'db_server'/c\\
396-
\$_DVWA[ 'db_server' ] = '$DB_HOST';
397-
" "$CONFIG_FILE"
398-
sed -i "/'db_user'/c\\
399-
\$_DVWA[ 'db_user' ] = '$DVWA_DB_USER';
400-
" "$CONFIG_FILE"
401-
sed -i "/'db_password'/c\\
402-
\$_DVWA[ 'db_password' ] = '$DVWA_DB_PASS';
403-
" "$CONFIG_FILE"
410+
411+
# Update db_server to $DB_HOST - e.g. '127.0.0.1' to force TCP connection
412+
sed -i "s|^\s*\$_DVWA\s*\[\s*'db_server'\s*\]\s*=\s*'[^']*';|\\\$_DVWA['db_server'] = '${DB_HOST}';|" "$CONFIG_FILE"
413+
sed -i "s|^\s*\$_DVWA\s*\[\s*'db_user'\s*\]\s*=\s*'[^']*';|\\\$_DVWA['db_user'] = '${DVWA_DB_USER}';|" "$CONFIG_FILE"
414+
sed -i "s|^\s*\$_DVWA\s*\[\s*'db_password'\s*\]\s*=\s*'[^']*';|\\\$_DVWA['db_password'] = '${DVWA_DB_PASS}';|" "$CONFIG_FILE"
415+
sed -i "s|^\s*\$_DVWA\s*\[\s*'db_database'\s*\]\s*=\s*'[^']*';|\\\$_DVWA['db_database'] = '${DVWA_DB_NAME}';|" "$CONFIG_FILE"
416+
sed -i "s|^\s*\$_DVWA\s*\[\s*'db_port'\s*\]\s*=\s*'[^']*';|\\\$_DVWA['db_port'] = '${DB_PORT:-3306}';|" "$CONFIG_FILE"
404417
print_info "DVWA configuration file updated for Dockerized MySQL."
405418

419+
print_info "Restart apache2 service.."
420+
sudo systemctl restart apache2
421+
406422
print_success "Vulnerable MySQL (${VULN_MYSQL_VERSION}) Docker container is running and DVWA is configured to use it!"
407423
print_info "Remember to ensure your DVWA web server (Apache2) is running on the Ubuntu VM."
408424
print_info "Go to DVWA's /setup.php page in your browser (http://localhost/dvwa/setup.php) to configure and create the database tables."
409425
print_info "MySQL Docker container '${CONTAINER_NAME}' is configured to start automatically with Docker."
426+
427+
print_info "Verifying DVWA login page is accessible..."
428+
429+
# Give the web server a few seconds to start fully
430+
sleep 5
431+
432+
if curl -s -o /dev/null -w "%{http_code}" http://localhost/dvwa/login.php | grep -q "200"; then
433+
print_success "DVWA login page is accessible at http://localhost/dvwa/login.php"
434+
else
435+
print_error "DVWA login page is NOT accessible. Check Apache2 and Docker MySQL container."
436+
print_info "Try checking Apache logs: journalctl -u apache2 or /var/log/apache2/error.log"
437+
print_info "Also check Docker MySQL logs: docker logs ${CONTAINER_NAME}"
438+
fi
410439
}
411440

412441
# Function: ubuntu_set_dvwa_permissions

0 commit comments

Comments
 (0)