Skip to content

CVE 資料同步工具,用於從多個漏洞資料庫(NVD、MITRE)同步 CVE 資訊到本地資料庫。

License

Notifications You must be signed in to change notification settings

leoshiang/cve-sync

Repository files navigation

CveSync

高效能 CVE 漏洞資料同步工具

自動從 NVD 和 MITRE 資料庫同步 CVE 漏洞資訊到本地資料庫

Node.js Database Support License Version

功能特色

核心功能

  • 增量同步 - 智慧比對,只同步變更資料
  • 並行處理 - 多資料來源同時處理
  • 錯誤隔離 - 單一來源失敗不影響整體
  • 進度追蹤 - 可視化同步進度

技術支援

  • 多資料庫 - MSSQL、MySQL、PostgreSQL
  • 多資料源 - NVD、MITRE CVE 資料庫
  • 速率控制 - 遵守 API 使用限制
  • 詳細日誌 - 完整的同步歷史記錄

系統要求

  • Node.js >= 18.0.0
  • 資料庫 支援其一:
    • Microsoft SQL Server
    • MySQL >= 5.7
    • PostgreSQL >= 10.0

快速開始

1. 安裝專案

# 複製專案
git clone [https://github.com/leoshiang/cve-sync.git](https://github.com/leoshiang/cve-sync.git) cd cve-sync
# 安裝依賴
npm install

2. 配置資料庫

根據你的資料庫類型建立以下表格:

CVE 資料表結構

CREATE TABLE [isms].[cves]
    (
        [cve_id] NVARCHAR(50)
            PRIMARY KEY,
        [description] NTEXT,
        [published_date] DATETIME,
        [last_modified_date] DATETIME,
        [cvss_score] DECIMAL(3, 1),
        [severity] NVARCHAR(20),
        [reference_urls] NTEXT,
        [affected_products] NTEXT,
        [source] NVARCHAR(20),
        [created_at] DATETIME DEFAULT GETDATE(),
        [updated_at] DATETIME DEFAULT GETDATE()
    );

同步歷史表結構

CREATE TABLE [isms].[sync_history]
    (
        [sync_history_id] INT IDENTITY (1,1)
            PRIMARY KEY,
        [source] NVARCHAR(20) NOT NULL,
        [start_time] DATETIME NOT NULL,
        [end_time] DATETIME,
        [status] NVARCHAR(20) NOT NULL,
        [records_processed] INT DEFAULT 0,
        [error_message] NTEXT,
        [created_at] DATETIME DEFAULT GETDATE()
    );

3. 建立配置文件

複製並修改 config.json

cp config.json.example config.json

4. 開始同步

npm start

配置指南

Microsoft SQL Server 配置

{
    "database": {
        "type": "mssql",
        "schema": "isms",
        "connectionString": "Server=localhost;Database=YourDB;User Id=username;Password=password;Encrypt=false;"
    },
    "sources": {
        "nvd": {
            "enabled": true,
            "apiKey": "your-nvd-api-key",
            "fetchBatchSize": 100,
            "maxRequestsPerMinute": 10
        }
    }
}

MySQL 配置

{
    "database": {
        "type": "mysql",
        "schema": "isms",
        "mysql": {
            "host": "localhost",
            "port": 3306,
            "user": "username",
            "password": "password",
            "database": "isms",
            "charset": "utf8mb4",
            "timezone": "+08:00",
            "acquireTimeout": 60000,
            "timeout": 60000,
            "reconnect": true,
            "ssl": false
        }
    }
}

PostgreSQL 配置

{
    "database": {
        "type": "postgresql",
        "schema": "isms",
        "postgresql": {
            "host": "localhost",
            "port": 5432,
            "user": "username",
            "password": "password",
            "database": "isms",
            "ssl": false,
            "max": 10,
            "idleTimeoutMillis": 30000,
            "connectionTimeoutMillis": 2000,
            "statement_timeout": 30000,
            "query_timeout": 30000
        }
    }
}

配置參數說明

參數 說明 預設值
fetchBatchSize 每次請求的批次大小 100
maxRequestsPerMinute 每分鐘最大請求數 10
maxDateRangeDays 單次查詢最大日期範圍 120
showProgressBar 是否顯示進度條 false

使用方法

基本命令

# 啟動同步
npm start

# 開發模式
npm run dev

# 執行測試
npm test

# 測試覆蓋率
npm run test:coverage

監控同步狀態

-- 檢查最近的同步記錄
SELECT * FROM [isms].[sync_history] 
ORDER BY start_time DESC;

-- 檢查最新的 CVE 資料
SELECT TOP 10 * FROM [isms].[cves] 
ORDER BY last_modified_date DESC;

專案結構

cve-sync/
├── index.js                 # 主程式入口
├── config.json              # 配置文件
├── package.json              # 專案依賴
├── database-manager.js       # 資料庫管理器
├── cve-service-base.js       # CVE 服務基底類別
├── cve-service-factory.js    # CVE 服務工廠
├── nvd-cve-service.js        # NVD 服務實作
├── mitre-cve-service.js      # MITRE 服務實作
├── test/                     # 測試文件
│   ├── DatabaseManager.test.js
│   ├── NvdCveService.test.js
│   ├── MitreCveService.test.js
│   ├── Integration.test.js
│   └── Utils.test.js
└── logs/                     # 日誌文件
    └── app.log

效能特色

特色 說明 效益
增量同步 只同步變更的資料 節省 80% 同步時間
批次處理 批量處理資料 避免記憶體溢出
並行處理 多來源同時執行 提高 50% 處理效率
連接池 智慧管理資料庫連接 優化資源使用

API 使用限制

NVD API

  • 無 API 金鑰:每分鐘 10 次請求
  • 有 API 金鑰:每分鐘 100 次請求
  • 單次查詢:最大 120 天範圍

MITRE API

  • 建議頻率:每分鐘 30 次請求
  • 單次查詢:最大 30 天範圍

日誌等級

等級 用途 範例
debug 詳細調試資訊 API 請求細節
info 一般資訊 同步開始/結束
warn 警告訊息 API 限制觸發
error 錯誤訊息 連接失敗

錯誤處理

  • 網路錯誤:自動重試機制
  • API 限制:智慧延遲和速率控制
  • 資料庫錯誤:事務回滾和錯誤記錄
  • 資料格式錯誤:跳過無效記錄並記錄錯誤

常見問題

如何取得 NVD API 金鑰? 訪問 NVD 官方網站 申請 API 金鑰。有了 API 金鑰可以提高請求頻率限制。 同步失敗怎麼辦?

  1. 檢查 logs/app.log 文件
  2. 確認網路連接和資料庫狀態
  3. 檢查 API 金鑰是否有效
  4. 確認資料庫表格是否已建立

如何新增資料來源?

  1. 繼承 類別 CveServiceBase
  2. 實作必要的同步方法
  3. 在 中註冊新服務 CveServiceFactory
  4. 更新配置文件

如何調整同步頻率?

  • 修改 config.json 中的 lastSyncDate
  • 設定 cron job 定期執行
  • 調整 maxRequestsPerMinute 參數

維護和監控

清理舊日誌

# Linux/Mac - 刪除 30 天前的日誌
find ./logs -name "*.log" -mtime +30 -delete

# Windows - 刪除 30 天前的日誌
forfiles /p logs /s /m *.log /d -30 /c "cmd /c del @path"

效能調優建議

  1. 資料庫索引:在 cve_idlast_modified_date 欄位建立索引
  2. 記憶體配置:根據資料量調整 Node.js 記憶體限制
  3. 批次大小:根據網路狀況調整 fetchBatchSize
  4. 並行限制:適當設定 maxRequestsPerMinute 避免被限制

貢獻指南

歡迎提交 Pull Request!請確保:

  1. 測試:新功能需要包含測試
  2. 文檔:更新相關文檔
  3. 程式碼風格:遵循專案風格
  4. 提交訊息:使用清楚的提交訊息

開發環境設置

# 安裝開發依賴
npm install

# 執行開發模式
npm run dev

# 執行測試
npm run test:watch

授權條款

本專案採用 MIT 授權條款 - 詳見 LICENSE 文件

About

CVE 資料同步工具,用於從多個漏洞資料庫(NVD、MITRE)同步 CVE 資訊到本地資料庫。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published