資安週報Security Weekly 攻擊手法通報 × 資安工具Advisories × Tooling
資產可視性Asset visibility Apache-2.0 OR GPL-2.0-only

osquery

把作業系統變成一個可以下 SQL 查詢的資料庫,用一句查詢就能問遍全機群「誰裝了哪個版本」。Exposes the operating system as a SQL-queryable database, so one statement asks your whole fleet which version of what is installed.

這是什麼What it is

重大元件漏洞公開的那天,第一個問題永遠是「我們哪些機器受影響?」。多數組織答不出來,或要花上數天。

osquery 把這件事變成一句 SQL。它在每台主機上執行,將執行中的程序、已安裝套件、監聽的連接埠、使用者帳號、排程工作、核心模組、瀏覽器擴充功能等系統狀態,全部表示成可查詢的資料表。想知道哪些機器跑著特定版本的 nginx,就 SELECT 它。

兩種使用模式:互動式(osqueryi,在單機上即時查詢,適合調查與驗證查詢語句)與常駐式(osqueryd,依排程持續收集並送往日誌平台,適合建立基準線與偵測異動)。

它跨 Windows、macOS、Linux 使用同一套查詢語法——這點在混合環境中價值很高,不必為每個平台維護不同的盤點腳本。

On the day a major component vulnerability lands, the first question is always "which of our machines are affected?" Most organisations cannot answer, or need days to.

osquery turns that into one SQL statement. Running on each host, it represents system state — running processes, installed packages, listening ports, user accounts, scheduled tasks, kernel modules, browser extensions — as queryable tables. To find machines running a particular nginx version, you SELECT it.

Two modes: interactive (osqueryi, live queries on one machine, good for investigation and for working out the query) and daemon (osqueryd, scheduled collection shipped to a logging platform, good for baselines and change detection).

It uses the same query syntax across Windows, macOS and Linux — valuable in mixed estates, since you no longer maintain a separate inventory script per platform.

什麼時候用得上When to use it

- 漏洞公開當天的曝險盤點:查詢特定套件與版本,在數分鐘內產出受影響清單。本期的 Java、nginx、PHP 三則通報都是這個場景
- 建立並監控基準線:常駐模式下記錄系統狀態變化,新增的排程工作、新出現的監聽埠、使用者帳號異動都看得到
- 事件調查的佐證:與專門的鑑識工具(如本站介紹過的 Velociraptor)互補——osquery 擅長持續的狀態盤點,鑑識工具擅長深入單一事件
- 合規佐證:產出實際安裝清單,而非依賴採購紀錄

建議從互動式開始:先在幾台主機上用 osqueryi 把查詢語句寫對,再決定要不要納入排程。

- Same-day exposure triage: query for a package and version and produce an affected-systems list within minutes. All three of this issue's Java, nginx and PHP advisories are this scenario
- Baseline and change monitoring: in daemon mode, record state over time so new scheduled tasks, newly listening ports, and account changes become visible
- Investigation support: complementary to dedicated forensics tooling such as Velociraptor, featured earlier here — osquery is strong at continuous state inventory, forensics tools at depth on a single incident
- Compliance evidence: report what is actually installed rather than relying on procurement records

Start interactive: get the query right with osqueryi on a few hosts before deciding what belongs on a schedule.

注意事項Caveats

它回報狀態,不做判斷。 osquery 不會告訴您某個版本有沒有漏洞——它只告訴您裝的是哪個版本。要把結果轉成風險判斷,仍需搭配漏洞資料來源。

另外兩點實務考量:部分查詢(特別是全檔案系統掃描類)在大型主機上相當耗資源,排程前應先量測;而常駐模式送出的資料需要有地方接收與查詢,若沒有既有的日誌平台,這部分的建置成本不可忽略。

授權為 Apache-2.0 OR GPL-2.0-only 雙授權,使用者可擇一,商業使用選 Apache-2.0 即無 copyleft 疑慮。

It reports state; it does not judge. osquery will not tell you whether a version is vulnerable — only which version is installed. Turning results into risk requires pairing it with a vulnerability data source.

Two practical notes: some queries (full filesystem scans especially) are resource-hungry on large hosts, so measure before scheduling them; and daemon-mode output needs somewhere to land and be queried — if you have no existing logging platform, that build-out cost is not trivial.

Licensing is dual, Apache-2.0 OR GPL-2.0-only, with the user choosing; selecting Apache-2.0 removes any copyleft concern for commercial use.

安裝Install

brew install osquery
# 或自官方套件庫安裝 / or install from the official package repository
# https://osquery.io/downloads

快速上手Quick start

# 互動式查詢
osqueryi

# 查出已安裝的特定套件與版本(Linux)
SELECT name, version FROM deb_packages WHERE name LIKE 'nginx%';

# 查出正在監聽的連接埠及其程序
SELECT DISTINCT p.name, p.path, l.port, l.protocol
FROM listening_ports l JOIN processes p ON l.pid = p.pid
WHERE l.address = '0.0.0.0';

# 查出開機自動啟動項目(跨平台)
SELECT name, path, source, status FROM startup_items;

出現在Featured in