Check provider logo

EC2 instance does not allow ingress from the Internet to TCP ports 27017 or 27018 (MongoDB)

ec2_instance_port_mongodb_exposed_to_internet

Severitycritical
Serviceec2
by Prowler

EC2 instances with security groups permitting inbound TCP 27017 or 27018 (MongoDB) from 0.0.0.0/0 or ::/0 are identified, factoring the instance's public reachability to gauge exposure.

Risk

Internet-exposed MongoDB invites scanning, brute force, and exploits leading to:

  • Data extraction (confidentiality)
  • Collection tampering or deletion (integrity)
  • DoS or ransomware disruptions (availability) A compromised DB host can also enable lateral movement within the environment.
Run this check with Prowler CLI

prowler aws --checks ec2_instance_port_mongodb_exposed_to_internet

Fix finding with Prowler CLI

prowler aws --checks ec2_instance_port_mongodb_exposed_to_internet --fixer

Recommendation

Apply least privilege to MongoDB access:

  • Remove Internet-wide rules; allow only trusted sources
  • Keep DBs on private subnets without public IPs; use private connectivity or proxies
  • Enforce strong auth and TLS
  • Add segmentation and monitoring for defense in depth

Remediation

CLI

aws ec2 revoke-security-group-ingress --group-id <SECURITY_GROUP_ID> --ip-permissions '[{"IpProtocol":"tcp","FromPort":27017,"ToPort":27017,"IpRanges":[{"CidrIp":"0.0.0.0/0"}],"Ipv6Ranges":[{"CidrIpv6":"::/0"}]},{"IpProtocol":"tcp","FromPort":27018,"ToPort":27018,"IpRanges":[{"CidrIp":"0.0.0.0/0"}],"Ipv6Ranges":[{"CidrIpv6":"::/0"}]}]'

Native IaC
Terraform
Other
  1. In the AWS Console, go to EC2 > Security Groups
  2. Select the security group attached to the affected instance
  3. Open the Inbound rules tab and click Edit inbound rules
  4. Delete any rule allowing TCP port 27017 or 27018 from 0.0.0.0/0 or ::/0
  5. If access is required, add a rule for those ports limited to a specific trusted CIDR (e.g., your VPC CIDR)
  6. Click Save rules

Source Code

Resource Type

AwsEc2Instance

References