id: azure-nsg-mssql-unrestricted
info:
  name: Unrestricted MS SQL Server Access in Azure NSGs
  author: princechaddha
  severity: high
  description: |
    Ensure that Microsoft Azure network security groups (NSGs) do not allow unrestricted inbound access on TCP port 1433, used by Microsoft SQL Server, to protect against unauthorized database access and potential data breaches.
  impact: |
    Unrestricted access on TCP port 1433 can expose SQL Servers to attacks such as SQL injection and brute-force attacks, leading to potential data loss and system compromise.
  remediation: |
    Restrict access to MS SQL Server by configuring NSG rules to only allow trusted sources to connect on TCP port 1433. Implement robust monitoring and alerting mechanisms to detect unauthorized access attempts.
  reference:
    - https://docs.microsoft.com/en-us/azure/virtual-network/network-security-groups-overview
  tags: cloud,devops,azure,microsoft,nsg,azure-cloud-config

flow: |
  code(1);
  for (let NsgData of iterate(template.nsgdata)) {
    NsgData = JSON.parse(NsgData)
    set("nsg", NsgData.name)
    set("resourcegroup", NsgData.resourceGroup)
    code(2)
  }

self-contained: true
code:
  - engine:
      - sh
      - bash
    source: |
      az network nsg list --query '[*].{name:name, resourceGroup:resourceGroup}' --output json

    extractors:
      - type: json
        name: nsgdata
        internal: true
        json:
          - '.[]'

  - engine:
      - sh
      - bash
    source: |
      az network nsg rule list --nsg-name $nsg --resource-group $resourcegroup --query "[?direction=='Inbound' && access=='Allow' && protocol=='TCP' && (destinationPortRange=='1433')]"

    matchers:
      - type: word
        words:
          - '"sourceAddressPrefix": "*"'
          - '"sourceAddressPrefix": "internet"'
          - '"sourceAddressPrefix": "any"'

    extractors:
      - type: dsl
        dsl:
          - 'nsg + " has unrestricted access on TCP port 1433"'
# digest: 4b0a00483046022100ecaacdbd77836b218f1a8c18118252838ed46cc7d5f73a0f28a24e0de755dd70022100eb0bcbd75c890450b6c3085ea90d6cc1bae09a91b4e68cf277de86b13d6792aa:922c64590222798bb761d5b6d8e72950