2022-10-12 01:53:36 +00:00
|
|
|
-- Find ssh sessions that are hiding from 'w'/'who'
|
2022-10-14 18:19:13 +00:00
|
|
|
--
|
2022-10-17 22:46:00 +00:00
|
|
|
-- false positives:
|
|
|
|
-- * ssh-driven automation which disables the terminal, such as Znapzend
|
|
|
|
--
|
2022-10-19 20:56:32 +00:00
|
|
|
-- references:
|
|
|
|
-- * https://attack.mitre.org/techniques/T1021/004/ (Remote Services: SSH)
|
|
|
|
-- * https://attack.mitre.org/techniques/T1564/ (Hide Artifacts)
|
|
|
|
--
|
2022-10-14 18:19:13 +00:00
|
|
|
-- tags: transient process state
|
|
|
|
-- platform: posix
|
2022-10-19 20:19:53 +00:00
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
p.pid,
|
2022-10-17 23:06:17 +00:00
|
|
|
p.name,
|
|
|
|
p.cmdline AS cmd,
|
2023-02-01 18:55:55 +00:00
|
|
|
p.cwd,
|
2022-10-17 23:06:17 +00:00
|
|
|
cp.name AS child_name,
|
|
|
|
cp.cmdline AS child_cmd,
|
|
|
|
gcp.name AS grandchild_name,
|
|
|
|
gcp.cmdline AS grandchild_cmd,
|
|
|
|
GROUP_CONCAT(DISTINCT pof.path) AS open_files
|
2022-10-19 20:19:53 +00:00
|
|
|
FROM
|
|
|
|
processes p
|
2022-10-17 23:06:17 +00:00
|
|
|
LEFT JOIN process_open_files pof ON p.pid = pof.pid
|
|
|
|
LEFT JOIN processes cp ON p.pid = cp.parent
|
|
|
|
LEFT JOIN processes gcp ON cp.pid = gcp.parent
|
2022-10-19 20:19:53 +00:00
|
|
|
WHERE
|
|
|
|
p.name = 'sshd'
|
|
|
|
GROUP BY
|
|
|
|
p.pid
|
2022-10-17 23:06:17 +00:00
|
|
|
)
|
2022-10-19 20:19:53 +00:00
|
|
|
WHERE
|
|
|
|
(
|
2022-10-17 23:06:17 +00:00
|
|
|
INSTR(cmd, '@notty') > 0
|
|
|
|
OR (
|
|
|
|
open_files != '/dev/null'
|
|
|
|
AND INSTR(open_files, '/dev/ptmx') = 0
|
2022-10-17 22:46:00 +00:00
|
|
|
)
|
2022-10-17 23:06:17 +00:00
|
|
|
)
|
2022-10-18 15:40:42 +00:00
|
|
|
-- You must specifically check for NULL here, or risk inadvertently filtering everything out.
|
|
|
|
AND (
|
2022-10-19 20:19:53 +00:00
|
|
|
grandchild_name IS NULL
|
|
|
|
OR grandchild_name != 'zfs'
|
2022-10-18 15:40:42 +00:00
|
|
|
)
|
2022-11-16 21:52:39 +00:00
|
|
|
AND child_name IS NOT NULL
|
|
|
|
AND child_name NOT IN ('', 'zfs')
|
2022-11-10 16:04:48 +00:00
|
|
|
AND cmd != 'sshd: docker@notty'
|