Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnrollOnBehalfOf Post Processing #176

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion cmd/api/src/analysis/ad/adcs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ package ad_test

import (
"context"

"github.com/specterops/bloodhound/analysis"

ad2 "github.com/specterops/bloodhound/analysis/ad"

"github.com/specterops/bloodhound/dawgs/ops"
Expand Down Expand Up @@ -177,3 +177,89 @@ func TestGoldenCert(t *testing.T) {
})

}

func TestEnrollOnBehalfOf(t *testing.T) {
testContext := integration.NewGraphTestContext(t)
testContext.DatabaseTestWithSetup(func(harness *integration.HarnessDetails) {
harness.EnrollOnBehalfOfHarnessOne.Setup(testContext)
}, func(harness integration.HarnessDetails, db graph.Database) error {
certTemplates, err := ad2.FetchNodesByKind(context.Background(), db, ad.CertTemplate)
v1Templates := make([]*graph.Node, 0)
v2Templates := make([]*graph.Node, 0)
for _, template := range certTemplates {
if version, err := template.Properties.Get(ad.SchemaVersion.String()).Float64(); err != nil {
continue
} else if version == 1 {
v1Templates = append(v1Templates, template)
} else if version >= 2 {
v2Templates = append(v2Templates, template)
}
}
require.Nil(t, err)
db.ReadTransaction(context.Background(), func(tx graph.Transaction) error {
results, err := ad2.EnrollOnBehalfOfVersionOne(tx, v1Templates, certTemplates)
require.Nil(t, err)

require.Len(t, results, 2)

require.Contains(t, results, analysis.CreatePostRelationshipJob{
FromID: harness.EnrollOnBehalfOfHarnessOne.CertTemplate11.ID,
ToID: harness.EnrollOnBehalfOfHarnessOne.CertTemplate12.ID,
Kind: ad.EnrollOnBehalfOf,
})

require.Contains(t, results, analysis.CreatePostRelationshipJob{
FromID: harness.EnrollOnBehalfOfHarnessOne.CertTemplate13.ID,
ToID: harness.EnrollOnBehalfOfHarnessOne.CertTemplate12.ID,
Kind: ad.EnrollOnBehalfOf,
})

return nil
})

return nil
})

testContext.DatabaseTestWithSetup(func(harness *integration.HarnessDetails) {
harness.EnrollOnBehalfOfHarnessTwo.Setup(testContext)
}, func(harness integration.HarnessDetails, db graph.Database) error {
certTemplates, err := ad2.FetchNodesByKind(context.Background(), db, ad.CertTemplate)
v1Templates := make([]*graph.Node, 0)
v2Templates := make([]*graph.Node, 0)
for _, template := range certTemplates {
if version, err := template.Properties.Get(ad.SchemaVersion.String()).Float64(); err != nil {
continue
} else if version == 1 {
v1Templates = append(v1Templates, template)
} else if version >= 2 {
v2Templates = append(v2Templates, template)
}
}
require.Nil(t, err)
db.ReadTransaction(context.Background(), func(tx graph.Transaction) error {
results, err := ad2.EnrollOnBehalfOfVersionTwo(tx, v2Templates, certTemplates)
require.Nil(t, err)

require.Len(t, results, 1)
require.Contains(t, results, analysis.CreatePostRelationshipJob{
FromID: harness.EnrollOnBehalfOfHarnessTwo.CertTemplate21.ID,
ToID: harness.EnrollOnBehalfOfHarnessTwo.CertTemplate23.ID,
Kind: ad.EnrollOnBehalfOf,
})

results, err = ad2.EnrollOnBehalfOfSelfControl(tx, v1Templates)
require.Nil(t, err)

require.Len(t, results, 1)
require.Contains(t, results, analysis.CreatePostRelationshipJob{
FromID: harness.EnrollOnBehalfOfHarnessTwo.CertTemplate25.ID,
ToID: harness.EnrollOnBehalfOfHarnessTwo.CertTemplate25.ID,
Kind: ad.EnrollOnBehalfOf,
})

return nil
})

return nil
})
}
5 changes: 4 additions & 1 deletion cmd/api/src/test/integration/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func (s *GraphTestContext) NewActiveDirectoryRootCA(name, domainSID string) *gra
}), ad.Entity, ad.RootCA)
}

func (s *GraphTestContext) NewActiveDirectoryCertTemplate(name, domainSID string, requiresManagerApproval, authenticationEnabled, enrolleeSupplieSubject bool, schemaVersion, authorizedSignatures int) *graph.Node {
func (s *GraphTestContext) NewActiveDirectoryCertTemplate(name, domainSID string, requiresManagerApproval, authenticationEnabled, enrolleeSupplieSubject, subjectAltRequireUpn bool, schemaVersion, authorizedSignatures int, ekus, applicationPolicies []string) *graph.Node {
return s.NewNode(graph.AsProperties(graph.PropertyMap{
common.Name: name,
common.ObjectID: must.NewUUIDv4().String(),
Expand All @@ -520,6 +520,9 @@ func (s *GraphTestContext) NewActiveDirectoryCertTemplate(name, domainSID string
ad.EnrolleeSuppliesSubject: enrolleeSupplieSubject,
ad.SchemaVersion: float64(schemaVersion),
ad.AuthorizedSignatures: float64(authorizedSignatures),
ad.EKUs: ekus,
ad.ApplicationPolicies: applicationPolicies,
ad.SubjectAltRequireUPN: subjectAltRequireUpn,
}), ad.Entity, ad.CertTemplate)
}

Expand Down
90 changes: 81 additions & 9 deletions cmd/api/src/test/integration/harnesses.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,12 +1134,13 @@ type ADCSESC1Harness struct {
}

func (s *ADCSESC1Harness) Setup(graphTestContext *GraphTestContext) {
emptyEkus := make([]string, 0)
sid := RandomDomainSID()
s.Domain1 = graphTestContext.NewActiveDirectoryDomain("domain 1", sid, false, true)
s.AuthStore1 = graphTestContext.NewActiveDirectoryNTAuthStore("ntauthstore 1", sid)
s.EnterpriseCA1 = graphTestContext.NewActiveDirectoryEnterpriseCA("eca 1", sid)
s.RootCA1 = graphTestContext.NewActiveDirectoryRootCA("rca 1", sid)
s.CertTemplate1 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 1", sid, false, true, true, 1, 0)
s.CertTemplate1 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 1", sid, false, true, true, false, 1, 0, emptyEkus, emptyEkus)
s.Group11 = graphTestContext.NewActiveDirectoryGroup("group1-1", sid)
s.Group12 = graphTestContext.NewActiveDirectoryGroup("group1-2", sid)
s.Group13 = graphTestContext.NewActiveDirectoryGroup("group1-3", sid)
Expand Down Expand Up @@ -1175,7 +1176,7 @@ func (s *ADCSESC1Harness) Setup(graphTestContext *GraphTestContext) {
s.EnterpriseCA22 = graphTestContext.NewActiveDirectoryEnterpriseCA("eca2-2", sid)
s.Group21 = graphTestContext.NewActiveDirectoryGroup("group2-1", sid)
s.Group22 = graphTestContext.NewActiveDirectoryGroup("group2-2", sid)
s.CertTemplate2 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 2", sid, false, true, true, 1, 0)
s.CertTemplate2 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 2", sid, false, true, true, false, 1, 0, emptyEkus, emptyEkus)

graphTestContext.NewRelationship(s.RootCA2, s.Domain2, ad.RootCAFor)
graphTestContext.NewRelationship(s.AuthStore2, s.Domain2, ad.NTAuthStoreFor)
Expand All @@ -1197,7 +1198,7 @@ func (s *ADCSESC1Harness) Setup(graphTestContext *GraphTestContext) {
s.EnterpriseCA32 = graphTestContext.NewActiveDirectoryEnterpriseCA("eca3-2", sid)
s.Group31 = graphTestContext.NewActiveDirectoryGroup("group3-1", sid)
s.Group32 = graphTestContext.NewActiveDirectoryGroup("group3-2", sid)
s.CertTemplate3 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 3", sid, false, true, true, 1, 0)
s.CertTemplate3 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 3", sid, false, true, true, false, 1, 0, emptyEkus, emptyEkus)

graphTestContext.NewRelationship(s.RootCA3, s.Domain3, ad.RootCAFor)
graphTestContext.NewRelationship(s.AuthStore3, s.Domain3, ad.NTAuthStoreFor)
Expand All @@ -1222,12 +1223,12 @@ func (s *ADCSESC1Harness) Setup(graphTestContext *GraphTestContext) {
s.Group44 = graphTestContext.NewActiveDirectoryGroup("group4-4", sid)
s.Group45 = graphTestContext.NewActiveDirectoryGroup("group4-5", sid)
s.Group46 = graphTestContext.NewActiveDirectoryGroup("group4-6", sid)
s.CertTemplate41 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-1", sid, false, true, true, 2, 1)
s.CertTemplate42 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-2", sid, false, true, true, 2, 0)
s.CertTemplate43 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-3", sid, false, true, true, 1, 0)
s.CertTemplate44 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-4", sid, true, true, true, 1, 0)
s.CertTemplate45 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-5", sid, false, false, true, 1, 0)
s.CertTemplate46 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-6", sid, false, true, false, 1, 0)
s.CertTemplate41 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-1", sid, false, true, true, false, 2, 1, emptyEkus, emptyEkus)
s.CertTemplate42 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-2", sid, false, true, true, false, 2, 0, emptyEkus, emptyEkus)
s.CertTemplate43 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-3", sid, false, true, true, false, 1, 0, emptyEkus, emptyEkus)
s.CertTemplate44 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-4", sid, true, true, true, false, 1, 0, emptyEkus, emptyEkus)
s.CertTemplate45 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-5", sid, false, false, true, false, 1, 0, emptyEkus, emptyEkus)
s.CertTemplate46 = graphTestContext.NewActiveDirectoryCertTemplate("certtemplate 4-6", sid, false, true, false, true, 1, 0, emptyEkus, emptyEkus)

graphTestContext.NewRelationship(s.AuthStore4, s.Domain4, ad.NTAuthStoreFor)
graphTestContext.NewRelationship(s.RootCA4, s.Domain4, ad.RootCAFor)
Expand All @@ -1251,7 +1252,76 @@ func (s *ADCSESC1Harness) Setup(graphTestContext *GraphTestContext) {
graphTestContext.NewRelationship(s.CertTemplate44, s.EnterpriseCA4, ad.PublishedTo)
graphTestContext.NewRelationship(s.CertTemplate45, s.EnterpriseCA4, ad.PublishedTo)
graphTestContext.NewRelationship(s.CertTemplate46, s.EnterpriseCA4, ad.PublishedTo)
}

type EnrollOnBehalfOfHarnessTwo struct {
Domain2 *graph.Node
AuthStore2 *graph.Node
RootCA2 *graph.Node
EnterpriseCA2 *graph.Node
CertTemplate21 *graph.Node
CertTemplate22 *graph.Node
CertTemplate23 *graph.Node
CertTemplate24 *graph.Node
CertTemplate25 *graph.Node
}

func (s *EnrollOnBehalfOfHarnessTwo) Setup(gt *GraphTestContext) {
certRequestAgentEKU := make([]string, 0)
certRequestAgentEKU = append(certRequestAgentEKU, adAnalysis.EkuCertRequestAgent)
emptyAppPolicies := make([]string, 0)
sid := RandomDomainSID()
s.Domain2 = gt.NewActiveDirectoryDomain("domain2", sid, false, true)
s.AuthStore2 = gt.NewActiveDirectoryNTAuthStore("authstore2", sid)
s.RootCA2 = gt.NewActiveDirectoryRootCA("rca2", sid)
s.EnterpriseCA2 = gt.NewActiveDirectoryEnterpriseCA("eca2", sid)
s.CertTemplate21 = gt.NewActiveDirectoryCertTemplate("certtemplate2-1", sid, false, false, false, false, 1, 0, certRequestAgentEKU, emptyAppPolicies)
s.CertTemplate22 = gt.NewActiveDirectoryCertTemplate("certtemplate2-2", sid, false, false, false, false, 1, 0, []string{adAnalysis.EkuCertRequestAgent, adAnalysis.EkuAnyPurpose}, emptyAppPolicies)
s.CertTemplate23 = gt.NewActiveDirectoryCertTemplate("certtemplate2-3", sid, false, false, false, false, 2, 1, certRequestAgentEKU, []string{adAnalysis.EkuCertRequestAgent})
s.CertTemplate24 = gt.NewActiveDirectoryCertTemplate("certtemplate2-4", sid, false, false, false, false, 2, 1, []string{}, emptyAppPolicies)
s.CertTemplate25 = gt.NewActiveDirectoryCertTemplate("certtemplate2-5", sid, false, false, false, true, 1, 1, []string{}, emptyAppPolicies)

gt.NewRelationship(s.AuthStore2, s.Domain2, ad.NTAuthStoreFor)
gt.NewRelationship(s.RootCA2, s.Domain2, ad.RootCAFor)
gt.NewRelationship(s.EnterpriseCA2, s.AuthStore2, ad.TrustedForNTAuth)
gt.NewRelationship(s.EnterpriseCA2, s.RootCA2, ad.EnterpriseCAFor)
gt.NewRelationship(s.CertTemplate21, s.EnterpriseCA2, ad.PublishedTo)
gt.NewRelationship(s.CertTemplate22, s.EnterpriseCA2, ad.PublishedTo)
gt.NewRelationship(s.CertTemplate23, s.EnterpriseCA2, ad.PublishedTo)
gt.NewRelationship(s.CertTemplate24, s.EnterpriseCA2, ad.PublishedTo)
gt.NewRelationship(s.CertTemplate25, s.EnterpriseCA2, ad.PublishedTo)
}

type EnrollOnBehalfOfHarnessOne struct {
Domain1 *graph.Node
AuthStore1 *graph.Node
RootCA1 *graph.Node
EnterpriseCA1 *graph.Node
CertTemplate11 *graph.Node
CertTemplate12 *graph.Node
CertTemplate13 *graph.Node
}

func (s *EnrollOnBehalfOfHarnessOne) Setup(gt *GraphTestContext) {
sid := RandomDomainSID()
anyPurposeEkus := make([]string, 0)
anyPurposeEkus = append(anyPurposeEkus, adAnalysis.EkuAnyPurpose)
emptyAppPolicies := make([]string, 0)
s.Domain1 = gt.NewActiveDirectoryDomain("domain1", sid, false, true)
s.AuthStore1 = gt.NewActiveDirectoryNTAuthStore("authstore1", sid)
s.RootCA1 = gt.NewActiveDirectoryRootCA("rca1", sid)
s.EnterpriseCA1 = gt.NewActiveDirectoryEnterpriseCA("eca1", sid)
s.CertTemplate11 = gt.NewActiveDirectoryCertTemplate("certtemplate1-1", sid, false, false, false, false, 2, 0, anyPurposeEkus, emptyAppPolicies)
s.CertTemplate12 = gt.NewActiveDirectoryCertTemplate("certtemplate1-2", sid, false, false, false, false, 1, 0, anyPurposeEkus, emptyAppPolicies)
s.CertTemplate13 = gt.NewActiveDirectoryCertTemplate("certtemplate1-3", sid, false, false, false, false, 2, 0, anyPurposeEkus, emptyAppPolicies)
rvazarkar marked this conversation as resolved.
Show resolved Hide resolved

gt.NewRelationship(s.AuthStore1, s.Domain1, ad.NTAuthStoreFor)
gt.NewRelationship(s.RootCA1, s.Domain1, ad.RootCAFor)
gt.NewRelationship(s.EnterpriseCA1, s.AuthStore1, ad.TrustedForNTAuth)
gt.NewRelationship(s.EnterpriseCA1, s.RootCA1, ad.EnterpriseCAFor)
gt.NewRelationship(s.CertTemplate11, s.EnterpriseCA1, ad.PublishedTo)
gt.NewRelationship(s.CertTemplate12, s.EnterpriseCA1, ad.PublishedTo)
gt.NewRelationship(s.CertTemplate13, s.EnterpriseCA1, ad.PublishedTo)
}

type ADCSGoldenCertHarness struct {
Expand Down Expand Up @@ -1414,6 +1484,8 @@ type HarnessDetails struct {
SearchHarness SearchHarness
ShortcutHarness ShortcutHarness
ADCSESC1Harness ADCSESC1Harness
EnrollOnBehalfOfHarnessOne EnrollOnBehalfOfHarnessOne
EnrollOnBehalfOfHarnessTwo EnrollOnBehalfOfHarnessTwo
ADCSGoldenCertHarness ADCSGoldenCertHarness
NumCollectedActiveDirectoryDomains int
AZInboundControlHarness AZInboundControlHarness
Expand Down
Loading
Loading