HAS_SPN
Summary
Description
Identification
PowerShell
function Find-HASSPN {
[CmdletBinding()]
param ([string]$Target = $null,[string]$SearchBase = $null,[string]$OutputPath = "ServicePrincipalName.csv")
Import-Module ActiveDirectory -ErrorAction Stop
Write-Host "Gathering computer objects with 'ServicePrincipalName' property..."
$adComputerParams = @{
Filter = "*"
Properties = "ServicePrincipalName"
ErrorAction = "Stop"
}
if ($SearchBase) {
$adComputerParams.Add("SearchBase", $SearchBase)
Write-Host "Searching for computers within '$SearchBase'."
} else {
$adComputerParams.Add("SearchBase", (Get-ADRootDSE).DefaultNamingContext)
Write-Host "Searching for all computers in the domain."
}
$computersToScan = @()
$spnResults = @()
try {
if ($Target) {
Write-Host "Searching for ServicePrincipalName on specific computer: '$Target'."
$computersToScan += Get-ADComputer -Identity $Target -Properties "ServicePrincipalName" -ErrorAction Stop
} else {
$computersToScan = Get-ADComputer @adComputerParams
}
}
catch {
Write-Error "Failed to retrieve computer objects from Active Directory: $($_.Exception.Message)"
return
}
if (-not $computersToScan) {
Write-Output "No computer objects found to process."
return
}
foreach ($computer in $computersToScan) {
if ($computer.ServicePrincipalName) {
foreach ($spn in $computer.ServicePrincipalName) {
$spnResults += [PSCustomObject]@{
'Service' = $spn
'Working on' = $computer.Name
}
}
}
}
if ($spnResults.Count -gt 0) {
Write-Host "Found $($spnResults.Count) ServicePrincipalName entry(ies)."
try {
$spnResults | Sort-Object -Unique 'Service', 'Working on' | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8 -ErrorAction Stop
Write-Output "Results exported successfully to '$OutputPath'"
}
catch { Write-Error "Failed to export results to CSV file '$OutputPath': $($_.Exception.Message)"}
} else { Write-Output "No computer objects found with 'ServicePrincipalName' property set."}
}Exploitation
Mitigation
Detection
Event ID
Description
Fields/Attributes
References
References
Last updated
Was this helpful?