During an investigation the slow part of using osquery is rarely the SQL itself but remembering which table holds the answer, which operating systems it exists on, and which columns it needs in the WHERE clause before it returns anything. This cheat sheet is a ready reference of osquery tables and read-only queries organised by investigation question: what is running and listening, what will start again after reboot, who has logged in and which keys grant access, and whether a file is what it claims to be. It also covers scheduling queries in packs and keeping them cheap. It is for threat hunters, DFIR analysts and endpoint engineers who already have osquery deployed; for what osquery is, deployment and how it fits beside EDR, see the osquery guide.
This is an independent guide from NHI Mgmt Group, based on the osquery project's public documentation and its table specifications as of November 2026 (latest listed release 5.24.0). It is not sponsored or reviewed by the osquery project. Product features change, so confirm table names and columns against the osquery schema for your installed version.
Key takeaways
- Check the platform first. Tables such as
launchd(macOS),systemd_units(Linux) andscheduled_tasks(Windows) exist on one OS only;crontab,authorized_keysandshell_historyare Linux and macOS only. - Some tables need input.
hashandfilerequire apathordirectoryconstraint, and joins throughusersare how per-user tables such asauthorized_keysreturn every user's rows. - Join on pid and uid. Most investigation queries are a join of
processeswith a socket, file or user table onpidoruid. - Point-in-time tables miss short-lived activity. For processes that start and exit between queries, enable the evented tables (
process_events,bpf_process_events,es_process_events,file_events) after testing their overhead. - Profile before you schedule. Joins across
processesand socket tables are among the more expensive queries; the watchdog restarts a worker that exceeds its limits, so test with the project's profiling tool and set sensible intervals.
How to use this template
- Start interactively: run queries in
osqueryion one host, or as a live query from your fleet manager, before scheduling anything. osquery uses SQLite syntax. - Confirm the schema: run
.tablesand.schema <table>inosqueryi, or check the schema page for your version. Column names change between releases. - Select columns, not
*: name the columns you need; it keeps output readable and reduces cost. - Record evidence: when a query answers an incident question, save the query text, host, time and output to your case record; see the Digital Forensics (DFIR) Guide.
- Promote what works: move proven hunting queries into a pack with a tested interval (see Scheduling queries and packs).
All queries below are read-only. Replace values in square brackets, such as [path] or [sha256], with your own.
Key tables by platform
| Question | Table | Platforms | Notes |
|---|---|---|---|
| What is running? | processes | Linux, macOS, Windows | on_disk = 0 means the binary path no longer exists |
| What is listening? | listening_ports | Linux, macOS, Windows | Join to processes on pid |
| What is connected out? | process_open_sockets | Linux, macOS, Windows | remote_address, remote_port, state |
| What starts at boot or login? | startup_items | Linux, macOS, Windows | Broad view; check the OS-specific tables too |
| macOS persistence | launchd | macOS | LaunchAgents and LaunchDaemons with program, program_arguments, run_at_load |
| Linux persistence | systemd_units, crontab | Linux; crontab also macOS | fragment_path shows the unit file |
| Windows persistence | services, scheduled_tasks, autoexec, registry, wmi_cli_event_consumers | Windows | autoexec aggregates several autostart sources |
| Who can log in with a key? | authorized_keys | Linux, macOS | Join through users on uid |
| Which private keys sit on disk? | user_ssh_keys | Linux, macOS, Windows | encrypted = 0 flags unprotected keys |
| Who is and was logged in? | logged_in_users, last | All; last Linux and macOS | host shows the remote source |
| What did users type? | shell_history | Linux, macOS | time can be 0 where the shell does not record it |
| Is this file what it claims? | hash, file, authenticode | All; authenticode Windows | hash and file require path or directory |
Query basics and joins
Treat each table as a view of current host state. Three patterns cover most investigations.
- Constrain required columns:
SELECT path, sha256 FROM hash WHERE path = '[path]';returns one row; without the constraint the table returns nothing. - Join on keys:
pidlinks processes to sockets;uidlinks users to keys and history;pathlinks files to hashes. - Use LIKE for paths:
%is the SQL wildcard, and for thefiletableWHERE path LIKE '/etc/%'lists one directory level. String comparisons are case sensitive unless the column is declared otherwise (Windows service names and hashes are case-insensitive in the schema).
1 Sample only, based on the public documentation cited. Assess it independently against the current official documentation, test outside production and back up first.
-- Per-user tables: iterate every local user
SELECT u.username, ak.key_file, ak.algorithm, ak.comment
FROM users u
JOIN authorized_keys ak USING (uid);Processes and listening ports
-- Processes whose binary is no longer on disk
SELECT pid, name, path, cmdline, parent, start_time
FROM processes
WHERE on_disk = 0;
-- Listening sockets with the owning process
SELECT DISTINCT p.pid, p.name, p.path, lp.address, lp.port, lp.protocol
FROM listening_ports lp
JOIN processes p USING (pid)
WHERE lp.port != 0;
-- Established outbound connections by process
SELECT p.name, p.path, s.remote_address, s.remote_port, s.state
FROM process_open_sockets s
JOIN processes p USING (pid)
WHERE s.remote_port != 0 AND s.remote_address NOT IN ('127.0.0.1', '::1');
-- Parent and child pairs, to spot unusual parents (for example an office app spawning a shell)
SELECT c.pid, c.name AS child, c.cmdline, p.name AS parent
FROM processes c
JOIN processes p ON c.parent = p.pid;The protocol column is numeric (6 is TCP, 17 is UDP).
Persistence locations
| Platform | Query | ATT&CK technique |
|---|---|---|
| macOS | SELECT label, program, program_arguments, run_at_load, path FROM launchd WHERE run_at_load = '1'; | T1543.001 Launch Agent; T1543.004 Launch Daemon |
| Linux | SELECT id, fragment_path, active_state, unit_file_state FROM systemd_units WHERE id LIKE '%.service' AND fragment_path NOT LIKE '/usr/lib/%'; | T1543.002 Systemd Service |
| Linux, macOS | SELECT minute, hour, command, path FROM crontab; | T1053.003 Cron |
| Windows | SELECT name, action, path, enabled, hidden, next_run_time FROM scheduled_tasks WHERE enabled = 1; | T1053.005 Scheduled Task |
| Windows | SELECT name, display_name, path, start_type, user_account FROM services WHERE start_type = 'AUTO_START'; | T1543.003 Windows Service |
| Windows | SELECT name, path, source FROM autoexec; | T1547.001 Registry Run Keys / Startup Folder |
| Windows | SELECT name, command_line_template, executable_path FROM wmi_cli_event_consumers; | T1546.003 WMI Event Subscription |
| All | SELECT name, path, type, source, status, username FROM startup_items; | TA0003 Persistence (broad view) |
Read results against a known-good baseline from a clean build of the same image; most rows are legitimate. The path filter on systemd_units is an example: adjust it to where your distribution installs vendor units. For Windows hunting depth see How to Hunt for Persistence on Windows, and for Linux see the Linux Persistence Forensics Guide.
Users, logins and keys
-- Interactive logins now, with remote source
SELECT user, type, tty, host, time, pid FROM logged_in_users;
-- Login history (Linux, macOS)
SELECT username, tty, host, time, type FROM last ORDER BY time DESC LIMIT 50;
-- Accounts with a login shell (Linux, macOS)
SELECT uid, username, shell, directory FROM users
WHERE shell NOT LIKE '%nologin' AND shell NOT LIKE '%false';
-- Unencrypted private keys in home directories
SELECT u.username, k.path, k.key_type
FROM users u JOIN user_ssh_keys k USING (uid)
WHERE k.encrypted = 0;
-- Recent shell commands for one user (Linux, macOS)
SELECT u.username, h.time, h.command
FROM users u JOIN shell_history h USING (uid)
WHERE u.username = '[username]';SSH keys are machine credentials: an unexpected entry in authorized_keys maps to ATT&CK T1098.004 SSH Authorized Keys, and an unencrypted private key found by user_ssh_keys is a credential to rotate, not just a finding. See How to find unused GitHub deploy keys and SSH keys and the SSH Key Management Guide. Shell history may contain secrets typed on the command line; restrict who can run that query and treat output as sensitive.
File and hash checks
-- Hash one file
SELECT path, md5, sha256 FROM hash WHERE path = '[path]';
-- Hash every file in a directory
SELECT path, sha256 FROM hash WHERE directory = '[directory]';
-- Hash the binaries of running processes
SELECT p.pid, p.name, p.path, h.sha256
FROM processes p JOIN hash h ON h.path = p.path
WHERE p.on_disk = 1;
-- Search a directory tree for a known bad hash (expensive: scope tightly)
SELECT f.path, h.sha256 FROM file f JOIN hash h USING (path)
WHERE f.directory = '[directory]' AND h.sha256 = '[sha256]';
-- Recently modified files in one directory
SELECT path, size, uid, mode, mtime FROM file
WHERE directory = '/etc' AND mtime > (strftime('%s','now') - 86400);
-- Windows: signature status of a binary
SELECT path, result, subject_name, issuer_name FROM authenticode WHERE path = '[path]';For continuous change tracking use file integrity monitoring instead of repeated scans: configure file_paths categories and query file_events (inotify on Linux, FSEvents on macOS) or ntfs_journal_events on Windows. In file_paths, % matches one level and %% matches recursively. FIM is off by default and needs --disable_events=false plus the matching flag, such as --enable_file_events=true.
Scheduling queries and packs
| Setting | What it does | Guidance |
|---|---|---|
interval | Seconds of daemon run time between executions, maximum 604,800 (one week) | Suspended laptops pause the clock, so daily queries may run less often than expected |
| Differential (default) | Logs only added and removed rows compared with the last run, cached in RocksDB | Best for persistence and listening port changes |
"snapshot": true | Logs the full result set every run | Use for inventories you want complete in each log |
"removed": false | Suppresses removed rows in differential output | Reduces noise where only additions matter |
Pack platform | Limits a pack or query to an OS | Stops Windows-only queries erroring on Linux |
Pack discovery | Runs the pack only where discovery queries return rows | Refreshed every 60 minutes by default (pack_refresh_interval) |
Pack shard | Runs on a percentage of hosts | Pilot new queries on a small shard first |
The project ships example packs in its repository, but they are a starting point: review intervals and remove queries you will not act on.
Performance and the evented tables
- Profile first: the project's
tools/analysis/profile.pyreports utilisation, CPU time, memory and file descriptors per query; its own example rates the processes and listening ports join as the most expensive query in the sample set and suggests running it less often. - Respect the watchdog:
osquerydrestarts a worker that breaches memory or CPU limits (--watchdog_level,--watchdog_memory_limit,--watchdog_utilization_limit, default 10 percent CPU). The project advises setting the level to-1rather than disabling the watchdog outright where limits must be lifted. A scheduled query stopped by the watchdog is denylisted for one day by default; set"denylist": falseonly for queries you must keep running. - Evented process tables:
process_eventsuses Audit on Linux (and OpenBSM on older macOS);bpf_process_eventsneeds Linux kernel 4.18 or newer;es_process_eventsuses EndpointSecurity on macOS 10.15 and newer. On Linux,auditdmust not run alongside osquery's Audit-based tables. Expect more CPU and much more log volume.
Common mistakes
- Querying a table on the wrong OS: a pack mixing
launchdandscheduled_taskswithout aplatformsetting errors or returns nothing on half the fleet. - Missing required constraints:
SELECT * FROM hash;returns no rows; it needspathordirectory. - Recursive hashing at scale: joining
fileandhashover large trees on a schedule triggers the watchdog and slows hosts. - Trusting point-in-time results for short-lived processes: a process that ran for two seconds between queries is invisible without evented tables.
- Treating every result as malicious: without a baseline from a clean image, persistence queries bury analysts in legitimate entries.
- Assuming column names are stable: tables and columns are added between releases; check
.schemaon the installed version before reusing an old query.
Framework mapping
| Framework | Reference | Where these queries help |
|---|---|---|
| NIST CSF 2.0 | DE.CM-09 | Monitoring computing hardware, software and runtime environments for adverse events |
| NIST SP 800-53 Rev. 5 | SI-4 System Monitoring; CM-8 System Component Inventory | Scheduled packs for change detection and inventory |
| MITRE ATT&CK | TA0003 Persistence; T1057 Process Discovery; T1049 System Network Connections Discovery | Persistence queries above; process and socket tables mirror the views attackers use, for defenders |
Practitioner checklist
- Confirm the installed osquery version and run
.schemafor each table before reusing saved queries. - Build baselines of persistence and listening ports from a clean image for each OS you run.
- Schedule persistence queries as differential with tested intervals and a
platformsetting per pack. - Profile every new scheduled query with
profile.pyand pilot it on a smallshard. - Enable evented process tables only after measuring CPU and log volume on representative hosts.
- Rotate any credential an investigation finds, such as unencrypted SSH keys, and record the action in the case.
- Restrict access to queries that return sensitive data, such as
shell_history. - Save query text, host, time and output as evidence for every incident question answered.
Standards and references
- osquery: Documentation
- osquery: Table schema
- osquery: Configuration, schedule and packs
- osquery: Performance safety
- osquery: Process and socket auditing
- osquery: File integrity monitoring
- osquery: Table specifications (GitHub)
- MITRE ATT&CK: Persistence (TA0003)
Related NHI Mgmt Group resources: osquery · How to Hunt for Persistence on Windows · Linux Persistence Forensics Guide · Digital Forensics (DFIR) Guide