Skip to main content

More Info:

No security group should allow unrestricted ingress access to MongoDB port 27017.

Risk Level

Medium

Address

Security

Compliance Standards

  • APRA CPS 234 (Australia)
  • AWS Startup Security Baseline
  • AWS Well Architected Framework
  • BSI C5 (Germany)
  • Brazil LGPD
  • CCPA / CPRA (California)
  • CIS Critical Security Controls v8
  • CMMC 2.0
  • CSA Cloud Controls Matrix v4
  • Cloudanix Best Practice
  • DPDPA
  • Digital Operational Resilience Act (EU)
  • Essential 8
  • FedRAMP
  • GDPR
  • HITRUST CSF
  • ISO/IEC 27017
  • ISO/IEC 27018
  • ISO/IEC 27701
  • KSA PDPL
  • MAS Technology Risk Management (Singapore)
  • MITRE ATT&CK (Cloud)
  • NIS2 Directive
  • NIST CSF
  • NIST SP 800-171
  • NYDFS 23 NYCRR 500
  • PCI
  • Reserve Bank of India (RBI) Cyber Security Framework
  • Reserve Bank of India (RBI) Master Direction – Information Technology Framework
  • SOC2
  • SWIFT Customer Security Controls Framework
  • Sarbanes-Oxley IT General Controls
  • UK NCSC Cyber Assessment Framework

Triage and Remediation

Remediation

Using Console

To remediate the unrestricted MongoDB access issue in AWS, you can follow these steps:
  1. Open the AWS Management Console and navigate to the EC2 dashboard.
  2. Select the Security Groups option from the left-hand menu.
  3. Find the security group associated with the MongoDB instance that you want to restrict access to.
  4. Click on the security group to open the details page.
  5. Select the Inbound Rules tab.
  6. Locate the rule that allows unrestricted access to MongoDB (default port 27017).
  7. Click on the “Edit” button next to the rule.
  8. Change the Source field to restrict access to only the IP addresses or CIDR ranges that require access to the MongoDB instance.
  9. Click “Save” to apply the changes.
By following these steps, you have successfully remediated the unrestricted MongoDB access issue in AWS by restricting access to only authorized IP addresses or CIDR ranges.

To remediate unrestricted MongoDB access in AWS using AWS CLI, follow the below steps:
  1. Open the AWS CLI on your local machine.
  2. Run the following command to list all the MongoDB instances running in your AWS account:
  1. Identify the MongoDB instances that have unrestricted access.
  2. Run the following command to modify the security group associated with the MongoDB instance to allow access only from specific IP addresses:
Note: Replace <db-instance-identifier> with the identifier of the MongoDB instance and <security-group-id> with the ID of the security group that allows access only from specific IP addresses. 5. Verify the changes by running the following command:
Note: Replace <db-instance-identifier> with the identifier of the MongoDB instance.After these steps, the MongoDB instance will only allow access from specific IP addresses.
To remediate unrestricted MongoDB access in AWS using Python, you can follow these steps:
  1. Identify the MongoDB instances running in your AWS environment.
  2. For each instance, check if the security group associated with it allows unrestricted access to the MongoDB port (default port is 27017).
  3. If the security group allows unrestricted access, update the security group to restrict access to the MongoDB port to only the necessary IP addresses or CIDR ranges.
  4. You can use the boto3 library in Python to interact with the AWS API and perform the above steps.
Here’s a sample Python code that can help you remediate the issue:
Note: This is just a sample code and may need to be modified based on your specific AWS environment and requirements. It’s recommended to test the code in a non-production environment before applying it to your production environment.
Substitute:
  • aws_vpc.MY_VPC.id with your VPC resource reference.
  • "ALLOWED_IPV4_CIDR" / "ALLOWED_IPV6_CIDR" with the specific networks that should reach MongoDB.
This change does not replace the security group itself, only its rule resources; it can disrupt traffic to MongoDB if you remove rules without adding appropriate restricted ones.Verification with terraform plan:
  • You should see the aws_vpc_security_group_ingress_rule resources that used 0.0.0.0/0 and/or ::/0 on port 27017 being destroyed.
  • You should see any new restricted aws_vpc_security_group_ingress_rule resources being created (if you added them).

Additional Reading: