Skip to content

Commit c53cd87

Browse files
committed
Fixed LDAP preference for when -ADSPath is passed to Get-NetDomainTrust
Fixed Get-DomainSID to allow for a -DomainController parameter Fixed Get-NetDomainTrust logic
1 parent b5172c2 commit c53cd87

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

Recon/PowerView.ps1

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,7 @@ filter Get-DomainSearcher {
17131713
}
17141714
}
17151715
elseif (-not $DomainController) {
1716+
# if a DC isn't specified
17161717
try {
17171718
$DomainController = ((Get-NetDomain -Credential $Credential).PdcRoleOwner).Name
17181719
}
@@ -4987,6 +4988,10 @@ function Get-DomainSID {
49874988
49884989
The domain to query, defaults to the current domain.
49894990
4991+
.PARAMETER DomainController
4992+
4993+
Domain controller to reflect LDAP queries through.
4994+
49904995
.EXAMPLE
49914996
49924997
C:\> Get-DomainSID -Domain TEST
@@ -4996,16 +5001,15 @@ function Get-DomainSID {
49965001

49975002
param(
49985003
[String]
4999-
$Domain
5004+
$Domain,
5005+
5006+
[String]
5007+
$DomainController
50005008
)
50015009

5002-
$FoundDomain = Get-NetDomain -Domain $Domain
5003-
5004-
if($FoundDomain) {
5005-
# query for the primary domain controller so we can extract the domain SID for filtering
5006-
$PrimaryDC = $FoundDomain.PdcRoleOwner
5007-
$PrimaryDCSID = (Get-NetComputer -Domain $Domain -ComputerName $PrimaryDC -FullData).objectsid
5008-
$Parts = $PrimaryDCSID.split("-")
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+
if($DCSID) {
5012+
$Parts = $DCSID.split("-")
50095013
$Parts[0..($Parts.length -2)] -join "-"
50105014
}
50115015
}
@@ -5342,7 +5346,7 @@ function Get-NetGroupMember {
53425346
}
53435347
else {
53445348
# default to domain admins
5345-
$SID = (Get-DomainSID -Domain $TargetDomain -Credential $Credential) + "-512"
5349+
$SID = (Get-DomainSID -Domain $TargetDomain -DomainController $TargetDomainController) + "-512"
53465350
$Group = Get-NetGroup -SID $SID -Domain $TargetDomain -DomainController $TargetDomainController -Credential $Credential -FullData -PageSize $PageSize
53475351
}
53485352
$GroupDN = $Group.distinguishedname
@@ -5368,7 +5372,7 @@ function Get-NetGroupMember {
53685372
}
53695373
else {
53705374
# default to domain admins
5371-
$SID = (Get-DomainSID -Domain $TargetDomain -Credential $Credential) + "-512"
5375+
$SID = (Get-DomainSID -Domain $TargetDomain -DomainController $TargetDomainController) + "-512"
53725376
$GroupSearcher.filter = "(&(objectCategory=group)(objectSID=$SID)$Filter)"
53735377
}
53745378

@@ -12288,7 +12292,7 @@ function Invoke-EnumerateLocalAdmin {
1228812292
}
1228912293

1229012294
# query for the primary domain controller so we can extract the domain SID for filtering
12291-
$DomainSID = Get-DomainSID -Domain $Domain
12295+
$DomainSID = Get-DomainSID -Domain $Domain -DomainController $DomainController
1229212296
}
1229312297

1229412298
# script block that enumerates a server
@@ -12466,7 +12470,7 @@ function Get-NetDomainTrust {
1246612470

1246712471
[CmdletBinding()]
1246812472
param(
12469-
[Parameter(Position=0,ValueFromPipeline=$True)]
12473+
[Parameter(Position=0, ValueFromPipeline=$True)]
1247012474
[String]
1247112475
$Domain,
1247212476

@@ -12492,14 +12496,19 @@ function Get-NetDomainTrust {
1249212496

1249312497
process {
1249412498

12495-
if((-not $Domain) -or ((-not $API) -and (-not $DomainController))) {
12496-
$Domain = (Get-NetDomain -Credential $Credential).Name
12499+
if(-not $Domain) {
12500+
# if not domain is specified grab the current domain
12501+
$SourceDomain = (Get-NetDomain -Credential $Credential).Name
1249712502
}
12503+
else {
12504+
$SourceDomain = $Domain
12505+
}
12506+
12507+
if($LDAP -or $ADSPath) {
1249812508

12499-
if($LDAP) {
12509+
$TrustSearcher = Get-DomainSearcher -Domain $SourceDomain -DomainController $DomainController -Credential $Credential -PageSize $PageSize -ADSpath $ADSpath
1250012510

12501-
$TrustSearcher = Get-DomainSearcher -Domain $Domain -DomainController $DomainController -Credential $Credential -PageSize $PageSize -ADSpath $ADSpath
12502-
$SourceSID = Get-DomainSID -Domain $Domain -DomainController $DomainController
12511+
$SourceSID = Get-DomainSID -Domain $SourceDomain -DomainController $DomainController
1250312512

1250412513
if($TrustSearcher) {
1250512514

@@ -12533,7 +12542,7 @@ function Get-NetDomainTrust {
1253312542
}
1253412543
$ObjectGuid = New-Object Guid @(,$Props.objectguid[0])
1253512544
$TargetSID = (New-Object System.Security.Principal.SecurityIdentifier($Props.securityidentifier[0],0)).Value
12536-
$DomainTrust | Add-Member Noteproperty 'SourceName' $Domain
12545+
$DomainTrust | Add-Member Noteproperty 'SourceName' $SourceDomain
1253712546
$DomainTrust | Add-Member Noteproperty 'SourceSID' $SourceSID
1253812547
$DomainTrust | Add-Member Noteproperty 'TargetName' $Props.name[0]
1253912548
$DomainTrust | Add-Member Noteproperty 'TargetSID' $TargetSID
@@ -12548,7 +12557,7 @@ function Get-NetDomainTrust {
1254812557
}
1254912558
elseif($API) {
1255012559
if(-not $DomainController) {
12551-
$DomainController = Get-NetDomainController -Credential $Credential -Domain $Domain | Select-Object -First 1 | Select-Object -ExpandProperty Name
12560+
$DomainController = Get-NetDomainController -Credential $Credential -Domain $SourceDomain | Select-Object -First 1 | Select-Object -ExpandProperty Name
1255212561
}
1255312562

1255412563
if($DomainController) {
@@ -12588,7 +12597,7 @@ function Get-NetDomainTrust {
1258812597
}
1258912598
else {
1259012599
$DomainTrust = New-Object PSObject
12591-
$DomainTrust | Add-Member Noteproperty 'SourceDomain' $Domain
12600+
$DomainTrust | Add-Member Noteproperty 'SourceDomain' $SourceDomain
1259212601
$DomainTrust | Add-Member Noteproperty 'SourceDomainController' $DomainController
1259312602
$DomainTrust | Add-Member Noteproperty 'NetbiosDomainName' $Info.NetbiosDomainName
1259412603
$DomainTrust | Add-Member Noteproperty 'DnsDomainName' $Info.DnsDomainName

0 commit comments

Comments
 (0)