Install Osquery on Debian 10 Buster

0
2252

In this guide, we are going to learn how to install osquery on Debian 10 Buster. Osquery is an opensource tool that queries an operating system as if it were a relational database. It leverage SQL-like queries to gather Operating System information for performance, security, compliance audit analysis. It runs on multiple platforms such as Linux, FreeBSD, MacOS, Windows systems.

Install Osquery on Debian 10 Buster

Install Osquery APT Repository

The default Debian 10 repositories does not contain the osquery package. However, osquery publishes an apt repository for each stable release.

Import and install the osquery repository signing keys.

sudo apt update -y && sudo apt install gnupg2 vim -y
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B

Next install osquery APT repo on Debian 10 Buster.

sudo apt install software-properties-common -y
echo 'deb [arch=amd64] https://pkg.osquery.io/deb deb main' | sudo tee /etc/apt/sources.list.d/osquery.list

Update your system packages

sudo apt update

Install Osquery on Debian 10 Buster

Once the update is done, install osquery.

sudo apt install osquery

Components of osquery

Osquery package installs three basic components;

  • osqueryctl – This is an osquery helper script for testing osquery configuration/deployment as well as managing the osqueryd service.
  • osqueryd – is an osquery daemon for scheduling queries and recording the changes in the state of OS.
  • osqueryi – is an osquery interactive shell. From the shell, you can run various queries to explore that state of your OS.

In order to learn the usage of the commands above, you can pass the -h/--help option. For example, to obtain osqueryctl help;

osqueryctl -h
Usage: /usr/bin/osqueryctl {clean|config-check|start|stop|status|restart}

For example to start, stop and restart osqueryd using osqueryctl, run the commands;

osqueryctl start osqueryd
osqueryctl stop osqueryd
osqueryctl restart osqueryd

Executing Osquery SQL queries

Osquery can be run in standalone mode using the osqueryi or it can be run as service using osqueryd. In this guide, we are going to focus on how to use the osquery interactive shell to query various system activities.

Running osquery in standalone mode

When osqueryi is run without any arguments, it takes you to the interactive shell prompt;

osqueryi
Using a virtual database. Need help, type '.help'
osquery>

You can obtain help within the osquery shell prompt by typing .help on the shell prompt.

Welcome to the osquery shell. Please explore your OS!
You are connected to a transient 'in-memory' virtual database.

.all [TABLE]     Select all from a table
.bail ON|OFF     Stop after hitting an error
.connect PATH    Connect to an osquery extension socket
.disconnect      Disconnect from a connected extension socket
.echo ON|OFF     Turn command echo on or off
.exit            Exit this program
.features        List osquery's features and their statuses
.headers ON|OFF  Turn display of headers on or off
.help            Show this message
.mode MODE       Set output mode where MODE is one of:
                   csv      Comma-separated values
                   column   Left-aligned columns see .width
                   line     One value per line
                   list     Values delimited by .separator string
                   pretty   Pretty printed SQL results (default)
.nullvalue STR   Use STRING in place of NULL values
.print STR...    Print literal STRING
.quit            Exit this program
.schema [TABLE]  Show the CREATE statements
.separator STR   Change separator used by output mode
.socket          Show the local osquery extensions socket path
.show            Show the current values for various settings
.summary         Alias for the show meta command
.tables [TABLE]  List names of tables
.types [SQL]     Show result of getQueryColumns for the given query
.width [NUM1]+   Set column widths for "column" mode
.timer ON|OFF      Turn the CPU timer measurement on or off

osqueryi accepts several meta-commands, prefixed with a dot (.).

With osquery, various OS attributes have been converted into tabular like database concepts. Hence, to list tables from which various system information is stored, run the .tables command. For example;

osquery> .tables
   => acpi_tables
   => apt_sources
   => arp_cache
   => augeas
   ...
   => ssh_configs
   => sudoers
   => suid_bin
   => syslog_events
   => system_controls
   => system_info
   => time
   => ulimit_info
   => uptime
   => usb_devices
   => user_events
   => user_groups
   => user_ssh_keys
   => users
   => yara
   => yara_events
   => yum_sources
osquery>

For example purposes, let us see what is contained on some of the tables, say the sudoers table.

osquery> select * from sudoers;
+----------+----------------------------------------------------------------------------+
| header   | rule_details                                                               |
+----------+----------------------------------------------------------------------------+
| Defaults | env_reset                                                                  |
| Defaults | mail_badpass                                                               |
| Defaults | secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" |
| root     | ALL=(ALL:ALL) ALL                                                          |
| %sudo    | ALL=(ALL:ALL) ALL                                                          |
+----------+----------------------------------------------------------------------------+
osquery>
osquery> select * from sudoers where header like '%root';
+--------+-------------------+
| header | rule_details      |
+--------+-------------------+
| root   | ALL=(ALL:ALL) ALL |
+--------+-------------------+
osquery>

To query only top 5 system users,

osquery> select * from users where uid <= 1000 limit 5;
+-----+-------+------------+------------+----------+-------------+-----------+-------------------+------+
| uid | gid   | uid_signed | gid_signed | username | description | directory | shell             | uuid |
+-----+-------+------------+------------+----------+-------------+-----------+-------------------+------+
| 0   | 0     | 0          | 0          | root     | root        | /root     | /bin/bash         |      |
| 1   | 1     | 1          | 1          | daemon   | daemon      | /usr/sbin | /usr/sbin/nologin |      |
| 2   | 2     | 2          | 2          | bin      | bin         | /bin      | /usr/sbin/nologin |      |
| 3   | 3     | 3          | 3          | sys      | sys         | /dev      | /usr/sbin/nologin |      |
| 4   | 65534 | 4          | 65534      | sync     | sync        | /bin      | /bin/sync         |      |
+-----+-------+------------+------------+----------+-------------+-----------+-------------------+------+
osquery>

To check logged in users;

osquery> select * from logged_in_users where type = 'user';
 +------+------+-------+----------------+------------+------+
 | type | user | tty   | host           | time       | pid  |
 +------+------+-------+----------------+------------+------+
 | user | root | tty1  |                | 1565598621 | 729  |
 | user | amos | pts/0 | 192.168.43.17  | 1565598768 | 851  |
 | user | amos | pts/1 | 192.168.43.162 | 1565602356 | 7712 |
 +------+------+-------+----------------+------------+------+
 osquery>

Check system uptime;

osquery> select * from uptime;
+------+-------+---------+---------+---------------+
| days | hours | minutes | seconds | total_seconds |
+------+-------+---------+---------+---------------+
| 0    | 1     | 10      | 13      | 4213          |
+------+-------+---------+---------+---------------+
osquery>

The view mode can be changed by running the command, .mode MODE where MODE can be line, csv, pretty (default), column, list. For exampl to set the view to line mode;

osquery> .mode line
osquery> select * from load_average;
 period = 1m
average = 0.080000

 period = 5m
average = 0.070000

 period = 15m
average = 0.120000
osquery>

List install packages and display only top 3.

osquery> select * from deb_packages top limit 3;
    name = adduser
 version = 3.118
  source = 
    size = 849
    arch = all
revision = 

    name = adwaita-icon-theme
 version = 3.30.1-1
  source = 
    size = 26804
    arch = all
revision = 1

    name = anacron
 version = 2.3-28
  source = 
    size = 99
    arch = amd64
revision = 28
osquery>

List system processes;

osquery> select pid,name,state,parent from processes order by start_time desc limit 10;
+------+-----------------------------+-------+--------+
| pid  | name                        | state | parent |
+------+-----------------------------+-------+--------+
| 8405 | kworker/0:0-ata_sff         | I     | 2      |
| 8332 | osqueryi                    | R     | 874    |
| 8329 | kworker/0:1-ata_sff         | I     | 2      |
| 8280 | kworker/u2:0-events_unbound | I     | 2      |
| 7726 | bash                        | S     | 7725   |
| 7725 | su                          | S     | 7722   |
| 7721 | sshd                        | S     | 7712   |
| 7722 | bash                        | S     | 7721   |
| 7712 | sshd                        | S     | 456    |
| 7599 | kworker/u2:1-events_unbound | I     | 2      |
+------+-----------------------------+-------+--------+
osquery>

Get system information.

osquery> select hostname,cpu_physical_cores,physical_memory from system_info;
+----------------------+--------------------+-----------------+
| hostname             | cpu_physical_cores | physical_memory |
+----------------------+--------------------+-----------------+
| debian10.example.com | 1                  | 1035452416      |
+----------------------+--------------------+-----------------+
osquery>

Using Osquery Daemon

Just instead of having to run osquery in an interactive mode using the osqueryi, you can configure Osquery to read the queries from the configuration file and save the results on a log file.

osqueryd makes it easy to schedule queries and record OS state changes. The daemon aggregates query results over time and generates logs, which indicate state change according to each query.

Osquery doesn’t installs a configuration file by default. Hence, copy the sample configuration to /etc/osquery directory.

cp /opt/osquery/share/osquery/osquery.example.conf /etc/osquery/osquery.conf

Our final osquery configuration file looks like;

cat /etc/osquery/osquery.conf
{
  // Configure the daemon below:
  "options": {

    // The log directory stores info, warning, and errors.
    // If the daemon uses the 'filesystem' logging retriever then the log_dir
    // will also contain the query results.
    // "logger_path": "/var/log/osquery",

    // Set 'disable_logging' to true to prevent writing any info, warning, error
    // logs. If a logging plugin is selected it will still write query results.
    //"disable_logging": "false",

    // Splay the scheduled interval for queries.
    // This is very helpful to prevent system performance impact when scheduling
    // large numbers of queries that run a smaller or similar intervals.
    //"schedule_splay_percent": "10",
  },

  // Define a schedule of queries:
  "schedule": {
    // This is a simple example query that outputs basic system information.
    "system_info": {
      // The exact query to run.
      "query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
      // The interval in seconds to run this query, not an exact interval.
      "interval": 3600
    }
  },

  // Decorators are normal queries that append data to every query.
  "decorators": {
    "load": [
      "SELECT uuid AS host_uuid FROM system_info;",
      "SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;"
    ]
  },

  // Add default osquery packs or install your own.
  //
  // There are several 'default' packs installed via
  // packages and/or Homebrew.
  //
  // Linux:        /opt/osquery/share/osquery/packs
  // OS X:         /var/osquery/packs
  // Homebrew:     /usr/local/share/osquery/packs
  // make install: {PREFIX}/share/osquery/packs
  //
  "packs": {
    // "osquery-monitoring": "/opt/osquery/share/osquery/packs/osquery-monitoring.conf",
    // "incident-response": "/opt/osquery/share/osquery/packs/incident-response.conf",
    // "it-compliance": "/opt/osquery/share/osquery/packs/it-compliance.conf",
    // "osx-attacks": "/var/osquery/packs/osx-attacks.conf",
    // "vuln-management": "/opt/osquery/share/osquery/packs/vuln-management.conf",
    // "hardware-monitoring": "/opt/osquery/share/osquery/packs/hardware-monitoring.conf",
    // "ossec-rootkit": "/opt/osquery/share/osquery/packs/ossec-rootkit.conf",
    // "windows-hardening": "C:\\Program Files\\osquery\\packs\\windows-hardening.conf",
    // "windows-attacks": "C:\\Program Files\\osquery\\packs\\windows-attacks.conf"
  },

  // Provides feature vectors for osquery to leverage in simple statistical 
  // analysis of results data. 
  //
  // Currently this configuration is only used by Windows in the Powershell
  // Events table, wherein character_frequencies is a list of doubles 
  // representing the aggregate occurrence of character values in Powershell 
  // Scripts. A default configuration is provided which was adapated from 
  // Lee Holmes cobbr project: 
  // https://gist.github.com/cobbr/acbe5cc7a186726d4e309070187beee6
  // 
  "feature_vectors": {
    "character_frequencies": [
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.00045,  0.01798,
      0.0,      0.03111,  0.00063,  0.00027,   0.0,      0.01336,  0.0133,
      0.00128,  0.0027,   0.00655,  0.01932,   0.01917,  0.00432,  0.0045,
      0.00316,  0.00245,  0.00133,  0.001029,  0.00114,  0.000869, 0.00067,
      0.000759, 0.00061,  0.00483,  0.0023,    0.00185,  0.01342,  0.00196,
      0.00035,  0.00092,  0.027875, 0.007465,  0.016265, 0.013995, 0.0490895,
      0.00848,  0.00771,  0.00737,  0.025615,  0.001725, 0.002265, 0.017875,
      0.016005, 0.02533,  0.025295, 0.014375,  0.00109,  0.02732,  0.02658,
      0.037355, 0.011575, 0.00451,  0.005865,  0.003255, 0.005965, 0.00077,
      0.00621,  0.00222,  0.0062,   0.0,       0.00538,  0.00122,  0.027875,
      0.007465, 0.016265, 0.013995, 0.0490895, 0.00848,  0.00771,  0.00737,
      0.025615, 0.001725, 0.002265, 0.017875,  0.016005, 0.02533,  0.025295,
      0.014375, 0.00109,  0.02732,  0.02658,   0.037355, 0.011575, 0.00451,
      0.005865, 0.003255, 0.005965, 0.00077,   0.00771,  0.002379, 0.00766,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0,      0.0,       0.0,      0.0,      0.0,
      0.0,      0.0,      0.0
    ]
  }    
}

Save the configuration file and run the command below to validate it.

osqueryctl config-check

Running osqueryd

systemctl enable --now osqueryd.service

The query logs are not populated to /var/log/osquery/osqueryd.results.log and you can view them in real time using the tail command,

tail -f /var/log/osquery/osqueryd.results.log

That is just about it on our on installing osquery on Debian 10 Buster.

You can read more about osquery here.

Related Tutorials;

How to Install Osquery on Ubuntu 18.04

Install Kolide Fleet Osquery Fleet Manager on Debian 10

Install and Setup Kolide Fleet on Ubuntu 18.04

LEAVE A REPLY

Please enter your comment!
Please enter your name here