PHP 怎么PHP CVE 查询

wen PHP项目 2

本文目录导读:

PHP 怎么PHP CVE 查询

  1. 方法一:使用 PHP file_get_contents 调用 NVD API(推荐)
  2. 方法二:使用 cURL + Mitre CVE 列表
  3. 方法三:离线查询本地 CVE 数据库(生产环境推荐)
  4. 方法四:手动检查(适合运维/快速查看)
  5. ⚠️ 重要注意事项

在 PHP 中查询 CVE(公共漏洞披露)信息,通常有几种方法,取决于你是想在 PHP 代码中动态查询,还是手动检查 PHP 版本对应的已知漏洞

以下是最常用的几种方式:


使用 PHP file_get_contents 调用 NVD API(推荐)

美国国家漏洞数据库(NVD)提供了免费的 REST API,可以直接在 PHP 中查询。

代码示例(查询 PHP 本身的 CVE):

<?php
function getCVEForPHP($version) {
    // NVD API 地址,查询产品为 PHP,版本为指定版本
    $url = "https://services.nvd.nist.gov/rest/json/cves/2.0?keywordSearch=PHP%20$version&resultsPerPage=20";
    // 发送请求
    $response = @file_get_contents($url);
    if ($response === false) {
        return "API 请求失败,请检查网络或 API 限制";
    }
    $data = json_decode($response, true);
    if (isset($data['vulnerabilities'])) {
        $cves = [];
        foreach ($data['vulnerabilities'] as $item) {
            $cveId = $item['cve']['id'] ?? 'Unknown';
            $description = $item['cve']['descriptions'][0]['value'] ?? '无描述';
            $cves[] = [
                'id' => $cveId,
                'desc' => substr($description, 0, 100) . '...'
            ];
        }
        return $cves;
    } else {
        return "未找到相关 CVE";
    }
}
// 使用示例:查询 PHP 8.1.0 的 CVE
$cves = getCVEForPHP('8.1.0');
print_r($cves);
?>

优点: 实时、数据准确
缺点: 需要外网访问,有 API 请求频率限制(约 5 次/30 秒)


使用 cURL + Mitre CVE 列表

如果你需要更稳定或批量查询,可以用 cURL 从 Mitre 或其他镜像站点获取数据。

<?php
function queryCVEDetail($cveId) {
    $url = "https://cve.circl.lu/api/cve/$cveId";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result, true);
}
// 查询特定 CVE 详情
$detail = queryCVEDetail('CVE-2023-3247');
print_r($detail['summary'] ?? '未找到');
?>

优点: 详细、可控制请求
缺点: 依赖第三方 API 稳定性


离线查询本地 CVE 数据库(生产环境推荐)

对于生产环境或无法联网的场景,建议:

  1. 下载 NVD 数据源(JSON 格式):https://nvd.nist.gov/vuln/data-feeds
  2. 存入本地数据库(如 SQLite)
  3. PHP 查询本地库

简化示例(SQLite):

<?php
$db = new SQLite3('cve.db');
$version = '8.1.0';
$stmt = $db->prepare("SELECT cve_id, description FROM cves WHERE product = 'php' AND version = :ver");
$stmt->bindValue(':ver', $version, SQLITE3_TEXT);
$result = $stmt->execute();
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
    echo "CVE: " . $row['cve_id'] . " - " . $row['description'] . PHP_EOL;
}
?>

手动检查(适合运维/快速查看)

如果你只是想确认某个 PHP 版本是否有已知漏洞,最直接的方法:

  1. 打开浏览器访问:https://www.cvedetails.com/version-search.php
  2. 搜索 PHP 和对应版本号
  3. 或者在命令行直接访问:
    curl "https://www.cvedetails.com/json-feed.php?vendor_id=74&product_id=128&version_id=0&hasexp=0&opec=0&opov=0&opcsrf=0&opfileinc=0&opxss=0&opdir=0&opmemc=0&ophttprs=0&opbyp=0&opginf=0&opdos=0&orderby=3&cvssscoremin=0"

⚠️ 重要注意事项

  1. API 限制: NVD API 2.0 未登录用户限制约 5 次/30 秒,建议加缓存。
  2. 版本格式化: 查询时要保持一致,如 1.0 而不是 1
  3. 生产环境: 建议使用 本地同步 方式,避免依赖外部 API。
  4. PHP 官方安全公告: 安全更新记录在 https://www.php.net/security.php

需求场景 推荐方法
临时查看 方法一(NVD API)
需要详细 CVE 信息 方法二(cURL + Circl API)
生产环境、离线 方法三(本地 SQLite 数据库)
运维快速检查 方法四(手动查看 CVE Details)

如果你能告诉我具体要查询哪个 PHP 版本,我可以直接帮你列出该版本对应的已知 CVE。

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