|
| 1 | +// Copyright 2024 Specter Ops, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +//go:build integration |
| 18 | + |
| 19 | +package ad_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/specterops/bloodhound/cmd/api/src/test/integration" |
| 25 | + "github.com/specterops/bloodhound/packages/go/graphschema" |
| 26 | + "github.com/specterops/bloodhound/packages/go/graphschema/ad" |
| 27 | + "github.com/specterops/bloodhound/packages/go/graphschema/common" |
| 28 | + "github.com/specterops/dawgs/graph" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | +) |
| 32 | + |
| 33 | +// addEnabledHostingComputer creates an enabled computer in the given domain and |
| 34 | +// links it to the CA via HostsCAService (mirrors the integration package's |
| 35 | +// unexported addHostingComputer). |
| 36 | +func addEnabledHostingComputer(testCtx *integration.GraphTestContext, name, domainSID string, enterpriseCA *graph.Node) { |
| 37 | + computer := testCtx.NewActiveDirectoryComputer(name, domainSID) |
| 38 | + computer.Properties.Set(common.Enabled.String(), true) |
| 39 | + testCtx.UpdateNode(computer) |
| 40 | + testCtx.NewRelationship(computer, enterpriseCA, ad.HostsCAService) |
| 41 | +} |
| 42 | + |
| 43 | +// linkEnterpriseCAToDomain adds the per-domain edges that make a domain "chain |
| 44 | +// valid" (RootCAFor ∩ TrustedForNTAuth) for the CA. The per-CA EnterpriseCAFor |
| 45 | +// edge is created once by the caller; each domain gets its own NTAuthStore so the |
| 46 | +// TrustedForNTAuth edges don't collide. |
| 47 | +func linkEnterpriseCAToDomain(testCtx *integration.GraphTestContext, enterpriseCA, rootCA *graph.Node, domain *graph.Node, domainSID string) { |
| 48 | + ntAuthStore := testCtx.NewActiveDirectoryNTAuthStore("NTAuthStore-"+domainSID, domainSID) |
| 49 | + |
| 50 | + // RootCAFor path: domain <-RootCAFor- rootCA <-EnterpriseCAFor- enterpriseCA |
| 51 | + testCtx.NewRelationship(rootCA, domain, ad.RootCAFor) |
| 52 | + |
| 53 | + // TrustedForNTAuth path: domain <-NTAuthStoreFor- ntAuthStore <-TrustedForNTAuth- enterpriseCA |
| 54 | + testCtx.NewRelationship(ntAuthStore, domain, ad.NTAuthStoreFor) |
| 55 | + testCtx.NewRelationship(enterpriseCA, ntAuthStore, ad.TrustedForNTAuth) |
| 56 | +} |
| 57 | + |
| 58 | +// TestADCSForestScoping_FiltersForeignForestDomain models shared ADCS across two |
| 59 | +// forests: a CA hosted in forest A whose cert chain also reaches forest B's |
| 60 | +// domain. The CA's hosting computer is in forest A, so the CA is retained but its |
| 61 | +// chained-domain set must be scoped to forest A only. |
| 62 | +func TestADCSForestScoping_FiltersForeignForestDomain(t *testing.T) { |
| 63 | + testContext := integration.NewGraphTestContext(t, graphschema.DefaultGraphSchema()) |
| 64 | + |
| 65 | + var ( |
| 66 | + domainASID = integration.RandomDomainSID() |
| 67 | + domainBSID = integration.RandomDomainSID() |
| 68 | + |
| 69 | + enterpriseCAID graph.ID |
| 70 | + domainAID graph.ID |
| 71 | + domainBID graph.ID |
| 72 | + ) |
| 73 | + |
| 74 | + testContext.DatabaseTestWithSetup( |
| 75 | + func(harness *integration.HarnessDetails) error { |
| 76 | + domainA := testContext.NewActiveDirectoryDomain("ForestA-Domain", domainASID, false, true) |
| 77 | + domainB := testContext.NewActiveDirectoryDomain("ForestB-Domain", domainBSID, false, true) |
| 78 | + |
| 79 | + // The CA and its root live in forest A. |
| 80 | + enterpriseCA := testContext.NewActiveDirectoryEnterpriseCA("SharedECA", domainASID) |
| 81 | + rootCA := testContext.NewActiveDirectoryRootCA("SharedRootCA", domainASID) |
| 82 | + |
| 83 | + // The CA chains up to its root once; the per-domain edges are added below. |
| 84 | + testContext.NewRelationship(enterpriseCA, rootCA, ad.EnterpriseCAFor) |
| 85 | + |
| 86 | + // Valid cert chain to forest A (the CA's own forest)... |
| 87 | + linkEnterpriseCAToDomain(testContext, enterpriseCA, rootCA, domainA, domainASID) |
| 88 | + // ...and a cross-forest chain into forest B (shared ADCS). |
| 89 | + linkEnterpriseCAToDomain(testContext, enterpriseCA, rootCA, domainB, domainBSID) |
| 90 | + |
| 91 | + // Hosting computer lives in the CA's own forest. |
| 92 | + addEnabledHostingComputer(testContext, "HostA", domainASID, enterpriseCA) |
| 93 | + |
| 94 | + enterpriseCAID = enterpriseCA.ID |
| 95 | + domainAID = domainA.ID |
| 96 | + domainBID = domainB.ID |
| 97 | + return nil |
| 98 | + }, |
| 99 | + func(harness integration.HarnessDetails, db graph.Database) { |
| 100 | + _, cache, err := FetchADCSPrereqs(db) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + chainedDomains := cache.GetECAHostedChainedDomains() |
| 104 | + |
| 105 | + require.Contains(t, chainedDomains, enterpriseCAID.Uint64(), "CA with an in-forest host should be retained") |
| 106 | + chains := chainedDomains[enterpriseCAID.Uint64()] |
| 107 | + assert.True(t, chains.Domains.Contains(domainAID.Uint64()), "in-forest domain should survive") |
| 108 | + assert.False(t, chains.Domains.Contains(domainBID.Uint64()), "foreign-forest domain should be filtered out") |
| 109 | + }, |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +// TestADCSForestScoping_DropsCAWithOnlyCrossForestHost models a CA whose only |
| 114 | +// HostsCAService computer was matched across a forest boundary. With no hosting |
| 115 | +// computer in the CA's own forest, the CA should be dropped entirely. |
| 116 | +func TestADCSForestScoping_DropsCAWithOnlyCrossForestHost(t *testing.T) { |
| 117 | + testContext := integration.NewGraphTestContext(t, graphschema.DefaultGraphSchema()) |
| 118 | + |
| 119 | + var ( |
| 120 | + domainASID = integration.RandomDomainSID() |
| 121 | + domainBSID = integration.RandomDomainSID() |
| 122 | + |
| 123 | + enterpriseCAID graph.ID |
| 124 | + ) |
| 125 | + |
| 126 | + testContext.DatabaseTestWithSetup( |
| 127 | + func(harness *integration.HarnessDetails) error { |
| 128 | + domainA := testContext.NewActiveDirectoryDomain("ForestA-Domain", domainASID, false, true) |
| 129 | + domainB := testContext.NewActiveDirectoryDomain("ForestB-Domain", domainBSID, false, true) |
| 130 | + |
| 131 | + enterpriseCA := testContext.NewActiveDirectoryEnterpriseCA("SharedECA", domainASID) |
| 132 | + rootCA := testContext.NewActiveDirectoryRootCA("SharedRootCA", domainASID) |
| 133 | + |
| 134 | + // The CA chains up to its root once; the per-domain edges are added below. |
| 135 | + testContext.NewRelationship(enterpriseCA, rootCA, ad.EnterpriseCAFor) |
| 136 | + |
| 137 | + linkEnterpriseCAToDomain(testContext, enterpriseCA, rootCA, domainA, domainASID) |
| 138 | + linkEnterpriseCAToDomain(testContext, enterpriseCA, rootCA, domainB, domainBSID) |
| 139 | + |
| 140 | + // Only hosting computer lives in forest B (cross-forest from the CA). |
| 141 | + addEnabledHostingComputer(testContext, "HostB", domainBSID, enterpriseCA) |
| 142 | + |
| 143 | + enterpriseCAID = enterpriseCA.ID |
| 144 | + return nil |
| 145 | + }, |
| 146 | + func(harness integration.HarnessDetails, db graph.Database) { |
| 147 | + _, cache, err := FetchADCSPrereqs(db) |
| 148 | + require.NoError(t, err) |
| 149 | + |
| 150 | + chainedDomains := cache.GetECAHostedChainedDomains() |
| 151 | + |
| 152 | + assert.NotContains(t, chainedDomains, enterpriseCAID.Uint64(), "CA with no in-forest hosting computer should be skipped") |
| 153 | + }, |
| 154 | + ) |
| 155 | +} |
0 commit comments