VPC Exposure Review: Routes, Security Groups and NACLs
"Is the database reachable from the Internet?" In a cloud account the answer is not in one place. It needs a route to an Internet gateway, a public address, a network ACL that lets the packet and its reply through, and a security group that allows the port from the source, all at the same time. Attackers find exposed services by scanning the cloud providers' public address ranges continuously (anything exposed is found within hours); auditors should find them first, from the configuration, without sending a packet. This lesson builds that review. The evaluation rules of security groups and NACLs themselves were covered in Cloud Security Groups and Security Groups vs NACLs; here they become one link in a chain.
The chain for inbound exposure
A service on an instance is reachable from the Internet on port p only if every link holds:
- Address: the network interface has a public IPv4 address or Elastic IP (or a global IPv6 address), or it sits behind an Internet-facing load balancer that forwards to it.
- Route: the subnet's route table sends
0.0.0.0/0(or::/0) to an Internet gateway. A route to a NAT gateway only allows outbound connections. - NACL: the subnet's NACL allows inbound p from the source and outbound ephemeral ports for the reply (stateless).
- Security group: some group on the interface allows p from the source (stateful, any matching rule).
- Something listening on p. Configuration can't tell you this one; it decides whether an exposure is a vulnerability today or a trap for tomorrow.
The same logic applies to Azure (public IP + NSG on subnet and NIC) and GCP (external IP + VPC firewall rules and hierarchical firewall policies), with different names for the links.
An exposure review in Python
The lab VPC from the introduction, written as an inventory like the one describe-* calls or a CSPM tool would return:
import ipaddress
subnets = {
"public": {"cidr": "10.10.0.0/26", "routes": {"0.0.0.0/0": "igw-01"}, "nacl": "nacl-default"},
"app": {"cidr": "10.10.0.64/26", "routes": {"0.0.0.0/0": "nat-01"}, "nacl": "nacl-default"},
"data": {"cidr": "10.10.0.128/26", "routes": {"0.0.0.0/0": "igw-01"}, "nacl": "nacl-data"}, # mis-associated route table
}
nacls = { # inbound rules (number, port range, source, action); outbound assumed to allow ephemeral replies
"nacl-default": [(100, (0, 65535), "0.0.0.0/0", "allow")],
"nacl-data": [(100, (5432, 5432), "10.10.0.64/26", "allow"), (200, (0, 65535), "0.0.0.0/0", "deny")],
}
sgs = {
"sg-alb": [(443, "0.0.0.0/0")],
"sg-bastion": [(22, "0.0.0.0/0")],
"sg-app": [(8080, "sg-alb"), (22, "sg-bastion")],
"sg-db": [(5432, "sg-app"), (5432, "0.0.0.0/0")], # "temporary" rule from a migration
"sg-metrics": [(9100, "10.0.0.0/8"), (9100, "::/0")],
}
instances = [ # name, subnet, private ip, public ip, security groups
("alb-web", "public", "10.10.0.5", "203.0.113.20", ["sg-alb"]),
("bastion", "public", "10.10.0.10", "203.0.113.21", ["sg-bastion"]),
("app-1", "app", "10.10.0.70", None, ["sg-app", "sg-metrics"]),
("db-1", "data", "10.10.0.140", "203.0.113.30", ["sg-db"]),
("old-test", "public", "10.10.0.40", "203.0.113.40", ["sg-metrics"]),
]
INTERNET = "198.51.100.77" # any outside address
def in_cidr(ip, cidr):
a, n = ipaddress.ip_address(ip), ipaddress.ip_network(cidr)
return a.version == n.version and a in n
def nacl_allows(nacl, port, src):
for num, (lo, hi), cidr, action in sorted(nacls[nacl]):
if lo <= port <= hi and in_cidr(src, cidr):
return action == "allow"
return False
def sg_allows(groups, port, src, v6=False):
for g in groups:
for p, source in sgs[g]:
if p == port and not source.startswith("sg-"):
if (source == "::/0") == v6 and (v6 or in_cidr(src, source)):
return True
return False
print(f"{'instance':9} {'port':>5} " + " ".join(f"{k:5}" for k in ["addr", "route", "nacl", "sg"]) + " verdict")
for name, sn, priv, pub, groups in instances:
ports = sorted({p for g in groups for p, _ in sgs[g]})
for port in ports:
links = {
"address": pub is not None,
"route": subnets[sn]["routes"].get("0.0.0.0/0", "").startswith("igw-"),
"nacl": nacl_allows(subnets[sn]["nacl"], port, INTERNET),
"sg": sg_allows(groups, port, INTERNET) or sg_allows(groups, port, INTERNET, v6=True),
}
if all(links.values()):
verdict = "EXPOSED"
elif links["sg"] and not all(links.values()):
verdict = "latent: SG open, blocked by " + ", ".join(k for k, v in links.items() if not v)
else:
verdict = "not exposed"
marks = " ".join(f"{'yes' if links[k] else 'no':5}" for k in links)
print(f"{name:9} {port:>5} {marks} {verdict}")instance port addr route nacl sg verdict
alb-web 443 yes yes yes yes EXPOSED
bastion 22 yes yes yes yes EXPOSED
app-1 22 no no yes no not exposed
app-1 8080 no no yes no not exposed
app-1 9100 no no yes yes latent: SG open, blocked by address, route
db-1 5432 yes yes no yes latent: SG open, blocked by nacl
old-test 9100 yes yes yes yes EXPOSED
The review produces three kinds of result, and each needs a different response:
- EXPOSED, as intended: the load balancer on 443. Document it as the account's front door.
- EXPOSED, not intended: the bastion's SSH to the world (an attack-surface finding: restrict it or replace the bastion with a session manager), and
old-test, a forgotten instance with a public IP whose node-exporter port is open to::/0. The IPv6 rule is the kind of thing a review that filters for0.0.0.0/0misses. (The model simplifies here: it checks the IPv4 address and route and counts a::/0rule as open. In a real VPC that rule exposes the port once the instance has an IPv6 address and the subnet routes::/0to the Internet gateway, which in a dual-stack subnet is one setting away.) - Latent: the database's security group allows 5432 from the whole Internet and the data subnet even routes to the Internet gateway; only the NACL stops it today. One NACL change (or moving the instance) and it is exposed. That is a HIGH finding even though nothing is reachable now, because the control holding it back is a single layer nobody intended to depend on.
How exposure is detected in real accounts
- From configuration: the review above, done by tools: AWS Network Access Analyzer and Reachability Analyzer, Azure Network Watcher's effective security rules, GCP Network Intelligence Center's connectivity tests, CSPM products (Posture Management). Trivy and other IaC scanners catch the security-group half before deployment (IaC Scanning).
- From traffic: VPC flow logs show accepted connections from outside to ports that should be internal; a spike of
REJECTrecords shows scanning. - From the outside, in scope only: an external attack-surface inventory of your own public IPs and DNS names, compared with the intended list (Asset Inventory).
How it is fixed
- Remove public addresses from everything that isn't a front door: application and database instances belong in subnets whose routes go to a NAT gateway or nowhere. Turn off "auto-assign public IP" on private subnets.
- Reference security groups, not CIDRs, for internal traffic (
sg-dballowssg-app), and delete "temporary"0.0.0.0/0rules; for IPv6, review::/0with the same care. - Replace SSH bastions with an identity-aware session service (AWS Systems Manager Session Manager, Azure Bastion, GCP IAP TCP forwarding), so no port 22 is exposed at all.
- Guardrails: an organisation-wide rule that blocks or removes security-group rules from
0.0.0.0/0or::/0on admin and database ports (Azure Policy and GCP organisation policies can deny them at creation; in AWS, AWS Firewall Manager or AWS Config rules with remediation, since SCP condition keys don't expose a rule's CIDR), and an automated check that fails the pipeline.
Pitfalls
- Reviewing one layer: a closed NACL hides an open security group until someone "simplifies" the NACL.
- Forgetting IPv6 (
::/0), load balancers forwarding to internal ports, and other front doors: API gateways, CDN origins, public database endpoints of managed services (publicly_accessible = trueon RDS). - Ignoring outbound: an instance that can't be reached but can connect anywhere is still an exfiltration path (the next lesson).
Practice
1. Change the data subnet's route to nat-01. What happens to db-1's verdict, and is the finding closed?
The verdict becomes "latent: SG open, blocked by route, nacl". Better, but not closed: the security group still allows the world, and the instance still has a public IP. Remove both.
2. Replace nacl-data's rule 200 with an allow-all rule numbered 50. What does the review say about db-1?
Rule 50 matches first, so the NACL allows 5432 from anywhere and db-1 becomes EXPOSED on 5432. This is exactly the one-change regression a latent finding warns about.
3. app-1's port 9100 is only "latent" (no address, no route). Why report it at all?
Because the rule that makes it latent lives in sg-metrics, a shared group: old-test shows what happens when the same group is attached to an instance with a public address. Fix the group (drop ::/0, keep the internal CIDR), not just the instance.
Test yourself
Which links must all hold for an instance port to be reachable from the Internet?
- Only the security group
- Public address, route to an Internet gateway, NACL allow, security-group allow (and a listener)
- Only a public IP
- A NAT gateway and a security group
Why is db-1 a HIGH finding although the review says it isn't reachable today?
- Databases are always high
- Its security group allows the world and its subnet routes to the IGW; only one NACL rule holds it back
- It has no encryption
- It is in the wrong region
What does a route to a NAT gateway allow?
- Inbound connections from the Internet
- Outbound connections initiated from the subnet only
- Nothing at all
- Inbound on port 443 only
Which rule did a 0.0.0.0/0-only review miss in the program?
- sg-alb 443
- sg-metrics 9100 from ::/0
- sg-app 8080
- sg-db 5432 from sg-app