Skip to content

Instantly share code, notes, and snippets.

@reza
Created January 23, 2018 12:49
Show Gist options
  • Save reza/4d9b8f9b18ba345e0b8156376636f8e3 to your computer and use it in GitHub Desktop.
Save reza/4d9b8f9b18ba345e0b8156376636f8e3 to your computer and use it in GitHub Desktop.
I have used a lambda function to automatically deactivate any access key associated with a high severity GuardDuty alert.
#!/usr/bin/env python
#developed for blog post at http://www.cloudten.com.au/aws-guardduty-intelligent-threat-detection/
from __future__ import print_function
import boto3
import json
def lambda_handler(event, context):
print("Received event: " + json.dumps(event))
try:
if ( event['service']['serviceName'] == 'guardduty'
and event['resource']['resourceType'] == "AccessKey"
and event['resource']['resourceRole'] == "TARGET"
and event['severity'] >= 5
):
iam= boto3.resource('iam')
access_details = event['detail']['resource']['accessKeyDetails']
access_key = iam.AccessKey(access_details['userName'],access_details['accessKeyId'])
response_status = access_key.deactivate()
status_code = response_status['ResponseMetadata']['HTTPStatusCode']
if status_code == 200:
print("Key Disabled Successfully")
else:
print("Key deactivation failed")
return event['title']
except:
pass
return 'AccessKey not found'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment