MSSQL (1433)

ko-fi

MSSQL supports two authentication modes, which means that users can be created in Windows or the SQL Server:

Authentication Type

Description

Windows authentication mode

This is the default, often referred to as integrated security because the SQL Server security model is tightly integrated with Windows/Active Directory. Specific Windows user and group accounts are trusted to log in to SQL Server. Windows users who have already been authenticated do not have to present additional credentials.

Mixed mode

Mixed mode supports authentication by Windows/Active Directory accounts and SQL Server. Username and password pairs are maintained within SQL Server.

Metasploit

Metasploit

Brute Force

Hydra

Medusa

Metasploit

Metasploit

Nmap

NXC

RID brute forcing

MSSQL coercion

SharpSQLPwn

pySQLRecon

MSSQL Databases

Default System Database
Description

master

Tracks all system information for an SQL server instance

model

Template database that acts as a structure for every new database created. Any setting changed in the model database will be reflected in any new database created after changes to the model database

msdb

The SQL Server Agent uses this database to schedule jobs & alerts

tempdb

Stores temporary objects

resource

Read-only database containing system objects included with SQL server

Nmap

Unicode Collation

Automated Exploitation

Linux

Mssqlclient.py

When using Windows Authentication, we need to specify the domain name or the hostname of the target machine. If we don't specify a domain or hostname, it will assume SQL Authentication and authenticate against the users created in the SQL Server. Instead, if we define the domain or hostname, it will use Windows Authentication. If we are targetting a local account, we can use SERVERNAME\\accountname or .\\accountname.

sqsh

Windows / Linux

SQLCMD

GUI - Dbeaver

Ref: https://dbeaver.io/download/

Commands

XP_CMDSHELL - command execution

Enable xp_cmdshell

If xp_cmdshell is not enabled, we can enable it, if we have the appropriate privileges, using the following command:

With mssqlclient.py

Note: We don't actually have to type RECONFIGURE as Impacket does this for us.

If The transaction ended in the trigger disable trigger

Reverse shell

References

Write files

To write files using MSSQL, we need to enable Ole Automation Procedures, which requires admin privileges, and then execute some stored procedures to create the file:

Create a file:

Read local files

mssqlclient

Steal MSSQL Service Hash

First start Responder or impacket-smbserver and execute one of the following SQL queries:

Cracking - Hashes

Hashes

Metasploit

Enum:

msf6 auxiliary(admin/mssql/mssql_enum)

set database master

Do not use "set SMBPROXY tun0"

Use IP: set SMBPROXY 10.10.14.9

Write Up HTB - Escape:

Or Zephyr:

Impersonate Existing Users

Identify Users that We Can Impersonate

With mssqlclient.py:

To get an idea of privilege escalation possibilities, let's verify if our current user has the sysadmin role:

Verifying our Current User and Role

As the returned value 0 indicates, we do not have the sysadmin role, but we can impersonate the sa user.

Impersonating the SA User

mssqlclient.py:

Communicate with Other Databases with MSSQL - Linked servers

Identify linked Servers in MSSQL

1 means is a remote server, and 0 is a linked server.

The EXECUTE statement can be used to send pass-through commands to linked servers. We add our command between parenthesis and specify the linked server between square brackets ([ ]).

If we need to use quotes in our query to the linked server, we need to use single double quotes to escape the single quote. To run multiples commands at once we can divide them up with a semi colon (;).

Exfiltrate file content:

Create new user

With PowerUpSQL

Enumeration

Discovery (SPN Scanning)

Check accessibility

Gather information

or manually

or manually

Executing commands

Option 1: xp_cmdshell is enabled on the target server

Option 2: if rpcout is enabled, xp_cmdshell can be enabled using EXECUTE('sp_configure ''xp_cmdshell'',1;reconfigure;') AT "target-sql"

Without -QueryTarget, the command tries to us xp_cmdshell on every link of the chain

or manually

sp_execute_external_script

Tools

Automated Exploitation

SQLRecon

MSQLPwner

Resources

Interesting Books

Interesting Books

Disclaimer: As an Amazon Associate, I earn from qualifying purchases. This helps support this GitBook project at no extra cost to you.

  • Nmap Network Scanning The official guide to the Nmap Security Scanner, a free and open source utility used by millions of people for network discovery, administration, and security auditing. From explaining port scanning basics for novices to detailing low-level packet crafting methods used by advanced hackers, this book by Nmap's original author suits all levels of security and networking professionals.

  • The Art of Network Penetration Testing A guide to simulating an internal security breach. You’ll take on the role of the attacker and work through every stage of a professional pentest, from information gathering to seizing control of a system and owning the network.

  • Network Basics for Hackers The book offers one of the most complete and in-depth analyses of Wi-Fi and Bluetooth networks, then progresses through the various protocols such as DNS, ARP, SMTP, and others.

Support this Gitbook

I hope it helps you as much as it has helped me. If you can support me in any way, I would deeply appreciate it.

ko-fi

buymeacoffee

Last updated