0
0

More than 1 year has passed since last update.

GuardDutyを重要度でフィルターして通知(CDK)

Last updated at Posted at 2023-06-23

概要

AWS Lambdaを使用せずにEventBridge + SNSでGuardDuty脅威検知のメール通知を行うCDKサンプルコード(TypeScript)を紹介します。

フィルター範囲

severityはGuardDutyで定義されている重要度レベルを示しており、数値と重要度は以下の通りです。

  • 高: 7.0〜8.9 の範囲
  • 中: 4.0〜6.9 の範囲
  • 低: 0.1〜3.9 の範囲

本記事の例では重要度中以上を通知対象としています。

ファイル構成

ファイル構成は以下です。

  • bin/
    • main.ts
  • lib/
    • GuardDuty/
      • GuardDutyStack.ts

サンプルコード

bin/main.ts

bin/main.ts
#!/usr/bin/env node

import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { Environment } from 'aws-cdk-lib';
import { GuardDutyStack } from '../lib/GuardDuty/GuardDutyStack';

if (process.env['NODE_ENV'] == null || process.env['NODE_ENV'] === '') {
    throw new Error('NODE_ENV is not set');
}
if (process.env['AWS_PROFILE'] == null || process.env['AWS_PROFILE'] === '') {
    throw new Error('AWS_PROFILE is not set');
}

const app = new cdk.App();

const env: Environment = {
    account: '<AWSアカウント番号>',
    region: '<AWSリージョン>',
};

new GuardDutyStack(app, {
    env: env,
});

lib/GuardDuty/GuardDutyStack.ts

lib/GuardDuty/GuardDutyStack.ts
import * as cdk from 'aws-cdk-lib';
import { Rule } from 'aws-cdk-lib/aws-events';
import { Topic } from 'aws-cdk-lib/aws-sns';
import { EmailSubscription } from 'aws-cdk-lib/aws-sns-subscriptions';
import { SnsTopic } from 'aws-cdk-lib/aws-events-targets';
import { Construct } from 'constructs';

/**
 * 構築プロパティ
 */
export interface GuardDutyProps extends cdk.StackProps {}

export class GuardDutyStack extends cdk.Stack {
    private readonly _props: GuardDutyProps;

    constructor(scope: Construct, props: GuardDutyProps) {
        super(scope);

        // アラートトピック作成
        const snsTopic = new Topic(this, 'SNSTopic', {
            displayName: 'GuardDutyFindingAlert',
            topicName: 'GuardDutyFindingTopic',
        });
        snsTopic.addSubscription(new EmailSubscription('user@example.com'));

        // GuardDuty通知イベント作成
        const guardDutyEvent = new Rule(this, 'GuardDutyEvent', {
            ruleName: 'GuardDutyFindingEvent',
            description: 'GuardDuty Finding Alert',
            eventPattern: {
                source: ['aws.guardduty'],
                detailType: ['GuardDuty Finding'],
                detail: {
                    severity: [
                        { numeric: ['>=', 4] }, // 重要度中以上のみ通知するようにフィルタリング
                    ],
                },
            },
        });

        // ターゲット追加
        guardDutyEvent.addTarget(new SnsTopic(snsTopic));
    }
}

メール通知の発砲

重要度中以上の脅威が検知された際に以下のような内容のメール通知が発砲されます。

{"title":"EC2 instance i-99999999 is behaving in a manner that may indicate it is being used to perform a Denial of Service (DoS) attack using an unusual protocol.","description":"EC2 instance i-99999999 is behaving in a manner that may indicate it is being used to perform a Denial of Service (DoS) attack using an unusual protocol.","type":"Backdoor:EC2/DenialOfService.UnusualProtocol","severity":8}

メールの本文は改行されていないJSONがそのまま出力されるため、必要に応じてEventBridgeの入力トランスフォーマー等で整形して下さい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0