query to associate the department with an MDC recommendation

Yue Ma 40 Reputation points
2024-03-21T15:33:25.52+00:00

How can I update the following query to associate the department with each MDC recommendation?

securityresources
        | where type == "microsoft.security/assessments"
        | extend source = trim(' ', tolower(tostring(properties.resourceDetails.Source)))
                                          | extend resourceId = trim(' ', tolower(tostring(case(
                                                                                    source =~ "azure", properties.resourceDetails.Id,
                                                                                    source =~ "aws" and isnotempty(tostring(properties.resourceDetails.ConnectorId)), properties.resourceDetails.Id,
                                                                                    source =~ "gcp" and isnotempty(tostring(properties.resourceDetails.ConnectorId)), properties.resourceDetails.Id,
                                                                                    source =~ 'aws', properties.resourceDetails.AzureResourceId,
                                                                                    source =~ 'gcp', properties.resourceDetails.AzureResourceId,
                                                                                    extract('^(.+)/providers/Microsoft.Security/assessments/.+$',1,id)
                                                                                    ))))
        | extend status = trim(" ", tostring(properties.status.code))
        | extend cause = trim(" ", tostring(properties.status.cause))
        | extend assessmentKey = tostring(name)
        | where assessmentKey == "d57a4221-a804-52ca-3dea-768284f06bb7"

I can get the department name with the following query:

Resources
| where type == 'microsoft.compute/virtualmachines'
| extend
    JoinID = toupper(id),
    OSName = tostring(properties.osProfile.computerName),
    OSType = tostring(properties.storageProfile.osDisk.osType),
    VMSize = tostring(properties.hardwareProfile.vmSize),
    departmentTag = tostring(tags['Department'])
| join kind=leftouter(
    Resources
    | where type == 'microsoft.compute/virtualmachines/extensions'
    | extend 
        VMId = toupper(substring(id, 0, indexof(id, '/extensions'))),
        ExtensionName = name
) on $left.JoinID == $right.VMId
| summarize Extensions = make_list(ExtensionName) by id, OSName, OSType, VMSize, name, resourceGroup, departmentTag
| order by tolower(OSName) asc
Microsoft Defender for Cloud
Microsoft Defender for Cloud
An Azure service that provides threat protection for workloads running in Azure, on-premises, and in other clouds. Previously known as Azure Security Center and Azure Defender.
1,373 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. James Hamil 24,661 Reputation points Microsoft Employee
    2024-03-21T17:21:18.6133333+00:00

    Hi @Yue Ma , you can modify your first query to join the results of the second query based on a common key. In this case, the resourceId from the first query and the JoinID from the second query can serve as the keys for the join operation. Here’s how you can update your query:

    // First query to get MDC recommendations
    securityresources
    | where type == "microsoft.security/assessments"
    | extend source = trim(' ', tolower(tostring(properties.resourceDetails.Source)))
    | extend resourceId = trim(' ', tolower(tostring(case(
        source =~ "azure", properties.resourceDetails.Id,
        source =~ "aws" and isnotempty(tostring(properties.resourceDetails.ConnectorId)), properties.resourceDetails.Id,
        source =~ "gcp" and isnotempty(tostring(properties.resourceDetails.ConnectorId)), properties.resourceDetails.Id,
        source =~ 'aws', properties.resourceDetails.AzureResourceId,
        source =~ 'gcp', properties.resourceDetails.AzureResourceId,
        extract('^(.+)/providers/Microsoft.Security/assessments/.+$',1,id)
    ))))
    | extend status = trim(" ", tostring(properties.status.code))
    | extend cause = trim(" ", tostring(properties.status.cause))
    | extend assessmentKey = tostring(name)
    | where assessmentKey == "d57a4221-a804-52ca-3dea-768284f06bb7"
    
    // Second query to get department names
    | join kind=leftouter (
        Resources
        | where type == 'microsoft.compute/virtualmachines'
        | extend JoinID = toupper(id)
        | extend departmentTag = tostring(tags['Department'])
    ) on $left.resourceId == $right.JoinID
    | project-away JoinID, JoinID1 // Remove duplicate columns after join
    | extend department = departmentTag // Rename for clarity
    

    Remember to replace the resourceId and JoinID with the actual column names used in your data if they are different.

    Please let me know if you have any questions or if this doesn't work and I can help you further.

    If this answer helps you please mark "Accept Answer" so other users can reference it.

    Thank you,

    James


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.