AWS CloudShell におけるファイルのアップロード・ダウンロードを禁止する権限を試してみた

2024.05.21

先日、AWS Security Hub において新しいセキュリティ基準「CIS AWS Foundations Benchmark v3.0.0」が追加され、それに伴って AWS CloudShell に関するコントロールとして AWS 管理ポリシーであるAWSCloudShellFullAccess をアタッチしていないことを確認する項目が追加されました。その対応策の一つとして AWS CloudShell において、ファイルのアップロード・ダウンロードができない権限を試してみました。


追加された AWS CloudShell に関するコントロールです。AWS CloudShell はインターネットにアクセスでき、sudo 権限を持つので、ファイル転送ソフトをインストールしてデータを外部に転送できてしまう懸念が記載されています。

This control checks whether an IAM identity (user, role, or group) has the AWS managed policy AWSCloudShellFullAccess attached. The control fails if an IAM identity has the AWSCloudShellFullAccess policy attached.

AWS CloudShell provides a convenient way to run CLI commands against AWS services. The AWS managed policy AWSCloudShellFullAccess provides full access to CloudShell, which allows file upload and download capability between a user's local system and the CloudShell environment. Within the CloudShell environment, a user has sudo permissions, and can access the internet. As a result, atttaching this managed policy to an IAM identity gives them the ability to install file transfer software and move data from CloudShell to external internet servers. We recommend following the principle of least privilege and attaching narrower permissions to your IAM identities.

AWS Identity and Access Management controls - AWS Security Hub


なお、セキュリティ基準「CIS AWS Foundations Benchmark v3.0.0」については次のブログで紹介されています。

アップロード・ダウンロードを禁止するポリシー

AWS CloudShell に関する IAM ポリシーのアクションは次のユーザーガイドに記載があります。AWS CloudShell を起動するために必要な権限かどうかも記載があります。

IAM ポリシーによる AWS CloudShell アクセスと使用状況の管理 - AWS CloudShell


上記ページにアップロード・ダウンロードを禁止する次のポリシー例も記載されています。1 つ目のステートメントで AWS CloudShell のすべてのアクションを許可して、2 つ目のステートメントでファイルのアップロード・ダウンロードを拒否しています。

{
    "Version": "2012-10-17",
    "Statement": [
        {
        "Sid": "AllowUsingCloudshell",
        "Effect": "Allow",
        "Action": [
            "cloudshell:*"
        ],
        "Resource": "*"
    },
    {
        "Sid": "DenyUploadDownload",
        "Effect": "Deny",
        "Action": [
            "cloudshell:GetFileDownloadUrls",
            "cloudshell:GetFileUploadUrls"
        ],
        "Resource": "*"
    }]
}

別のパターンとして次のポリシーでも同様にアップロード・ダウンロードを禁止できます。ファイルのアップロード・ダウンロード以外のアクションを列挙しています。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudshell:DeleteEnvironment",
                "cloudshell:PutCredentials",
                "cloudshell:StopEnvironment",
                "cloudshell:CreateEnvironment",
                "cloudshell:GetEnvironmentStatus",
                "cloudshell:StartEnvironment",
                "cloudshell:CreateSession"
            ],
            "Resource": "*"
        }
    ]
}

両者の違いとしては、仮に AWS CloudShell に新しいアクションが追加された場合に、前者の場合は新しいアクションが許可されていますが、後者の場合は新しいアクションが許可されていない状態となります。そのため、新しいアクションに対して許可状態をデフォルトにしたいかどうかがポリシーの選択基準の一つになります。


上述した IAM ポリシーを AWS 管理ポリシーであるReadOnlyAccessと一緒にアタッチした場合の動作を確認してみます。

AWS CloudShell を起動すると、マネジメントコンソールにサインインしているユーザー/ロールのクレデンシャルで AWS CLI は利用できる状態です。cloudshell:PutCredentialsアクションの権限でクレデンシャルを利用できるようになります。下記は AWS CLI コマンドの実行例です。この例では AWS IAM Identity Center を利用しています。

$ aws sts get-caller-identity
{
    "UserId": "AROA5FTZEOZKO3EXAMPLE:test-user",
    "Account": "111122223333",
    "Arn": "arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnlyAccessWithCloudShell_cb9799051dexample/test-user"
}

ファイルのアップロードを試したところ、エラーメッセージと共に失敗しました。

ファイルのダウンロードも想定通り拒否されます。

意図した通り、アップロード・ダウンロードを禁止できていました。

注意点として、ファイルのアップロードとダウンロードを禁止した場合でも AWS CloudShell がインターネットにアクセスできる状態であることには変わりはありません。例えば、下記はexample.comへのアクセスを試した例です。

$ curl https://example.com
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

以上で AWS CloudShell におけるファイルのアップロード・ダウンロード制限のお試しは終わりです。

さいごに

AWS Security Hub で新しいセキュリティ基準「CIS AWS Foundations Benchmark v3.0.0」において、AWS CloudShell の権限に関するコントロールが追加されたことをきっかけに、AWS CloudShell においてファイルのアップロード・ダウンロードを制限する IAM ポリシーを試してみました。

AWS CloudShell の権限については次の最小権限で起動してみたブログも参考になります。

以上、このブログがどなたかのご参考になれば幸いです。