Skip to content

Commit

Permalink
fix: ESC3 enrollment agent restrictions logic (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBK authored Feb 12, 2024
1 parent e563545 commit 20b2708
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 10 deletions.
65 changes: 65 additions & 0 deletions cmd/api/src/analysis/ad/adcs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,71 @@ func TestADCSESC3(t *testing.T) {
return nil
})
})

testContext.DatabaseTestWithSetup(func(harness *integration.HarnessDetails) error {
harness.ESC3Harness3.Setup(testContext)
return nil
}, func(harness integration.HarnessDetails, db graph.Database) {
operation := analysis.NewPostRelationshipOperation(context.Background(), db, "ADCS Post Process Test - ESC3")

groupExpansions, err := ad2.ExpandAllRDPLocalGroups(context.Background(), db)
require.Nil(t, err)
enterpriseCertAuthorities, err := ad2.FetchNodesByKind(context.Background(), db, ad.EnterpriseCA)
require.Nil(t, err)
certTemplates, err := ad2.FetchNodesByKind(context.Background(), db, ad.CertTemplate)
require.Nil(t, err)
domains, err := ad2.FetchNodesByKind(context.Background(), db, ad.Domain)
require.Nil(t, err)

cache := ad2.NewADCSCache()
cache.BuildCache(context.Background(), db, enterpriseCertAuthorities, certTemplates)

for _, domain := range domains {
innerDomain := domain

operation.Operation.SubmitReader(func(ctx context.Context, tx graph.Transaction, outC chan<- analysis.CreatePostRelationshipJob) error {
if enterpriseCAs, err := ad2.FetchEnterpriseCAsTrustedForNTAuthToDomain(tx, innerDomain); err != nil {
return err
} else {
for _, enterpriseCA := range enterpriseCAs {
if cache.DoesCAChainProperlyToDomain(enterpriseCA, innerDomain) {
if err := ad2.PostADCSESC3(ctx, tx, outC, groupExpansions, enterpriseCA, innerDomain, cache); err != nil {
t.Logf("failed post processing for %s: %v", ad.ADCSESC3.String(), err)
} else {
return nil
}
}
}
}
return nil
})
}
operation.Done()

db.ReadTransaction(context.Background(), func(tx graph.Transaction) error {
if results, err := ops.FetchStartNodes(tx.Relationships().Filterf(func() graph.Criteria {
return query.Kind(query.Relationship(), ad.ADCSESC3)
})); err != nil {
t.Fatalf("error fetching esc3 edges in integration test; %v", err)
} else {
assert.Equal(t, 1, len(results))

require.True(t, results.Contains(harness.ESC3Harness3.Group1))
}

if edge, err := tx.Relationships().Filterf(func() graph.Criteria {
return query.Kind(query.Relationship(), ad.ADCSESC3)
}).First(); err != nil {
t.Fatalf("error fetching esc3 edges in integration test; %v", err)
} else {
comp, err := ad2.GetADCSESC3EdgeComposition(context.Background(), db, edge)
assert.Nil(t, err)
assert.Equal(t, 7, len(comp.AllNodes()))
assert.False(t, comp.AllNodes().Contains(harness.ESC3Harness3.User2))
}
return nil
})
})
}

func TestADCSESC9a(t *testing.T) {
Expand Down
62 changes: 62 additions & 0 deletions cmd/api/src/test/integration/harnesses.go
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,67 @@ func (s *ESC3Harness2) Setup(c *GraphTestContext) {
c.UpdateNode(s.EnterpriseCA1)
}

type ESC3Harness3 struct {
CertTemplate1 *graph.Node
CertTemplate2 *graph.Node
Domain *graph.Node
EnterpriseCA1 *graph.Node
Group1 *graph.Node
NTAuthStore *graph.Node
RootCA *graph.Node
User2 *graph.Node
}

func (s *ESC3Harness3) Setup(c *GraphTestContext) {
sid := RandomDomainSID()
emptyEkus := make([]string, 0)
s.User2 = c.NewActiveDirectoryUser("User2", sid)
s.Group1 = c.NewActiveDirectoryGroup("Group1", sid)
s.CertTemplate1 = c.NewActiveDirectoryCertTemplate("CertTemplate1", sid, CertTemplateData{
RequiresManagerApproval: false,
AuthenticationEnabled: true,
EnrolleeSuppliesSubject: false,
SubjectAltRequireUPN: false,
SubjectAltRequireSPN: false,
NoSecurityExtension: false,
SchemaVersion: 2,
AuthorizedSignatures: 0,
EKUS: emptyEkus,
ApplicationPolicies: emptyEkus,
})
s.CertTemplate2 = c.NewActiveDirectoryCertTemplate("CertTemplate2", sid, CertTemplateData{
RequiresManagerApproval: false,
AuthenticationEnabled: true,
EnrolleeSuppliesSubject: false,
SubjectAltRequireUPN: true,
SubjectAltRequireSPN: false,
NoSecurityExtension: false,
SchemaVersion: 1,
AuthorizedSignatures: 0,
EKUS: emptyEkus,
ApplicationPolicies: emptyEkus,
})
s.EnterpriseCA1 = c.NewActiveDirectoryEnterpriseCA("EnterpriseCA1", sid)
s.NTAuthStore = c.NewActiveDirectoryNTAuthStore("NTAuthStore", sid)
s.RootCA = c.NewActiveDirectoryRootCA("RootCA", sid)
s.Domain = c.NewActiveDirectoryDomain("ESC3-1Domain", sid, false, true)

c.NewRelationship(s.User2, s.Group1, ad.MemberOf)
c.NewRelationship(s.Group1, s.CertTemplate1, ad.Enroll)
c.NewRelationship(s.Group1, s.EnterpriseCA1, ad.Enroll)
c.NewRelationship(s.Group1, s.CertTemplate2, ad.AllExtendedRights)
c.NewRelationship(s.CertTemplate1, s.EnterpriseCA1, ad.PublishedTo)
c.NewRelationship(s.CertTemplate1, s.CertTemplate2, ad.EnrollOnBehalfOf)
c.NewRelationship(s.CertTemplate2, s.EnterpriseCA1, ad.PublishedTo)
c.NewRelationship(s.EnterpriseCA1, s.NTAuthStore, ad.TrustedForNTAuth)
c.NewRelationship(s.EnterpriseCA1, s.RootCA, ad.IssuedSignedBy)
c.NewRelationship(s.NTAuthStore, s.Domain, ad.NTAuthStoreFor)
c.NewRelationship(s.RootCA, s.Domain, ad.RootCAFor)

s.EnterpriseCA1.Properties.Set(ad.EnrollmentAgentRestrictionsCollected.String(), false)
c.UpdateNode(s.EnterpriseCA1)
}

type ESC9aPrincipalHarness struct {
CertTemplate *graph.Node
DC *graph.Node
Expand Down Expand Up @@ -5487,6 +5548,7 @@ type HarnessDetails struct {
AZInboundControlHarness AZInboundControlHarness
ESC3Harness1 ESC3Harness1
ESC3Harness2 ESC3Harness2
ESC3Harness3 ESC3Harness3
ESC6aHarnessPrincipalEdges ESC6aHarnessPrincipalEdges
ESC6aHarnessECA ESC6aHarnessECA
ESC6aHarnessTemplate1 ESC6aHarnessTemplate1
Expand Down
Loading

0 comments on commit 20b2708

Please sign in to comment.