-
Notifications
You must be signed in to change notification settings - Fork 135
Azurehound enhancement - Fetch Managed Devices from Intune #136
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
Open
manas-metron
wants to merge
17
commits into
SpecterOps:main
Choose a base branch
from
metron-labs:vishalk-metron-azurehound-enhancement-metronlabs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
af39e51
Updated to the latest commit of main branch on the original repo
vishalk-metron 848b218
Update .gitignore
vishalk-metron 62498f4
Partial code completion for fetching devices from intune
vishalk-metron 0d5c890
sample integration example (partial, may not work)
vishalk-metron 45f8965
Merge remote-tracking branch 'spectreops-ah-repo/main' into vishalk-m…
vishalk-metron a62a190
Intune basic APIs have been implemented
vishalk-metron 688ccf2
Added powershell script to get JWT Token from graph
vishalk-metron c6a476f
Update .gitignore
vishalk-metron 373cfc2
Updated the file 'list-intune-script-results.go' to get results from …
vishalk-metron c85f340
Added files for Registry values module - incomplete
vishalk-metron ab1d17b
Removed Registry Values Files to clean up the PR
vishalk-metron 8c4e2a8
Delete registry.go
vishalk-metron 6c20e19
Removed references for script execution module
vishalk-metron 14f9461
Removed Duplicate Code, Unused Code
vishalk-metron 5fcda19
Reduced Code Duplication
vishalk-metron 078d2a7
CodeRabbit PR Comments Resolved
vishalk-metron bff0f20
Delete get_token.ps1
vishalk-metron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
| // File: client/intune_devices.go | ||
| // Copyright (C) 2022 SpecterOps | ||
| // Implementation of Intune device management API calls | ||
|
|
||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/bloodhoundad/azurehound/v2/client/query" | ||
| "github.com/bloodhoundad/azurehound/v2/constants" | ||
| "github.com/bloodhoundad/azurehound/v2/models/intune" | ||
| ) | ||
|
|
||
| func setDefaultParams(params *query.GraphParams) { | ||
| if params.Top == 0 { | ||
| params.Top = 999 | ||
| } | ||
| } | ||
|
|
||
| // ListIntuneManagedDevices retrieves all managed devices from Intune | ||
| // GET /deviceManagement/managedDevices | ||
| func (s *azureClient) ListIntuneManagedDevices(ctx context.Context, params query.GraphParams) <-chan AzureResult[intune.ManagedDevice] { | ||
| var ( | ||
| out = make(chan AzureResult[intune.ManagedDevice]) | ||
| path = fmt.Sprintf("/%s/deviceManagement/managedDevices", constants.GraphApiVersion) | ||
| ) | ||
|
|
||
| setDefaultParams(¶ms) | ||
|
|
||
| go getAzureObjectList[intune.ManagedDevice](s.msgraph, ctx, path, params, out) | ||
| return out | ||
| } | ||
|
|
||
| // GetIntuneDeviceCompliance retrieves compliance information for a specific device | ||
| // GET /deviceManagement/managedDevices/{id}/deviceCompliancePolicyStates | ||
| func (s *azureClient) GetIntuneDeviceCompliance(ctx context.Context, deviceId string, params query.GraphParams) <-chan AzureResult[intune.ComplianceState] { | ||
| var ( | ||
| out = make(chan AzureResult[intune.ComplianceState]) | ||
| path = fmt.Sprintf("/%s/deviceManagement/managedDevices/%s/deviceCompliancePolicyStates", constants.GraphApiVersion, deviceId) | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| setDefaultParams(¶ms) | ||
|
|
||
| go getAzureObjectList[intune.ComplianceState](s.msgraph, ctx, path, params, out) | ||
| return out | ||
| } | ||
|
|
||
| // GetIntuneDeviceConfiguration retrieves configuration information for a specific device | ||
| // GET /deviceManagement/managedDevices/{id}/deviceConfigurationStates | ||
| func (s *azureClient) GetIntuneDeviceConfiguration(ctx context.Context, deviceId string, params query.GraphParams) <-chan AzureResult[intune.ConfigurationState] { | ||
| var ( | ||
| out = make(chan AzureResult[intune.ConfigurationState]) | ||
| path = fmt.Sprintf("/%s/deviceManagement/managedDevices/%s/deviceConfigurationStates", constants.GraphApiVersion, deviceId) | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| setDefaultParams(¶ms) | ||
|
|
||
| go getAzureObjectList[intune.ConfigurationState](s.msgraph, ctx, path, params, out) | ||
| return out | ||
| } | ||
This file contains hidden or 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,204 @@ | ||
| // File: cmd/list-intune-compliance.go | ||
| // Command for listing Intune device compliance information | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/bloodhoundad/azurehound/v2/client" | ||
| "github.com/bloodhoundad/azurehound/v2/client/query" | ||
| "github.com/bloodhoundad/azurehound/v2/config" | ||
| "github.com/bloodhoundad/azurehound/v2/enums" | ||
| "github.com/bloodhoundad/azurehound/v2/models/intune" | ||
| "github.com/bloodhoundad/azurehound/v2/panicrecovery" | ||
| "github.com/bloodhoundad/azurehound/v2/pipeline" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func createBasicComplianceState(device intune.ManagedDevice, suffix string) intune.ComplianceState { | ||
| return intune.ComplianceState{ | ||
| Id: device.Id + suffix, | ||
| DeviceId: device.Id, | ||
| DeviceName: device.DeviceName, | ||
| State: device.ComplianceState, | ||
| Version: 1, | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| var ( | ||
| complianceState string | ||
| includeDetails bool | ||
| ) | ||
|
|
||
| func init() { | ||
| listRootCmd.AddCommand(listIntuneComplianceCmd) | ||
|
|
||
| listIntuneComplianceCmd.Flags().StringVar(&complianceState, "state", "", "Filter by compliance state: compliant, noncompliant, conflict, error, unknown") | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| listIntuneComplianceCmd.Flags().BoolVar(&includeDetails, "details", false, "Include detailed compliance settings") | ||
| } | ||
|
|
||
| var listIntuneComplianceCmd = &cobra.Command{ | ||
| Use: "intune-compliance", | ||
| Short: "List Intune device compliance information", | ||
| Long: `List compliance information for Intune managed devices. | ||
|
|
||
| Examples: | ||
| # List all device compliance | ||
| azurehound list intune-compliance --jwt $JWT | ||
|
|
||
| # List only non-compliant devices | ||
| azurehound list intune-compliance --state noncompliant --jwt $JWT | ||
|
|
||
| # Include detailed compliance settings | ||
| azurehound list intune-compliance --details --jwt $JWT`, | ||
| Run: listIntuneComplianceCmdImpl, | ||
| SilenceUsage: true, | ||
| } | ||
|
|
||
| func listIntuneComplianceCmdImpl(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 intune device compliance...") | ||
| start := time.Now() | ||
| stream := listIntuneCompliance(ctx, azClient) | ||
| panicrecovery.HandleBubbledPanic(ctx, stop, log) | ||
| outputStream(ctx, stream) | ||
| duration := time.Since(start) | ||
| log.Info("collection completed", "duration", duration.String()) | ||
| } | ||
|
|
||
| func listIntuneCompliance(ctx context.Context, client client.AzureClient) <-chan interface{} { | ||
| var ( | ||
| out = make(chan interface{}) | ||
| ) | ||
|
|
||
| go func() { | ||
| defer panicrecovery.PanicRecovery() | ||
| defer close(out) | ||
|
|
||
| // First get all managed devices | ||
| devices := getComplianceTargetDevices(ctx, client) | ||
|
|
||
| // Then collect compliance data for each device | ||
| collectDeviceCompliance(ctx, client, devices, out) | ||
| }() | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| func getComplianceTargetDevices(ctx context.Context, client client.AzureClient) <-chan intune.ManagedDevice { | ||
| var ( | ||
| out = make(chan intune.ManagedDevice) | ||
| params = query.GraphParams{ | ||
| Filter: "operatingSystem eq 'Windows'", | ||
| } | ||
| ) | ||
|
|
||
| // Apply compliance state filter if specified | ||
| if complianceState != "" { | ||
| if params.Filter != "" { | ||
| params.Filter += " and " | ||
| } | ||
| params.Filter += fmt.Sprintf("complianceState eq '%s'", complianceState) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| go func() { | ||
| defer panicrecovery.PanicRecovery() | ||
| defer close(out) | ||
|
|
||
| count := 0 | ||
| for item := range client.ListIntuneManagedDevices(ctx, params) { | ||
| if item.Error != nil { | ||
| log.Error(item.Error, "unable to continue processing devices") | ||
| } else { | ||
| log.V(2).Info("found device for compliance check", "device", item.Ok.DeviceName) | ||
| count++ | ||
| select { | ||
| case out <- item.Ok: | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
| } | ||
| log.V(1).Info("finished collecting target devices", "count", count) | ||
| }() | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| func collectDeviceCompliance(ctx context.Context, client client.AzureClient, devices <-chan intune.ManagedDevice, out chan<- interface{}) { | ||
| var ( | ||
| streams = pipeline.Demux(ctx.Done(), devices, config.ColStreamCount.Value().(int)) | ||
| wg sync.WaitGroup | ||
| ) | ||
|
|
||
| wg.Add(len(streams)) | ||
| for i := range streams { | ||
| stream := streams[i] | ||
| go func() { | ||
| defer panicrecovery.PanicRecovery() | ||
| defer wg.Done() | ||
|
|
||
| for device := range stream { | ||
| if includeDetails { | ||
| collectDetailedCompliance(ctx, client, device, out) | ||
| } else { | ||
| basicCompliance := createBasicComplianceState(device, "-basic") | ||
| select { | ||
| case out <- NewAzureWrapper(enums.KindAZIntuneCompliance, basicCompliance): | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
| } | ||
| }() | ||
| } | ||
| wg.Wait() | ||
| } | ||
|
|
||
| func collectDetailedCompliance(ctx context.Context, client client.AzureClient, device intune.ManagedDevice, out chan<- interface{}) { | ||
| log.V(2).Info("collecting detailed compliance", "device", device.DeviceName) | ||
|
|
||
| params := query.GraphParams{} | ||
| count := 0 | ||
|
|
||
| for complianceResult := range client.GetIntuneDeviceCompliance(ctx, device.Id, params) { | ||
| if complianceResult.Error != nil { | ||
| log.Error(complianceResult.Error, "failed to get detailed compliance", "device", device.DeviceName) | ||
|
|
||
| // Fall back to basic compliance info using helper | ||
| basicCompliance := createBasicComplianceState(device, "-fallback") | ||
| select { | ||
| case out <- NewAzureWrapper(enums.KindAZIntuneCompliance, basicCompliance): | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| log.V(2).Info("found detailed compliance state", | ||
| "device", device.DeviceName, | ||
| "state", complianceResult.Ok.State, | ||
| "settingsCount", len(complianceResult.Ok.SettingStates)) | ||
|
|
||
| count++ | ||
| select { | ||
| case out <- NewAzureWrapper(enums.KindAZIntuneCompliance, complianceResult.Ok): | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if count > 0 { | ||
| log.V(1).Info("finished detailed compliance collection", "device", device.DeviceName, "policies", count) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
This file contains hidden or 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,76 @@ | ||
| // File: cmd/list-intune-devices.go | ||
| // Copyright (C) 2022 SpecterOps | ||
| // Command implementation for listing Intune managed devices | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "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/panicrecovery" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func init() { | ||
| listRootCmd.AddCommand(listIntuneDevicesCmd) | ||
| } | ||
|
|
||
| var listIntuneDevicesCmd = &cobra.Command{ | ||
| Use: "intune-devices", | ||
| Long: "Lists Intune Managed Devices", | ||
| Run: listIntuneDevicesCmdImpl, | ||
| SilenceUsage: true, | ||
| } | ||
|
|
||
| func listIntuneDevicesCmdImpl(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 intune managed devices...") | ||
| start := time.Now() | ||
| stream := listIntuneDevices(ctx, azClient) | ||
| panicrecovery.HandleBubbledPanic(ctx, stop, log) | ||
| outputStream(ctx, stream) | ||
| duration := time.Since(start) | ||
| log.Info("collection completed", "duration", duration.String()) | ||
| } | ||
|
|
||
| func listIntuneDevices(ctx context.Context, client client.AzureClient) <-chan interface{} { | ||
| var ( | ||
| out = make(chan interface{}) | ||
| params = query.GraphParams{ | ||
| Filter: "operatingSystem eq 'Windows'", // Focus on Windows devices for BloodHound | ||
| } | ||
| ) | ||
|
|
||
| go func() { | ||
| defer panicrecovery.PanicRecovery() | ||
| defer close(out) | ||
|
|
||
| count := 0 | ||
| for item := range client.ListIntuneManagedDevices(ctx, params) { | ||
| if item.Error != nil { | ||
| log.Error(item.Error, "unable to continue processing intune devices") | ||
| } else { | ||
| log.V(2).Info("found intune device", "device", item.Ok) | ||
| count++ | ||
| select { | ||
| case out <- NewAzureWrapper(enums.KindAZIntuneDevice, item.Ok): | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
| } | ||
| log.V(1).Info("finished listing intune devices", "count", count) | ||
| }() | ||
|
|
||
| return out | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.