> 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 all Existing Databases
> select name from sys.databases;
> select * from master.dbo.sysdatabases;
Use a Specific Database
> use <DB_NAME>;
List all the Tables in a Database
> select name from <DB_NAME>.sys.tables;
> select table_name from <DB_NAME>.information_schema.tables where table_type='base table';
> select table_name from information_schema.tables where table_type='base table' and table_catalog='<DB_NAME>';
Info
In SQL Server, information_schema is a schema that belong to a database, unlike MySQL, where information_schema is a database itself
Therefore, each database in SQL Server has its own information_schema schema, which contains metadata views and other data
MSSQL Command Execution
xp_cmdshell
It is possible to run OS Commands from the SQL Server context through xp_cmdshell
It is an extended procedure provided by Microsoft and stored in the Master database
The commands will be executed using the MSSQL’s service account privileges
Check if xp_cmdshell is Enabled
> select * from sys.configurations where name = 'xp_cmdshell';
Enable xp_cmdshell
Enable Advanced Options
The xp_cmdshell is considered an advanced feature due to its security risks
Therefore, in order to be able to enable it, It is necessary to first enable the advanced options to access the configuration that can activate xp_cmdshell
Note that, in addition to having enabled the above procedure, It is necessary to have the EXECUTE Permission on the xp_cmdshell extended procedure in order to be able to execute OS Commands
To check which users can run xp_cmdshell →
> user master;> exec sp_helprotect 'xp_cmdshell';
If the current database user is part of the SYSADMIN role and xp_cmdshell is enabled, it can be executed directly
Execute OS Commands via xp_cmdshell
> exec xp_cmdshell '<COMMAND>'
Check which Service Account is running xp_cmdshell