-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathlist-partners.go
More file actions
224 lines (190 loc) · 7.47 KB
/
Copy pathlist-partners.go
File metadata and controls
224 lines (190 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (C) 2026 Specter Ops, Inc.
//
// This file is part of AzureHound.
//
// AzureHound is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// AzureHound is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package cmd
import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"regexp"
"time"
"github.com/bloodhoundad/azurehound/v2/client"
"github.com/bloodhoundad/azurehound/v2/client/query"
"github.com/bloodhoundad/azurehound/v2/enums"
"github.com/bloodhoundad/azurehound/v2/models"
"github.com/bloodhoundad/azurehound/v2/models/azure"
"github.com/bloodhoundad/azurehound/v2/panicrecovery"
"github.com/bloodhoundad/azurehound/v2/pipeline"
"github.com/spf13/cobra"
)
var externalLinkSuffix = regexp.MustCompile(`\s@\([^)]*\)`)
func init() {
listRootCmd.AddCommand(listPartnersCmd)
}
var listPartnersCmd = &cobra.Command{
Use: "partners",
Long: "Lists Azure Active Directory Delegated Partners",
Run: listPartnersCmdImpl,
SilenceUsage: true,
}
func listPartnersCmdImpl(cmd *cobra.Command, args []string) {
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, os.Kill)
defer gracefulShutdown(stop)
log.V(1).Info("testing connections")
azClient := connectAndCreateClient()
log.Info("collecting azure active directory delegated partners...")
start := time.Now()
stream := listPartners(ctx, azClient)
panicrecovery.HandleBubbledPanic(ctx, stop, log)
outputStream(ctx, stream)
duration := time.Since(start)
log.Info("collection completed", "duration", duration.String())
}
func listPartners(ctx context.Context, client client.AzureClient) <-chan interface{} {
out := make(chan interface{})
go func() {
defer panicrecovery.PanicRecovery()
defer close(out)
count := 0
partnerTenants := make(map[string]azure.Tenant, 10)
for partner := range client.ListAzureADPartners(ctx, query.GraphParams{}) {
if partner.Error != nil {
log.Error(partner.Error, "unable to continue processing partners")
return
}
log.V(2).Info("found partner", "companyName", partner.Ok.CompanyName, "partnerTenantId", partner.Ok.PartnerTenantId)
count++
// Begin by fetching the partner tenant information
externalTenant, err := client.GetAzureADTenantInfoById(ctx, partner.Ok.PartnerTenantId)
if err != nil {
log.Error(err, "failed to retrieve tenant information for external partner", "companyName", partner.Ok.CompanyName, "partnerTenantId", partner.Ok.PartnerTenantId)
}
externalTenant.Id = fmt.Sprintf("/tenants/%s", externalTenant.TenantId)
externalTenant.TenantType = partner.Ok.CompanyType
if ok := pipeline.SendAny(ctx.Done(), out, AzureWrapper{
Kind: enums.KindAZTenant,
Data: models.Tenant{
Tenant: externalTenant,
External: true,
},
}); !ok {
return
}
partnerTenants[externalTenant.TenantId] = externalTenant
}
log.Info("finished listing all delegated partners", "count", count)
count = 0
// This part is a bit hacky but i'll try to explain what's going on:
//
// For partners, associated pricipal data is stored in their tenant.
// This means that you unfortunately can't just directly query our own
// list of groups/users/service principals and get back the principal
// information directly. For some reason Microsoft decided to lock this
// info behind calls that let you query information via `$expand` queries.
//
// While I'd love to filter based on `principalOrganizationId`, this field
// seems to be some dynamic magic field on the backend and therefor can't be
// filtered on. Morover, if you just use `$expand` on `principal` and try to
// list all role assignments, you still won't get the information you're looking
// for. So far the only way I'm able to reliably filter external tenant's
// information is by passing a `roleDefinitionId` filter on `roleAssignments`
// which results in the `principal` field and the `principalOrganizationId` field
// being present.
//
// If you find a more efficient way of getting this info I'd love to see an
// improved version :)
observedPrincipalIds := make(map[string]bool)
for role := range client.ListAzureADRoles(ctx, query.GraphParams{}) {
if role.Error != nil {
log.Error(role.Error, "unable to continue processing partner roles")
break
}
for item := range client.ListAzureADRoleAssignments(ctx, query.GraphParams{
Filter: fmt.Sprintf("roleDefinitionId eq '%s'", role.Ok.Id),
Expand: "principal",
}) {
if item.Error != nil {
log.Error(item.Error, "unable to continue processing partner role assignments")
break
}
tenant, exists := partnerTenants[item.Ok.PrincipalOrganizationId]
if !exists {
continue
}
var header struct {
Type string `json:"@odata.type"`
Id string `json:"Id"`
DisplayName string `json:"DisplayName,omitempty"`
}
if err := json.Unmarshal(item.Ok.Principal, &header); err != nil {
log.Error(err, "unable to determine principal type")
continue
}
if _, ok := observedPrincipalIds[header.Id]; ok {
continue
}
var (
kind enums.Kind
data any
)
switch header.Type {
case "#microsoft.graph.user":
var user azure.User
if err := json.Unmarshal(item.Ok.Principal, &user); err != nil {
log.Error(err, "unable to unmarshal user principal")
continue
}
user.DisplayName = externalLinkSuffix.ReplaceAllString(user.DisplayName, "")
kind = enums.KindAZUser
data = models.User{User: user, TenantId: tenant.TenantId, TenantName: tenant.DisplayName}
log.V(2).Info("found partner user information", "id", item.Ok.Id)
case "#microsoft.graph.group":
var group azure.Group
if err := json.Unmarshal(item.Ok.Principal, &group); err != nil {
log.Error(err, "unable to unmarshal group principal")
continue
}
group.DisplayName = externalLinkSuffix.ReplaceAllString(group.DisplayName, "")
kind = enums.KindAZGroup
data = models.Group{Group: group, TenantId: tenant.TenantId, TenantName: tenant.DisplayName}
log.V(2).Info("found partner group information", "id", item.Ok.Id)
case "#microsoft.graph.servicePrincipal":
var sp azure.ServicePrincipal
if err := json.Unmarshal(item.Ok.Principal, &sp); err != nil {
log.Error(err, "unable to unmarshal service principal")
continue
}
sp.DisplayName = externalLinkSuffix.ReplaceAllString(sp.DisplayName, "")
kind = enums.KindAZServicePrincipal
data = models.ServicePrincipal{ServicePrincipal: sp, TenantId: tenant.TenantId, TenantName: tenant.DisplayName}
log.V(2).Info("found partner service principal information", "id", item.Ok.Id)
default:
log.V(2).Info("skipping unknown principal type", "type", header.Type)
continue
}
observedPrincipalIds[header.Id] = true
if ok := pipeline.SendAny(ctx.Done(), out, AzureWrapper{Kind: kind, Data: data}); !ok {
break
}
count++
}
}
log.Info("finished listing all delegated partner principals", "count", count)
}()
return out
}