Skip to main content

Triage and Remediation

Remediation

Using Console

To remediate the MySQL port open misconfiguration on GCP using the GCP console, please follow the below steps:
  1. Log in to the GCP Console (https://console.cloud.google.com/).
  2. Navigate to the GCP project that has the misconfigured MySQL port.
  3. In the navigation menu, click on “Compute Engine”.
  4. Select the VM instance that has the open MySQL port.
  5. Click on the “Edit” button at the top of the page.
  6. Scroll down to the “Firewall” section and click on “Networking”.
  7. Under the “Firewall rules” section, click on “default-allow-mysql”.
  8. Click on the “Delete” button to remove the rule.
  9. Click on the “Save” button at the bottom of the page to apply the changes.
By following the above steps, you have successfully remediated the MySQL port open misconfiguration on GCP.

To remediate the MySQL port being open on GCP using GCP CLI, follow these steps:
  1. Open the GCP Console and go to the Cloud Shell.
  2. Run the following command to list all the instances in your project:
gcloud compute instances list
  1. Identify the instance that has the open MySQL port.
  2. Run the following command to SSH into the instance:
gcloud compute ssh [INSTANCE_NAME]Replace [INSTANCE_NAME] with the name of the instance that you identified in step 3.
  1. Once you are logged in to the instance, run the following command to edit the mysqld.cnf file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Look for the following line in the file:
bind-address = 0.0.0.0
  1. Comment out this line by adding a # at the beginning of the line:
# bind-address = 0.0.0.0
  1. Save the changes and exit the file.
  2. Restart the MySQL service by running the following command:
sudo service mysql restart
  1. Exit the SSH session by running the following command:
exitWith these steps, you have successfully remediated the MySQL port being open on your GCP instance using GCP CLI.
To remediate the MySQL Port open misconfiguration in GCP using Python, you can follow the below steps:
  1. Import the necessary libraries:
  1. Authenticate and create a client object:
  1. Define the project and zone where the instance is located:
  1. Define the instance name and the network tag associated with the instance:
  1. Get the instance details using the get method:
  1. Check if the MySQL port is open by looking at the instance’s network tags:
  1. If the MySQL port is open, remove it from the instance’s metadata:
This will remove the MySQL port from the instance’s metadata, effectively closing the port.
Substitute:
  • VPC_NETWORK_NAME with the Terraform resource name or your existing network (google_compute_network.main, etc.).
  • GCP_PROJECT_ID with your project ID.
  • OFFICE_CIDR_BLOCK/BASTION_HOST_IP/32 with the specific trusted CIDR blocks/IPs that are allowed to reach MySQL.
If you currently have a google_compute_firewall rule on this network that:
  • has allows covering tcp ports 3306/4333, and
  • uses source_ranges = ["0.0.0.0/0"] (or other overly broad ranges),
update that existing resource’s source_ranges to the restricted list above (or split out a dedicated rule for MySQL and remove those ports from any public rule). This change is in-place and does not force replacement of the firewall rule.For verification, terraform plan should show the existing firewall rule’s source_ranges changing from ["0.0.0.0/0"] (or other broad ranges) to the specific trusted CIDR blocks, and no other attributes of the rule should change.