@@ -1071,17 +1071,17 @@ filter Convert-ADName {
1071
1071
)
1072
1072
1073
1073
$NameTypes = @ {
1074
- " Canonical" = 2
1075
- " NT4" = 3
1076
- " Simple" = 5
1074
+ ' Canonical' = 2
1075
+ ' NT4' = 3
1076
+ ' Simple' = 5
1077
1077
}
1078
1078
1079
- if (! $PSBoundParameters [' InputType' ]) {
1079
+ if (-not $PSBoundParameters [' InputType' ]) {
1080
1080
if ( ($ObjectName.split (' /' )).Count -eq 2 ) {
1081
1081
$ObjectName = $ObjectName.replace (' /' , ' \' )
1082
1082
}
1083
1083
1084
- if ($ObjectName -match " ^[A-Za-z]+\\[A-Za-z ]+$ " ) {
1084
+ if ($ObjectName -match " ^[A-Za-z]+\\[A-Za-z ]+" ) {
1085
1085
$InputType = ' NT4'
1086
1086
}
1087
1087
elseif ($ObjectName -match " ^[A-Za-z ]+@[A-Za-z\.]+" ) {
@@ -1099,7 +1099,7 @@ filter Convert-ADName {
1099
1099
$ObjectName = $ObjectName.replace (' /' , ' \' )
1100
1100
}
1101
1101
1102
- if (! $PSBoundParameters [' OutputType' ]) {
1102
+ if (-not $PSBoundParameters [' OutputType' ]) {
1103
1103
$OutputType = Switch ($InputType ) {
1104
1104
' NT4' {' Canonical' }
1105
1105
' Simple' {' NT4' }
@@ -5009,8 +5009,10 @@ function Get-DomainSID {
5009
5009
5010
5010
$DCSID = Get-NetComputer - Domain $Domain - DomainController $DomainController - FullData - Filter ' (userAccountControl:1.2.840.113556.1.4.803:=8192)' | Select-Object - First 1 - ExpandProperty objectsid
5011
5011
if ($DCSID ) {
5012
- $Parts = $DCSID.split (" -" )
5013
- $Parts [0 .. ($Parts.length -2 )] -join " -"
5012
+ $DCSID.Substring (0 , $DCSID.LastIndexOf (' -' ))
5013
+ }
5014
+ else {
5015
+ Write-Warning " Error extracting domain SID for $Domain "
5014
5016
}
5015
5017
}
5016
5018
@@ -5147,35 +5149,40 @@ function Get-NetGroup {
5147
5149
5148
5150
if ($UserName ) {
5149
5151
# get the raw user object
5150
- $User = Get-ADObject - SamAccountName $UserName - Domain $Domain - DomainController $DomainController - Credential $Credential - ReturnRaw - PageSize $PageSize
5152
+ $User = Get-ADObject - SamAccountName $UserName - Domain $Domain - DomainController $DomainController - Credential $Credential - ReturnRaw - PageSize $PageSize | Select-Object - First 1
5151
5153
5152
- # convert the user to a directory entry
5153
- $UserDirectoryEntry = $User.GetDirectoryEntry ()
5154
+ if ($User ) {
5155
+ # convert the user to a directory entry
5156
+ $UserDirectoryEntry = $User.GetDirectoryEntry ()
5154
5157
5155
- # cause the cache to calculate the token groups for the user
5156
- $UserDirectoryEntry.RefreshCache (" tokenGroups" )
5158
+ # cause the cache to calculate the token groups for the user
5159
+ $UserDirectoryEntry.RefreshCache (" tokenGroups" )
5157
5160
5158
- $UserDirectoryEntry.TokenGroups | ForEach-Object {
5159
- # convert the token group sid
5160
- $GroupSid = (New-Object System.Security.Principal.SecurityIdentifier($_ , 0 )).Value
5161
-
5162
- # ignore the built in users and default domain user group
5163
- if (! ($GroupSid -match ' ^S-1-5-32-545|-513$' )) {
5164
- if ($FullData ) {
5165
- $Group = Get-ADObject - SID $GroupSid - PageSize $PageSize - Domain $Domain - DomainController $DomainController - Credential $Credential
5166
- $Group.PSObject.TypeNames.Add (' PowerView.Group' )
5167
- $Group
5168
- }
5169
- else {
5170
- if ($RawSids ) {
5171
- $GroupSid
5161
+ $UserDirectoryEntry.TokenGroups | ForEach-Object {
5162
+ # convert the token group sid
5163
+ $GroupSid = (New-Object System.Security.Principal.SecurityIdentifier($_ , 0 )).Value
5164
+
5165
+ # ignore the built in groups
5166
+ if ($GroupSid -notmatch ' ^S-1-5-32-.*' ) {
5167
+ if ($FullData ) {
5168
+ $Group = Get-ADObject - SID $GroupSid - PageSize $PageSize - Domain $Domain - DomainController $DomainController - Credential $Credential
5169
+ $Group.PSObject.TypeNames.Add (' PowerView.Group' )
5170
+ $Group
5172
5171
}
5173
5172
else {
5174
- Convert-SidToName $GroupSid
5173
+ if ($RawSids ) {
5174
+ $GroupSid
5175
+ }
5176
+ else {
5177
+ Convert-SidToName - SID $GroupSid
5178
+ }
5175
5179
}
5176
5180
}
5177
5181
}
5178
5182
}
5183
+ else {
5184
+ Write-Warning " UserName '$UserName ' failed to resolve."
5185
+ }
5179
5186
}
5180
5187
else {
5181
5188
if ($SID ) {
@@ -5456,11 +5463,39 @@ function Get-NetGroupMember {
5456
5463
$GroupMember | Add-Member Noteproperty ' GroupDomain' $TargetDomain
5457
5464
$GroupMember | Add-Member Noteproperty ' GroupName' $GroupFoundName
5458
5465
5466
+ if ($Properties.objectSid ) {
5467
+ $MemberSID = ((New-Object System.Security.Principal.SecurityIdentifier $Properties.objectSid [0 ], 0 ).Value)
5468
+ }
5469
+ else {
5470
+ $MemberSID = $Null
5471
+ }
5472
+
5459
5473
try {
5460
5474
$MemberDN = $Properties.distinguishedname [0 ]
5461
-
5462
- # extract the FQDN from the Distinguished Name
5463
- $MemberDomain = $MemberDN.subString ($MemberDN.IndexOf (" DC=" )) -replace ' DC=' , ' ' -replace ' ,' , ' .'
5475
+
5476
+ if (($MemberDN -match ' ForeignSecurityPrincipals' ) -and ($MemberDN -match ' S-1-5-21' )) {
5477
+ try {
5478
+ if (-not $MemberSID ) {
5479
+ $MemberSID = $Properties.cn [0 ]
5480
+ }
5481
+ $MemberSimpleName = Convert-SidToName - SID $MemberSID | Convert-ADName - InputType ' NT4' - OutputType ' Simple'
5482
+ if ($MemberSimpleName ) {
5483
+ $MemberDomain = $MemberSimpleName.Split (' @' )[1 ]
5484
+ }
5485
+ else {
5486
+ Write-Warning " Error converting $MemberDN "
5487
+ $MemberDomain = $Null
5488
+ }
5489
+ }
5490
+ catch {
5491
+ Write-Warning " Error converting $MemberDN "
5492
+ $MemberDomain = $Null
5493
+ }
5494
+ }
5495
+ else {
5496
+ # extract the FQDN from the Distinguished Name
5497
+ $MemberDomain = $MemberDN.subString ($MemberDN.IndexOf (" DC=" )) -replace ' DC=' , ' ' -replace ' ,' , ' .'
5498
+ }
5464
5499
}
5465
5500
catch {
5466
5501
$MemberDN = $Null
@@ -5481,17 +5516,10 @@ function Get-NetGroupMember {
5481
5516
$MemberName = $Properties.cn
5482
5517
}
5483
5518
}
5484
-
5485
- if ($Properties.objectSid ) {
5486
- $MemberSid = ((New-Object System.Security.Principal.SecurityIdentifier $Properties.objectSid [0 ], 0 ).Value)
5487
- }
5488
- else {
5489
- $MemberSid = $Null
5490
- }
5491
5519
5492
5520
$GroupMember | Add-Member Noteproperty ' MemberDomain' $MemberDomain
5493
5521
$GroupMember | Add-Member Noteproperty ' MemberName' $MemberName
5494
- $GroupMember | Add-Member Noteproperty ' MemberSid ' $MemberSid
5522
+ $GroupMember | Add-Member Noteproperty ' MemberSID ' $MemberSID
5495
5523
$GroupMember | Add-Member Noteproperty ' IsGroup' $IsGroup
5496
5524
$GroupMember | Add-Member Noteproperty ' MemberDN' $MemberDN
5497
5525
$GroupMember.PSObject.TypeNames.Add (' PowerView.GroupMember' )
@@ -5507,7 +5535,6 @@ function Get-NetGroupMember {
5507
5535
}
5508
5536
}
5509
5537
}
5510
-
5511
5538
}
5512
5539
}
5513
5540
}
@@ -7063,26 +7090,29 @@ function Find-GPOLocation {
7063
7090
$OUComputers = Get-NetComputer - Domain $Domain - DomainController $DomainController - Credential $Credential - ADSpath $_.ADSpath - PageSize $PageSize
7064
7091
}
7065
7092
7066
- if ($OUComputers -isnot [System.Array ]) {$OUComputers = @ ($OUComputers )}
7093
+ if ($OUComputers ) {
7094
+ if ($OUComputers -isnot [System.Array ]) {$OUComputers = @ ($OUComputers )}
7067
7095
7068
- ForEach ($TargetSid in $TargetObjectSIDs ) {
7069
- $Object = Get-ADObject - SID $TargetSid - Domain $Domain - DomainController $DomainController - Credential $Credential - PageSize $PageSize
7096
+ ForEach ($TargetSid in $TargetObjectSIDs ) {
7097
+ $Object = Get-ADObject - SID $TargetSid - Domain $Domain - DomainController $DomainController - Credential $Credential - PageSize $PageSize
7070
7098
7071
- $IsGroup = @ (' 268435456' , ' 268435457' , ' 536870912' , ' 536870913' ) -contains $Object.samaccounttype
7099
+ $IsGroup = @ (' 268435456' , ' 268435457' , ' 536870912' , ' 536870913' ) -contains $Object.samaccounttype
7072
7100
7073
- $GPOLocation = New-Object PSObject
7074
- $GPOLocation | Add-Member Noteproperty ' ObjectName' $Object.samaccountname
7075
- $GPOLocation | Add-Member Noteproperty ' ObjectDN' $Object.distinguishedname
7076
- $GPOLocation | Add-Member Noteproperty ' ObjectSID' $Object.objectsid
7077
- $GPOLocation | Add-Member Noteproperty ' Domain' $Domain
7078
- $GPOLocation | Add-Member Noteproperty ' IsGroup' $IsGroup
7079
- $GPOLocation | Add-Member Noteproperty ' GPODisplayName' $GPOname
7080
- $GPOLocation | Add-Member Noteproperty ' GPOGuid' $GPOGuid
7081
- $GPOLocation | Add-Member Noteproperty ' GPOPath' $GPOPath
7082
- $GPOLocation | Add-Member Noteproperty ' GPOType' $GPOType
7083
- $GPOLocation | Add-Member Noteproperty ' ContainerName' $_.distinguishedname
7084
- $GPOLocation | Add-Member Noteproperty ' ComputerName' $OUComputers
7085
- $GPOLocation
7101
+ $GPOLocation = New-Object PSObject
7102
+ $GPOLocation | Add-Member Noteproperty ' ObjectName' $Object.samaccountname
7103
+ $GPOLocation | Add-Member Noteproperty ' ObjectDN' $Object.distinguishedname
7104
+ $GPOLocation | Add-Member Noteproperty ' ObjectSID' $Object.objectsid
7105
+ $GPOLocation | Add-Member Noteproperty ' Domain' $Domain
7106
+ $GPOLocation | Add-Member Noteproperty ' IsGroup' $IsGroup
7107
+ $GPOLocation | Add-Member Noteproperty ' GPODisplayName' $GPOname
7108
+ $GPOLocation | Add-Member Noteproperty ' GPOGuid' $GPOGuid
7109
+ $GPOLocation | Add-Member Noteproperty ' GPOPath' $GPOPath
7110
+ $GPOLocation | Add-Member Noteproperty ' GPOType' $GPOType
7111
+ $GPOLocation | Add-Member Noteproperty ' ContainerName' $_.distinguishedname
7112
+ $GPOLocation | Add-Member Noteproperty ' ComputerName' $OUComputers
7113
+ $GPOLocation.PSObject.TypeNames.Add (' PowerView.GPOLocalGroup' )
7114
+ $GPOLocation
7115
+ }
7086
7116
}
7087
7117
}
7088
7118
@@ -7106,6 +7136,7 @@ function Find-GPOLocation {
7106
7136
$AppliedSite | Add-Member Noteproperty ' GPOType' $GPOType
7107
7137
$AppliedSite | Add-Member Noteproperty ' ContainerName' $_.distinguishedname
7108
7138
$AppliedSite | Add-Member Noteproperty ' ComputerName' $_.siteobjectbl
7139
+ $AppliedSite.PSObject.TypeNames.Add (' PowerView.GPOLocalGroup' )
7109
7140
$AppliedSite
7110
7141
}
7111
7142
}
@@ -7678,10 +7709,10 @@ function Get-NetLocalGroup {
7678
7709
$LocalUser | Add-Member Noteproperty ' AccountName' $Info.lgrmi2_domainandname
7679
7710
$LocalUser | Add-Member Noteproperty ' SID' $SidString
7680
7711
7681
- $IsGroup = $ ($Info.lgrmi2_sidusage -eq ' SidTypeGroup ' )
7712
+ $IsGroup = $ ($Info.lgrmi2_sidusage -ne ' SidTypeUser ' )
7682
7713
$LocalUser | Add-Member Noteproperty ' IsGroup' $IsGroup
7683
7714
7684
- $LocalUser.PSObject.TypeNames.Add (' PowerView.LocalUser ' )
7715
+ $LocalUser.PSObject.TypeNames.Add (' PowerView.LocalUserAPI ' )
7685
7716
7686
7717
$LocalUsers += $LocalUser
7687
7718
}
@@ -9772,23 +9803,12 @@ function Invoke-UserHunter {
9772
9803
$FoundUser | Add-Member Noteproperty ' SessionFrom' $CName
9773
9804
9774
9805
# Try to resolve the DNS hostname of $Cname
9775
- if ($Cname -match ' [a-zA-Z]' ) {
9776
- Try {
9777
- $CNameDNSName = [System.Net.Dns ]::GetHostByName($CName ).Hostname
9778
- }
9779
- Catch {
9780
- $CNameDNSName = $Cname
9781
- }
9806
+ try {
9807
+ $CNameDNSName = [System.Net.Dns ]::GetHostEntry($CName ) | Select-Object - ExpandProperty HostName
9782
9808
$FoundUser | Add-Member NoteProperty ' SessionFromName' $CnameDNSName
9783
9809
}
9784
- else {
9785
- Try {
9786
- $CNameDNSName = [System.Net.Dns ]::Resolve($Cname ).HostName
9787
- }
9788
- Catch {
9789
- $CNameDNSName = $Cname
9790
- }
9791
- $FoundUser | Add-Member NoteProperty ' SessionFromName' $CnameDNSName
9810
+ catch {
9811
+ $FoundUser | Add-Member NoteProperty ' SessionFromName' $Null
9792
9812
}
9793
9813
9794
9814
# see if we're checking to see if we have local admin access on this machine
@@ -12768,7 +12788,7 @@ function Find-ForeignUser {
12768
12788
$Domain = $DistinguishedDomainName -replace ' DC=' , ' ' -replace ' ,' , ' .'
12769
12789
}
12770
12790
12771
- Get-NetUser - Domain $Domain - DomainController $DomainController - UserName $UserName - PageSize $PageSize | Where-Object { $_ . memberof} | ForEach-Object {
12791
+ Get-NetUser - Domain $Domain - DomainController $DomainController - UserName $UserName - PageSize $PageSize - Filter ' ( memberof=*) ' | ForEach-Object {
12772
12792
ForEach ($Membership in $_.memberof ) {
12773
12793
$Index = $Membership.IndexOf (" DC=" )
12774
12794
if ($Index ) {
@@ -12900,7 +12920,7 @@ function Find-ForeignGroup {
12900
12920
$ExcludeGroups = @ (" Users" , " Domain Users" , " Guests" )
12901
12921
12902
12922
# get all the groupnames for the given domain
12903
- Get-NetGroup - GroupName $GroupName - Domain $Domain - DomainController $DomainController - FullData - PageSize $PageSize | Where-Object { $_ .member } | Where-Object {
12923
+ Get-NetGroup - GroupName $GroupName - Filter ' (member=*) ' - Domain $Domain - DomainController $DomainController - FullData - PageSize $PageSize | Where-Object {
12904
12924
# exclude common large groups
12905
12925
-not ($ExcludeGroups -contains $_.samaccountname ) } | ForEach-Object {
12906
12926
0 commit comments