An EC2 instance shows signs of compromise: unexpected outbound connections, unusual processes, modified system files, or GuardDuty alerts. The instance may be used as a pivot point for lateral movement within your VPC. Isolate first, investigate second.
GuardDuty detects command-and-control communication, port scanning, DNS exfiltration, and other indicators of compromise.
Look for unexpected outbound connections, especially to known malicious IPs or unusual ports.
aws ec2 describe-flow-logs \ --filter "Name=resource-id,Values=<vpc-id>"
# Query flow logs in CloudWatch Logs Insights # fields @timestamp, srcAddr, dstAddr, dstPort, action # | filter srcAddr = "<instance-private-ip>" # | filter action = "ACCEPT" # | sort @timestamp desc # | limit 100
Run commands on the instance to check for suspicious processes, network connections, and recently modified files.
# List unusual network connections aws ssm send-command \ --instance-ids <instance-id> \ --document-name "AWS-RunShellScript" \ --parameters 'commands=["netstat -tlnp","ss -tlnp","cat /etc/crontab","ls -la /tmp"]'
Replace all security groups with a quarantine SG that has no inbound or outbound rules. Create it first if needed (see Crypto Mining playbook for creation steps including removing the default outbound rule).
aws ec2 modify-instance-attribute \ --instance-id <instance-id> \ --groups <isolation-sg-id>
Do NOT stop or terminate the instance yet. Live memory and running processes are crucial for forensics. Ensure your isolation SG has no outbound rules (new SGs have a default allow-all outbound rule that must be removed).
Snapshot all EBS volumes attached to the instance for offline analysis.
# List all volumes attached to the instance aws ec2 describe-volumes \ --filters "Name=attachment.instance-id,Values=<instance-id>" \ --query 'Volumes[].VolumeId' --output text
# Create snapshots
aws ec2 create-snapshot \
--volume-id <vol-id> \
--description "IR-forensic-snapshot-$(date +%Y%m%d)" \
--tag-specifications 'ResourceType=snapshot,Tags=[{Key=IncidentResponse,Value=true}]'Record the instance metadata, console output, and screenshot for evidence.
aws ec2 get-console-output --instance-id <instance-id>
aws ec2 get-console-screenshot --instance-id <instance-id>
After forensic evidence is preserved, terminate the instance. Do NOT try to clean it - rebuild from a known-good AMI.
aws ec2 terminate-instances --instance-ids <instance-id>
Never attempt to "clean" a compromised instance. You cannot trust anything on it. Always rebuild from scratch.
If the instance had an IAM role, the temporary credentials may have been exfiltrated. Revoke them.
aws iam put-role-policy \
--role-name <instance-role> \
--policy-name RevokeOldSessions \
--policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Deny","Action":"*","Resource":"*","Condition":{"DateLessThan":{"aws:TokenIssueTime":"<current-timestamp>"}}}]}'The attacker may have moved laterally. Check other instances in the VPC for similar indicators.
aws ec2 describe-instances \
--filters "Name=vpc-id,Values=<vpc-id>" "Name=instance-state-name,Values=running" \
--query 'Reservations[].Instances[].{ID:InstanceId,IP:PrivateIpAddress,Role:IamInstanceProfile.Arn}' \
--output tableDeploy a new instance from a known-good AMI with updated security patches.
Use a golden AMI from your pipeline. Never reuse AMIs that were running during the incident.
If application data was on the instance, restore from backups taken before the compromise.
Monitor the new instance for 24-48 hours and run vulnerability scans before returning to production.
Eliminate exposed SSH ports. Use SSM Session Manager for shell access with CloudTrail logging.
Require IMDSv2 (hop limit = 1) to prevent SSRF-based credential theft from the metadata service.
aws ec2 modify-instance-metadata-options \ --instance-id <instance-id> \ --http-tokens required \ --http-put-response-hop-limit 1
Enable Inspector to continuously scan EC2 instances for software vulnerabilities and network exposure.
When an incident strikes, every minute counts. We help AWS teams prepare, detect, and respond to security incidents with proven expertise.