Lesson 2 of 6 Incident Response and Forensics

Containing, Eradicating and Recovering from Incidents

This lesson explains how containment stops damage spreading, eradication removes the attacker's foothold and recovery restores normal service, with steps that differ by resource type.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how a compromised EC2 instance is contained without destroying evidence.
  • Describe containment for compromised credentials, data and containers.
  • Explain eradication and recovery from trusted sources.
  • Revoke every active session of a compromised role.

Containing an EC2 instance

A compromised EC2 instance must be contained without destroying evidence. Containment consists of the following steps.

  1. Isolate with a security group

    Replace its security groups with an isolation security group with no rules.

  2. Cut existing connections

    Add NACL deny rules for its IP: security groups are stateful, so existing tracked connections continue.

  3. Take it out of service

    Detach it from Auto Scaling, so it is not replaced, and deregister it from load balancers.

  4. Preserve evidence

    Enable termination protection, snapshot EBS volumes and tag it with the incident ID.

  5. Capture memory first

    Do not terminate or reboot until evidence, including memory, is captured.

Put in order

Containing a compromised instance

Exam skills 2.2.4

Arrange the containment steps for a compromised EC2 instance.

Log in to try this activity Interactive activities are free with an account.

Containing credentials, data and containers

Containment depends on what was compromised. For leaked access keys, deactivate them and attach a deny-all inline policy to the user, so nothing more can be done with them. For a role, deleting keys is not possible, because its credentials are temporary and remain valid until they expire. Revoking active sessions attaches a policy that denies every action to sessions issued before the current time, using the aws:TokenIssueTime condition, so stolen credentials stop working while new sessions are unaffected.

Containment must be followed up. Reset passwords and MFA devices, rotate secrets the principal could read, and search CloudTrail for persistence the attacker may have created: new users, access keys, roles, functions or changed trust policies. For exposed S3 data, enable Block Public Access and correct bucket policies and ACLs. For a compromised container, isolate the pod with network policies, cordon the node so nothing new is scheduled on it, and capture the container for forensics. The following table summarises the actions.

ResourceActions
Access keysDeactivate; attach a deny-all inline policy
RolesRevoke active sessions (a policy denying sessions issued before a time, via aws:TokenIssueTime)
Follow-upReset passwords and MFA; rotate secrets the principal could read; check CloudTrail for persistence (new users, keys, roles, functions, changed trust policies)
S3 dataEnable Block Public Access; fix bucket policies and ACLs
ContainersIsolate the pod with network policies, cordon the node, capture the container

The following diagram shows which sessions a deny policy based on token issue time stops when a role's credentials are compromised.

Revoking a compromised role's sessions without deleting it. An inline policy denies every action for sessions whose aws:TokenIssueTime is before now: stolen credentials stop working, while workloads obtain fresh credentials automatically. It does not fix the cause, so isolation and IMDSv2 follow.
The scenario is illustrative.
Match the pairs

Containment by resource type

Exam skills 2.2.4

Match each compromised resource with its containment action.

Log in to try this activity Interactive activities are free with an account.

Eradication and recovery

After containment, the threat must be eradicated and normal operations restored. The following list describes both activities.

  • Eradicate by rebuilding from trusted AMIs and IaC rather than cleaning, and remove the root cause: patch, close the port, tighten the policy, remove the leaked key.
  • Recover from AWS Backup recovery points, EBS snapshots or S3 versions (immutable backups matter most against ransomware), return services gradually and watch for reinfection.
True or false

Eradicating and recovering

Exam skills 2.2.4

Check how threats are removed and services restored.

Log in to try this activity Interactive activities are free with an account.
Worked example

Revoking every session of a compromised role

GuardDuty shows a role's credentials being used from an unknown network. Deleting the role would destroy evidence and break the application. Instead, an inline policy denies everything for sessions issued before now; legitimate workloads obtain fresh credentials automatically, while stolen ones stop working:

NOW=$(date -u +%Y-%m-%dT%H:%M:%SZ)
aws iam put-role-policy --role-name web-app-role --policy-name AWSRevokeOlderSessions \
  --policy-document "{
    \"Version\": \"2012-10-17\",
    \"Statement\": [{
      \"Effect\": \"Deny\", \"Action\": \"*\", \"Resource\": \"*\",
      \"Condition\": { \"DateLessThan\": { \"aws:TokenIssueTime\": \"$NOW\" } }
    }]
  }"

This is exactly what the console's Revoke active sessions button does. It does not fix the cause: if the attacker can still reach the instance metadata service through a vulnerable application, they can obtain new credentials, so containment continues with isolating the instance and enforcing IMDSv2.

Check yourself

Why are security group changes not enough to cut existing connections to an instance?

Show answer

Security groups are stateful, so tracked connections continue. A network ACL deny rule cuts them.

Exam Tips

Remember for the exam

  • To cut existing connections to an instance, add a network ACL deny, because security group changes do not break tracked flows.
  • To keep a compromised instance for forensics, isolate it, snapshot it and enable termination protection; do not terminate it.
  • To invalidate credentials already issued from a role, revoke older sessions.
  • For a leaked access key, deactivate it, review its CloudTrail activity, and then rotate it.
  • Rebuild a compromised host from a trusted image rather than cleaning it.