MSSQL - Database Enumeration
Summary
Tools
- NetSPI/PowerUpSQL - A PowerShell Toolkit for Attacking SQL Server
- skahwah/SQLRecon - A C# MS SQL toolkit designed for offensive reconnaissance and post-exploitation.
Identify Instances and Databases
Discover Local SQL Server Instances
Discover Domain SQL Server Instances
Get-SQLInstanceDomain -Verbose
# Get Server Info for Found Instances
Get-SQLInstanceDomain | Get-SQLServerInfo -Verbose
# Get Database Names
Get-SQLInstanceDomain | Get-SQLDatabase -NoDefaults
Discover Remote SQL Server Instances
Identify Encrypted databases
Note: These are automatically decrypted for admins
Get-SQLDatabase -Username sa -Password Password1234 -Instance "<DBSERVERNAME\DBInstance>" -Verbose | Where-Object {$_.is_encrypted -eq "True"}
Version Query
Identify Users and Roles
-
Query Current User & determine if the user is a sysadmin
-
Current Role
-
All Logins on Server
-
All Database Users for a Database
-
List All Sysadmins
-
List All Database Roles
SELECT DB1.name AS DatabaseRoleName, isnull (DB2.name, 'No members') AS DatabaseUserName FROM sys.database_role_members AS DRM RIGHT OUTER JOIN sys.database_principals AS DB1 ON DRM.role_principal_id = DB1.principal_id LEFT OUTER JOIN sys.database_principals AS DB2 ON DRM.member_principal_id = DB2.principal_id WHERE DB1.type = 'R' ORDER BY DB1.name;
Identify Sensitive Information
Get Tables from a Specific Database
Get-SQLInstanceDomain | Get-SQLTable -DatabaseName <DBNameFromGet-SQLDatabaseCommand> -NoDefaults
Get Column Details from a Table
Get-SQLInstanceDomain | Get-SQLColumn -DatabaseName <DBName> -TableName <TableName>
-
Current database
-
List all tables
-
List all databases
Gather 5 Entries from Each Column
Get-SQLInstanceDomain | Get-SQLColumnSampleData -Keywords "<columnname1,columnname2,columnname3,columnname4,columnname5>" -Verbose -SampleSize 5
Gather 5 Entries from a Specific Table
Get-SQLQuery -Instance "<DBSERVERNAME\DBInstance>" -Query 'select TOP 5 * from <DatabaseName>.dbo.<TableName>'