MSSQL

sqlcmd -S SRVMSSQL -U julio -P 'MyPassword!' -y 30 -Y 30

MSSQL default system schemas/databases:

  • master - keeps the information for an instance of SQL Server.

  • msdb - used by SQL Server Agent.

  • model - a template database copied for each new database.

  • resource - a read-only database that keeps system objects visible in every database on the server in sys schema.

  • tempdb - keeps temporary objects for SQL queries.

Login it to mssql remotely

alternatively use

or without --windows-auth

Interactive with MSSQL

Check for users with SA level permissions (users that can enable xp_cmdshell)

Run after spinning up an smbserver to capture hash

Check if xp_cmdshell is enabled

Show Advanced Options

Enable xp_cmdshell

Enable File Write (Ole Automation Procedures)

To write files using MSSQL, we need to enable Ole Automation Proceduresarrow-up-right, which requires admin privileges, and then execute some stored procedures to create the file

Write File

Read File

By default, MSSQL allows file read on any file in the operating system to which the account has read access

Impersonate Existing Users

Identify Users that We Can Impersonate

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. Sysadmins can impersonate anyone by default, But for non-administrator users, privileges must be explicitly assigned.

Verifying our Current User and Role

the returned value 0 indicates, we do not have the sysadmin role, but we can impersonate the sa user

Impersonating the SA User

It's recommended to run EXECUTE AS LOGIN within the master DB, because all users, by default, have access to that database. If a user you are trying to impersonate doesn't have access to the DB you are connecting to it will present an error. Try to move to the master DB using USE masterWe can now execute any command as a sysadmin as the returned value 1 indicates.

Communicate with Other Databases with MSSQL

Identify linked Servers in MSSQL

If we manage to gain access to a SQL Server with a linked server configured, we may be able to move laterally to that database server. Administrators can configure a linked server using credentials from the remote server. If those credentials have sysadmin privileges, we may be able to execute commands in the remote SQL instance. As we can see in the query's output, we have the name of the server and the column isremote, where 1 means is a remote server, and 0 is a linked server
Attempt to identify the user used for the connection and its privileges. The EXECUTEarrow-up-right statement can be used to send pass-through commands to linked servers. If we need to use quotes in our query to the linked server, we need to use single double quotes to escape the single quote. To run multiples commands at once we can divide them up with a semi colon (;).

Last updated