githubEdit

MSSQL

Pentesting MSSQL - TCP Port 1433

Theory

Microsoft SQL Server (MSSQL) is a relational database management system developed by Microsoft. By default, it runs on port TCP 1433

Default MS-SQL System Tables:

  • master Database: Records all the system-level information for an instance of SQL Server.

  • msdb Database: Is used by SQL Server Agent for scheduling alerts and jobs.

  • model Database: Is used as the template for all databases created on the instance of SQL Server. Modifications made to the model database, such as database size, collation, recovery model, and other database options, are applied to any databases created afterwards.

  • Resource Databas: Is a read-only database that contains system objects that are included with SQL Server. System objects are physically persisted in the Resource database, but they logically appear in the sys schema of every database.

  • tempdb Database : Is a work-space for holding temporary objects or intermediate result sets.

Practice

Enumerate

Using nmap scripts, we can enumerate the version of the TNS-Listener

# Usefull Scipts
nmap --script ms-sql-info -p 1433 <target-ip>
nmap --script ms-sql-config -p 1433 <target-ip>
nmap --script ms-sql-empty-password,ms-sql-xp-cmdshell -p 1433 <target-ip>
nmap --script ms-sql-* -p 1433 <target-ip>

# Run all Scripts
nmap --script ms-sql-info,ms-sql-empty-password,ms-sql-xp-cmdshell,ms-sql-config,ms-sql-ntlm-info,ms-sql-tables,ms-sql-hasdbaccess,ms-sql-dac,ms-sql-dump-hashes --script-args mssql.instance-port=1433,mssql.username=sa,mssql.password=,mssql.instance-name=MSSQLSERVER -sV -p 1433 <IP>

Enumerate DB Objects

To enumerate Databases, Tables, Columns, Users, Permissions, refers to the following page

Enum Databaseschevron-right

Brute Force Credentials

triangle-exclamation

Using NetExecarrow-up-right, we may bruteforce MSSQL credentials.

Sign-in

Using mssqlclientarrow-up-right from Impacketarrow-up-right, we can login to an MSSQL instance.

Remote Code Execution

Tools like NetExecarrow-up-right can be used to execute OS commands from MSSQL

Local Code Execution

To localy execute/read/write files on an MSSQL instance, see the following page:

Read/Write/Executechevron-right

Coerced Auths (Stealing NTLM Hash)

On MS-SQL (Microsoft SQL) servers, the EXEC method can be used to access a remote SMB share. MSSQL uses Keberos to authenticate users so we can retrieve the NTLM hash.

Living off the landchevron-right

MSSQL Privilege Escalation

SQL Server has a special permission, named IMPERSONATE, that allows the executing user to take on the permissions of another user or login until the context is reset or the session ends.

UNIX-Like

From an UNIX-Like host, using NetExecarrow-up-right, we can enumerate for impersonation privileges and PrivEsc as follows

Windows

To enumerate users that you can impersonate, run the following queries

We may also use mssqlclientarrow-up-right from Impacketarrow-up-right to enumerate users that we can impersonate

If you can impersonate a user, even if he isn't sysadmin, you should check if the user has access to other databases or linked servers.

Note that once you are sysadmin you can impersonate any other one:

Local Privilege Escalation

The user running MSSQL server will have enabled the privilege token SeImpersonatePrivilege. You probably will be able to escalate to Administrator or NT AUTHORITY\SYSTEM following this page:

Abusing Tokenschevron-right

Linked SQL Servers Abuse

Linked serversarrow-up-right are typically configured to enable the database engine to execute a Transact-SQL statement that includes tables in another instance of SQL Server. From an attacking perspective, misconfigured linked servers can enable privilege escalation, lateral movement, and unauthorized data access by pivoting through trusted database connections.

From an UNIX-Like machine, we can enumerate Linked SQL Servers using MssqlClient.pyarrow-up-right or MSSqlPwnerarrow-up-right.

circle-info

We can also enumerate Linked Servers using the followins SQL query on a MSSQL instance:

Resources

Last updated