by Rachel Cipkins, Stevens Institute of Technology
During my summer internship at Trail of Bits I worked on osquery, the massively popular open-source endpoint monitoring agent used for intrusion detection, threat hunting, operational monitoring, and many other functions. Available for Windows, macOS, Linux, and FreeBSD, osquery exposes an operating system as a high-performance relational database, which allows you to write SQL-based queries to explore operating system data.
My initial task was to port osquery’s startup_items table to Linux. Since the
startup_items table is only available on macOS and Windows, we wanted to port it to Linux while keeping the current schema. Porting to Linux is complicated, though; like macOS and Windows, Linux has an indefinite number of locations for startup items, so I needed to parse the data in each location and insert it into the table. This would have been fairly simple, but we couldn’t directly parse the data for the systemd location. Ultimately, we added systemd support to the table through the D-Bus API and created a brand-new table for systemd units.
A note on the startup_items table…
Startup items are applications and binaries that run when your system is booted up, but “startup items” is also an abstract concept indicating some set of locations and subsystems that you want to enumerate. There are two primary types of startup item locations on Linux: user-specific locations and system-specific locations. The user-specific locations include
~/.config/autostart-scripts, while the system-specific locations include XDG and SysV. These all contain user-specific desktop entries and scripts, respectively.
You can see an example of the
startup_items table for Linux at https://asciinema.org/a/354125.
Systemd and D-Bus
Systemd is not as simple to implement as the other locations; it’s an init system and service manager for Linux that uses units to represent resources. Units are resources that the system knows how to manage and operate, and these are defined in unit files. We are not able to parse these unit files directly, as we did for files relating to the other locations, and we could only get the information we needed by using an API.
So we turned to D-Bus, an interprocess communication system that allows us to interact directly with systemd to extract information about startup items. Now, systemd does have its own bus library, sd-bus, but we still prefer D-Bus because:
- osquery uses CMake as its build system; and systemd does not. Meanwhile, D-Bus does use CMake, so it was simpler to integrate with osquery.
- D-Bus can be used to query things other than systemd.
Here are some of the API calls we used from D-Bus to extract the information we needed:
// Connection dbus_error_init(&error); conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error); ... // Message message = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "ListUnits"); ... // Reply reply = dbus_connection_send_with_reply_and_block(conn, message, -1, &error);
And here’s an example of the
startup_items table with systemd:
The systemd units table rises again
A few years ago the osquery community determined there was a need for some sort of table related to systemd. We restarted that conversation after successfully implementing D-Bus, and it was agreed that our unit-based table was the right direction. There are many different types of systemd units—some of the most common ones are service, socket, and device—and the systemd unit table has a column that differentiates them.
This example shows the many distinct types of units currently on my computer and narrows the results by type. Here we have executed a query for all of the services: https://asciinema.org/a/354127.
There are three states associated with each unit:
- load_state indicates whether or not the unit has been properly loaded onto the system.
- active_state indicates whether or not the unit is activated.
- sub_state is an additional state specific to each type of unit.
Here you can see all the active units on the system: https://asciinema.org/a/354130.
What’s next for D-Bus and osquery?
D-Bus will allow us to query a lot of other things on osquery, including desktop environment configurations for GNOME and KDE, and network devices. Be sure to check out the new
startup_items and systemd_units tables once they are merged and keep an eye out for new D-Bus features on osquery.
Want to learn more about osquery or contribute to the project? Check it out here!
Awesome work! Really excited to see the new startup_items and systemd_units tables merged.