この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
こんにちは、コンサルティング部の鈴木(純)です。
最近GuardDutyのFindingsを確認したいと思った時に、Amazon Guardduty tester
というgithubのawslabs
にあるテスターを教えていただいたので利用してみました。
ちゃんと公式から提供されているテスターがあると知らなかったのですが、結構手軽に利用できたので紹介します。
Amazon Guardduty Testerとは?
以下の公式ドキュメントで紹介されているGuardDutyのFindingsをいくつか検知させる方法です。
次のスクリプトを使用して、いくつかの一般的な GuardDuty 結果を自動的に生成できます。guardduty-tester.template は AWS を使用して、踏み台ホスト、SSH 経由でアクセスできるテスター CloudFormation インスタンス、および 2 つのターゲット EC2 インスタンスを持つ独立した環境を作成します。Amazon EC2次に、guardduty_tester.sh を実行して、テスター EC2 インスタンス、ターゲット Windows EC2 インスタンス、ターゲット Linux EC2 インスタンス間のインタラクションを開始し、GuardDuty が生成した結果を検出して通知できる 5 種類の一般的な攻撃をシミュレートします。
6つのFindingsをお試しで検知させることができます。個別のFindings詳細についてはドキュメントを参照して下さい。全てEC2系のFindingsになります。
- Recon:EC2/Portscan
- UnauthorizedAccess:EC2/SSHBruteForce
- UnauthorizedAccess:EC2/RDPBruteForce
- CryptoCurrency:EC2/BitcoinTool.B!DNS
- Backdoor:EC2/DNSDataExfiltration
- Backdoor:EC2/C&CActivity.B!DNS
サンプルイベントではなく、実際の検知内容を数パターン出力させたい!ってときに使えそうです。
注意事項
手順の中でCloudFormationのテンプレートを展開するのですが、そのなかで4つEC2インスタンスが起動されます。(m4.large×3、t2.micro)
実行後環境を削除しないとそこそこ料金がかかるので、テストしたあとはEC2の停止するかCloudFormationスタックの削除を忘れないようにしましょう。踏み台サーバー(LinuxBastion)にはAutoScalingが設定されているため、EC2を停止する場合はAutoScalingグループからデタッチしておいた方がいいかもしれません。
やってみる
前提条件
- 実行するリージョンでGuardDutyが既に有効化されていること
- EC2キーペアを用意しておく
Amazon Guardduty Testerを用意する
最初に今回のテストで使用するファイルを以下から取得しましょう。
gitでcloneしてもいいですし、zipでダウンロードするのもOKです。
awslabs/amazon-guardduty-tester
テンプレートを展開する
先ほど取得したファイルの中にguardduty-tester.template
というファイルがあるので、これをGuardDutyが有効化されているリージョンで展開します。
CloudFormationのスタック作成から、テンプレートをアップロードします。
次にスタックのパラメータ入力が求められます。スタック名と展開するAZ、事前に準備したキーペアを入力します。
あとはデフォルトのままスタックを作成しましょう。10分程度で完了します。
作成が完了したら、スタックの情報から出力タブを確認してBastionIp
とRedTeamIp
のIPを控えておいて下さい。
EC2へssh接続する
私はmacユーザーなので、EC2へsshするためにキーペアを~/.ssh
配下にコピーして実行しています。接続情報を~/.ssh/config
に以下の情報を追記してください。(先ほど控えておいたBastionIp
とRedTeamIp
、pemキーをそれぞれ書き換えます。)
Host bastion
HostName {Elastic IP Address of Bastion}
User ec2-user
IdentityFile ~/.ssh/{your-ssh-key.pem}
Host tester
ForwardAgent yes
HostName {Local IP Address of RedTeam Instance}
User ec2-user
IdentityFile ~/.ssh/{your-ssh-key.pem}
ProxyCommand ssh bastion nc %h %p
ServerAliveInterval 240
これで接続準備が整ったので実際に接続してみます。
$ ssh tester
The authenticity of host 'XX.XXX.XX.XX (XX.XXX.XX.XX)' can't be established.
ECDSA key fingerprint is SHA256:m9JeKdL5wDdQqq22ZKNJvMaLyibPT06dPuPqStdOOrM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'XX.XXX.XX.XX' (ECDSA) to the list of known hosts.
###############################################################################
# ____ _ _ #
# | __ ) __ _ ___| |_(_) ___ _ __ #
# | _ \ / _` / __| __| |/ _ \| '_ \ #
# | |_) | (_| \__ \ |_| | (_) | | | | #
# |____/ \__,_|___/\__|_|\___/|_| |_| #
#-----------------------------------------------------------------------------#
# Authorized access only! #
# Disconnect IMMEDIATELY if you are not an authorized user!!! #
# All actions will be monitored and recorded. #
###############################################################################
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for ‘/Users/XXX/.ssh/sample.pem’ are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/XXX/.ssh/sample.pem": bad permissions
ec2-user@XX.XXX.XXX.XX: Permission denied (publickey).
上記のように権限が広すぎると怒られてしまった場合は、以下のコマンドを実行して下さい。
$ sudo chmod 0600 ~/.ssh/sample.pem
それでは再度ssh接続してみます。
$ ssh tester
###############################################################################
# ____ _ _ #
# | __ ) __ _ ___| |_(_) ___ _ __ #
# | _ \ / _` / __| __| |/ _ \| '_ \ #
# | |_) | (_| \__ \ |_| | (_) | | | | #
# |____/ \__,_|___/\__|_|\___/|_| |_| #
#-----------------------------------------------------------------------------#
# Authorized access only! #
# Disconnect IMMEDIATELY if you are not an authorized user!!! #
# All actions will be monitored and recorded. #
###############################################################################
The authenticity of host 'XX.XXX.XX.XX (<no hostip for proxy command>)' can't be established.
ECDSA key fingerprint is SHA256:Ht0u0F0qepgDiEKf1StpWqlcuSgsphomF33wCdgKdGQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'XX.XXX.XX.XX' (ECDSA) to the list of known hosts.
__| __|_ )
_| ( / Amazon Linux 2 AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-2/
無事ログインできました!ファイルを確認してみると、以下のようなファイルがログイン直下にあるのが確認できます。
$ ll
合計 532
drwxr-xr-x 21 ec2-user ec2-user 4096 3月 1 02:59 FreeRDP
drwxr-xr-x 2 ec2-user ec2-user 4096 3月 1 02:57 compromised_keys
drwxr-xr-x 5 ec2-user ec2-user 155 3月 1 02:59 crowbar
drwxr-xr-x 2 ec2-user ec2-user 25 3月 1 02:57 domains
-rwxr-xr-x 1 ec2-user ec2-user 7754 3月 1 02:57 guardduty_tester.sh
drwxr-xr-x 9 ec2-user ec2-user 4096 3月 1 02:57 libssh-0.9.4
-rw-r--r-- 1 ec2-user ec2-user 500776 4月 9 2020 libssh.tar.xz
-rw-r--r-- 1 ec2-user ec2-user 222 3月 1 02:57 localIps.sh
drwxr-xr-x 2 ec2-user ec2-user 31 3月 1 02:57 passwords
drwxr-xr-x 5 ec2-user ec2-user 8192 3月 1 02:59 thc-hydra
-rw-r--r-- 1 ec2-user ec2-user 49 3月 1 02:57 users
GuarddutyのFindingsをテストするためにguardduty_tester.sh
というスクリプトを実行します。こんな感じにずらっと出力されるので、終わるまで少し待ちましょう。
$ bash ./guardduty_tester.sh
***********************************************************************
* Test #1 - Internal port scanning *
* This simulates internal reconaissance by an internal actor or an *
* external actor after an initial compromise. This is considered a *
* low priority finding for GuardDuty because its not a clear indicator*
* of malicious intent on its own. *
***********************************************************************
...
...
...
Expected GuardDuty Findings
Test 1: Internal Port Scanning
Expected Finding: EC2 Instance i-000000000000000 is performing outbound port scans against remote host. XX.XXX.XX.XX
Finding Type: Recon:EC2/Portscan
Test 2: SSH Brute Force with Compromised Keys
Expecting two findings - one for the outbound and one for the inbound detection
Outbound: i-000000000000000 is performing SSH brute force attacks against 172.16.0.25
Inbound: 172.16.0.24 is performing SSH brute force attacks against i-000df38543eb6d684
Finding Type: UnauthorizedAccess:EC2/SSHBruteForce
Test 3: RDP Brute Force with Password List
Expecting two findings - one for the outbound and one for the inbound detection
Outbound: i-000000000000000 is performing RDP brute force attacks against 172.16.0.22
Inbound: 172.16.0.24 is performing RDP brute force attacks against i-02a19b24471d0ceda
Finding Type : UnauthorizedAccess:EC2/RDPBruteForce
Test 4: Cryptocurrency Activity
Expected Finding: EC2 Instance i-000000000000000 is querying a domain name that is associated with bitcoin activity
Finding Type : CryptoCurrency:EC2/BitcoinTool.B!DNS
Test 5: DNS Exfiltration
Expected Finding: EC2 instance i-000000000000000 is attempting to query domain names that resemble exfiltrated data
Finding Type : Backdoor:EC2/DNSDataExfiltration
Test 6: C&C Activity
Expected Finding: EC2 instance i-000000000000000 is querying a domain name associated with a known Command & Control server.
Finding Type : Backdoor:EC2/C&CActivity.B!DNS
これでテスト用のスクリプト実行は完了です。スクリプトの中では5つの攻撃が行われて、実際にGuardDutyがそれを検知するというものになっています。
実行結果
全ての検知結果が表示されるまで20分くらいかかったので、スクリプト実行してから少し待ってみましょう。GuardDutyを確認してみると無事5つのFindingsが通知されていました。
まとめ
GuardDutyのFindingsをテストするための方法を紹介しました。環境を用意必要はありますが、CloudFormationを展開してスクリプトを実行するだけでEC2系5つのFindingsをまとめて確認できるので是非利用してみて下さい。
ご参考
もっと簡単にFindingsを確認したいよ!って人はこちらの記事で紹介されている方法でやってみるのもおすすめです。