SQL-powered forensic investigation and system interrogation using osquery to query operating systems as relational databases. Enables rapid evidence collection, threat hunting, and incident response across Linux, macOS, and Windows endpoints. Use when: (1) Investigating security incidents and collecting forensic artifacts, (2) Threat hunting across endpoints for suspicious activity, (3) Analyzing running processes, network connections, and persistence mechanisms, (4) Collecting system state during incident response, (5) Querying file hashes, user activity, and system configuration for compromise indicators, (6) Building detection queries for continuous monitoring with osqueryd.
Instrucciones de origen · Vista previa de solo lectura
name
forensics-osquery
description
SQL-powered forensic investigation and system interrogation using osquery to query operating systems as relational databases. Enables rapid evidence collection, threat hunting, and incident response across Linux, macOS, and Windows endpoints. Use when: (1) Investigating security incidents and collecting forensic artifacts, (2) Threat hunting across endpoints for suspicious activity, (3) Analyzing running processes, network connections, and persistence mechanisms, (4) Collecting system state during incident response, (5) Querying file hashes, user activity, and system configuration for compromise indicators, (6) Building detection queries for continuous monitoring with osqueryd.
osquery transforms operating systems into queryable relational databases, enabling security analysts to investigate compromises using SQL rather than traditional CLI tools. This skill provides forensic investigation workflows, common detection queries, and incident response patterns for rapid evidence collection across Linux, macOS, and Windows endpoints.
Core capabilities:
SQL-based system interrogation for process, network, file, and user analysis
Live system analysis without deploying heavyweight forensic tools
Threat hunting queries mapped to MITRE ATT&CK techniques
Scheduled monitoring with osqueryd for continuous detection
Integration with SIEM and incident response platforms
Quick Start
Interactive Investigation (osqueryi)
# Launch interactive shell
osqueryi
# Check running processes
SELECT pid, name, path, cmdline, uid FROM processes WHERE name LIKE '%suspicious%';
# Identify listening network services
SELECT DISTINCT processes.name, listening_ports.port, listening_ports.address, processes.pid, processes.path
FROM listening_ports
JOIN processes USING (pid)
WHERE listening_ports.address != '127.0.0.1';
# Find processes with deleted executables (potential malware)
SELECT name, path, pid, cmdline FROM processes WHERE on_disk = 0;
# Check persistence mechanisms (Linux/macOS cron jobs)
SELECT command, path FROM crontab;
One-Liner Forensic Queries
# Single query execution
osqueryi --json "SELECT * FROM logged_in_users;"# Export query results for analysis
osqueryi --json "SELECT * FROM processes;" > processes_snapshot.json
# Check for suspicious kernel modules (Linux)
osqueryi --line "SELECT name, used_by, status FROM kernel_modules WHERE name NOT IN (SELECT name FROM known_good_modules);"
Core Workflows
Workflow 1: Initial Incident Response Triage
For rapid assessment of potentially compromised systems:
Progress:
[ ] 1. Collect running processes and command lines
[ ] 2. Identify network connections and listening ports
[ ] 3. Check user accounts and recent logins
[ ] 4. Examine persistence mechanisms (scheduled tasks, startup items)
[ ] 5. Review suspicious file modifications and executions
[ ] 6. Document findings with timestamps and process ancestry
[ ] 7. Export evidence to JSON for preservation
Work through each step systematically. Use bundled triage script for automated collection.
-- Example: Hunt for credential dumping (T1003)SELECT p.pid, p.name, p.cmdline, p.path, p.parent, pm.permissions
FROM processes p
JOIN process_memory_map pm ON p.pid = pm.pid
WHERE p.name IN ('mimikatz.exe', 'procdump.exe', 'pwdump.exe')
OR p.cmdline LIKE'%sekurlsa%'OR (pm.path ='/etc/shadow'OR pm.path LIKE'%SAM%');
Analyze Results
Review process ancestry and command-line arguments
Check file hashes against threat intelligence
Document timeline of suspicious activity
Pivot Investigation
Use findings to identify additional indicators
Query related artifacts (network connections, files, registry)
Expand hunt scope if compromise confirmed
Workflow 3: Persistence Mechanism Analysis
Detecting persistence across platforms:
Linux/macOS Persistence:
-- Cron jobsSELECT*FROM crontab;
-- Systemd services (Linux)SELECT name, path, status, source FROM systemd_units WHERE source !='/usr/lib/systemd/system';
-- Launch Agents/Daemons (macOS)SELECT name, path, program, run_at_load FROM launchd WHERE run_at_load =1;
-- Bash profile modificationsSELECT*FROM file WHERE path IN ('/etc/profile', '/etc/bash.bashrc', '/home/*/.bashrc', '/home/*/.bash_profile');
Windows Persistence:
-- Registry Run keysSELECT key, name, path, type FROM registry WHERE key LIKE'%Run%'OR key LIKE'%RunOnce%';
-- Scheduled tasksSELECT name, action, path, enabled FROM scheduled_tasks WHERE enabled =1;
-- ServicesSELECT name, display_name, status, path, start_type FROM services WHERE start_type ='AUTO_START';
-- WMI event consumersSELECT name, command_line_template FROM wmi_cli_event_consumers;
Review results for:
Unusual executables in startup locations
Base64-encoded or obfuscated commands
Executables in temporary or user-writable directories
Recently modified persistence mechanisms
Workflow 4: Network Connection Analysis
Investigating suspicious network activity:
-- Active network connections with process detailsSELECT p.name, p.pid, p.path, p.cmdline, ps.remote_address, ps.remote_port, ps.state
FROM processes p
JOIN process_open_sockets ps ON p.pid = ps.pid
WHERE ps.remote_address NOTIN ('127.0.0.1', '::1', '0.0.0.0')
ORDERBY ps.remote_port;
-- Listening ports mapped to processesSELECTDISTINCT p.name, lp.port, lp.address, lp.protocol, p.path, p.cmdline
FROM listening_ports lp
LEFTJOIN processes p ON lp.pid = p.pid
WHERE lp.address NOTIN ('127.0.0.1', '::1')
ORDERBY lp.port;
-- DNS lookups (requires events table or process monitoring)SELECT name, domains, pid FROM dns_resolvers;
Review destination IPs against threat intelligence
Correlate connections with process execution timeline
Validate legitimate business purpose for connections
Workflow 5: File System Forensics
Analyzing file modifications and suspicious files:
-- Recently modified files in sensitive locationsSELECT path, filename, size, mtime, ctime, md5, sha256
FROM hash
WHERE path LIKE'/etc/%'OR path LIKE'/tmp/%'OR path LIKE'C:\Windows\Temp\%'AND mtime > (strftime('%s', 'now') -86400); -- Last 24 hours-- Executable files in unusual locationsSELECT path, filename, size, md5, sha256
FROM hash
WHERE (path LIKE'/tmp/%'OR path LIKE'/var/tmp/%'OR path LIKE'C:\Users\%\AppData\%')
AND (filename LIKE'%.exe'OR filename LIKE'%.sh'OR filename LIKE'%.py');
-- SUID/SGID binaries (Linux/macOS) - potential privilege escalationSELECT path, filename, mode, uid, gid
FROM file
WHERE (mode LIKE'%4%'OR mode LIKE'%2%')
AND path LIKE'/usr/%'OR path LIKE'/bin/%';
File analysis workflow:
Identify suspicious files by location and timestamp
Extract file hashes (MD5, SHA256) for threat intel lookup
Review file permissions and ownership
Check for living-off-the-land binaries (LOLBins) abuse
Document file metadata for forensic timeline
Forensic Query Patterns
Pattern 1: Process Analysis
Standard process investigation queries:
-- Processes with network connectionsSELECT p.pid, p.name, p.path, p.cmdline, ps.remote_address, ps.remote_port
FROM processes p
JOIN process_open_sockets ps ON p.pid = ps.pid;
-- Process tree (parent-child relationships)SELECT p1.pid, p1.name AS process, p1.cmdline,
p2.pid AS parent_pid, p2.name AS parent_name, p2.cmdline AS parent_cmdline
FROM processes p1
LEFTJOIN processes p2 ON p1.parent = p2.pid;
-- High-privilege processes (UID 0 / SYSTEM)SELECT pid, name, path, cmdline, uid, euid FROM processes WHERE uid =0OR euid =0;
Pattern 2: User Activity Monitoring
Track user accounts and authentication:
-- Currently logged in usersSELECTuser, tty, host, time, pid FROM logged_in_users;
-- User accounts with login shellsSELECT username, uid, gid, shell, directory FROM users WHERE shell NOTLIKE'%nologin%';
-- Recent authentication events (requires auditd/Windows Event Log integration)SELECT*FROM user_events WHEREtime> (strftime('%s', 'now') -3600);
-- Sudo usage history (Linux/macOS)SELECT username, command, timeFROM sudo_usage_history ORDERBYtimeDESC LIMIT 50;
Pattern 3: System Configuration Review
Identify configuration changes:
-- Kernel configuration and parameters (Linux)SELECT name, valueFROM kernel_info;
SELECT path, key, valueFROM sysctl WHERE key LIKE'kernel.%';
-- Installed packages (detect unauthorized software)SELECT name, version, install_time FROM deb_packages ORDERBY install_time DESC LIMIT 20; -- Debian/UbuntuSELECT name, version, install_time FROM rpm_packages ORDERBY install_time DESC LIMIT 20; -- RHEL/CentOS-- System informationSELECT hostname, computer_name, local_hostname FROM system_info;
Security Considerations
Sensitive Data Handling: osquery can access sensitive system information (password hashes, private keys, process memory). Limit access to forensic analysts and incident responders. Export query results to encrypted storage. Sanitize logs before sharing with third parties.
Access Control: Requires root/administrator privileges on investigated systems. Use dedicated forensic user accounts with audit logging. Restrict osqueryd configuration files (osquery.conf) to prevent query tampering. Implement least-privilege access to query results.
Audit Logging: Log all osquery executions for forensic chain-of-custody. Record analyst username, timestamp, queries executed, and systems queried. Maintain immutable audit logs for compliance and legal requirements. Use osqueryd --audit flag for detailed logging.
Compliance: osquery supports NIST SP 800-53 AU (Audit and Accountability) controls and NIST Cybersecurity Framework detection capabilities. Enables evidence collection for GDPR data breach investigations (Article 33). Query results constitute forensic evidence - maintain integrity and chain-of-custody.
Safe Defaults: Use read-only queries during investigations to avoid system modification. Test complex queries in lab environments before production use. Monitor osqueryd resource consumption to prevent denial of service. Disable dangerous tables (e.g., curl, yara) in osqueryd configurations unless explicitly needed.
Bundled Resources
Scripts
scripts/osquery_triage.sh - Automated triage collection script for rapid incident response
scripts/osquery_hunt.py - Threat hunting query executor with MITRE ATT&CK mapping
scripts/parse_osquery_json.py - Parse and analyze osquery JSON output
scripts/osquery_to_timeline.py - Generate forensic timelines from osquery results
References
references/table-guide.md - Comprehensive osquery table reference for forensic investigations
references/mitre-attack-queries.md - Pre-built queries mapped to MITRE ATT&CK techniques
references/platform-differences.md - Platform-specific tables and query variations (Linux/macOS/Windows)
references/osqueryd-deployment.md - Deploy osqueryd for continuous monitoring and fleet management
Assets
assets/osquery.conf - Production osqueryd configuration template for security monitoring
assets/forensic-packs/ - Query packs for incident response scenarios
-- Check web server processes with suspicious child processesSELECT p1.name AS webserver, p1.pid, p1.cmdline,
p2.name AS child, p2.cmdline AS child_cmdline
FROM processes p1
JOIN processes p2 ON p1.pid = p2.parent
WHERE p1.name IN ('httpd', 'nginx', 'apache2', 'w3wp.exe')
AND p2.name IN ('bash', 'sh', 'cmd.exe', 'powershell.exe', 'perl', 'python');
-- Files in web directories with recent modificationsSELECT path, filename, mtime, md5, sha256
FROM hash
WHERE path LIKE'/var/www/%'OR path LIKE'C:\inetpub\wwwroot\%'AND (filename LIKE'%.php'OR filename LIKE'%.asp'OR filename LIKE'%.jsp')
AND mtime > (strftime('%s', 'now') -604800); -- Last 7 days
Scenario 2: Ransomware Investigation
Identify ransomware indicators:
-- Processes writing to many files rapidly (potential encryption activity)SELECT p.name, p.pid, p.cmdline, COUNT(fe.path) AS files_modified
FROM processes p
JOIN file_events fe ON p.pid = fe.pid
WHERE fe.action ='WRITE'AND fe.time > (strftime('%s', 'now') -300)
GROUPBY p.pid
HAVING files_modified >100;
-- Look for ransom note filesSELECT path, filename FROM file
WHERE filename LIKE'%DECRYPT%'OR filename LIKE'%README%'OR filename LIKE'%RANSOM%';
-- Check for file extension changes (encrypted files)SELECT path, filename FROM file
WHERE filename LIKE'%.locked'OR filename LIKE'%.encrypted'OR filename LIKE'%.crypto';
Scenario 3: Privilege Escalation Detection
Detect privilege escalation attempts:
-- Processes running as root from non-standard pathsSELECT pid, name, path, cmdline, uid, euid FROM processes
WHERE (uid =0OR euid =0)
AND path NOTLIKE'/usr/%'AND path NOTLIKE'/sbin/%'AND path NOTLIKE'/bin/%'AND path NOTLIKE'C:\Windows\%';
-- SUID binaries (Linux/macOS)SELECT path, filename, uid, gid FROM file
WHERE mode LIKE'%4%'AND path NOTIN (SELECT path FROM known_suid_binaries);
-- Sudoers file modificationsSELECT*FROM file WHERE path ='/etc/sudoers'AND mtime > (strftime('%s', 'now') -86400);
Integration Points
SIEM Integration
Forward osqueryd logs to SIEM platforms:
Splunk: Use Splunk Add-on for osquery or universal forwarder
Elasticsearch: Configure osqueryd to output JSON logs, ingest with Filebeat
Sentinel: Stream logs via Azure Monitor Agent or custom ingestion
QRadar: Use QRadar osquery app or log source extension