Other Articles

EC2 Security Group – Restrict Public Database Access

Secure Configuration Checks > AWS

This check ensures that no EC2 security group allows common database ports to be reached from the entire internet. Exposed databases are a leading cause of data theft and ransom attacks.

Check Details

  • Resource: EC2 Security Group
  • Check: Restrict public database access (NoUnrestrictedDatabasePorts)
  • Risk: Attackers can read, change or delete data, or lock it for ransom

Ports covered

  • MySQL / MariaDB: 3306
  • PostgreSQL: 5432
  • SQL Server: 1433
  • MongoDB: 27017
  • Redis: 6379
  • Elasticsearch: 9200 and 9300
  • Redshift: 5439

Remediation via AWS Console

  1. Log in to the AWS Management Console and open the Amazon EC2 console.

    Amazon EC2 Console
  2. In the left navigation pane, select Security Groups and choose the affected security group.

  3. Open the Inbound rules tab and click Edit inbound rules.

    Security group inbound rules with database ports
  4. Identify database port rules that allow unrestricted access from 0.0.0.0/0 (IPv4) or ::/0 (IPv6).

    Common database ports include:

    • 3306: MySQL / Aurora
    • 5432: PostgreSQL
    • 1433: Microsoft SQL Server
    • 1521: Oracle
    • 5439: Amazon Redshift
  5. For each affected rule, change the Source to the application's security group or a trusted IP address range. If external access is not required, click Delete to remove the rule.

    Edit inbound database security group rules
  6. Click Save rules to apply the changes.

  7. Repeat these steps for every affected security group. Verify that no database port allows unrestricted access from 0.0.0.0/0 or ::/0.

Note: Prefer referencing the application's security group as the source when database access is required between resources in the same VPC. Ensure that other associated security groups do not independently allow unrestricted access to the database ports.

Remediation via AWS CLI

  1. Log in to the AWS Management Console and click the CloudShell icon (>_) in the top-right corner.

    AWS CloudShell
  2. List the open rules that include a database port. The output shows the group ID, name and port range:

    PORTS="3306 5432 1433 27017 6379 9200 9300 5439"
    
    aws ec2 describe-security-groups --output json | \
    jq -r --arg ports "$PORTS" '
      ($ports | split(" ") | map(tonumber)) as $p
      | .SecurityGroups[] as $sg
      | $sg.IpPermissions[] as $r
      | select(
          ([$r.IpRanges[].CidrIp] | index("0.0.0.0/0")) != null
          or ([$r.Ipv6Ranges[].CidrIpv6] | index("::/0")) != null)
      | select(
          $r.IpProtocol == "-1"
          or ($r.IpProtocol == "tcp"
              and any($p[]; . >= $r.FromPort and . <= $r.ToPort)))
      | [$sg.GroupId, $sg.GroupName, ($r.FromPort // "all"), ($r.ToPort // "all")]
      | @tsv'
    

    The command covers one region. Add --region <region> to check another.

  3. Allow the port only from the application security group:

    aws ec2 authorize-security-group-ingress \
    --group-id <security-group-id> \
    --protocol tcp \
    --port <port> \
    --source-group <app-security-group-id>
    

    To allow an IP range instead, replace --source-group with --cidr <trusted-cidr>.

  4. Remove the open rules for the same port, for both IPv4 and IPv6:

    aws ec2 revoke-security-group-ingress \
    --group-id <security-group-id> \
    --protocol tcp \
    --port <port> \
    --cidr 0.0.0.0/0
    
    aws ec2 revoke-security-group-ingress \
    --group-id <security-group-id> \
    --ip-permissions 'IpProtocol=tcp,FromPort=<port>,ToPort=<port>,Ipv6Ranges=[{CidrIpv6=::/0}]'
    

    If a rule allows all traffic or a wide port range, remove it and add only the ports you need.

  5. Verify the remaining inbound rules:

    aws ec2 describe-security-groups \
    --group-ids <security-group-id> \
    --query 'SecurityGroups[0].IpPermissions'
    

Replace <security-group-id> with the group ID, for example sg-0123456789abcdef0. Replace <port> with the database port, for example 3306. Repeat steps 3 and 4 for each port.

Updated on 03 November, 2026