id: gcloud-unrestricted-icmp-access

info:
  name: Check for Unrestricted ICMP Access
  author: princechaddha
  severity: high
  description: |
    Ensure that your Google Cloud VPC network firewall rules do not allow unrestricted access (0.0.0.0/0) using ICMP. Restrict ICMP-based access to trusted IP addresses or IP ranges to implement the principle of least privilege (POLP) and reduce the attack surface.
  impact: |
    Allowing unrestricted ICMP access exposes cloud resources to potential network reconnaissance and exploitation of vulnerabilities.
  remediation: |
    Update your VPC firewall rules to restrict ICMP-based access to trusted IP addresses or ranges only.
  reference:
    - https://cloud.google.com/vpc/docs/firewalls
  tags: cloud,devops,gcp,gcloud,vpc,firewall,icmp,security,networking,gcp-cloud-config

flow: |
  code(1)
  for(let projectId of iterate(template.projectIds)){
    set("projectId", projectId)
    code(2)
    for(let networkName of iterate(template.networks)){
      set("networkName", networkName)
      code(3)
      set("firewallRules", template.firewallRules)
      javascript(1)
    }
  }

self-contained: true

code:
  - engine:
      - sh
      - bash
    source: |
      gcloud projects list --format="json(projectId)"

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

  - engine:
      - sh
      - bash
    source: |
      gcloud compute networks list --project $projectId --format="json(name)"

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

  - engine:
      - sh
      - bash
    source: |
      gcloud compute firewall-rules list --filter network=$networkName --sort-by priority --format="json(name,disabled,direction,sourceRanges,allowed[].IPProtocol)"

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

javascript:
  - code: |
      let firewallRules = template.firewallRules;
      if (typeof firewallRules === "string") {
        firewallRules = JSON.parse(firewallRules);
      }

      let insecureRules = [];
      for (let rule of firewallRules) {
        if (
          rule.disabled === false &&
          rule.direction === "INGRESS" &&
          Array.isArray(rule.sourceRanges) && rule.sourceRanges.includes("0.0.0.0/0") &&
          Array.isArray(rule.allowed) &&
          rule.allowed.some(allowed => allowed.IPProtocol === "icmp")
        ) {
          insecureRules.push(rule.name);
        }
      }

      if (insecureRules.length > 0) {
        Export(`The firewall rules in network ${template.networkName} of project ${template.projectId} allow unrestricted ICMP access: ${insecureRules.join(", ")}`);
      }

    extractors:
      - type: dsl
        dsl:
          - response
# digest: 4a0a0047304502204ceff14f0f9af20ca74761fc0345fa56667d025c01823a01381cbd6869b429730221009616e9b65e546da1e482b76b76f52e5c4bfb7bf70f9c9b763635e750f645f4a0:922c64590222798bb761d5b6d8e72950