Deploying Osquery for Endpoint Monitoring
When to Use
Use this skill when:
- Deploying osquery across Windows, macOS, and Linux endpoints for fleet-wide visibility
- Building threat hunting queries using osquery's SQL interface
- Monitoring endpoint compliance (installed software, open ports, running services)
- Integrating osquery data with SIEM or Kolide/Fleet for centralized management
Do not use for real-time alerting (osquery is periodic/on-demand; use EDR for real-time).
Prerequisites
- Osquery package for target OS (https://osquery.io/downloads)
- Fleet management server (Kolide Fleet or FleetDM) for enterprise deployment
- TLS certificates for secure agent-to-server communication
- Log aggregation pipeline (Filebeat, Fluentd) for osquery result logs
Workflow
Step 1: Install Osquery
# Ubuntu/Debian
export OSQUERY_KEY=1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys $OSQUERY_KEY
add-apt-repository 'deb [arch=amd64] https://pkg.osquery.io/deb deb main'
apt-get update && apt-get install osquery -y
# Windows (MSI)
# Download from https://osquery.io/downloads/official
msiexec /i osquery-5.12.1.msi /quiet
# macOS
brew install osquery
Step 2: Configure Osquery
// /etc/osquery/osquery.conf (Linux/macOS) or C:\ProgramData\osquery\osquery.conf
{
"options": {
"config_plugin": "filesystem",
"logger_plugin": "filesystem",
"logger_path": "/var/log/osquery",
"disable_logging": "false",
"schedule_splay_percent": "10",
"events_expiry": "3600",
"verbose": "false",
"worker_threads": "2",
"enable_monitor": "true",
"disable_events": "false",
"disable_audit": "false",
"audit_allow_config": "true",
"host_identifier": "hostname",
"enable_syslog": "true"
},
"schedule": {
"process_monitor": {
"query": "SELECT pid, name, path, cmdline, uid, parent FROM processes WHERE
"interval": 300,
"description": "Detect processes running without on-disk binary (fileless)"
},
"listening_ports": {
"query": "SELECT DISTINCT p.name, p.path, lp.port, lp.protocol, lp.address FROM listening_ports lp JOIN processes p ON lp.pid = p.pid WHERE lp.port != 0;",
"interval": 600,
"description": "Monitor listening network ports"
},
"persistence_check": {
"query": "SELECT name, path, source FROM startup_items;",
"interval": 3600,
"description": "Monitor persistence mechanisms"
},
"installed_packages": {
"query": "SELECT name, version, source FROM deb_packages;",
"interval": 86400,
"description": "Daily software inventory"
},
"users_and_groups": {
"query": "SELECT u.username, u.uid, u.gid, u.shell, u.directory FROM users u WHERE u.uid >= 1000;",
"interval": 3600
},
"crontab_monitor": {
"query": "SELECT * FROM crontab;",
"interval": 3600,
"description": "Monitor scheduled tasks"
},
"suid_binaries": {
"query": "SELECT path, username, permissions FROM suid_bin;",
"interval": 86400,
"description": "Detect SUID binaries"
}
},
"packs": {
"incident-response": "/usr/share/osquery/packs/incident-response.conf",
"ossec-rootkit": "/usr/share/osquery/packs/ossec-rootkit.conf",
"vuln-management": "/usr/share/osquery/packs/vuln-management.conf"
}
}
Step 3: Threat Hunting Queries
-- Detect processes with no on-disk binary (potential fileless malware)
SELECT pid, name, path, cmdline FROM processes WHERE
-- Find listening ports not associated with known services
SELECT lp.port, lp.protocol, p.name, p.path
FROM listening_ports lp JOIN processes p ON lp.pid = p.pid
WHERE lp.port NOT IN (22, 80, 443, 3306, 5432);
-- Detect unauthorized SSH keys
SELECT * FROM authorized_keys WHERE NOT key LIKE '%admin-team%';
-- Find recently modified system binaries
SELECT path, mtime, size FROM file
WHERE path LIKE '/usr/bin/%' AND mtime > (strftime('%s', 'now') - 86400);
-- Detect processes connecting to external IPs
SELECT DISTINCT p.name, p.path, pn.remote_address, pn.remote_port
FROM process_open_sockets pn JOIN processes p ON pn.pid = p.pid
WHERE pn.remote_address NOT LIKE '10.%'
AND pn.remote_address NOT LIKE '172.16.%'
AND pn.remote_address NOT LIKE '192.168.%'
AND pn.remote_address != '127.0.0.1'
AND pn.remote_address != '0.0.0.0';
-- Windows: Detect unsigned running executables
SELECT p.name, p.path, a.result AS signature_status
FROM processes p JOIN authenticode a ON p.path = a.path
WHERE a.result != 'trusted';
Step 4: Deploy FleetDM for Centralized Management
# FleetDM provides centralized osquery management
# Deploy FleetDM server, configure agents to report to it
# Agents use TLS enrollment and config from Fleet
# Agent configuration for Fleet:
# --tls_hostname=fleet.corp.com
# --tls_server_certs=/etc/osquery/fleet.pem
# --enroll_secret_path=/etc/osquery/enroll_secret
Key Concepts
| Term |
Definition |
| Osquery |
Open-source endpoint agent that exposes OS state as SQL tables for querying |
| Schedule |
Periodic queries that run at defined intervals and log results |
| Pack |
Collection of related queries grouped for specific use cases (IR, compliance) |
| FleetDM |
Open-source osquery fleet management platform |
| Differential Results |
Osquery logs only changes between query executions, reducing data volume |
Tools & Systems
- Osquery: https://osquery.io/ - endpoint visibility agent
- FleetDM: https://fleetdm.com/ - centralized fleet management
- Kolide: Cloud-based osquery management with Slack integration
- osquery-go: Go client library for osquery extensions
Common Pitfalls
- Query performance: Complex queries with large table scans impact endpoint performance. Use WHERE clauses and test query cost with
EXPLAIN.
- Schedule intervals too aggressive: Running heavy queries every 60 seconds causes CPU spikes. Use 300-3600 second intervals for most queries.
- Not using differential mode: Without differential logging, osquery logs all results every interval. Differential mode logs only changes.
- Missing event tables: Some osquery tables require events framework enabled (process_events, socket_events). Enable with
--disable_events=false.
1---2name: deploying-osquery-for-endpoint-monitoring3description: Deploys and configures osquery for real-time endpoint monitoring using SQL-based queries to inspect running processes, open ports, installed software, and system configuration. Use when building visibility into endpoint state, threat hunting across fleet, or implementing compliance monitoring. Activates for requests involving osquery deployment, endpoint visibility, fleet management, or SQL-based endpoint querying.4license: Apache-2.05---6# Deploying Osquery for Endpoint Monitoring
7
8## When to Use
9
10Use this skill when:
11- Deploying osquery across Windows, macOS, and Linux endpoints for fleet-wide visibility
12- Building threat hunting queries using osquery's SQL interface
13- Monitoring endpoint compliance (installed software, open ports, running services)
14- Integrating osquery data with SIEM or Kolide/Fleet for centralized management
15
16**Do not use** for real-time alerting (osquery is periodic/on-demand; use EDR for real-time).
17
18## Prerequisites
19
20- Osquery package for target OS (https://osquery.io/downloads)
21- Fleet management server (Kolide Fleet or FleetDM) for enterprise deployment
22- TLS certificates for secure agent-to-server communication
23- Log aggregation pipeline (Filebeat, Fluentd) for osquery result logs
24
25## Workflow
26
27### Step 1: Install Osquery
28
29```bash
30# Ubuntu/Debian
31export OSQUERY_KEY=1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B
32apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys $OSQUERY_KEY
33add-apt-repository 'deb [arch=amd64] https://pkg.osquery.io/deb deb main'
34apt-get update && apt-get install osquery -y
35
36# Windows (MSI)
37# Download from https://osquery.io/downloads/official
38msiexec /i osquery-5.12.1.msi /quiet
39
40# macOS
41brew install osquery
42```
43
44### Step 2: Configure Osquery
45
46```json
47// /etc/osquery/osquery.conf (Linux/macOS) or C:\ProgramData\osquery\osquery.conf
48{
49 "options": {
50 "config_plugin": "filesystem",
51 "logger_plugin": "filesystem",
52 "logger_path": "/var/log/osquery",
53 "disable_logging": "false",
54 "schedule_splay_percent": "10",
55 "events_expiry": "3600",
56 "verbose": "false",
57 "worker_threads": "2",
58 "enable_monitor": "true",
59 "disable_events": "false",
60 "disable_audit": "false",
61 "audit_allow_config": "true",
62 "host_identifier": "hostname",
63 "enable_syslog": "true"
64 },
65 "schedule": {
66 "process_monitor": {
67 "query": "SELECT pid, name, path, cmdline, uid, parent FROM processes WHERE on_disk = 0;",
68 "interval": 300,
69 "description": "Detect processes running without on-disk binary (fileless)"
70 },
71 "listening_ports": {
72 "query": "SELECT DISTINCT p.name, p.path, lp.port, lp.protocol, lp.address FROM listening_ports lp JOIN processes p ON lp.pid = p.pid WHERE lp.port != 0;",
73 "interval": 600,
74 "description": "Monitor listening network ports"
75 },
76 "persistence_check": {
77 "query": "SELECT name, path, source FROM startup_items;",
78 "interval": 3600,
79 "description": "Monitor persistence mechanisms"
80 },
81 "installed_packages": {
82 "query": "SELECT name, version, source FROM deb_packages;",
83 "interval": 86400,
84 "description": "Daily software inventory"
85 },
86 "users_and_groups": {
87 "query": "SELECT u.username, u.uid, u.gid, u.shell, u.directory FROM users u WHERE u.uid >= 1000;",
88 "interval": 3600
89 },
90 "crontab_monitor": {
91 "query": "SELECT * FROM crontab;",
92 "interval": 3600,
93 "description": "Monitor scheduled tasks"
94 },
95 "suid_binaries": {
96 "query": "SELECT path, username, permissions FROM suid_bin;",
97 "interval": 86400,
98 "description": "Detect SUID binaries"
99 }
100 },
101 "packs": {
102 "incident-response": "/usr/share/osquery/packs/incident-response.conf",
103 "ossec-rootkit": "/usr/share/osquery/packs/ossec-rootkit.conf",
104 "vuln-management": "/usr/share/osquery/packs/vuln-management.conf"
105 }
106}
107```
108
109### Step 3: Threat Hunting Queries
110
111```sql
112-- Detect processes with no on-disk binary (potential fileless malware)
113SELECT pid, name, path, cmdline FROM processes WHERE on_disk = 0;
114
115-- Find listening ports not associated with known services
116SELECT lp.port, lp.protocol, p.name, p.path
117FROM listening_ports lp JOIN processes p ON lp.pid = p.pid
118WHERE lp.port NOT IN (22, 80, 443, 3306, 5432);
119
120-- Detect unauthorized SSH keys
121SELECT * FROM authorized_keys WHERE NOT key LIKE '%admin-team%';
122
123-- Find recently modified system binaries
124SELECT path, mtime, size FROM file
125WHERE path LIKE '/usr/bin/%' AND mtime > (strftime('%s', 'now') - 86400);
126
127-- Detect processes connecting to external IPs
128SELECT DISTINCT p.name, p.path, pn.remote_address, pn.remote_port
129FROM process_open_sockets pn JOIN processes p ON pn.pid = p.pid
130WHERE pn.remote_address NOT LIKE '10.%'
131 AND pn.remote_address NOT LIKE '172.16.%'
132 AND pn.remote_address NOT LIKE '192.168.%'
133 AND pn.remote_address != '127.0.0.1'
134 AND pn.remote_address != '0.0.0.0';
135
136-- Windows: Detect unsigned running executables
137SELECT p.name, p.path, a.result AS signature_status
138FROM processes p JOIN authenticode a ON p.path = a.path
139WHERE a.result != 'trusted';
140```
141
142### Step 4: Deploy FleetDM for Centralized Management
143
144```bash
145# FleetDM provides centralized osquery management
146# Deploy FleetDM server, configure agents to report to it
147# Agents use TLS enrollment and config from Fleet
148
149# Agent configuration for Fleet:
150# --tls_hostname=fleet.corp.com
151# --tls_server_certs=/etc/osquery/fleet.pem
152# --enroll_secret_path=/etc/osquery/enroll_secret
153```
154
155## Key Concepts
156
157| Term | Definition |
158|------|-----------|
159| **Osquery** | Open-source endpoint agent that exposes OS state as SQL tables for querying |
160| **Schedule** | Periodic queries that run at defined intervals and log results |
161| **Pack** | Collection of related queries grouped for specific use cases (IR, compliance) |
162| **FleetDM** | Open-source osquery fleet management platform |
163| **Differential Results** | Osquery logs only changes between query executions, reducing data volume |
164
165## Tools & Systems
166
167- **Osquery**: https://osquery.io/ - endpoint visibility agent
168- **FleetDM**: https://fleetdm.com/ - centralized fleet management
169- **Kolide**: Cloud-based osquery management with Slack integration
170- **osquery-go**: Go client library for osquery extensions
171
172## Common Pitfalls
173
174- **Query performance**: Complex queries with large table scans impact endpoint performance. Use WHERE clauses and test query cost with `EXPLAIN`.
175- **Schedule intervals too aggressive**: Running heavy queries every 60 seconds causes CPU spikes. Use 300-3600 second intervals for most queries.
176- **Not using differential mode**: Without differential logging, osquery logs all results every interval. Differential mode logs only changes.
177- **Missing event tables**: Some osquery tables require events framework enabled (process_events, socket_events). Enable with `--disable_events=false`.