-
Notifications
You must be signed in to change notification settings - Fork 67
/
list-azure-ad.go
135 lines (112 loc) · 3.84 KB
/
list-azure-ad.go
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
// Copyright (C) 2022 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"
"fmt"
"os"
"os/signal"
"time"
"github.com/bloodhoundad/azurehound/client"
"github.com/bloodhoundad/azurehound/pipeline"
"github.com/spf13/cobra"
)
func init() {
listRootCmd.AddCommand(listAzureADCmd)
}
var listAzureADCmd = &cobra.Command{
Use: "az-ad",
Long: "Lists All Azure AD Entities",
PersistentPreRunE: persistentPreRunE,
Run: listAzureADCmdImpl,
SilenceUsage: true,
}
func listAzureADCmdImpl(cmd *cobra.Command, args []string) {
if len(args) > 0 {
exit(fmt.Errorf("unsupported subcommand: %v", args))
}
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, os.Kill)
defer gracefulShutdown(stop)
log.V(1).Info("testing connections")
if err := testConnections(); err != nil {
exit(err)
} else if azClient, err := newAzureClient(); err != nil {
exit(err)
} else {
log.Info("collecting azure ad objects...")
start := time.Now()
stream := listAllAD(ctx, azClient)
outputStream(ctx, stream)
duration := time.Since(start)
log.Info("collection completed", "duration", duration.String())
}
}
func listAllAD(ctx context.Context, client client.AzureClient) <-chan interface{} {
var (
apps = make(chan interface{})
apps2 = make(chan interface{})
devices = make(chan interface{})
devices2 = make(chan interface{})
groups = make(chan interface{})
groups2 = make(chan interface{})
groups3 = make(chan interface{})
roles = make(chan interface{})
roles2 = make(chan interface{})
servicePrincipals = make(chan interface{})
servicePrincipals2 = make(chan interface{})
servicePrincipals3 = make(chan interface{})
tenants = make(chan interface{})
)
// Enumerate Apps, AppOwners and AppMembers
pipeline.Tee(ctx.Done(), listApps(ctx, client), apps, apps2)
appOwners := listAppOwners(ctx, client, apps2)
// Enumerate Devices and DeviceOwners
pipeline.Tee(ctx.Done(), listDevices(ctx, client), devices, devices2)
deviceOwners := listDeviceOwners(ctx, client, devices2)
// Enumerate Groups, GroupOwners and GroupMembers
pipeline.Tee(ctx.Done(), listGroups(ctx, client), groups, groups2, groups3)
groupOwners := listGroupOwners(ctx, client, groups2)
groupMembers := listGroupMembers(ctx, client, groups3)
// Enumerate ServicePrincipals and ServicePrincipalOwners
pipeline.Tee(ctx.Done(), listServicePrincipals(ctx, client), servicePrincipals, servicePrincipals2, servicePrincipals3)
servicePrincipalOwners := listServicePrincipalOwners(ctx, client, servicePrincipals2)
// Enumerate Tenants
pipeline.Tee(ctx.Done(), listTenants(ctx, client), tenants)
// Enumerate Users
users := listUsers(ctx, client)
// Enumerate Roles and RoleAssignments
pipeline.Tee(ctx.Done(), listRoles(ctx, client), roles, roles2)
roleAssignments := listRoleAssignments(ctx, client, roles2)
// Enumerate AppRoleAssignments
appRoleAssignments := listAppRoleAssignments(ctx, client, servicePrincipals3)
return pipeline.Mux(ctx.Done(),
appOwners,
appRoleAssignments,
apps,
deviceOwners,
devices,
groupMembers,
groupOwners,
groups,
roleAssignments,
roles,
servicePrincipalOwners,
servicePrincipals,
tenants,
users,
)
}