2023-01-26 16:40:54 +00:00
|
|
|
-- Suspicious calls to systemctl(event-based)
|
|
|
|
--
|
|
|
|
-- refs:
|
|
|
|
-- * https://attack.mitre.org/techniques/T1543/002/ (Create or Modify System Process: Systemd Service)
|
|
|
|
--
|
2024-09-23 15:07:53 +00:00
|
|
|
-- tags: transient process events extra
|
2023-01-26 16:40:54 +00:00
|
|
|
-- platform: linux
|
|
|
|
-- interval: 300
|
2023-02-24 21:30:17 +00:00
|
|
|
SELECT -- Child
|
2023-01-26 16:40:54 +00:00
|
|
|
pe.path AS p0_path,
|
|
|
|
REGEX_MATCH (pe.path, '.*/(.*)', 1) AS p0_name,
|
|
|
|
TRIM(pe.cmdline) AS p0_cmd,
|
2023-02-01 18:55:55 +00:00
|
|
|
pe.cwd AS p0_cwd,
|
2023-05-16 21:18:39 +00:00
|
|
|
pe.time AS p0_time,
|
2023-01-26 16:40:54 +00:00
|
|
|
pe.pid AS p0_pid,
|
|
|
|
p.cgroup_path AS p0_cgroup,
|
|
|
|
-- Parent
|
|
|
|
pe.parent AS p1_pid,
|
|
|
|
p1.cgroup_path AS p1_cgroup,
|
|
|
|
TRIM(COALESCE(p1.cmdline, pe1.cmdline)) AS p1_cmd,
|
|
|
|
COALESCE(p1.path, pe1.path) AS p1_path,
|
|
|
|
COALESCE(p_hash1.sha256, pe_hash1.sha256) AS p1_hash,
|
2023-02-02 22:58:19 +00:00
|
|
|
REGEX_MATCH (COALESCE(p1.path, pe1.path), '.*/(.*)', 1) AS p1_name,
|
2023-01-26 16:40:54 +00:00
|
|
|
-- Grandparent
|
|
|
|
COALESCE(p1.parent, pe1.parent) AS p2_pid,
|
|
|
|
COALESCE(p1_p2.cgroup_path, pe1_p2.cgroup_path) AS p2_cgroup,
|
2023-02-02 22:58:19 +00:00
|
|
|
TRIM(
|
|
|
|
COALESCE(p1_p2.cmdline, pe1_p2.cmdline, pe1_pe2.cmdline)
|
|
|
|
) AS p2_cmd,
|
2023-01-26 16:40:54 +00:00
|
|
|
COALESCE(p1_p2.path, pe1_p2.path, pe1_pe2.path) AS p2_path,
|
2023-02-02 22:58:19 +00:00
|
|
|
COALESCE(
|
|
|
|
p1_p2_hash.path,
|
|
|
|
pe1_p2_hash.path,
|
|
|
|
pe1_pe2_hash.path
|
|
|
|
) AS p2_hash,
|
|
|
|
REGEX_MATCH (
|
|
|
|
COALESCE(p1_p2.path, pe1_p2.path, pe1_pe2.path),
|
|
|
|
'.*/(.*)',
|
|
|
|
1
|
|
|
|
) AS p2_name,
|
2023-01-26 16:40:54 +00:00
|
|
|
-- Exception key
|
2023-02-02 22:58:19 +00:00
|
|
|
REGEX_MATCH (pe.path, '.*/(.*)', 1) || ',' || MIN(pe.euid, 500) || ',' || REGEX_MATCH (COALESCE(p1.path, pe1.path), '.*/(.*)', 1) || ',' || REGEX_MATCH (
|
|
|
|
COALESCE(p1_p2.path, pe1_p2.path, pe1_pe2.path),
|
|
|
|
'.*/(.*)',
|
|
|
|
1
|
|
|
|
) AS exception_key
|
2023-01-26 16:40:54 +00:00
|
|
|
FROM
|
|
|
|
process_events pe,
|
|
|
|
uptime
|
2023-02-24 21:30:17 +00:00
|
|
|
LEFT JOIN processes p ON pe.pid = p.pid -- Parents (via two paths)
|
2023-01-26 16:40:54 +00:00
|
|
|
LEFT JOIN processes p1 ON pe.parent = p1.pid
|
|
|
|
LEFT JOIN hash p_hash1 ON p1.path = p_hash1.path
|
2023-02-02 22:58:19 +00:00
|
|
|
LEFT JOIN process_events pe1 ON pe.parent = pe1.pid
|
|
|
|
AND pe1.cmdline != ''
|
2023-02-24 21:30:17 +00:00
|
|
|
LEFT JOIN hash pe_hash1 ON pe1.path = pe_hash1.path -- Grandparents (via 3 paths)
|
2023-01-26 16:40:54 +00:00
|
|
|
LEFT JOIN processes p1_p2 ON p1.parent = p1_p2.pid -- Current grandparent via parent processes
|
|
|
|
LEFT JOIN processes pe1_p2 ON pe1.parent = pe1_p2.pid -- Current grandparent via parent events
|
2023-02-02 22:58:19 +00:00
|
|
|
LEFT JOIN process_events pe1_pe2 ON pe1.parent = pe1_p2.pid
|
|
|
|
AND pe1_pe2.cmdline != '' -- Past grandparent via parent events
|
2023-01-26 16:40:54 +00:00
|
|
|
LEFT JOIN hash p1_p2_hash ON p1_p2.path = p1_p2_hash.path
|
|
|
|
LEFT JOIN hash pe1_p2_hash ON pe1_p2.path = pe1_p2_hash.path
|
|
|
|
LEFT JOIN hash pe1_pe2_hash ON pe1_pe2.path = pe1_pe2_hash.path
|
|
|
|
WHERE
|
2023-02-24 21:30:17 +00:00
|
|
|
uptime.total_seconds > 30 -- NOTE: The remainder of this query is synced with unexpected-fetcher-parents
|
2023-01-26 16:40:54 +00:00
|
|
|
AND pe.path IN (
|
|
|
|
'/usr/bin/systemctl',
|
|
|
|
'/bin/systemctl',
|
|
|
|
'/sbin/systemctl'
|
|
|
|
)
|
|
|
|
AND pe.cmdline != ''
|
|
|
|
AND pe.time > (strftime('%s', 'now') -300)
|
|
|
|
AND NOT exception_key IN (
|
2023-02-24 21:30:17 +00:00
|
|
|
'systemctl,0,,containerd-shim-runc-v2',
|
2023-08-15 22:13:06 +00:00
|
|
|
'systemctl,0,apt-helper,',
|
|
|
|
'systemctl,0,bash,pacman',
|
2023-01-26 16:40:54 +00:00
|
|
|
'systemctl,0,dash,logrotate',
|
2023-08-15 22:13:06 +00:00
|
|
|
'systemctl,0,kubeadm,containerd-shim-runc-v2',
|
2023-01-26 16:40:54 +00:00
|
|
|
'systemctl,0,pacman,pacman',
|
|
|
|
'systemctl,0,pacman,sudo',
|
2024-09-23 15:07:53 +00:00
|
|
|
'systemctl,500,snap,update-notifier',
|
2023-02-24 21:30:17 +00:00
|
|
|
'systemctl,0,snapd,systemd',
|
2023-01-26 16:40:54 +00:00
|
|
|
'systemctl,0,tailscaled,',
|
2024-06-26 00:48:09 +00:00
|
|
|
'systemctl,500,strace,bash',
|
2023-02-24 21:30:17 +00:00
|
|
|
'systemctl,127,snap,systemd',
|
2024-08-27 22:40:43 +00:00
|
|
|
'systemctl,120,systemd-executor,systemd',
|
|
|
|
'systemctl,128,systemd,systemd',
|
2024-03-07 21:33:01 +00:00
|
|
|
'systemctl,500,snapd,systemd',
|
|
|
|
'systemctl,500,systemd,systemd',
|
2023-02-24 21:30:17 +00:00
|
|
|
'systemctl,500,bash,gnome-terminal-server',
|
|
|
|
'systemctl,500,snap,systemd',
|
2023-08-15 22:13:06 +00:00
|
|
|
'systemctl,500,systemd,',
|
|
|
|
'systemctl,500,zsh,tmux'
|
2023-01-26 16:40:54 +00:00
|
|
|
)
|
|
|
|
AND NOT p0_cmd IN (
|
|
|
|
'/bin/systemctl is-enabled -q whoopsie.path',
|
|
|
|
'/bin/systemctl -q is-enabled whoopsie.path',
|
2023-02-17 16:57:23 +00:00
|
|
|
'/bin/systemctl --quiet is-enabled whoopsie.path',
|
2023-01-26 16:40:54 +00:00
|
|
|
'/bin/systemctl stop --no-block nvidia-persistenced',
|
2024-09-23 15:07:53 +00:00
|
|
|
'/usr/bin/systemctl is-system-running',
|
|
|
|
'systemctl is-system-running',
|
2023-01-26 16:40:54 +00:00
|
|
|
'/sbin/runlevel',
|
|
|
|
'systemctl is-active systemd-resolved.service',
|
|
|
|
'systemctl is-enabled power-profiles-daemon.service',
|
|
|
|
'systemctl is-enabled snapd.apparmor',
|
|
|
|
'systemctl is-enabled systemd-rfkill.service',
|
|
|
|
'systemctl is-enabled systemd-rfkill.socket',
|
|
|
|
'systemctl is-enabled tlp.service',
|
|
|
|
'systemctl kill -s HUP rsyslog.service',
|
|
|
|
'systemctl -p LoadState show cups.service',
|
|
|
|
'systemctl -q is-enabled whoopsie',
|
|
|
|
'systemctl --quiet is-enabled cups.service',
|
2023-02-17 16:57:23 +00:00
|
|
|
'systemctl reboot',
|
2023-01-26 16:40:54 +00:00
|
|
|
'systemctl restart cups.service',
|
2024-08-27 22:40:43 +00:00
|
|
|
'systemctl restart NetworkManager.service',
|
|
|
|
'systemctl restart reflector.service',
|
2023-01-26 16:40:54 +00:00
|
|
|
'systemctl status kubelet',
|
|
|
|
'systemctl stop kubelet',
|
2024-08-27 22:40:43 +00:00
|
|
|
'systemctl stop libvirtd.service',
|
|
|
|
'systemctl --system daemon-reexec',
|
2023-01-26 16:40:54 +00:00
|
|
|
'systemctl --user import-environment DISPLAY XAUTHORITY',
|
2024-08-27 22:40:43 +00:00
|
|
|
'/usr/bin/systemctl try-reload-or-restart dbus',
|
2024-09-23 15:07:53 +00:00
|
|
|
'/usr/bin/systemctl --user is-active slack',
|
|
|
|
'systemctl --user is-active slack'
|
2023-02-24 21:30:17 +00:00
|
|
|
) -- apt-helper form
|
2023-01-26 16:40:54 +00:00
|
|
|
AND NOT p0_cmd LIKE '%systemctl is-active -q %.service'
|
|
|
|
AND NOT p0_cmd LIKE '%systemctl show --property=%'
|
2024-08-27 22:40:43 +00:00
|
|
|
AND NOT p0_cmd LIKE '/usr/bin/systemctl --user set-environment%'
|
2023-01-26 16:40:54 +00:00
|
|
|
AND NOT p0_cmd LIKE '%systemctl % snap-kubectl-%.mount'
|
|
|
|
AND NOT p0_cmd LIKE '%systemctl --user set-environment DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%/bus'
|
|
|
|
GROUP BY
|
|
|
|
pe.pid
|