-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for key vault KVContribtors
- Loading branch information
Showing
15 changed files
with
619 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// 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" | ||
"path" | ||
"time" | ||
|
||
"github.com/bloodhoundad/azurehound/client" | ||
"github.com/bloodhoundad/azurehound/constants" | ||
"github.com/bloodhoundad/azurehound/enums" | ||
"github.com/bloodhoundad/azurehound/models" | ||
"github.com/bloodhoundad/azurehound/pipeline" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
listRootCmd.AddCommand(listKeyVaultKVContributorsCmd) | ||
} | ||
|
||
var listKeyVaultKVContributorsCmd = &cobra.Command{ | ||
Use: "key-vault-kvcontributors", | ||
Long: "Lists Azure Key Vault KVContributors", | ||
Run: listKeyVaultKVContributorsCmdImpl, | ||
SilenceUsage: true, | ||
} | ||
|
||
func listKeyVaultKVContributorsCmdImpl(cmd *cobra.Command, args []string) { | ||
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 key vault kvcontributors...") | ||
start := time.Now() | ||
subscriptions := listSubscriptions(ctx, azClient) | ||
keyVaults := listKeyVaults(ctx, azClient, subscriptions) | ||
kvRoleAssignments := listKeyVaultRoleAssignments(ctx, azClient, keyVaults) | ||
stream := listKeyVaultKVContributors(ctx, azClient, kvRoleAssignments) | ||
outputStream(ctx, stream) | ||
duration := time.Since(start) | ||
log.Info("collection completed", "duration", duration.String()) | ||
} | ||
} | ||
|
||
func listKeyVaultKVContributors(ctx context.Context, client client.AzureClient, vmRoleAssignments <-chan interface{}) <-chan interface{} { | ||
out := make(chan interface{}) | ||
|
||
go func() { | ||
defer close(out) | ||
|
||
for result := range pipeline.OrDone(ctx.Done(), vmRoleAssignments) { | ||
if roleAssignments, ok := result.(AzureWrapper).Data.(models.KeyVaultRoleAssignments); !ok { | ||
log.Error(fmt.Errorf("failed type assertion"), "unable to continue enumerating key vault kvContributors", "result", result) | ||
return | ||
} else { | ||
var ( | ||
keyVaultKVContributors = models.KeyVaultKVContributors{ | ||
KeyVaultId: roleAssignments.KeyVaultId, | ||
} | ||
count = 0 | ||
) | ||
for _, item := range roleAssignments.RoleAssignments { | ||
roleDefinitionId := path.Base(item.RoleAssignment.Properties.RoleDefinitionId) | ||
|
||
if roleDefinitionId == constants.KeyVaultContributorRoleID { | ||
keyVaultKVContributor := models.KeyVaultKVContributor{ | ||
KVContributor: item.RoleAssignment, | ||
KeyVaultId: item.KeyVaultId, | ||
} | ||
log.V(2).Info("found key vault kvContributor", "keyVaultKVContributor", keyVaultKVContributor) | ||
count++ | ||
keyVaultKVContributors.KVContributors = append(keyVaultKVContributors.KVContributors, keyVaultKVContributor) | ||
} | ||
} | ||
out <- AzureWrapper{ | ||
Kind: enums.KindAZKeyVaultContributor, | ||
Data: keyVaultKVContributors, | ||
} | ||
log.V(1).Info("finished listing key vault kvContributors", "keyVaultId", roleAssignments.KeyVaultId, "count", count) | ||
} | ||
} | ||
log.Info("finished listing all key vault kvContributors") | ||
}() | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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" | ||
"testing" | ||
|
||
"github.com/bloodhoundad/azurehound/client/mocks" | ||
"github.com/bloodhoundad/azurehound/constants" | ||
"github.com/bloodhoundad/azurehound/models" | ||
"github.com/bloodhoundad/azurehound/models/azure" | ||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func init() { | ||
setupLogger() | ||
} | ||
|
||
func TestListKeyVaultKVContributors(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
ctx := context.Background() | ||
|
||
mockClient := mocks.NewMockAzureClient(ctrl) | ||
|
||
mockRoleAssignmentsChannel := make(chan interface{}) | ||
mockTenant := azure.Tenant{} | ||
mockClient.EXPECT().TenantInfo().Return(mockTenant).AnyTimes() | ||
channel := listKeyVaultKVContributors(ctx, mockClient, mockRoleAssignmentsChannel) | ||
|
||
go func() { | ||
defer close(mockRoleAssignmentsChannel) | ||
|
||
mockRoleAssignmentsChannel <- AzureWrapper{ | ||
Data: models.KeyVaultRoleAssignments{ | ||
KeyVaultId: "foo", | ||
RoleAssignments: []models.KeyVaultRoleAssignment{ | ||
{ | ||
RoleAssignment: azure.RoleAssignment{ | ||
Name: constants.KeyVaultContributorRoleID, | ||
Properties: azure.RoleAssignmentPropertiesWithScope{ | ||
RoleDefinitionId: constants.KeyVaultContributorRoleID, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
}() | ||
|
||
if result, ok := <-channel; !ok { | ||
t.Fatalf("failed to receive from channel") | ||
} else if wrapper, ok := result.(AzureWrapper); !ok { | ||
t.Errorf("failed type assertion: got %T, want %T", result, AzureWrapper{}) | ||
} else if _, ok := wrapper.Data.(models.KeyVaultKVContributors); !ok { | ||
t.Errorf("failed type assertion: got %T, want %T", wrapper.Data, models.KeyVaultKVContributors{}) | ||
} | ||
|
||
if _, ok := <-channel; ok { | ||
t.Error("should not have recieved from channel") | ||
} | ||
} |
Oops, something went wrong.