設定及檢查 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 Level 1,並提供符合 CIS Level 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 Level 1。
  • cis-level2.service:預設為停用。這項服務可讓您設定執行個體,使其符合 CIS 第 2 級標準,並檢查是否符合第 1 級和第 2 級標準。

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

查看 CIS Level 1 法規遵循狀態

如要確認執行個體是否符合 CIS Level 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 Level 2 合規性並檢查狀態

您可以使用 cis-level2 服務,將執行個體設定為符合 CIS 第 2 級標準,並檢查是否符合第 1 級和第 2 級標準。系統服務支援所有 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 Level 1,且處於停用狀態。
  • cis-compliance-scanner.timer:定期執行 cis-compliance-scanner.service。預設為每天一次。

設定掃描器服務

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

如要檢查是否符合 CIS Level 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 Level 1 合規性。
  • 範例 2:每小時檢查一次 CIS Level 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 Level 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 政策,包括:

此外,您必須在下列部署範例中新增 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 Level 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 Level 1 或 Level 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。