id: azure-nsg-smtp-unrestricted
info:
  name: Unrestricted SMTP 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 25, used for Simple Mail Transfer Protocol (SMTP), to prevent spam and unauthorized email relay.
  impact: |
    Allowing unrestricted access on TCP port 25 can lead to the misuse of email services, potentially resulting in blacklisting and denial of email service.
  remediation: |
    Configure NSG rules to restrict access to SMTP services on TCP port 25. Allow only trusted IP addresses to send emails and implement proper email authentication mechanisms.
  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=='25')]"

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

    extractors:
      - type: dsl
        dsl:
          - 'nsg + " has unrestricted access on TCP port 25"'
# digest: 490a0046304402205f66e5d17279530d6dee792b670a3e2e9829be1efa2e29eca372c28c19f9625c02207b708fbb20821697c64ba0f7545be011d623adf21de9194794dc2b293119e846:922c64590222798bb761d5b6d8e72950