

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

# 降低使用存储 AWS Secrets Manager 密钥 AWS CLI 的风险
<a name="security_cli-exposure-risks"></a>

使用 AWS Command Line Interface (AWS CLI) 调用 AWS 操作时，可以在命令外壳中输入这些命令。例如，你可以使用 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. 以管理员身份打开命令提示符（**以管理员身份运行**）。

1. 选择左上角的图标，然后选择**属性**。

1. 在**选项**选项卡上，将**缓冲区大小**和**缓冲区数**均设置为 **1**，然后选择**确定**。

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

   ```
   echo.
   ```

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

对于 Windows 命令提示符外壳，你可以下载该[SysInternals SDelete](https://docs.microsoft.com/en-us/sysinternals/downloads/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.
```