CAN_RDP
Summary
Description
Identification
Powershell
function Find-CAN_RDP {
[CmdletBinding()]
param( [string[]]$Target = $null, [string] $SearchBase = $null, [string] $OutputPath = "CAN_RDP.csv", [int] $TimeoutSec = 6 )
Import-Module ActiveDirectory -ErrorAction Stop
Write-Host "Gathering computer objects from Active Directory..."
try {
$computers = @()
if ($Target) {
foreach ($t in $Target) {
try {
$computers += (Get-ADComputer -Identity $t -ErrorAction Stop | Select-Object -ExpandProperty Name)
} catch {
Write-Warning "Target computer '$t' not found in AD: $($_.Exception.Message)"
}
}
if ($computers) { Write-Host "Using explicit target computer(s): $($computers -join ', ')" }
}
elseif ($SearchBase) {
Write-Host "Searching for computers within '$SearchBase'."
$computers = Get-ADComputer -Filter * -SearchBase $SearchBase -ErrorAction Stop | Select-Object -ExpandProperty Name
}
else {
Write-Host "Searching for all computers in the domain."
$computers = Get-ADComputer -Filter * -ErrorAction Stop | Select-Object -ExpandProperty Name
}
} catch {
Write-Error "Failed to retrieve computer objects from Active Directory: $($_.Exception.Message)"
return
}
if (-not $computers) {
Write-Output "No computer objects found to process."
return
}
$computers = $computers | Select-Object -Unique
Write-Host "Enumerating local 'Remote Desktop Users' on $($computers.Count) computer(s)..."
$results = New-Object System.Collections.Generic.List[object]
foreach ($c in $computers) {
try {
$opt = New-CimSessionOption -Protocol Dcom
$sess = New-CimSession -ComputerName $c -SessionOption $opt -OperationTimeoutSec $TimeoutSec -ErrorAction Stop
try {
# Don't use LocalAccount=TRUE to avoid excluding DCs (no local SAM)
$grp = Get-CimInstance -CimSession $sess -ClassName Win32_Group -Filter "Name='Remote Desktop Users'" -ErrorAction Stop
if (-not $grp) {
Write-Warning "'Remote Desktop Users' group not found on '$c'."
continue
}
$members = Get-CimAssociatedInstance -CimSession $sess -InputObject $grp -Association Win32_GroupUser -ErrorAction Stop
foreach ($m in $members) {
$memberType = if ($m.CimClass.CimClassName -eq 'Win32_Group') { 'Group' } else { 'User' }
$memberName = if ($m.Domain) { "$($m.Domain)\$($m.Name)" } else { $m.Name }
$results.Add([PSCustomObject]@{
ComputerName = $c
MemberName = $memberName
MemberType = $memberType
})
}
}
finally {
if ($sess) { $sess | Remove-CimSession -ErrorAction SilentlyContinue }
}
} catch { Write-Warning "Unable to enumerate 'Remote Desktop Users' on '$c': $($_.Exception.Message)" }
}
if ($results.Count -gt 0) {
try {
$results |
Select-Object ComputerName, MemberName, MemberType |
Sort-Object ComputerName, MemberType, MemberName |
Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8 -ErrorAction Stop
Write-Output "Results exported to '$OutputPath'"
} catch { Write-Error "Failed to export results to CSV: $($_.Exception.Message)"}
} else {Write-Output "No 'Remote Desktop Users' members found across the scanned computers."}
}
Computer Management

Exploitation
Windows

Linux

Mitigation

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