IP Forwarding Should Be Disabled
More Info:
IP forwarding should be disabled on all instances. This ensures that the instance sends and receives packets with matching destination or source IPs.
Risk Level
Medium
Address
Reliability, Security
Compliance Standards
- CIS GCP
- CIS GCP 2.0.0
- Cloudanix Best Practice
- NIST CSF
- PCI
- SOC2
Triage and Remediation
- Remediation
Remediation
Using Console
To remediate the IP forwarding misconfiguration in GCP using the GCP console, follow these steps:
-
Open the GCP console and select the project where the misconfiguration needs to be remediated.
-
In the left navigation pane, select "Compute Engine" and then select "VM instances".
-
Select the VM instance where IP forwarding needs to be disabled.
-
Click on the "Edit" button at the top of the page.
-
Scroll down to the "Network interfaces" section and select the network interface where IP forwarding needs to be disabled.
-
In the "Network interface details" section, uncheck the "Enable IP forwarding" checkbox.
-
Click on the "Save" button at the bottom of the page to save the changes.
-
Repeat steps 3-7 for any other VM instances where IP forwarding needs to be disabled.
Once you have completed these steps, IP forwarding will be disabled for the selected VM instances, and the misconfiguration will be remediated.
Using CLI
To remediate the IP forwarding misconfiguration for GCP using GCP CLI, follow these steps:
-
Open the Google Cloud Console and select the project where the misconfiguration exists.
-
Open the Cloud Shell by clicking on the icon in the top right corner of the console.
-
In the Cloud Shell, run the following command to disable IP forwarding for all instances in the default network:
gcloud compute networks subnets update default --no-enable-alias-ip-range --no-enable-ip-forwarding
- If you have custom networks or subnets, run the following command to disable IP forwarding for those:
gcloud compute networks subnets update [SUBNET_NAME] --no-enable-alias-ip-range --no-enable-ip-forwarding
Replace [SUBNET_NAME] with the name of the subnet where you want to disable IP forwarding.
- Verify that IP forwarding is disabled by running the following command:
gcloud compute networks subnets describe [SUBNET_NAME] --format="value(enableIpForwarding)"
This command should return "False" if IP forwarding is disabled.
- Repeat steps 4 and 5 for all other custom subnets in your project.
By following these steps, you can remediate the IP forwarding misconfiguration in GCP using GCP CLI.
Using Python
To remediate the IP Forwarding misconfiguration in GCP using Python, you can use the following steps:
- Import the necessary libraries:
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
- Set up the credentials:
credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'v1', credentials=credentials)
- Get the current status of IP Forwarding:
project = 'your-project-id'
zone = 'your-zone'
instance = 'your-instance-name'
response = service.instances().get(project=project, zone=zone, instance=instance).execute()
ip_forwarding = response['canIpForward']
- If IP Forwarding is enabled, disable it:
if ip_forwarding:
response = service.instances().setIamPolicy(
project=project,
zone=zone,
resource=instance,
body={
"canIpForward": False
}
).execute()
print(f"IP Forwarding has been disabled for {instance}.")
else:
print(f"IP Forwarding is already disabled for {instance}.")
- Verify that IP Forwarding has been disabled by checking the current status again:
response = service.instances().get(project=project, zone=zone, instance=instance).execute()
ip_forwarding = response['canIpForward']
if not ip_forwarding:
print(f"IP Forwarding has been successfully disabled for {instance}.")
else:
print(f"Failed to disable IP Forwarding for {instance}.")
Note: Make sure to replace the project, zone, and instance variables with your own values.
Using Terraform
resource "google_compute_instance" "EXAMPLE_INSTANCE" {
name = "REPLACE_WITH_INSTANCE_NAME"
machine_type = "REPLACE_WITH_MACHINE_TYPE"
zone = "REPLACE_WITH_ZONE"
# Disables IP forwarding so the instance only sends/receives packets
# whose source/destination IPs match its own interfaces.
can_ip_forward = false
boot_disk {
initialize_params {
image = "REPLACE_WITH_IMAGE"
}
}
network_interface {
network = "REPLACE_WITH_NETWORK"
subnetwork = "REPLACE_WITH_SUBNETWORK"
# Add access_config only if you need a public IP
# access_config {}
}
service_account {
email = "REPLACE_WITH_SERVICE_ACCOUNT_EMAIL"
scopes = ["REPLACE_WITH_SCOPES"]
}
# ...other arguments as needed...
}
Changing can_ip_forward from true to false forces replacement of the instance (google_compute_instance is destroyed and recreated), which is an outage unless you handle cutover (e.g., via MIGs or blue/green).
To verify, run terraform plan and ensure it shows can_ip_forward changing from true to false with the instance marked for replacement (-/+ change).