- Newest
- Most votes
- Most comments
Hello.
I think the contents of the following document will be helpful.
https://repost.aws/knowledge-center/potential-account-compromise
Identify the IAM user used for unauthorized access from CloudTrail, and check the API history from CloudTrail to see if that IAM user has created any other AWS resources.
If you can identify the AWS resources created during unauthorized access, you will delete them all.
Preventing further unauthorized access: What immediate steps should I take to secure my account? I have deleted all IAM users and keys and credentials, what else to do? Are there any recommended security best practices I should implement?
Please make sure to change the passwords of IAM users and root users, and set up MFA.
Handling the charges: Will AWS be able to waive the charges for unauthorized activity? What is the process for requesting a refund or credit due to security breaches? This is an urgent matter, and I would appreciate your guidance on next steps as soon as possible.
If you experience unauthorized access, please contact AWS Support by opening a case under "Account and billing".
Inquiries under "Account and billing" can be made free of charge.
Although the full amount is not guaranteed, a portion may be refunded.
You can contact AWS Support from the URL below.
https://console.aws.amazon.com/support
This is a critical security issue and re:Post is not the best place to get a rapid response to your questions. Please raise a support case about this issue. Our support team operates 24x7 and treat security issues with the highest priority.
Most of your questions can be answered by using CloudTrail and the team can help you do that and identify next steps.
I don't have a support plan on my account to contact the support team. How do i reach out to someone for help with my account and finding more details regarding who did these changes.
You don't need a support plan to open a security ticket. And even if you did I'd say that the small amount required to sign up for developer support is worth it in cases like this.
Do you have a example script or any knowledge base on how to create this script and how to run it.
Created with Python.
I think running this for each region will solve the problem.
You can delete a security group by running the code below.
For "default_security_group_id", enter the default security group ID of the VPC.
Default security groups are excluded because they cannot be deleted.
import boto3
ec2 = boto3.client('ec2')
# default security group id
default_security_group_id = 'sg-yyyyyyyyy'
def delete_security_groups():
next_token = None
while True:
if next_token:
response = ec2.describe_security_groups(NextToken=next_token)
else:
response = ec2.describe_security_groups()
security_group_ids = [sg['GroupId'] for sg in response['SecurityGroups']]
security_group_ids_to_delete = [sg_id for sg_id in security_group_ids if sg_id != default_security_group_id]
for sg_id in security_group_ids_to_delete:
try:
ec2.delete_security_group(GroupId=sg_id)
print(f"Deleted Security Group with ID: {sg_id}")
except Exception as e:
print(f"Failed to delete Security Group with ID {sg_id}: {e}")
if 'NextToken' in response:
next_token = response['NextToken']
else:
break
delete_security_groups()
You can delete a key pair by running the code below.
import boto3
ec2 = boto3.client('ec2')
response = ec2.describe_key_pairs()
key_pair_names = [kp['KeyName'] for kp in response['KeyPairs']]
for key_pair_name in key_pair_names:
try:
ec2.delete_key_pair(KeyName=key_pair_name)
print(f"Deleted Key Pair: {key_pair_name}")
except Exception as e:
print(f"Failed to delete Key Pair {key_pair_name}: {e}")
How do I run this, create a EC2 instance and do it? Does it require me to create access token etc..? I cannot use cloudshell which requires me to clear all the unauthorised resources in the account like security group and key pair. I got a reply from support asking me to change root password and to delete manually or run script on cloudshell. in order to get the account reinstated.
Current there are around 1500-2000 security group and key pair in each region.
how do i go about??
I have currently thousands of key pairs and security groups on each region. I also have two instances each on each region which i terminated. I don't know how will i be able to delete all the key pairs and security groups which is a tides process for me. Is there a automated way to get this resolved?
Additionally, i want to find who was involved in this security breach, which ip, region etc.
answered a year ago
Relevant content
- AWS OFFICIALUpdated 6 months ago

I have currently thousands of key pairs and security groups on each region. I also have two instances each on each region which i terminated. I don't know how will i be able to delete all the key pairs and security groups which is a tides process for me. Is there a automated way to get this resolved?
Additionally, i want to find who was involved in this security breach, which ip, region etc..
I think it would be a good idea to create a program that can be deleted using a shell script with the AWS CLI or an AWS SDK. If it's a script, you should be able to delete it by making adjustments to leave the resources you don't want to delete, and then looping with a for statement.
Although it is difficult to determine the region, you can check the source IP address from CloudTrail. When creating a key pair and creating a security group, the events "CreateKeyPair" and "CreateSecurityGroup" are recorded in CloudTrail, so you can identify the IP address from there.
'I think it would be a good idea to create a program that can be deleted using a shell script with the AWS CLI or an AWS SDK. If it's a script, you should be able to delete it by making adjustments to leave the resources you don't want to delete, and then looping with a for statement.'
Do you have a example script or any knowledge base on how to create this script and how to run it. Having these many key pairs and security groups, will it create issues in future if I don't delete them?
There is no charge for key pairs and security groups, so there is no problem in leaving them, but I recommend that you delete them as much as possible, as wasted resources may cause operational errors.