2022-10-19 20:56:32 +00:00
|
|
|
-- Find processes that run with a lower effective UID than their parent (event-based)
|
2022-10-14 18:19:13 +00:00
|
|
|
--
|
2023-02-17 15:40:58 +00:00
|
|
|
--
|
2022-10-19 20:56:32 +00:00
|
|
|
-- references:
|
|
|
|
-- * https://attack.mitre.org/techniques/T1548/001/ (Setuid and Setgid)
|
|
|
|
-- * https://cybersecurity.att.com/blogs/labs-research/shikitega-new-stealthy-malware-targeting-linux
|
|
|
|
--
|
|
|
|
-- related:
|
|
|
|
-- * unexpected-privilege-escalation.sql
|
|
|
|
--
|
|
|
|
-- tags: events process escalation
|
2022-11-08 01:36:37 +00:00
|
|
|
-- platform: linux
|
2023-02-17 15:40:58 +00:00
|
|
|
-- interval: 600
|
|
|
|
SELECT file.mode AS p0_binary_mode,
|
2023-02-01 18:55:55 +00:00
|
|
|
pe.cmdline_size AS p0_cmd_size,
|
|
|
|
-- Child
|
|
|
|
pe.path AS p0_path,
|
|
|
|
REGEX_MATCH (pe.path, '.*/(.*)', 1) AS p0_name,
|
|
|
|
TRIM(pe.cmdline) AS p0_cmd,
|
|
|
|
pe.cwd AS p0_cwd,
|
|
|
|
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(p1.euid, pe1.euid) AS p1_euid,
|
|
|
|
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-02-01 18:55:55 +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-02-01 18:55:55 +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-02-17 15:40:58 +00:00
|
|
|
FROM process_events pe
|
2023-02-01 18:55:55 +00:00
|
|
|
LEFT JOIN file ON pe.path = file.path
|
2023-02-17 15:40:58 +00:00
|
|
|
LEFT JOIN processes p ON pe.pid = pe.pid -- Parents (via two paths)
|
2023-02-01 18:55:55 +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-17 15:40:58 +00:00
|
|
|
LEFT JOIN hash pe_hash1 ON pe1.path = pe_hash1.path -- Grandparents (via 3 paths)
|
2023-02-01 18:55:55 +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-02-01 18:55:55 +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
|
2023-02-17 15:40:58 +00:00
|
|
|
WHERE pe.pid IN (
|
|
|
|
SELECT pid
|
|
|
|
FROM process_events
|
|
|
|
WHERE time > (strftime('%s', 'now') -600)
|
|
|
|
AND syscall = "execve"
|
|
|
|
AND euid < 500
|
|
|
|
AND (
|
|
|
|
uid = 0
|
|
|
|
OR euid < uid
|
|
|
|
)
|
|
|
|
)
|
|
|
|
AND pe.time > (strftime('%s', 'now') -600)
|
|
|
|
AND pe.syscall = "execve"
|
|
|
|
AND pe.euid < 500
|
|
|
|
AND (
|
|
|
|
pe.euid < pe.uid
|
|
|
|
OR pe.euid < p1_euid
|
|
|
|
OR pe.euid < pe1.euid
|
|
|
|
)
|
2023-02-01 18:55:55 +00:00
|
|
|
AND pe.path NOT IN (
|
2022-10-27 14:23:15 +00:00
|
|
|
'/bin/ps',
|
|
|
|
'/usr/bin/doas',
|
2022-10-07 20:19:18 +00:00
|
|
|
'/usr/bin/fusermount',
|
|
|
|
'/usr/bin/fusermount3',
|
|
|
|
'/usr/bin/login',
|
2022-12-16 22:37:32 +00:00
|
|
|
'/usr/bin/i3lock',
|
2022-10-07 20:19:18 +00:00
|
|
|
'/usr/bin/sudo',
|
2023-01-14 13:19:26 +00:00
|
|
|
'/usr/bin/nvidia-modprobe',
|
2023-01-06 22:11:24 +00:00
|
|
|
'/usr/bin/unix_chkpwd',
|
2022-11-08 17:59:11 +00:00
|
|
|
'/usr/bin/gpgsm',
|
|
|
|
'/usr/bin/gpgconf',
|
|
|
|
'/usr/bin/gpg',
|
2022-10-27 14:23:15 +00:00
|
|
|
'/usr/bin/top',
|
|
|
|
'/usr/lib/snapd/snap-confine',
|
2022-10-27 20:55:00 +00:00
|
|
|
'/usr/lib/snapd/snap-update-ns',
|
2022-11-03 15:51:54 +00:00
|
|
|
'/opt/1Password/1Password-KeyringHelper',
|
2022-10-27 20:55:00 +00:00
|
|
|
'/usr/lib/systemd/systemd',
|
2022-10-28 23:24:00 +00:00
|
|
|
'/usr/lib/Xorg.wrap',
|
|
|
|
'/usr/lib/xorg/Xorg.wrap'
|
2022-10-07 20:19:18 +00:00
|
|
|
)
|
2023-02-01 18:55:55 +00:00
|
|
|
AND pe.path NOT LIKE '/nix/store/%/bin/sudo'
|
|
|
|
AND pe.path NOT LIKE '/nix/store/%/bin/dhcpcd'
|
|
|
|
AND pe.path NOT LIKE '/snap/snapd/%/usr/lib/snapd/snap-confine'
|
|
|
|
AND pe.path NOT LIKE '/snap/snapd/%/usr/lib/snapd/snap-update-ns'
|
|
|
|
AND NOT p1_cmd IN (
|
2023-01-09 14:34:20 +00:00
|
|
|
'/usr/lib/systemd/systemd --user',
|
|
|
|
'/bin/sh -c /usr/bin/pkexec /usr/share/apport/apport-gtk'
|
|
|
|
)
|
2022-10-07 20:19:18 +00:00
|
|
|
AND NOT (
|
2023-02-01 18:55:55 +00:00
|
|
|
p0_name = 'polkit-agent-helper-1'
|
|
|
|
AND p1_path = '/usr/bin/gnome-shell'
|
2022-10-07 20:19:18 +00:00
|
|
|
)
|
|
|
|
AND NOT (
|
2023-02-01 18:55:55 +00:00
|
|
|
p0_name = 'fusermount3'
|
|
|
|
AND p1_path = '/usr/lib/xdg-document-portal'
|
2022-10-07 20:19:18 +00:00
|
|
|
)
|
2022-10-27 14:23:15 +00:00
|
|
|
AND NOT (
|
2023-02-01 18:55:55 +00:00
|
|
|
p0_name IN ('dash', 'pkexec')
|
|
|
|
AND p1_path = '/usr/bin/update-notifier'
|
2022-10-27 14:23:15 +00:00
|
|
|
)
|
2023-02-01 18:55:55 +00:00
|
|
|
AND NOT p.cgroup_path LIKE '/system.slice/docker-%'
|
2023-02-17 15:40:58 +00:00
|
|
|
AND NOT p1.cgroup_path LIKE '/system.slice/docker-%'
|
|
|
|
GROUP BY pe.pid
|