id: gcloud-unrestricted-outbound-access

info:
  name: Check for Unrestricted Outbound Access on All Ports
  author: princechaddha
  severity: high
  description: |
    Check your Google Cloud VPC network firewall for any egress rules that allow unrestricted access (0.0.0.0/0) to any TCP/UDP ports. Restrict outbound traffic to only those IP addresses and/or IP ranges that require it in order to implement the principle of least privilege and reduce the attack surface.
  impact: |
    Allowing unrestricted outbound access on all ports increases the risk of data exfiltration and potential exploitation of cloud resources.
  remediation: |
    Update your VPC firewall rules to restrict outbound traffic to trusted IP addresses and ports only.
  reference:
    - https://cloud.google.com/vpc/docs/firewalls
  tags: cloud,devops,gcp,gcloud,vpc,firewall,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,destinationRanges,allowed[].ports)"

    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 === "EGRESS" &&
          Array.isArray(rule.destinationRanges) && rule.destinationRanges.includes("0.0.0.0/0") &&
          Array.isArray(rule.allowed) &&
          rule.allowed.some(allowed =>
            Array.isArray(allowed.ports) &&
            (allowed.ports.includes("0-65535") || allowed.ports.some(port => port.startsWith("tcp:0-") || port.startsWith("udp:0-")))
          )
        ) {
          insecureRules.push(rule.name);
        }
      }

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

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