怎么用脚本获取处理器漏洞

wen 实用脚本 1

本文目录导读:

怎么用脚本获取处理器漏洞

  1. Linux 系统 (最常用)
  2. Windows 系统
  3. macOS 系统
  4. 跨平台 Python 脚本 (读取 CPU 特性)
  5. 常见漏洞名称对照表 (CVE)

获取处理器漏洞信息通常涉及读取操作系统和 CPU 提供的特定状态寄存器或系统文件,以下是针对不同操作系统的常用脚本方法,但请注意,这些操作通常需要管理员/root权限

重要安全警告:

  1. 仅用于合法目的:在自己的设备或已获得授权的系统上进行测试。
  2. 漏洞利用风险:部分漏洞(如Spectre/Meltdown)的缓解措施可能被系统更新禁用,执行脚本前确保系统已安装最新安全补丁。
  3. 性能影响:读取某些调试寄存器或使用特定CPU指令可能临时降低性能。

Linux 系统 (最常用)

Linux 内核在 /sys/devices/system/cpu/vulnerabilities/ 目录下提供了所有已知处理器漏洞的状态。

方法 A:直接读取系统文件 (Bash/Shell)

#!/bin/bash
# 显示所有已知的 CPU 漏洞状态
echo "=== 处理器漏洞状态 ==="
ls /sys/devices/system/cpu/vulnerabilities/ 2>/dev/null | while read vuln; do
    printf "%-20s : %s\n" "$vuln" "$(cat /sys/devices/system/cpu/vulnerabilities/$vuln)"
done
# 也可以一键查看:
# cat /sys/devices/system/cpu/vulnerabilities/*

输出示例:

meltdown           : Mitigation: PTI
spectre_v1         : Mitigation: usercopy/swapgs barriers and __user pointer sanitization
spectre_v2         : Mitigation: Full generic retpoline, IBPB, IBRS_FW, STIBP: enabled, RSB filling
spec_store_bypass  : Mitigation: Speculative Store Bypass disabled via prctl
...

方法 B:使用第三方工具 spectre-meltdown-checker

这是一个更专业的脚本,能进行深度检测并给出具体缓解措施建议。

  1. 下载并运行 (无需安装,直接执行):
    wget https://raw.githubusercontent.com/speed47/spectre-meltdown-checker/master/spectre-meltdown-checker.sh
    chmod +x spectre-meltdown-checker.sh
    sudo ./spectre-meltdown-checker.sh

    它会输出类似:

    CVE-2017-5753 [spectre_v1]  : Vulnerable (Mitigation: usercopy/swapgs barriers)
    CVE-2017-5715 [spectre_v2]  : Mitigation: Full retpoline + IBPB
    CVE-2017-5754 [meltdown]    : Mitigation: PTI

Windows 系统

Windows 通过注册表、PowerShell 和工具 Get-SpeculationControlSettings 提供信息。

方法 A:使用 PowerShell (推荐)

# 获取 Speculation Control 设置 (Spectre/Meltdown相关)
Get-SpeculationControlSettings
# 输出示例:
# ...
# Speculation Control Enabled: True
# Speculation Control Mitigation Enabled: True
# BpbEnabled: True
# ...

方法 B:使用命令行工具 InspectSpectre

微软官方工具 InspectSpectre.exe 可以直接扫描。

  1. 下载:从 Microsoft Sysinternals 下载。
  2. 运行
    InspectSpectre.exe

    输出示例

    Spectre Variant 1 (CVE-2017-5753):    Mitigated
    Spectre Variant 2 (CVE-2017-5715):    Mitigated
    Meltdown (CVE-2017-5754):             Not applicable (AMD/...)

方法 C:读取注册表 (PowerShell)

# 查看与漏洞缓解相关的注册表键
Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" -Name "FeatureSettingsOverride*" 2>$null

注意:Windows 的漏洞状态信息没有 Linux 那么集中,Get-SpeculationControlSettings 是最权威的命令。


macOS 系统

macOS 没有像 Linux 那样直接暴露的系统文件,但可通过 sysctl 命令查看。

使用终端命令 (Bash/Zsh)

# 查看所有与 CPU 相关的安全特性
sysctl machdep.cpu.brand_string           # CPU型号
sysctl machdep.cpu.features               # 支持的CPU特性
sysctl macos.hv.allow.all_os_boot_arguments # HV相关(通常不直接显示漏洞)
# 更直接:检查 Spectre/Meltdown 缓解是否启用
sysctl kern.speculative_store_bypass_disable
sysctl kern.ibrs
sysctl kern.pti

macOS 通常不提供像 Linux 那样的“漏洞名称 → 状态”映射。 如果系统已更新到最新版本(macOS High Sierra 10.13.2+),则默认已包含缓解措施。


跨平台 Python 脚本 (读取 CPU 特性)

如果你需要编写跨平台脚本,可以读取 CPU 的特定特性寄存器(需要 cpuid 指令),Python 的 cpuid 库或 psutil 可以辅助。

import platform
import subprocess
def get_cpu_vulnerabilities():
    system = platform.system()
    if system == "Linux":
        # 读取 /sys/devices/system/cpu/vulnerabilities/
        import os
        vulns = {}
        path = "/sys/devices/system/cpu/vulnerabilities/"
        if os.path.exists(path):
            for f in os.listdir(path):
                with open(os.path.join(path, f)) as fh:
                    vulns[f] = fh.read().strip()
        return vulns
    elif system == "Windows":
        # 使用 PowerShell 命令
        cmd = "powershell -Command \"Get-SpeculationControlSettings | ConvertTo-Json\""
        result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
        return result.stdout
    elif system == "Darwin":
        # macOS 使用 sysctl
        cmd = "sysctl kern.ibrs kern.pti kern.speculative_store_bypass_disable"
        result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
        return result.stdout
    else:
        return "Unsupported system"
print(get_cpu_vulnerabilities())

常见漏洞名称对照表 (CVE)

漏洞名称 CVE 编号 类型
Meltdown CVE-2017-5754 熔断 (仅影响Intel)
Spectre v1 CVE-2017-5753 幽灵 (边界检查绕过)
Spectre v2 CVE-2017-5715 幽灵 (分支目标注入)
Speculative Store Bypass CVE-2018-3639 预取侧信道 (SSB)
L1 Terminal Fault (L1TF) CVE-2018-3620 L1D 缓存刷新 (L1TF)
Microarchitectural Data Sampling (MDS) CVE-2019-11091等 微架构数据采样 (如ZombieLoad、RIDL)
Retbleed CVE-2022-29901/2 针对Retpoline的旁路

  • Linux:直接读 /sys/devices/system/cpu/vulnerabilities/* 最方便。
  • Windows:用 Get-SpeculationControlSettingsInspectSpectre.exe
  • macOS:用 sysctl 查询特性(不如前两者直观)。
  • 通用:使用 spectre-meltdown-checker (Linux) 或 InspectSpectre (Windows) 获得最详细的报告。

请始终在合法授权的环境下进行此类操作。

抱歉,评论功能暂时关闭!