1433 - Pentesting MSSQL - Microsoft SQL Server

Basic Information

From wikipedia:

Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which may run either on the same computer or on another computer across a network (including the Internet).\

Default port: 1433

1433/tcp open  ms-sql-s      Microsoft SQL Server 2017 14.00.1000.00; RTM

Default MS-SQL System Tables

  • master Database: This database is crucial as it captures all system-level details for a SQL Server instance.

  • msdb Database: SQL Server Agent utilizes this database to manage scheduling for alerts and jobs.

  • model Database: Acts as a blueprint for every new database on the SQL Server instance, where any alterations like size, collation, recovery model, and more are mirrored in newly created databases.

  • Resource Database: A read-only database that houses system objects that come with SQL Server. These objects, while stored physically in the Resource database, are logically presented in the sys schema of every database.

  • tempdb Database: Serves as a temporary storage area for transient objects or intermediate result sets.

Enumeration

Automatic Enumeration

If you don't know anything about the service:

If you don't have credentials you can try to guess them. You can use nmap or metasploit. Be careful, you can block accounts if you fail login several times using an existing username.

Metasploit (need creds)

Manual Enumeration

Login

MSSQLPwner

Common Enumeration

Get User

Types of MSSQL Users

Get Permissions

  1. Securable: Defined as the resources managed by SQL Server for access control. These are categorized into:

    • Server – Examples include databases, logins, endpoints, availability groups, and server roles.

    • Database – Examples cover database role, application roles, schema, certificates, full text catalogs, and users.

    • Schema – Includes tables, views, procedures, functions, synonyms, etc.

  2. Permission: Associated with SQL Server securables, permissions such as ALTER, CONTROL, and CREATE can be granted to a principal. Management of permissions occurs at two levels:

    • Server Level using logins

    • Database Level using users

  3. Principal: This term refers to the entity that is granted permission to a securable. Principals mainly include logins and database users. The control over access to securables is exercised through the granting or denying of permissions or by including logins and users in roles equipped with access rights.

Tricks

Execute OS Commands

MSSQLPwner

Steal NetNTLM hash / Relay attack

You should start a SMB server to capture the hash used in the authentication (impacket-smbserver or responder for example).

MSSQLPwner

Using tools such as responder or Inveigh it's possible to steal the NetNTLM hash. You can see how to use these tools in:

Spoofing LLMNR, NBT-NS, mDNS/DNS and WPAD and Relay Attacks

Read this post to find more information about how to abuse this feature:

MSSQL AD Abuse

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:

Read file with OPENROWSET

By default, MSSQL allows file read on any file in the operating system to which the account has read access. We can use the following SQL query:

However, the BULK option requires the ADMINISTER BULK OPERATIONS or the ADMINISTER DATABASE BULK OPERATIONS permission.

Error-based vector for SQLi:

RCE/Read files executing scripts (Python and R)

MSSQL could allow you to execute scripts in Python and/or R. These code will be executed by a different user than the one using xp_cmdshell to execute commands.

Example trying to execute a 'R' "Hellow World!" not working:

Example using configured python to perform several actions:

Read Registry

Microsoft SQL Server provides multiple extended stored procedures that allow you to interact with not only the network but also the file system and even the Windows Registry:

Regular

Instance-Aware

sys.xp_regread

sys.xp_instance_regread

sys.xp_regenumvalues

sys.xp_instance_regenumvalues

sys.xp_regenumkeys

sys.xp_instance_regenumkeys

sys.xp_regwrite

sys.xp_instance_regwrite

sys.xp_regdeletevalue

sys.xp_instance_regdeletevalue

sys.xp_regdeletekey

sys.xp_instance_regdeletekey

sys.xp_regaddmultistring

sys.xp_instance_regaddmultistring

sys.xp_regremovemultistring

sys.xp_instance_regremovemultistring

For more examples check out the original source.

RCE with MSSQL User Defined Function - SQLHttp

It's possible to load a .NET dll within MSSQL with custom functions. This, however, requires dbo access so you need a connection with database as sa or an Administrator role.

Following this link to see an example.

Other ways for RCE

There are other methods to get command execution, such as adding extended stored procedures, CLR Assemblies, SQL Server Agent Jobs, and external scripts.

MSSQL Privilege Escalation

From db_owner to sysadmin

If a regular user is given the role db_owner over the database owned by an admin user (such as sa) and that database is configured as trustworthy, that user can abuse these privileges to privesc because stored procedures created in there that can execute as the owner (admin).

You can use a metasploit module:

Or a PS script:

Impersonation of other users

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.

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:

You can perform this attack with a metasploit module:

or with a PS script:

Using MSSQL for Persistence

https://blog.netspi.com/sql-server-persistence-part-1-startup-stored-procedures/

Extracting passwords from SQL Server Linked Servers

An attacker can extract SQL Server Linked Servers passwords from the SQL Instances and get them in clear text, granting the attacker passwords that can be used to acquire a greater foothold on the target. The script to extract and decrypt the passwords stored for the Linked Servers can be found here

Some requirements, and configurations must be done in order for this exploit to work. First of all, you must have Administrator rights on the machine, or the ability to manage the SQL Server Configurations.

After validating your permissions, you need to configure three things, which are the following:

  1. Enable TCP/IP on the SQL Server instances;

  2. Add a Start Up parameter, in this case, a trace flag will be added, which is -T7806.

  3. Enable remote admin connection.

To automate these configurations, this repository has the needed scripts. Besides having a powershell script for each step of the configuration, the repository also has a full script which combines the configuration scripts and the extraction and decryption of the passwords.

For further information, refer to the following links regarding this attack: Decrypting MSSQL Database Link Server Passwords

Troubleshooting the SQL Server Dedicated Administrator Connection

Local Privilege Escalation

The user running MSSQL server will have enabled the privilege token SeImpersonatePrivilege. You probably will be able to escalate to Administrator following one of these 2 paged:

RoguePotato, PrintSpoofer, SharpEfsPotato, GodPotatoJuicyPotato

Shodan

  • port:1433 !HTTP

References

HackTricks Automatic Commands

Last updated