降低使用 AWS CLI 存储 AWS Secrets Manager 密钥的风险 - AWS Secrets Manager

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

降低使用 AWS CLI 存储 AWS Secrets Manager 密钥的风险

使用 AWS Command Line Interface (AWS CLI) 调用 AWS 操作时,您会在命令 Shell 中输入这些命令。例如,您可以使用 Windows 命令提示符或 Windows PowerShell,或者 Bash 或 Z Shell,或者其他类型。其中的很多命令 Shell 包含旨在提高工作效率的功能。但其他人可能会使用该功能窃取您的密钥。例如,在大多数 Shell 中,您可以使用上箭头键查看最后输入的命令。访问不受保护的会话的任何人可能会利用命令历史记录功能。另外,在后台工作的其他实用程序可能有权访问您的命令参数,这些参数旨在帮助您更高效地执行任务。为了减轻此类风险,请确保执行以下步骤:

  • 在离开计算机前始终记得锁定计算机。

  • 卸载或禁用不需要或不再使用的控制台实用程序。

  • 确保 Shell 或远程访问程序(如果您在使用其中之一)不会记录键入的命令。

  • 使用一些方法传递 Shell 命令历史记录未捕获的参数。以下示例说明了如何在文本文件中键入密钥文本,然后将该文件传递给 AWS Secrets Manager 命令并立即销毁文件。这意味着典型的 Shell 历史记录不会捕获密钥文本。

    以下示例显示典型的 Linux 命令(但您的 shell 可能需要稍有不同的命令):

    $ touch secret.txt # Creates an empty text file $ chmod go-rx secret.txt # Restricts access to the file to only the user $ cat > secret.txt # Redirects standard input (STDIN) to the text file ThisIsMyTopSecretPassword^D # Everything the user types from this point up to the CTRL-D (^D) is saved in the file $ aws secretsmanager create-secret --name TestSecret --secret-string file://secret.txt # The Secrets Manager command takes the --secret-string parameter from the contents of the file $ shred -u secret.txt # The file is destroyed so it can no longer be accessed.

运行这些命令后,在使用向上和向下箭头滚动命令历史记录时就不会在任何一行中看到密钥文本。

重要

默认情况下,不能在 Windows 中使用这类技术,除非您先将命令历史记录的缓冲区大小减小为 1

将 Windows 命令提示符配置为 1 条命令只有 1 条命令的历史记录缓冲区
  1. 以管理员身份打开命令提示符(以管理员身份运行)。

  2. 选择左上角的图标,然后选择属性

  3. 选项选项卡上,将缓冲区大小缓冲区数均设置为 1,然后选择确定

  4. 每次您必须键入不希望保留在历史记录中的命令时,请在紧靠该命令后面键入另一条命令,例如:

    echo.

    这可以确保您刷新敏感命令。

对于 Windows 命令提示符 Shell,您可以下载 SysInternals SDelete 工具,然后使用类似下面的命令:

C:\> echo. 2> secret.txt # Creates an empty file C:\> icacls secret.txt /remove "BUILTIN\Administrators" "NT AUTHORITY/SYSTEM" /inheritance:r # Restricts access to the file to only the owner C:\> copy con secret.txt /y # Redirects the keyboard to text file, suppressing prompt to overwrite THIS IS MY TOP SECRET PASSWORD^Z # Everything the user types from this point up to the CTRL-Z (^Z) is saved in the file C:\> aws secretsmanager create-secret --name TestSecret --secret-string file://secret.txt # The Secrets Manager command takes the --secret-string parameter from the contents of the file C:\> sdelete secret.txt # The file is destroyed so it can no longer be accessed.