MSSQL
About
Microsoft SQL (MSSQL) is Microsoft's SQL-based relational database management system. It is popular among database administrators and developers when building applications that run on Microsoft's .NET framework due to its strong native support for .NET. MSSQL have a lot of different targets. Default port is 1433 and 1434. More about MSSQL Hacking you could read here [LINK]
Authentication Mods
MSSQL have 2 authentication mods: windows authentication mode (default) and mixed. Windows authentication mode is often referred as integrated because of high integration with AD and Windows. With this you could authenticate into database automatically if you have right user or member of right group. Mixed mode is supporting both AD/Windows authentication and SQL one. If we are specifying domain, it'll use Windows auth, if we don't specify domain, it'll use SQL auth. We should use servername\\accountname
for domain specification.
Databases
MSSQL has default system databases that can help us understand the structure of all the databases that may be hosted on a target server.
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
MSSQL Useful Commands
HackTricks: [LINK]
impacket-mssqlclient carni17@13.13.13.13
Connect to the MSSQL server.
SELECT name FROM master.dbo.sysdatabases;
Show all databases
use <database>;
Select one of the existing databases
SELECT * FROM databasename.INFORMATION_SCHEMA.TABLES;
Show all available tables in the selected database
select sp.name as login, sp.type_desc as login_type, sl.password_hash, sp.create_date, sp.modify_date, case when sp.is_disabled = 1 then 'Disabled' else 'Enabled' end as status from sys.server_principals sp left join sys.sql_logins sl on sp.principal_id = sl.principal_id where sp.type not in ('G', 'R') order by sp.name;
List users
select * from <table>;
Show everything in the desired table
xp_cmdshell 'whoami'
NOT DEFAULT. System command execution via MSSQL
SELECT * FROM OPENROWSET(BULK N'C:/Windows/System32/drivers/etc/hosts', SINGLE_CLOB) AS Contents
NOT DEFAULT. Reading local files
CMD Interacting
Linux
Windows
Attacks
Capture MSSQL Service Hash
Impersonating other users
Check what users can be impersonated
Check current role and user
Impersonating
sa
user
Check linked server
Identify linked Servers
There would be a server list, those with "1" is remote, those with "0" is linked.
Check rights
Execute commands at linked server
If you have quotes in command, use double singe quotes for it (''example'')
Tips2Hack
Nmap MSSQL Script Scan
Metasploit scan. We can user
mssql_ping
to get more useful info about MSSQL server.
Last updated