設定及檢查 CIS 法規遵循

本文說明 CIS 基準、基準與 Container-Optimized OS (COS) 的關係、如何稽核執行個體的法規遵循狀態,以及如何排解失敗問題。

總覽

網際網路安全中心 (CIS) 會發布基準,為各種平台提供安全性最佳做法建議。Container-Optimized OS CIS 基準是一組建議,可協助您設定使用 Container-Optimized OS 的執行個體,支援強大的資安態勢。COS x86 和 ARM 映像檔皆符合 CIS 標準。

存取基準

Container-Optimized OS CIS 基準可在 CIS 網站上取得:

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

安全性建議等級

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

1 級

這個層級的建議適用於大多數環境。這個等級包含下列建議:

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

第 2 級

這個層級的建議是第 1 級建議的延伸,可打造更嚴格的安全環境。第 2 級建議可能需要變更應用程式,因此不一定適用於所有情況。套用第 2 級建議前,請先評估是否適用於您的環境。這個等級包含下列建議:

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

Container-Optimized OS 如何遵守 CIS 基準

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

定義建議的 CIS 設定位於 /usr/share/google/security/cis-compliance/cis_config.textproto。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 第 1 級法規遵循狀態

如要確認執行個體是否符合 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 確保 Logging 正在執行 (ID:logging-service-running)

    這項建議根據預設為選擇不採用,但只要從 /etc/cis-scanner/env_vars 檔案的選擇不採用清單中刪除 ID,即可重新啟用。這項檢查僅適用於 109 以上版本。在先前的里程碑中,自行啟用 Logging 即可符合這項建議。如果取消選擇不採用,執行 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 政策,自動檢查執行個體的法規遵循狀態。以下範例說明各項工具的用途:

  • 範例 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 政策,包括下列事項:

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

範例 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 基準中未通過檢查的建議,請在 CIS 掃描器設定檔 (位於 /usr/share/google/security/cis-compliance/cis_config.textproto) 中查詢 non_compliant_benchmark's ID。