{
"Version": "2012-10-17",
"Statement": [
{
"Action":[
"ssm:PutParameter",
"ec2:*",
"lambda:InvokeFunction",
"cloudwatch:PutMetricData"
],
"Resource": "*",
"Effect":"Allow"
},
{
"Action":[
"logs:*"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect":"Allow"
},
{
"Action":[
"sns:Publish"
],
"Resource": "arn:aws:sns:*:*:guardduty",
"Effect":"Allow"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ssm:GetParameter",
"ssm:GetParameters",
"ssm:DescribeParameter"
],
"Resource": "arn:aws:ssm:*:*:*",
"Effect": "Allow"
}
]
}
Guardduty에서 탐지된 로그를 분석하여 보안 조치할 수 있도록 설정
python 코드
from __future__ import print_function
from botocore.exceptions import ClientError
import boto3
import json
import os
def lambda_handler(event, context):
# Log out event
print("log -- Event: %s " % json.dumps(event))
# Create generic function response
response = "Error auto-remediating the finding."
try:
ec2 = boto3.client('ec2')
# Set Variables
instanceID = event["detail"]["resource"]["instanceDetails"]["instanceId"]
security_group_id = os.environ['FORENSIC_SG']
if instanceID == os.environ['INSTANCE_ID']:
print("log -- Security Group Created %s : " % (security_group_id))
# Isolate Instance
ec2 = boto3.resource('ec2')
instance = ec2.Instance(instanceID)
print("log -- %s, %s" % (instance.id, instance.instance_type))
instance.modify_attribute(Groups=[security_group_id])
# Send Response Email
response = "GuardDuty Remediation | ID:%s: GuardDuty discovered an EC2 instance (Instance ID: %s) that is communicating outbound with an IP Address on a threat list that you uploaded. All security groups have been removed and it has been isolated. Please follow up with any additional remediation actions." % (event['detail']['id'], event['detail']['resource']['instanceDetails']['instanceId'])
sns = boto3.client('sns')
sns.publish(
TopicArn=os.environ['TOPIC_ARN'],
Message=response
)
print("log -- Response: %s " % response)
else:
print("log -- Instance unrelated to GuardDuty-Hands-On environment.")
except ClientError as e:
print(e)
print("log -- Response: %s " % response)
return response
구성 > 시간 30초로 변경
환경변수
- python코드에서 사용하는 변수를 미리 세팅
- forensic_sg = security group 신규생성 했던걸로.
- instance_id= victim
- topic_arn = sns arn
guardduty의 로그가 생성되었을때 lambda를 실행할수있도록 이벤트 규칙을 생성.
규칙 생성
유형 참고
입력변환기
#****UnauthorizedAccess:EC2/MaliciousIPCaller.Custom****
#입력경로
{"gdid":"$.detail.id","instanceid":"$.detail.resource.instanceDetails.instanceId"}
#템플릿
"GuardDuty Finding | ID:<gdid>: The EC2 instance <instanceid> may be compromised and should be investigated. Go to https://console.aws.amazon.com/guardduty"
#UnauthorizedAccess:IAMUser/MaliciousIPCaller.Custom, Discovery:S3/MaliciousIPCaller.Custom
#입력경로
{"gdid":"$.detail.id","userName":"$.detail.resource.accessKeyDetails.userName"}
#템플릿
"GuardDuty Finding | ID:<gdid>: An AWS API operation was invoked (userName: <userName>) from an IP address that is included on your threat list and should be investigated. Go to https://console.aws.amazon.com/guardduty"
step1. 취약한 ec2에서 공격자의 서버로 ping을 날린다
step1. 공격자 서버에 sh 생성
step2. iam 계정 생성시 만든 액세스키 를 “aws configure” 명령을 통해 설정
step3. 미리 지정된 스크립트를 실행.
create user 명령에 대한 탐지
sns event 확인