

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 降低使用 AWS CLI 存放 AWS Secrets Manager 秘密的風險
<a name="security_cli-exposure-risks"></a>

當您使用 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.
  ```

在您執行這些命令後，應要能夠使用上下箭號來捲動命令歷程記錄，並可看到秘密文字未顯示於任何列。

**重要**  
在預設情況下，您必須先將命令歷史記錄緩衝區的大小降低至 **1**，否則無法在 Windows 中執行相同技術。

**若要設定 Windows 命令提示字元為只有 1 個命令的 1 個歷程記錄緩衝區**

1. 開啟管理員命令提示字元，或選擇 **Run as administrator (以系統管理員身分執行)**。

1. 選擇左上角的圖示，然後選擇**屬性**。

1. 在 **Options (選項)** 索引標籤上，將 **Buffer Size (緩衝區大小)** 及 **Number of Buffers (緩衝區數量)** 皆設定為 **1**，然後選擇 ​**OK (確定)**。

1. 當您需要輸入不想留存於歷史記錄中的命令時，請立即接續輸入另一個命令，例如：

   ```
   echo.
   ```

   這可確保您刷新敏感命令。

對於 Windows 命令提示字元 Shell，您可以下載 [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.
```