---
# host-query / linux / t0 "Quick start"
#
# osquery on every Linux host, asking the same questions on a schedule: who
# can sign in, which SSH keys let them, what listens on the network, what is
# set to start on its own, and what is running from a deleted file. Answers
# are written as changes, so the log shows what appeared and what went away.
#
# osquery publishes a new package with every release, so there is no safe
# default to download. From osquery.io/downloads, pick the .deb or .rpm for
# your hosts, check it, and set the two values under vars.
#
# Run:
#   ansible-playbook -i inventory.ini playbook.yml
#
# Verify:
#   ansible all -i inventory.ini -b -a "osqueryi --json 'select version from osquery_info'"
#   ansible all -i inventory.ini -b -a "tail -n 5 /var/log/osquery/osqueryd.results.log"
#   The results log fills within an hour, when each query first runs.

- name: Ask every host the same questions
  hosts: all
  become: true
  gather_facts: true

  vars:
    osquery_package_url: "" # change me: the .deb or .rpm for these hosts
    osquery_package_sha256: "" # change me: its SHA-256

  tasks:
    - name: Stop if the package or its checksum was not set
      ansible.builtin.assert:
        that:
          - osquery_package_url is match('https://.+\.(deb|rpm)$')
          - osquery_package_sha256 | length == 64
        fail_msg: Set osquery_package_url to the .deb or .rpm, and osquery_package_sha256 to its SHA-256.

    - name: Download osquery, refusing it if the checksum differs
      ansible.builtin.get_url:
        url: "{{ osquery_package_url }}"
        dest: "/var/tmp/{{ osquery_package_url | basename }}"
        checksum: "sha256:{{ osquery_package_sha256 }}"
        mode: "0644"

    - name: Install it (Debian and Ubuntu)
      ansible.builtin.apt:
        deb: "/var/tmp/{{ osquery_package_url | basename }}"
      when: osquery_package_url.endswith('.deb')
      notify: Restart osqueryd

    - name: Install it (Red Hat family)
      ansible.builtin.dnf:
        name: "/var/tmp/{{ osquery_package_url | basename }}"
        state: present
        disable_gpg_check: false
      when: osquery_package_url.endswith('.rpm')
      notify: Restart osqueryd

    - name: Make the log folder
      ansible.builtin.file:
        path: /var/log/osquery
        state: directory
        owner: root
        group: root
        mode: "0750"

    # Results go to a file and to syslog. The host log template forwards
    # syslog off the machine.
    - name: Write the daemon flags
      ansible.builtin.copy:
        dest: /etc/osquery/osquery.flags
        owner: root
        group: root
        mode: "0640"
        content: |
          --config_path=/etc/osquery/osquery.conf
          --logger_plugin=filesystem,syslog
          --logger_path=/var/log/osquery
          --disable_events=true
      notify: Restart osqueryd

    - name: Write the scheduled questions
      ansible.builtin.copy:
        dest: /etc/osquery/osquery.conf
        owner: root
        group: root
        mode: "0640"
        content: |
          {
            "schedule": {
              "users": {
                "query": "SELECT uid, username, shell, directory FROM users;",
                "interval": 3600
              },
              "ssh_authorized_keys": {
                "query": "SELECT u.username, k.key_file, k.algorithm, k.key FROM users u CROSS JOIN authorized_keys k USING (uid);",
                "interval": 3600
              },
              "listening_ports": {
                "query": "SELECT p.name, p.path, lp.port, lp.address, lp.protocol FROM listening_ports lp JOIN processes p USING (pid) WHERE lp.port != 0;",
                "interval": 3600
              },
              "crontab": {
                "query": "SELECT event, minute, hour, command, path FROM crontab;",
                "interval": 3600
              },
              "systemd_units_added_locally": {
                "query": "SELECT id, active_state, fragment_path FROM systemd_units WHERE fragment_path LIKE '/etc/systemd/system/%';",
                "interval": 3600
              },
              "kernel_modules": {
                "query": "SELECT name, size, status FROM kernel_modules;",
                "interval": 3600
              },
              "running_from_deleted_file": {
                "query": "SELECT pid, name, path, cmdline FROM processes WHERE on_disk = 0;",
                "interval": 300
              }
            }
          }
      notify: Restart osqueryd

    - name: Make sure osqueryd is enabled and running
      ansible.builtin.service:
        name: osqueryd
        enabled: true
        state: started

  handlers:
    - name: Restart osqueryd
      ansible.builtin.service:
        name: osqueryd
        state: restarted
