Unexpected Pcap User Linux

osquery
Find root-run processes which link against libpcap
Author

Chainguard

Published

January 29, 2025

Description

ODK (osquery-defense-kit) is unique in that the queries are designed to be used as part of a production detection & response pipeline. The detection queries are formulated to return zero rows during normal expected behavior, so that they may be configured to generate alerts when rows are returned.

Query

-- Find root-run processes which link against libpcap
--
-- WARNING: This check consumes an unusual amount of system memory (up to 225MB)
--
-- references:
--   * https://attack.mitre.org/techniques/T1205/001/ (Traffic Signaling: Port Knocking)
--
-- platform: linux
-- tags: persistent state process sniffer
SELECT
  pmm.pid,
  p.uid,
  p.gid,
  pmm.path AS lib_path,
  p.path AS child_path,
  p.name AS child_name,
  p.cmdline AS child_cmd,
  p.cwd AS child_cwd,
  h.sha256 AS child_sha256,
  pp.path AS parent_path,
  pp.name AS parent_name,
  pp.cmdline AS parent_cmd,
  pp.cwd AS parent_cwd,
  pp.euid AS parent_euid,
  ph.sha256 AS parent_sha256
  -- Using processes is much faster than process_memory_map
FROM
  processes p
  LEFT JOIN process_memory_map pmm ON p.pid = pmm.pid
  LEFT JOIN hash h ON p.path = h.path
  LEFT JOIN processes pp ON p.parent = pp.pid
  LEFT JOIN hash AS ph ON pp.path = ph.path
WHERE
  p.euid = 0
  AND pmm.path LIKE '%libpcap%'
  AND child_path NOT LIKE '/nix/store/%-systemd-%/bin/udevadm'
  AND child_path NOT LIKE '/nix/store/%-systemd-%/lib/systemd/systemd%'
  AND child_path NOT LIKE '/nix/store/%/bin/nix'
  AND child_path NOT LIKE '/System/Library/%'
  AND child_path NOT LIKE '/usr/local/kolide-k2/bin/osqueryd-updates/%/osqueryd'
  AND child_path NOT IN (
    '/run/current-system/systemd/lib/systemd/systemd',
    '/usr/bin/libvirtd',
    '/usr/bin/tcpdump',
    '/usr/libexec/UserEventAgent',
    '/usr/sbin/cupsd',
    '/usr/sbin/systemstats'
  )
  AND child_cmd NOT IN (
    '/nix/var/nix/profiles/default/bin/nix-daemon',
    '/run/current-system/systemd/lib/systemd/systemd'
  )
  AND child_cmd NOT LIKE '/usr/bin/python3 -s%/usr/sbin/firewalld%'
GROUP BY
  p.pid

tags: SStagSS

Reference

https://github.com/chainguard-dev/osquery-defense-kit