iac.htora.dev · security templates

Home/Templates/Ask every host the same questions

Ask every host the same questions

osquery on Linux, macOS, and Windows, logging changes to accounts, keys, persistence, listening ports, and drivers.

Ansible

Why bother

During an incident the questions are the same on every machine: who can sign in, what starts on its own, what listens on the network, what is running that should not be. Answering them host by host is slow, and the answer from before the incident is usually gone. The template has osquery ask them on a schedule and log what changed, so the history is already there.

How you know it worked

Add a local user on a test host. Within an hour the results log shows the user as added.

How it works

1osquery asks on a schedulethe same questions on every host2It compares with the last answerand logs only what appeared or went away3The results log keeps the historyso the day before is still there4Your forwarder carries it offsyslog on Linux and macOS (Standard)
Step by step, from the thing that happens to the result you want.

Set it up

Pick your platform, then open the level you want. Each level is complete on its own. Read the comments in the files as you go. Anything with a real consequence is explained on the line where it happens.

Ansible. osquery from a checksummed .deb or .rpm, with a scheduled configuration

What you need first

  • An inventory, and an account you can become root with.
  • The osquery .deb or .rpm URL for your hosts from osquery.io/downloads, and its SHA-256. The templates stop without them.

What it creates

  • osqueryd, with flags and a schedule in /etc/osquery
  • Questions about users, SSH keys, listening ports, cron, locally added systemd units, kernel modules, and programs running from a deleted file
  • Results written to /var/log/osquery and to syslog
  • Sudo rules and setuid programs added, questions kept as data, and the configuration checked by osquery before it is used (Standard)

The code

Quick startReadyone file, 133 lines
One playbook. A checksummed install, seven scheduled questions, and results to a file and to syslog.
Change these first
  • osquery_package_url: "" playbook.yml, line 27
  • osquery_package_sha256: "" playbook.yml, line 28
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini playbook.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini playbook.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini playbook.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download playbook.yml
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/host-query/linux/t0
playbook.yml
Open playbook.yml as plain text
StandardReadyfive files, 170 lines
A role. Adds sudo rules and setuid programs, questions kept as data you extend, a configuration check before use, and a read-back.
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini site.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini site.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini site.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download all five files as a zip
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/host-query/linux/t1
roles/osquery/defaults/main.yml
Open main.yml as plain text
roles/osquery/handlers/main.yml
Open main.yml as plain text
roles/osquery/meta/main.yml
Open main.yml as plain text
roles/osquery/tasks/main.yml
Open main.yml as plain text
site.yml
Open site.yml as plain text
HardenedReadythree files, 161 lines
Standard's roles, unchanged, with your settings in settings.yml. Rules refuse settings that remove or replace a default question, leave a query empty, set an out-of-range interval, or stop logging to file and syslog. A daily check-mode run reports drift, and ansible-lint runs on the production profile.
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini site.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini site.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini site.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download all three files as a zip
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/host-query/linux/t2
guard.yml
Open guard.yml as plain text
settings.yml
Open settings.yml as plain text
site.yml
Open site.yml as plain text

How to check it worked

osqueryi --json 'select version from osquery_info' answers, and /var/log/osquery/osqueryd.results.log fills as each question first runs.

What catches people

Each osquery release is a new package, so neither template has a default URL. Pin the version you tested, and change it on purpose.

Questions run first when their interval first passes, so an hourly question logs nothing for up to an hour after install.

How to undo it

Remove the osquery package. The results log stays until you delete /var/log/osquery.

What it costs

Nothing to license. Small CPU cost per question, and a results log that grows with the number of changes.

Good to know

  • Results are differences. A new SSH key, a new scheduled task, or a new listening port appears as one added row, which is easy to alert on.
  • osqueryi on the host answers the same questions on demand, which is how a responder checks one machine during triage.

Registry 0.23.0. Built 2026-11-17.

Made by Habibullah Tora. Source on GitHub. Code under the MIT licence, writing under CC BY 4.0.