πŸ—„οΈSQL Database Attacks

🎯 Overview

This document covers exploitation techniques against SQL databases (MySQL and MSSQL), focusing on practical attack methodologies from HTB Academy's "Attacking Common Services" module. Database attacks can lead to data extraction, command execution, privilege escalation, and lateral movement.

"Database hosts are considered to be high targets since they are responsible for storing all kinds of sensitive data, including user credentials, PII, business-related data, and payment information. These services often are configured with highly privileged users."

πŸ—οΈ SQL Attack Methodology

Attack Chain Overview

Service Discovery β†’ Authentication Bypass β†’ Database Enumeration β†’ Data Extraction β†’ Command Execution β†’ Lateral Movement

Key Attack Vectors

  • Authentication Bypass (Default credentials, timing attacks)

  • Database Enumeration (Tables, schemas, sensitive data)

  • Command Execution (xp_cmdshell, UDF functions)

  • File Operations (Read/write local files)

  • Hash Stealing (SMB integration attacks)

  • Privilege Escalation (User impersonation)

  • Lateral Movement (Linked servers)


πŸ“ Service Discovery & Analysis

Default Ports & Scanning

Key Information to Extract

  • Database Version (vulnerability research)

  • Authentication Mode (Windows vs Mixed)

  • Domain Information (for privilege escalation)

  • SSL Configuration (encryption status)

  • Service Account details


πŸ” Authentication Mechanisms & Bypass

1. MSSQL Authentication Types

Windows Authentication Mode

  • Integrated Security with Windows/Active Directory

  • Pre-authenticated Windows users don't need additional credentials

  • Domain-based privilege management

Mixed Mode Authentication

  • Windows/AD accounts + SQL Server accounts

  • Username/password pairs maintained within SQL Server

  • Higher attack surface due to dual authentication

2. MySQL Authentication Methods

  • Username/password authentication

  • Windows authentication (plugin required)

  • Socket-based authentication

3. Historical Vulnerabilities

CVE-2012-2122 - MySQL Timing Attack


πŸ”“ Protocol Specific Attacks

1. Database Connection & Authentication

MySQL Connection

MSSQL Connection Methods

Windows Authentication


πŸ—„οΈ Database Enumeration & Data Extraction

1. Default System Databases

MySQL System Schemas

  • mysql - System database with server information

  • information_schema - Database metadata access

  • performance_schema - Server execution monitoring

  • sys - Performance Schema interpretation objects

MSSQL System Databases

  • master - SQL Server instance information

  • msdb - SQL Server Agent usage

  • model - Template for new databases

  • resource - Read-only system objects

  • tempdb - Temporary objects storage

2. Database Enumeration Commands

Show Databases

Select Database

Show Tables

Extract Table Data


πŸ’» Command Execution Techniques

1. MSSQL Command Execution

xp_cmdshell Usage

Enable xp_cmdshell

2. MySQL Command Execution

User Defined Functions (UDF)


πŸ“‚ File Operations

1. Write Local Files

MySQL File Writing

MSSQL File Writing

2. Read Local Files

MSSQL File Reading

MySQL File Reading


πŸ•·οΈ Hash Stealing Attacks

1. MSSQL Service Hash Capture

Using xp_dirtree

Using xp_subdirs

2. Capture Setup

Responder Setup

Impacket SMB Server


πŸ‘€ Privilege Escalation

1. User Impersonation

Identify Impersonatable Users

Check Current Privileges

Impersonate Higher Privileged User


🌐 Lateral Movement

1. Linked Servers

Identify Linked Servers

Execute Commands on Linked Servers


πŸ“ Skills Assessment Examples

Example 1: Service Hash Capture

Task: Capture MSSQL service hash using xp_dirtree

Example 2: Database Enumeration

Task: Find flag in "flagDB" database

Example 3: Privilege Escalation

Task: Escalate to sysadmin via impersonation


πŸ›‘οΈ Defense & Mitigation

Database Security Hardening

  • Disable unnecessary features (xp_cmdshell, Ole Automation)

  • Implement strong authentication

  • Use least privilege principles

  • Network segmentation for database servers

  • Regular security updates

  • Monitor file operations

Detection Strategies

  • Monitor failed authentication attempts

  • Alert on xp_cmdshell usage

  • Track file read/write operations

  • Log impersonation activities

  • Monitor linked server queries

  • Detect SMB connection attempts



πŸ“š References

  • HTB Academy - Attacking Common Services Module

  • Microsoft SQL Server Documentation - Security best practices

  • MySQL Security Documentation - Hardening guidelines

  • OWASP Database Security - Common vulnerabilities

  • CVE-2012-2122 - MySQL authentication bypass


🎯 HTB Academy Lab Scenarios

Scenario 1: Initial Database Access

Scenario 2: MSSQL Service Hash Capture

Task: Find password for "mssqlsvc" user via hash stealing

Terminal 1 - Start SMB Server

Terminal 2 - Execute Hash Stealing Attack

Captured Hash Output

Scenario 3: Flag Enumeration with Escalated Privileges

Task: Enumerate "flagDB" database and extract flag

Connect with mssqlsvc Account

Database and Table Enumeration

Flag Extraction

Answer: HTB{...}


πŸ“‹ SQL Attack Checklist

Authentication Attacks

Database Exploitation

Post-Exploitation


πŸ›‘οΈ Defense & Detection

Security Hardening

  • Disable xp_cmdshell and dangerous stored procedures

  • Implement least privilege database access

  • Use strong authentication and password policies

  • Network segmentation for database servers

  • Regular security updates and patches

Detection Strategies

  • Monitor xp_cmdshell usage and command execution

  • Alert on file operations (LOAD_FILE, INTO OUTFILE)

  • Track authentication failures and unusual login patterns

  • Monitor SMB connections from database servers

  • Log impersonation activities and privilege changes



This document provides comprehensive SQL database attack methodologies based on HTB Academy's "Attacking Common Services" module, focusing on practical exploitation techniques for penetration testing and security assessment.

Last updated