Process and socket auditing with osquery

Osquery has the ability to leverage the Linux Audit System to record process executions and network connections in near real-time on Linux and MacOS systems. Although these auditing features are extremely powerful for recording the activity from a host, they may introduce additional computational overhead and greatly increase the number of log events generated by osquery.

Enabling these auditing features requires additional configuration to osquery. The process_events and socket_events tables use the same event-based architecture as the File Integrity Monitoring (FIM). To read more about how event-based tables are created and designed, check out the osquery Table Pubsub Framework. On all supported platforms, process events are abstracted into the process_events table. Similarly, socket events are abstracted into the socket_events table.

To collect process events add a query like:

  1. SELECT * FROM process_events;

to your query schedule, or to a query pack.

Linux process auditing

osquery uses the Linux Audit System to collect and process audit events from the kernel. It accomplishes this by monitoring the execve() syscall. Auditd should not be running when using osquery's process auditing, as it will conflict with osqueryd over access to the audit netlink socket. You should also ensure auditd is not configured to start at boot.

The only prerequisite for using osquery's auditing functionality on Linux is that you must use a kernel version that contains the audit functionality. Most kernels over version 2.6 have this capability.

There is no requirement to install auditd or libaudit. Osquery only uses the audit features that exist in the kernel.

A sample log entry from process_events may look something like this:

  1. {
  2. "action": "added",
  3. "columns": {
  4. "uid": "0",
  5. "time": "1527895541",
  6. "pid": "30219",
  7. "path": "/usr/bin/curl",
  8. "auid": "1000",
  9. "cmdline": "curl google.com",
  10. "ctime": "1503452096",
  11. "cwd": "",
  12. "egid": "0",
  13. "euid": "0",
  14. "gid": "0",
  15. "parent": ""
  16. },
  17. "unixTime": 1527895550,
  18. "hostIdentifier": "vagrant",
  19. "name": "process_events"
  20. }

To better understand how this works, let's walk through 4 configuration options. These flags can be set at the command line or placed into the osquery.flags file.

  • —disable_audit=false by default this is set to true and prevents osquery from opening the kernel audit's netlink socket. By setting it to false, we are telling osquery that we want to enable auditing functionality.
  • —auditallow_config=true by default this is set to false and prevents osquery from making changes to the audit configuration settings. These changes include adding/removing rules, setting the global enable flags, and adjusting performance and rate parameters. Unless you plan to set all of those things manually, you should leave this as true. If you are configuring audit, using a control binary, or /etc/audit.conf, your osquery _may override your settings.
  • —audit_persist=true but default this is true and instructs osquery to 'regain' the audit netlink socket if another process also accesses it. However, you should do your best to ensure there will be no other program running which is attempting to access the audit netlink socket.
  • —audit_allow_process_events=true this flag indicates that you would like to record process events

Linux socket auditing

Osquery can also be used to record network connections by enabling socket_events. This table uses the syscalls bind() and connect() to gather information about network connections. This table is not automatically enabled when process_events are enabled because it can introduce considerable load on the system.

To enable socket events, use the —audit_allow_sockets flag.

A sample socket_event log entry looks like this:

  1. {
  2. "action": "added",
  3. "columns": {
  4. "time": "1527895541",
  5. "success": "1",
  6. "remote_port": "80",
  7. "action": "connect",
  8. "auid": "1000",
  9. "family": "2",
  10. "local_address": "",
  11. "local_port": "0",
  12. "path": "/usr/bin/curl",
  13. "pid": "30220",
  14. "remote_address": "172.217.164.110"
  15. },
  16. "unixTime": 1527895545,
  17. "hostIdentifier": "vagrant",
  18. "name": "socket_events"
  19. }

If you would like to log UNIX domain sockets use the hidden flag: —audit_allow_unix. This will put considerable strain on the system as many default actions use domain sockets. You will also need to explicitly select the socket column from the socket_events table.

Troubleshooting Auditing on Linux

There are a few different methods to ensure you have configured auditing correctly.

  • Ensure you are supplied all of the necessary flags mentioned above in either a command-line argument or in your flagfile.
  • Verify auditd is not running if it is installed on the system.
  • Run auditctl -s if the binary is present on your system and verify that enable is not set to zero and the pid corresponds to a process for osquery
  • Verify that your osquery configuration has a query to SELECT from the process_events and/or socket_events tables
  • You may also run auditing using osqueryi as root:
  1. $ osqueryi --audit_allow_config=true --audit_allow_sockets=true --audit_persist=true --disable_audit=false --events_expiry=1 --events_max=50000 --logger_plugin=filesystem --disable_events=false

If you would like to debug the raw audit events as osqueryd sees them, use the hidden flag —audit_debug. This will print all of the RAW audit lines to osquery's stdout.

User events

On Linux, a companion table called user_events is included that provides several authentication-based events. If you are enabling process auditing it should be trivial to also include this table.

macOS process auditing

osquery has support for OpenBSM audit on Darwin platforms. This feature is already enabled on all macOS installations but doesn't audit process execution or the root user with default settings. To start process auditing on macOS, edit the audit_control file in /etc/security/. An example configuration is provided below but the important flags are: ex, pc, argv, and arge. The ex flag will log exec events while pc logs exec, fork, and exit. If you don't need fork and exit you may leave that flag out however in future, getting parent pid may require fork. If you care about getting the arguments and environment variables you also need argv and arge. More about these flags can be found here. Note that it might require a reboot of the system for these new flags to take effect. audit -s should restart the system but your mileage may vary.

  1. #
  2. # $P4: //depot/projects/trustedbsd/openbsm/etc/audit_control#8 $
  3. #
  4. dir:/var/audit
  5. flags:ex,pc,ap,aa,lo,ad
  6. minfree:5
  7. naflags:no
  8. policy:cnt,argv,arge
  9. filesz:2M
  10. expire-after:10M
  11. superuser-set-sflags-mask:has_authenticated,has_console_access
  12. superuser-clear-sflags-mask:has_authenticated,has_console_access
  13. member-set-sflags-mask:
  14. member-clear-sflags-mask:has_authenticated

osquery events optimization

This section provides a brief overview of common and recommended optimizations for event-based tables. These optimizations also apply to the FIM events.

  • —events_optimize=true apply optimizations when SELECTing from events-based tables, enabled by default.
  • —events_expiry the lifetime of buffered events in seconds with a default value of 86000.
  • —events_max the maximum number of events to store in the buffer before expiring them with a default value of 1000.The goal of optimizations are to protect the running process and system from impacting performance. By default these are all enabled, which is good for configuration and performance, but may introduce inconsistencies on highly-stressed systems using process auditing.

Optimizations work best when SELECTing often from event-based tables. Otherwise the events are in a buffered state. When an event-based table is selected within the daemon, the backing storage maintaining event data is cleared according to the —event_expiry lifetime. Setting this value to 1 will auto-clear events whenever a SELECT is performed against the table, reducing all impact of the buffer.