1433 - MSSQL
Basic Information
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). From wikipedia.
Default port: 1433
1433/tcp open ms-sql-s Microsoft SQL Server 2017 14.00.1000.00; RTMDefault 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.
Enumeration
Automatic Enumeration
If you don't know nothing about the service:
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>
msf> use auxiliary/scanner/mssql/mssql_pingIf 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
Common Enumeration
Get User
Get Permissions
Some introduction about some MSSQL terms:
Securable: These are the resources to which the SQL Server Database Engine authorization system controls access. There are three broader categories under which a securable can be differentiated:
Server – For example databases, logins, endpoints, availability groups and server roles
Database – For example database role, application roles, schema, certificate, full text catalog, user
Schema – For example table, view, procedure, function, synonym
Permission: Every SQL Server securable has associated permissions like ALTER, CONTROL, CREATE that can be granted to a principal. Permissions are managed at the server level using logins and at the database level using users.
Principal: The entity that receives permission to a securable is called a principal. The most common principals are logins and database users. Access to a securable is controlled by granting or denying permissions or by adding logins and users to roles which have access.
Tricks
Execute OS Commands
{% hint style="danger" %} Note that in order to be able to execute commands it's not only necessary to have xp_cmdshell enabled, but also have the EXECUTE permission on the xp_cmdshell stored procedure. You can get who (except sysadmins) can use xp_cmdshell with:
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).
You can check if who (apart sysadmins) has permissions to run those MSSQL functions with:
Using tools such as responder or Inveigh it's possible to steal the NetNTLM hash. You can see how to use these tools in:
Abusing MSSQL trusted Links
Read this post to find more information about how to abuse this feature
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
Did you know that crypto projects pay more bounty rewards than their web2 counterparts? This crypto bounty alone is worth $1.000.000! Check out the top-paying bounties among crypto projects. Sign up on HackenProof to get rewarded without delays and become the web3 hacker legend.
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.
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/
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:
{% content-ref url="../../windows-hardening/windows-local-privilege-escalation/roguepotato-and-printspoofer.md" %} roguepotato-and-printspoofer.md {% endcontent-ref %}
{% content-ref url="../../windows-hardening/windows-local-privilege-escalation/juicypotato.md" %} juicypotato.md {% endcontent-ref %}
Shodan
port:1433 !HTTP
References
Did you know that crypto projects pay more bounty rewards than their web2 counterparts? This crypto bounty alone is worth $1.000.000! Check out the top-paying bounties among crypto projects. Sign up on HackenProof to get rewarded without delays and become the web3 hacker legend.
HackTricks Automatic Commands
Last updated