Kusto Query to get the destination port from properties JSON in resources table where resource type is network security group

Saini, Sandeep 46 Reputation points
2022-12-12T10:52:08.587+00:00

Hello Everyone,

I have extracted the list of network security groups by using:

 Resources  
        | where type =~ 'microsoft.network/networksecuritygroups'  

The output has the properties column which contains JSON in the below format:

{  
    "provisioningState": "Succeeded",  
    "resourceGuid": "1111-2222-3333-4444-5555-6666",  
    "networkInterfaces": [],  
    "defaultSecurityRules": [],  
    "securityRules": [  
        {  
            "properties": {  
                "provisioningState": "Succeeded",  
                "destinationAddressPrefixes": [],  
                "destinationAddressPrefix": "*",  
                "sourceAddressPrefixes": [],  
                "destinationPortRanges": [],  
                "sourceAddressPrefix": "Internet",  
                "destinationPortRange": "22",  
                "sourcePortRanges": [],  
                "sourcePortRange": "*",  
                "protocol": "Tcp",  
                "description": "Allow SSH",  
                "direction": "Inbound",  
                "priority": 100,  
                "access": "Allow"  
            },  
            "name": "SSH-Rule",  
            "type": "Microsoft.Network/networkSecurityGroups/securityRules",  
            "id": "/subscriptions/1111-2222-3333-4444-5555-6666/resourceGroups/demoRG/providers/Microsoft.Network/networkSecurityGroups/demoSecurityGroup/securityRules/SSH-Rule",  
            "etag": "W/\"1111-2222-3333-4444-5555-6666\""  
        },  
        {  
            "properties": {  
                "provisioningState": "Succeeded",  
                "destinationAddressPrefixes": [],  
                "destinationAddressPrefix": "*",  
                "sourceAddressPrefixes": [],  
                "destinationPortRanges": [],  
                "sourceAddressPrefix": "Internet",  
                "destinationPortRange": "3389",  
                "sourcePortRanges": [],  
                "sourcePortRange": "*",  
                "protocol": "Tcp",  
                "description": "Allow RDP",  
                "direction": "Inbound",  
                "priority": 150,  
                "access": "Allow"  
            },  
            "name": "RDP-Rule",  
            "type": "Microsoft.Network/networkSecurityGroups/securityRules",  
            "id": "/subscriptions/1111-2222-3333-4444-5555-6666/resourceGroups/demoRG/providers/Microsoft.Network/networkSecurityGroups/demoSecurityGroup/securityRules/RDP-Rule",  
            "etag": "W/\"1111-2222-3333-4444-5555-6666\""  
        }  
    ]  
}  

By using properties.securityRules I am able to get the JSON that contains the security rules, but I need to display "sourcePortRange" value for "destinationPortRange" values 22 & 3389 in two separate columns along with the "networkSecurityGroupId".

I have already tried achieving by using bag_unpack and mv-apply but not able to extract the required fields with the conditional clause of destination Port range values.
Any help would be highly appreciated.

Thanks!

@Subhash Vasarapu @ChaitanyaNaykodi-MSFT

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,776 questions
0 comments No comments
{count} votes

Accepted answer
  1. GitaraniSharma-MSFT 50,096 Reputation points Microsoft Employee Moderator
    2022-12-12T14:40:13.5+00:00

    Hello @Anonymous ,

    Welcome to Microsoft Q&A Platform. Thank you for reaching out & hope you are doing well.

    I understand that you would like to know how to use Kusto Query for resource type "network security group" within Azure Resource Graph Explorer to display "sourcePortRange" value for "destinationPortRange" values 22 & 3389 in two separate columns along with the "networkSecurityGroupId" from the NSG security rules.

    Below is the Kusto query that I used in my lab which displays the sourcePortRange in a column for destinationPortRange values 22 & 3389 in another column with the networkSecurityGroupId, name and access type.

    resources   
    | where type startswith 'microsoft.network/networksecuritygroups'   
    | mv-expand rules=properties.securityRules  
    | where rules.properties.destinationPortRange in ("3389", "22")  
    | project   
        name        = rules.name,  
        nsgid       = id,  
        access      = rules.properties.access,  
        DestPort    = rules.properties.destinationPortRange,  
        SourcePort  = rules.properties.sourcePortRange    
    

    269676-image.png

    Kindly let us know if the above helps or you need further assistance on this issue.

    ----------------------------------------------------------------------------------------------------------------

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

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.