設定及檢查 CIS 法規遵循

本文將說明 CIS 基準測試為何,基準測試與 Container-Optimized OS (COS) 的關聯性、如何稽核執行個體的合規性狀態,以及在發生錯誤時如何排解問題。

總覽

網際網路安全中心 (CIS) 會針對各種平台發布最佳做法安全性建議的基準。Container-Optimized OS CIS 基準是由一組建議組成,可協助您設定使用 Container-Optimized OS 的執行個體,以支援功能強大的安全防護措施。COS x86 和 ARM 映像檔皆符合 CIS 規範。

存取基準

您可以在 CIS 網站上找到 Container-Optimized OS CIS 基準測試:

  • 前往 CIS 基準下載頁面
  • 搜尋 CIS Google Container-Optimized OS 基準測試
  • 按一下「下載 PDF」

安全性建議等級

CIS 為 Container-Optimized OS 定義了下列建議等級。

1 級

這類建議適用於大多數環境。這一等級包含的建議事項包括:

  • 已啟用位址空間配置隨機載入功能
  • /tmp 無法用於執行可執行的二進位檔
  • 封包重新導向傳送功能已停用

第 2 級

這一層級的建議是延伸自第 1 級建議,可打造更嚴格的安全環境。由於第 2 級建議可能需要應用程式變更,因此不一定適用於所有情況。您應先評估環境的 Level 2 最佳化建議,再套用這些最佳化建議。這一等級包含的建議事項包括:

  • 所有開放的通訊埠都有防火牆規則
  • 不接受 ICMP 重新導向和路由器廣告
  • 預設的使用者殼層逾時時間為 900 秒以下

Container-Optimized OS 如何符合 CIS 基準

里程碑 97 開始,Container-Optimized OS 映像檔預設符合 CIS 級別 1,並提供符合 CIS 級別 2 的選項。我們也提供掃描工具,可讓您根據 CIS 建議等級稽核執行個體。

/usr/share/google/security/cis-compliance/cis_config.textproto 中會顯示定義最佳化建議的 CIS 設定。CIS 掃描器會使用設定檢查執行個體的合規性狀態。每次執行 CIS 層級法規遵循掃描工具的結果都會寫入 /var/lib/google/cis_scanner_scan_result.textproto。每次執行 CIS 掃描器時,這個檔案都會遭到覆寫。如果任何 CIS 1 級或 2 級掃描作業失敗,cis_scanner_scan_result.textproto 檔案會列出所有失敗的檢查。

檢查執行個體相容性狀態

Container-Optimized OS 映像檔提供下列 systemd 服務,用於檢查及設定法規遵循性:

  • cis-level1.service:預設為啟用,並在啟動時啟動。服務啟動時,會檢查執行個體是否符合 CIS 1 級。
  • cis-level2.service:預設為停用。這項服務可讓您將執行個體設定為符合 CIS 第 2 級規定,並檢查是否符合第 1 級和第 2 級規定。

以下各節將說明如何檢查執行個體的合規狀態,以及如何自動執行稽核程序。

查看 CIS 第一級法規遵循狀態

如要瞭解執行個體是否符合 CIS 1 級標準,請檢查 cis-level1.service 的狀態:

systemctl status cis-level1

輸出結果會與下列內容相似:

Reading scan config from /usr/share/google/security/cis-compliance/cis_config.textproto
Running scan of 62 benchmarks
Scan status: SUCCEEDED
Found 0 non-compliant benchmarks
Writing scan results to /var/lib/google/cis_scanner_scan_result.textproto

如果發現任何不符合規定的檢查項目,請參閱CIS 法規遵循度第 1 級/第 2 級檢查失敗

cis-level1.service 只會在執行個體啟動時檢查是否符合 CIS 1 級規定。如要設定定期檢查法規遵循狀況,請參閱「定期檢查 CIS 法規遵循狀態」一文。

設定 CIS 2 級法規遵循和檢查狀態

您可以使用 cis-level2 服務,將執行個體設定為符合 CIS 第 2 級標準,並檢查第 1 級和第 2 級的符合性狀態。systemd 服務支援所有 CIS 2 級建議,但下列項目除外

  • 4.1.1.2 確保記錄功能正在執行 (ID:logging-service-running)

    系統預設會停用這項最佳化建議,但您可以從 /etc/cis-scanner/env_vars 檔案中的停用清單中刪除 ID,重新啟用這項最佳化建議。這項檢查僅存在於 109 以上里程碑。在早期里程碑中,只要啟用記錄功能,就能符合這項建議。如果您取消選擇不採用,執行 cis-level2 服務時,系統會執行指令碼,啟動 Fluent Bit 記錄功能。如果不想使用我們的預設記錄功能,或想要使用自己的記錄功能,請保持 logging-service-running 停用狀態。

針對下列建議,cis-level2 服務會設定執行個體,但不會驗證法規遵循狀態:

  • 105 以下:

    • 3.3.1.1 確保 IPv6 預設拒絕防火牆政策
    • 3.3.1.2 確認已設定 IPv6 迴圈流量
    • 3.3.1.3 確認已設定 IPv6 傳出和已建立的連線
    • 3.3.1.4 確保所有開放的通訊埠都有 IPv6 防火牆規則
    • 3.3.2.1 確保預設拒絕防火牆政策
    • 3.3.2.2 確認已設定迴圈流量
    • 3.3.2.3 確認已設定輸出和已建立的連線
  • 109 以上版本:

    • 3.3.1.4 確保所有開放的通訊埠都有 IPv6 防火牆規則

cis-level2 服務預設為停用。如要啟動服務,請執行下列指令:

systemctl start cis-level2.service

如要查看執行個體是否已成功設定並符合 CIS 第 2 級建議,請檢查 cis-level2.service 的狀態:

systemctl status cis-level2

輸出結果會與下列內容相似:

Reading scan config from /usr/share/google/security/cis-compliance/cis_config.textproto
Running scan of 112 benchmarks
Scan status: SUCCEEDED
Found 0 non-compliant benchmarks
Writing scan results to /var/lib/google/cis_scanner_scan_result.textproto

如果執行個體設定失敗或發現任何不符合規定的檢查項目,請參閱疑難排解

cis-level2 服務只會設定執行個體,並檢查 CIS 2 級法規遵循狀況一次。如要設定定期檢查法規遵循狀況,請參閱「定期檢查 CIS 法規遵循狀態」一文。

定期檢查 CIS 法規遵循狀態

Container-Optimized OS 映像檔包含下列服務,可定期檢查 CIS 法規遵循情況:

  • cis-compliance-scanner.service:根據 /etc/cis-scanner/env_vars 中定義的環境變數檢查法規遵循狀態。根據預設,這項服務會檢查 CIS 1 級法規遵循情況,並停用。
  • cis-compliance-scanner.timer:定期執行 cis-compliance-scanner.service。預設週期為每天一次。

設定掃描器服務

cis-compliance-scanner.service 負責根據 /etc/cis-scanner/env_vars 定義的環境變數,檢查 CIS 法規遵循狀態。根據預設,這項服務會檢查 CIS 1 級合規性。

如要檢查 CIS 2 級法規遵循情況,請將 /etc/cis-scanner/env_vars 中的 LEVEL 環境變數設為 2/etc/cis-scanner/env_vars 檔案會類似如下:

# cis-compliance-scanner.service environment variables
# The config file defines which checks to perform by cis_scanner
CONFIG="/usr/share/google/security/cis-compliance/cis_config.textproto"
# Where to store the result of the scan
RESULT="/var/lib/google/cis_scanner_scan_result.textproto"
# Upto which level to scan. It can be 1 or 2
LEVEL="2"
# Extra options that can be passed to cis_scanner
# For valid options, see output of `cis_scanner -h`
EXTRA_OPTIONS=""

設定計時器

如要設定定期規範掃描,請啟動 cis-compliance-scanner.timer 單元:

systemctl start cis-compliance-scanner.timer

根據預設,cis-compliance-scanner.timer 會每天開始 cis-compliance-scanner.service 一次。如要變更掃描週期,請覆寫 cis-compliance-scanner.timer 單位的 OnUnitActiveSec 欄位:

sudo mkdir /etc/systemd/system/cis-compliance-scanner.timer.d
sudo tee /etc/systemd/system/cis-compliance-scanner.timer.d/override.conf <<EOF
[Unit]
Description=Run CIS Scanner once an hour
[Timer]
OnUnitActiveSec=1h
EOF

這個範例將掃描器週期設為每小時一次。

如要套用變更,請重新載入 systemd 單位:

systemctl daemon-reload

選擇不執行特定 CIS 法規遵循檢查

CIS 層級 1 和層級 2 的最佳化建議適用於大多數環境。不過,部分最佳化建議可能不適用於您的特定環境。如要停用特定最佳化建議,請在 /etc/cis-scanner/env_vars 中使用 EXTRA_OPTIONS 環境變數。

以下 env_vars 檔案範例會停用 etc-passwd-permissions 最佳化建議:

# cis-compliance-scanner.service environment variables
# The config file defines which checks to perform by cis_scanner
CONFIG="/usr/share/google/security/cis-compliance/cis_config.textproto"
# Where to store the result of the scan
RESULT="/var/lib/google/cis_scanner_scan_result.textproto"
# Upto which level to scan. It can be 1 or 2
LEVEL="1"
# Extra options that can be passed to cis_scanner
# For valid options:`cis_scanner -h`
EXTRA_OPTIONS="--benchmark-opt-out-ids=etc-passwd-permissions"

自動啟用及檢查 CIS 法規遵循狀態

您可以使用 cloud-init 或 OS Policy,自動執行執行個體的規定遵循檢查程序。以下範例說明每個工具的幾種用途:

  • 示例 1:每天檢查一次 CIS 1 級合規性。
  • 範例 2:每小時檢查一次 CIS 1 級合規性。
  • 示例 3:每天檢查一次 CIS 2 級法規遵循情況。
  • 示例 4:選擇不採用特定 CIS 法規遵循檢查。

使用 cloud-init

在嘗試下列範例之前,請先按照「使用 cloud-init 搭配 Cloud 設定格式」中的說明,熟悉如何使用 cloud-init 設定 COS 執行個體。

範例 1

以下範例設定會啟動定期的 CIS 層級 1 掃描作業,預設週期為每天一次。

#cloud-config

runcmd:
# Check the compliance status of the instance once a day.
- systemctl start cis-compliance-scanner.timer

範例 2

以下範例會設定每小時執行一次的定期 CIS 層級 1 掃描作業。

#cloud-config

# Override cis-compliance-scanner.timer with 1 hour frequency.
write_files:

- path: /etc/systemd/system/cis-compliance-scanner.timer.d/override.conf
  permissions: 0600
  owner: root
  content: |
    [Unit]
    Description=Run CIS Scanner once an hour

    [Timer]
    OnUnitActiveSec=1h

runcmd:
# Reload systemd units.
- systemctl daemon-reload
# Check the compliance status of the instance once an hour.
- systemctl start cis-compliance-scanner.timer

範例 3

以下範例會設定定期的 CIS 2 級掃描,使用預設的每天一次的時間間隔。

#cloud-config

runcmd:
# Configure the instance for CIS level 2.
- systemctl start cis-level2.service
# Change the scan level to CIS Level 2.
- sed -i 's/^LEVEL=.*$/LEVEL="2"/' /etc/cis-scanner/env_vars
# Check the compliance status of the instance once a day.
- systemctl start cis-compliance-scanner.timer

範例 4

以下範例會將掃描器設定為每天執行一次,並選擇停用特定的 CIS 建議。

#cloud-config

runcmd:
# Opt-out of the etc-passwd-permissions check.
- sed -i 's/^EXTRA.*$/EXTRA_OPTIONS="--benchmark-opt-out-ids=etc-passwd-permissions"/' /etc/cis-scanner/env_vars
# Check the compliance of the instance once a day.
- systemctl start cis-compliance-scanner.timer

使用 OS 政策

您可以使用 OS 政策設定 CIS 基準掃描。開始前,請務必熟悉 OS Policy,包括下列項目:

此外,您必須在下列部署範例中新增 instanceFilterrollout 選項。

範例 1

以下範例設定會啟動定期的 CIS 層級 1 掃描作業,預設週期為每天一次。

# An OS policy to check CIS level 1 compliance once a day.
id: ensure-cis-level1-compliance-once-a-day-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      id: ensure-cis-level1-compliance-once-a-day
      exec:
        validate:
          interpreter: SHELL
          # If cis-compliance-scanner.service is active, return an exit code
          # 100 to indicate that the instance is in compliant state.
          # Otherwise, return an exit code of 101 to run `enforce` step.
          script: |-
            is_active=$(systemctl is-active cis-compliance-scanner.timer)
            result=$(systemctl show -p Result --value cis-compliance-scanner.service)

            if [ "$is_active" == "active" ] && [ "$result" == "success" ]; then
              exit 100;
            else
              exit 101;
            fi
        enforce:
          interpreter: SHELL
          # COS 97 images are by-default CIS Level 1 compliant and there is no
          # additional configuration needed. However, if certain changes
          # cause non-compliance because of the workload on the instance, this
          # section can be used to automate to make fixes. For example, the
          # workload might generate a file that does not comply with the
          # recommended file permissions.
          # Return an exit code of 100 to indicate that the desired changes
          # successfully applied.
          script: |-
            # optional <your code>
            # Check the compliance of the instance once a day.
            systemctl start cis-compliance-scanner.timer && exit 100

範例 2

以下範例會設定每小時執行一次的定期 CIS 層級 1 掃描作業。

# An OS policy to check CIS level 1 compliance once an hour.
id: ensure-cis-level1-compliance-once-an-hour-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      id: ensure-cis-level1-compliance-once-an-hour
      exec:
        validate:
          interpreter: SHELL
          # If cis-compliance-scanner.service is active, return an exit code
          # 100 to indicate that the instance is in compliant state.
          # Otherwise, return an exit code of 101 to run `enforce` step.
          script: |-
            is_active=$(systemctl is-active cis-compliance-scanner.timer)
            result=$(systemctl show -p Result --value cis-compliance-scanner.service)

            if [ "$is_active" == "active" ] && [ "$result" == "success" ]; then
              exit 100;
            else
              exit 101;
            fi
        enforce:
          interpreter: SHELL
          # Return an exit code of 100 to indicate that the desired changes
          # were successfully applied.
          script: |-
            # Overwrite "OnUnitActiveSec" field of the
            # cis-compliance-scanner.timer to trigger
            # cis-compliance-scanner.service once an hour
            # instead of once a day.
            mkdir /etc/systemd/system/cis-compliance-scanner.timer.d
            tee /etc/systemd/system/cis-compliance-scanner.timer.d/override.conf <<EOF
            [Unit]
            Description=Run CIS Scanner once an hour

            [Timer]
            OnUnitActiveSec=1h
            EOF
            # Reload systemd units.
            systemctl daemon-reload
            # Check the compliance of the instance once an hour.
            systemctl start cis-compliance-scanner.timer && exit 100

範例 3

以下範例會設定定期的 CIS 2 級掃描,使用預設的每天一次的時間間隔。

# An OS policy to check CIS level 2 compliance once a day.
id: ensure-cis-level2-compliance-once-a-day-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      id: ensure-cis-level2-compliance-once-a-day
      exec:
        validate:
          interpreter: SHELL
          # If cis-compliance-scanner.service is active, return an exit code
          # 100 to indicate that the instance is in compliant state.
          # Otherwise, return an exit code of 101 to run `enforce` step.
          script: |-
            is_active=$(systemctl is-active cis-compliance-scanner.timer)
            result=$(systemctl show -p Result --value cis-compliance-scanner.service)

            if [ "$is_active" == "active" ] && [ "$result" == "success" ]; then
              exit 100;
            else
              exit 101;
            fi
        enforce:
          interpreter: SHELL
          # Return an exit code of 100 to indicate that the desired changes
          # were successfully applied.
          script: |-
            # Configure the instance for CIS level 2.
            systemctl start cis-level2.service
            # Change the scan level to 2.
            sed -i 's/^LEVEL=.*$/LEVEL="2"/' /etc/cis-scanner/env_vars
            # Check the compliance of the instance once a day.
            systemctl start cis-compliance-scanner.timer && exit 100

範例 4

以下範例會將掃描器設定為每天執行一次,並選擇停用特定的 CIS 建議。

# An OS policy to opt-out of CIS check and check compliance status once a day.
id: exclude-cis-check-and-check-compliance-once-a-day-policy
mode: ENFORCEMENT
resourceGroups:
  - resources:
      id: exclude-cis-check-and-check-compliance-once-a-day
      exec:
        validate:
          interpreter: SHELL
          # If cis-compliance-scanner.service is active, return an exit code
          # 100 to indicate that the instance is in compliant state.
          # Otherwise, return an exit code of 101 to run `enforce` step.
          script: |-
            is_active=$(systemctl is-active cis-compliance-scanner.timer)
            result=$(systemctl show -p Result --value cis-compliance-scanner.service)

            if [ "$is_active" == "active" ] && [ "$result" == "success" ]; then
              exit 100;
            else
              exit 101;
            fi
        enforce:
          interpreter: SHELL
          # Return an exit code of 100 to indicate that the desired changes
          # were successfully applied.
          script: |-
            # Opt-out of the etc-passwd-permissions check.
            sed -i 's/^EXTRA.*$/EXTRA_OPTIONS="--benchmark-opt-out-ids=etc-passwd-permissions"/' /etc/cis-scanner/env_vars &&
            # Check the compliance of the instance once a day.
            systemctl start cis-compliance-scanner.timer
            # Ensure cis-compliance-scanner completes before exiting
            PID=$(systemctl show --property MainPID --value cis-compliance-scanner.service) &&
            timeout 5m bash -c -- 'while [ -e /proc/'$PID' ]; do echo "CIS Scanner with PID:'$PID' is still running"; sleep 1; done' &&
            exit 100

疑難排解

本節說明如何解決與 CIS 基準掃描相關的問題。

無法設定符合 CIS 2 級最佳做法的執行個體

cis-level2 服務會先將執行個體設定為符合 CIS 第 2 級建議,然後檢查是否符合 CIS 第 1 級和第 2 級規定。如果設定例項失敗,cis-level2 服務會退出並顯示以下錯誤訊息:

Job for cis-level2.service failed because the control process exited with error code.
See "systemctl status cis-level2.service" and "journalctl -xeu cis-level2.service" for details.

日誌記錄會提及無法在執行個體上套用的最佳化建議,導致 cis-level2 systemd 服務失敗。

CIS 法規遵循第 1 級或第 2 級檢查失敗

每個 CIS 等級法規遵循執行作業的掃描結果會寫入 /var/lib/google/cis_scanner_scan_result.textproto。如果任何 CIS 1 級或 2 級掃描作業失敗,textproto 檔案就會包含所有失敗檢查的清單,例如以下範例:

cat /var/lib/google/cis_scanner_scan_result.textproto

# Output
start_time: {
  seconds: 1648241700
  nanos: 763152171
}
end_time: {
  seconds: 1648241700
  nanos: 812992527
}
scanner_version: "1.1.4.3"
benchmark_version: "1.0.0"
status: {
  status: SUCCEEDED
}
non_compliant_benchmarks: {
  id: "etc-passwd-permissions"
  compliance_occurrence: {
    non_compliant_files: {
      path: "/etc/passwd"
      reason: "File permission is 0664, expected the following bits to be set: 0444 and the following bits to be clear: 0133"
    }
  }
}
compliant_benchmarks:  {
  id:  "etc-passwd-permissions"
  compliance_occurrence:  {}
}

如要緩解檢查失敗的問題,請使用 CIS 基準,並按照 Remediation 部分的步驟,針對檢查失敗的項目讓執行個體符合規定。如要找出哪些建議與 CIS 基準中的失敗檢查項目相符,請在位於 /usr/share/google/security/cis-compliance/cis_config.textproto 的 CIS 掃描器設定檔中查詢 non_compliant_benchmark's ID。