SQLAdmin
SQLAdmin
Summary
Description
Identification
PowerShell
function Find-SQLAdmin {
[CmdletBinding()] param(
[string]$OutputPath = "SQLReport.csv",
[int]$ConnectTimeoutSeconds = 3,
[string]$Target # Optional computer name
)
Import-Module ActiveDirectory -ErrorAction Stop
if (Get-Module -ListAvailable SqlServer) { Import-Module SqlServer -ErrorAction SilentlyContinue }
else { [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') }
$seen=@{}; $rows=@()
# Build AD query based on Target
if ($Target) {
$filter = { Name -eq $Target -and ServicePrincipalName -like "MSSQLSvc*" }
} else {
$filter = { ServicePrincipalName -like "MSSQLSvc*" }
}
Get-ADComputer -Filter $filter -Properties ServicePrincipalName |
ForEach-Object {
$_.ServicePrincipalName | Where-Object { $_ -like 'MSSQLSvc*' } | ForEach-Object {
$hp = ($_ -split '/',2)[1]; if (-not $hp) { continue }
$parts = $hp -split ':',2
$hname = $parts[0]
$token = if ($parts.Count -gt 1) { $parts[1] } else { $null }
$target = if ($token) { if ($token -match '^\d+$') { "$hname,$token" } else { "$hname\$token" } } else { $hname }
try {
$s = New-Object Microsoft.SqlServer.Management.Smo.Server $target
$s.ConnectionContext.ConnectTimeout = [Math]::Max(1,$ConnectTimeoutSeconds)
$s.ConnectionContext.Connect()
$iname = if ([string]::IsNullOrEmpty($s.InstanceName)) { 'MSSQLSERVER' } else { $s.InstanceName }
$port = $null; try { $port = $s.ConnectionContext.Port } catch {}
$iid = '{0}|{1}|{2}' -f $s.NetName, $iname, $port
if ($seen.ContainsKey($iid)) { continue }; $seen[$iid] = $true
$admins = @(); if ($s.Roles['sysadmin']) { $admins = $s.Roles['sysadmin'].EnumMemberNames() }
$dbs = $s.Databases | ForEach-Object { [pscustomobject]@{ DatabaseName=$_.Name; DatabaseOwner=$_.Owner } }
$rows += [pscustomobject]@{
Server = $target
SysAdmins = ($admins -join ', ')
Databases = ($dbs | ConvertTo-Json -Compress)
}
} catch {
Write-Warning "$target Connection Error: $_"
}
}
}
if ($OutputPath) { $rows | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8 }
$rows
}
SQL Server Management Studio (SSMS)

Exploitation
Windows


Linux



Mitigation

Detection
Event ID
Description
Fields/Attributes
References
References
Last updated
Was this helpful?