Amazon S3 File GatewayでもGuardDuty Malware Protection for S3は動作するのか確認してみた

Amazon S3 File GatewayでもGuardDuty Malware Protection for S3は動作するのか確認してみた

S3 File Gateway経由で作成されたオブジェクトもGuardDuty Malware Protection for S3で正常にスキャン、判定される
Clock Icon2025.05.28

S3 File Gateway経由で作成されたオブジェクトに対しても検出できるか気になる

こんにちは、のんピ(@non____97)です。

皆さんはS3 File Gateway経由で作成されたオブジェクトに対してもGuardDuty Malware Protection for S3で検出できるか気になったことはありますか? 私はあります。

GuardDuty Malware Protection for S3を使うことによってS3バケットにPUTされたオブジェクトをスキャンして、マルウェアが含まれている場合は検出することができます。バケットポリシーを用いることでマルウェアと判定されたオブジェクトにアクセスさせないようにすることも可能です。

https://dev.classmethod.jp/articles/release-guardduty-s3-malware-protection/

S3 File Gatewayのファイル共有として指定したS3バケットでも同様に検出できるのでしょうか?

もし、検出できるのであれば「NFSやSMB経由で操作したいアーカイブデータに対してもマルウェアスキャンを実施したい」といった要件が出てきた際にピッタリです。

実際に試してみます。

いきなりまとめ

  • Amazon S3 File GatewayでもGuardDuty Malware Protection for S3は動作する
  • ただし、GuardDuty Malware Protection for S3で検出されたオブジェクトをS3 File Gatewayのファイル共有から参照させないようにするためには、「THREATS_FOUNDのタグが付与されたオブジェクトを検出したら削除する」といったEventBridge RuleとLambda関数 or Step Functionsを用意してあげるようなケアが必要

やってみた

検証環境

検証環境は以下のとおりです。

Amazon S3 File GatewayでもGuardDuty Malware Protection for S3は動作するのか確認してみた検証環境構成図.png

S3 File GatewayのインスタンスはEC2インスタンスとして動作させます。

S3 File Gatewayの作成

まず、S3 File Gatewayの作成です。

S3 File Gateway用のEC2インスタンスは作成して、アクティベーション済みです。インスタンスタイプはt3.largeにしました。

キャッシュボリュームやCloudWatch Logs、CloudWatchアラームの設定を行います。

1.ゲートウェイの設定.png

設定が完了すると、S3 File Gatewayのステータスが実行中になります。

2_作成完了.png

ファイル共有も作成しましょう。

Storage Gatewayのコンソールから新規にS3バケットを作成します。

3.新しい S3 バケットを作成.png

作成したS3バケットをNFSのファイル共有のバケットとして指定します。

4.ファイル共有の作成.png

ファイル共有の作成が完了したことを確認します。

5_ファイル共有が作成完了したことを確認.png

テキストファイルの作成

NFSクライアントからS3 File GatewayのNFSファイル共有をマウントして、テキストファイルを作成します。

まず、マウントです。

$ sudo mkdir -p /mnt/sgw
$ sudo mount -t nfs -o nolock,hard 10.0.0.243:/sgw-2025-05-22-l2o8h /mnt/sgw

$ df -hT -t nfs4
Filesystem                       Type  Size  Used Avail Use% Mounted on
10.0.0.243:/sgw-2025-05-22-l2o8h nfs4  8.0E     0  8.0E   0% /mnt/sgw

$ mount | grep nfs4
10.0.0.243:/sgw-2025-05-22-l2o8h on /mnt/sgw type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.0.0.57,local_lock=none,addr=10.0.0.243)
$
$ ls -ld /mnt/sgw/
drwxrwxrwx. 1 nobody nobody 0 May 22 04:37 /mnt/sgw/

正常にマウントできました。

ファイルを作成します。

$ echo test | sudo tee -a /mnt/sgw/test.txt > /dev/null

$ ls -l /mnt/sgw/
total 1
-rw-r--r--. 1 nobody nobody 5 May 22 04:40 test.txt

$ cat /mnt/sgw/test.txt
test

正常に作成されましたね。

S3バケットを除くと作成したテキストファイルに対応したオブジェクトが追加されていました。

6.ファイル作成後のS3バケット.png

GuardDuty Malware Protection for S3の設定

次にGuardDuty Malware Protection for S3の設定を行います。

GuardDutyのコンソールのGuardDuty Malware Protection for S3から有効にするをクリックします。

7.S3 の Malware Protection.png

S3 File GatewayのS3バケットを選択して有効にします。また、後々タグによってマルウェアと判定されたオブジェクトにはアクセスを禁止させたいので、タグ付けを有効にしています。

8.S3 の Malware Protection を有効化.png

設定完了後の状態は以下のとおりです。

9.S3 の Malware Protection の詳細.png

EICARファイルの作成

NFSファイル共有上にEICARファイルを作成して、マルウェアとして検出されるか確認します。

$ sudo curl https://secure.eicar.org/eicar.com -s -o /mnt/sgw/eicar.com

$ ls -l /mnt/sgw/
total 1
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody  5 May 22 04:40 test.txt

$ cat /mnt/sgw/eicar.com
X5O!P%@AP[4\..(中略)..H*

GuardDutyのコンソールを確認すると、作成したEICARファイルが検出されたことを確認しました。

10.検出されたことを確認.png

EICARファイルのオブジェクトを確認すると、"GuardDutyMalwareScanStatus": "THREATS_FOUND"というタグが付与されていることが分かります。

11.タグが付与されたことを確認.png

今度は通常のテキストファイルの確認します。

現在のtest.txtオブジェクトにはタグは付与されていません。

12.test.txtのタグとメタデータの確認.png

NFSファイル共有からこちらのファイルに追記を行います。

$ echo test | sudo tee -a /mnt/sgw/test.txt > /dev/null
$ cat /mnt/sgw/test.txt
test
test

追記後のオブジェクトを確認すると、"GuardDutyMalwareScanStatus": "NO_THREATS_FOUND"のタグが付与されていました。

13.追記後のtest.txt.png

マルウェアとして検出されなかったオブジェクトの読み取りの禁止

マルウェアとして検出されなかったオブジェクトの読み取りの禁止をするようにバケットポリシーを変更し、その後の挙動を確認します。

設定するバケットポリシーとオブジェクトスキャンステータスは以下AWS公式ドキュメントにまとまっています。

https://docs.aws.amazon.com/ja_jp/guardduty/latest/ug/tag-based-access-s3-malware-protection.html

https://docs.aws.amazon.com/ja_jp/guardduty/latest/ug/monitoring-malware-protection-s3-scans-gdu.html#s3-object-scan-result-value-malware-protection

現在のS3バケットのバケットポリシーは以下のとおりです。

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::sgw-2025-05-22-l2o8h/*",
                "arn:aws:s3:::sgw-2025-05-22-l2o8h"
            ],
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                }
            }
        }
    ]
}

こちらに、以下のようにマルウェアとして検出されなかったオブジェクトの読み取りの禁止をするようステートメントを追加しました。

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::sgw-2025-05-22-l2o8h/*",
                "arn:aws:s3:::sgw-2025-05-22-l2o8h"
            ],
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                }
            }
        },
        {
            "Sid": "NoReadUnlessClean",
            "Effect": "Deny",
            "NotPrincipal": {
                "AWS": [
                    "arn:aws:sts::984900217833:assumed-role/GuardDutyS3MalwareScanRole-05408a31-6a45-4d06-9054-25cff9835c2a/GuardDutyMalwareProtection",
                    "arn:aws:iam::984900217833:role/service-role/GuardDutyS3MalwareScanRole-05408a31-6a45-4d06-9054-25cff9835c2a"
                ]
            },
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": "arn:aws:s3:::sgw-2025-05-22-l2o8h/*",
            "Condition": {
                "StringNotEquals": {
                    "s3:ExistingObjectTag/GuardDutyMalwareScanStatus": "NO_THREATS_FOUND"
                }
            }
        },
        {
            "Sid": "OnlyGuardDutyCanTagScanStatus",
            "Effect": "Deny",
            "NotPrincipal": {
                "AWS": [
                    "arn:aws:sts::984900217833:assumed-role/GuardDutyS3MalwareScanRole-05408a31-6a45-4d06-9054-25cff9835c2a/GuardDutyMalwareProtection",
                    "arn:aws:iam::984900217833:role/service-role/GuardDutyS3MalwareScanRole-05408a31-6a45-4d06-9054-25cff9835c2a"
                ]
            },
            "Action": "s3:PutObjectTagging",
            "Resource": "arn:aws:s3:::sgw-2025-05-22-l2o8h/*",
            "Condition": {
                "ForAnyValue:StringEquals": {
                    "s3:RequestObjectTagKeys": "GuardDutyMalwareScanStatus"
                }
            }
        }
    ]
}

バケットポリシー更新後にS3のコンソールからEICARファイルのオブジェクトを確認しようとしてみます。

14.オブジェクトの情報を確認できない.png

はい、チェックサムや各種メタデータが表示されなくなりました。

ダウンロードをしようとしてもAccessDeniedとエラーになります。

15.ダウンロードしようとしてもエラーになる.png

NFSクライアントからマルウェア判定されたオブジェクトのファイルを開けられるか

NFSクライアントからマルウェア判定されたオブジェクトのファイルを開いてみます。

新規にEICARファイルを作成します。

$ sudo curl https://secure.eicar.org/eicar.com -s -o /mnt/sgw/eicar.com_2

$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

$ date
Thu May 22 05:23:58 UTC 2025
$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

$ date
Thu May 22 05:24:06 UTC 2025
$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

$ date
Thu May 22 05:24:09 UTC 2025
$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

$ date
Thu May 22 05:24:40 UTC 2025
$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

$ date
Thu May 22 05:25:01 UTC 2025
$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

$ cat /mnt/sgw/eicar.com_2
X5O!P%@AP[4\..(中略)..H*

$ date
Thu May 22 05:28:42 UTC 2025
$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

作成完了しました。特にNFSクライアントから表示できなくなるということは発生していないですね。

スキャン自体は行われています。

16.スキャンが行われていること1.png

検出もされていますね。

17.検出されたことを確認.png

S3 File Gatewayのキャッシュを更新してみましょう。

現在はファイルキャッシュのTTLは設定していません。

19.ファイルキャッシュのTTLは設定なし.png

キャッシュの更新を行います。

18.sgw-2025-05-22-l2o8h (share-DA23ACB1) のキャッシュを更新.png

キャッシュ更新後にNFSクライアントから新規作成したEICARファイルの内容を出力します。

$ date
Thu May 22 05:36:21 UTC 2025
$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-rw-rw-. 1 nobody nobody  0 May 22 05:34 malware-protection-resource-validation-object
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

$ head -c 10 /mnt/sgw/eicar.com_2
X5O!P%@AP[$

$ cat /mnt/sgw/eicar.com_2
X5O!P%@AP[4\..(中略)..H*

$ date
Thu May 22 05:37:15 UTC 2025
$ head -c 10 /mnt/sgw/eicar.com_2
X5O!P%@AP[

特に問題なく出力されていますね。

キャッシュの更新は最後に結果をキャッシュしてから追加/削除/置換されたオブジェクトのみ更新します。

Refreshes the cached inventory of objects for the specified file share. This operation finds objects in the Amazon S3 bucket that were added, removed, or replaced since the gateway last listed the bucket's contents and cached the results. This operation does not import files into the S3 File Gateway cache storage. It only updates the cached inventory to reflect changes in the inventory of the objects in the S3 bucket. This operation is only supported in the S3 File Gateway types.

RefreshCache - Storage Gateway

アクセス制御はあくまでバケットポリシーによるもので、オブジェクト自体には手を加えてはいません。

ということで、GuardDuty Malware Protection for S3で検出されたオブジェクトをバケットポリシーで読み込み制御をすることはS3 File Gatewayのファイル共有には影響を及ぼしません。

最後のS3 File Gatewayを停止→起動をした際の状態を確認します。

EC2インスタンスを停止すると、S3 File Gatewayのステータスがオフラインになりました。

20停止状態.png

EC2インスタンスを起動すると、S3 File Gatewayのステータスが実行中になりました。

21_実行中になったことを確認.png

起動完了後、NFSクライアントからNFSファイル共有の領域を確認します。

$ sudo mount -t nfs -o nolock,hard 10.0.0.243:/sgw-2025-05-22-l2o8h /mnt/sgw/
$ ls -l /mnt/sgw/
total 2
-rw-r--r--. 1 nobody nobody 68 May 22 04:57 eicar.com
-rw-r--r--. 1 nobody nobody 68 May 22 05:23 eicar.com_2
-rw-rw-rw-. 1 nobody nobody  0 May 22 05:34 malware-protection-resource-validation-object
-rw-r--r--. 1 nobody nobody 10 May 22 05:05 test.txt

$ head -c 10 /mnt/sgw/eicar.com_2
X5O!P%@AP[

はい、特に変わりありません。

GuardDuty Malware Protection for S3で検出されたオブジェクトをS3 File Gatewayのファイル共有から参照させないようにするためには、「THREATS_FOUNDのタグが付与されたオブジェクトを検出したら削除する」といったEventBridge RuleとLambda関数 or Step Functionsを用意してあげると良さそうです。

S3 File Gateway経由で作成されたオブジェクトもGuardDuty Malware Protection for S3で正常にスキャン、判定される

Amazon S3 File GatewayでもGuardDuty Malware Protection for S3は動作するのか確認してみました。

結論、検出はしますがGuardDuty Malware Protection for S3で検出されたオブジェクトをS3 File Gatewayのファイル共有から参照させないようにするためには、「THREATS_FOUNDのタグが付与されたオブジェクトを検出したら削除する」といったEventBridge RuleとLambda関数 or Step Functionsを用意してあげるようなケアが必要です。

この記事が誰かの助けになれば幸いです。

以上、クラウド事業本部 コンサルティング部の のんピ(@non____97)でした!

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.