Description
Ensure that no Amazon EC2 security group allows inbound TCP traffic from the Internet (0.0.0.0/0 or ::/0) to the MongoDB ports 27017 and 27018. Access to these ports should be limited to trusted networks or specific security groups. Security groups that have all ports open to the Internet are reported by the separate check for all ports and pass this check, so they are not reported twice.
Rationale
If the MongoDB ports are open to the Internet, anyone can reach the database and attempt unauthenticated access, brute-force logins or exploits against weak configurations. Attackers can then read or change data, drop collections or hold the data for ransom. Publicly exposed database ports are also continuously scanned and enumerated. Restricting ingress to trusted sources applies least privilege at the network layer and reduces the attack surface of the database tier.
Impact
Prevents direct Internet access to MongoDB
Reduces the risk of data exposure, data loss and ransom attacks
Clients or administrators currently connecting over the Internet will lose access until their source addresses are added as trusted CIDRs, or they connect through a VPN or bastion host
Default Value
A newly created security group has no inbound rules, so all inbound traffic is denied by default. Unrestricted access to ports 27017 or 27018 exists only if a rule was explicitly added.
Pre-Requisites
IAM permissions: ec2:DescribeSecurityGroups, ec2:DescribeSecurityGroupRules, ec2:RevokeSecurityGroupIngress and ec2:AuthorizeSecurityGroupIngress
A list of the trusted CIDR ranges or security groups that legitimately need access to MongoDB
Confirmation from the application owner that no required client depends on Internet access to these ports
Remediation
Test Plan
Console:
Sign in to the AWS Management Console at https://console.aws.amazon.com.
Open the Amazon EC2 console and select Security Groups under Network & Security.
Select a security group and open the Inbound rules tab.
Check for any rule whose source is 0.0.0.0/0 or ::/0 and that either:
has type All traffic, or
uses protocol TCP with a port or port range that includes 27017 or 27018.
Repeat for each security group in each region in use.
If such a rule exists, follow the implementation steps.
CLI:
List the security groups that have any rule open to the Internet:
aws
ec2 describe-security-groups --region <REGION> --filters
Name=ip-permission.cidr,Values=0.0.0.0/0 --query
"SecurityGroups[].GroupId"
aws ec2
describe-security-groups --region <REGION> --filters
Name=ip-permission.ipv6-cidr,Values=::/0 --query
"SecurityGroups[].GroupId"
For each group returned, list its public inbound rules:
aws ec2 describe-security-group-rules --region <REGION> --filters Name=group-id,Values=<SECURITY_GROUP_ID> --query "SecurityGroupRules[?IsEgress==\`false\` && (CidrIpv4=='0.0.0.0/0' || CidrIpv6=='::/0')].[SecurityGroupRuleId,IpProtocol,FromPort,ToPort,CidrIpv4,CidrIpv6]" --output table
The group fails if any row has protocol tcp with a FromPort–ToPort range that includes 27017 or 27018 but is narrower than 0-65535. Rows with protocol -1 (all traffic) or the full range 0-65535 mean all ports are open; such groups are reported by the separate check for all ports and pass this check. A security group that has all ports open to the Internet passes this check, because it is reported by the separate check for all ports.
Implementation Steps
Console:
Sign in to the AWS Management Console at https://console.aws.amazon.com.
Open the Amazon EC2 console and select Security Groups under Network & Security.
Select the security group found in the Test Plan and open the Inbound rules tab.
Choose Edit inbound rules and delete any rule whose source is 0.0.0.0/0 or ::/0 and that either:
has type All traffic, or
uses protocol TCP with a port or port range that includes 27017 or 27018.
If access to MongoDB is still needed, choose Add rule, select type Custom TCP, enter the required port and set the source to a trusted IP range or security group.
Choose Save rules.
Repeat for each affected security group in each region in use.
CLI:
Remove the public rules for the MongoDB ports. The provided command removes only a rule that covers exactly the range 27017-27018; this version removes the individual port rules:
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}]'
If the rule is a port range or All traffic, remove it using the rule ID from the Test Plan output:
aws ec2 revoke-security-group-ingress --group-id <SECURITY_GROUP_ID> --security-group-rule-ids <SECURITY_GROUP_RULE_ID>
If access is still needed, allow the port from a trusted IP range only (repeat for each port):
aws ec2 authorize-security-group-ingress --group-id <SECURITY_GROUP_ID> --ip-permissions IpProtocol=tcp,FromPort=27017,ToPort=27017,IpRanges='[{CidrIp=<TRUSTED_CIDR>,Description="MongoDB access from trusted network"}]'
Backout Plan
Before making changes, save the current inbound rules:
aws ec2 describe-security-group-rules --filters Name=group-id,Values=<SECURITY_GROUP_ID> > <SECURITY_GROUP_ID>-rules-backup.json
To roll back:
Sign in to the AWS Management Console at https://console.aws.amazon.com.
Go to EC2 > Security Groups and select the security group.
In Inbound rules, choose Edit inbound rules.
Re-add the removed rules using the saved backup (protocol, port range and source).
Choose Save rules.
Note: rolling back re-exposes the MongoDB ports to the Internet. Use it only as a temporary measure while access through trusted sources is arranged.
Reference
https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html
https://docs.aws.amazon.com/vpc/latest/userguide/security-group-rules.html
https://docs.aws.amazon.com/cli/latest/reference/ec2/revoke-security-group-ingress.html
https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-group-rules.html