2023-01-04 16:43:26 +00:00
|
|
|
-- Unexpected process that spawns shell processes (event-based)
|
|
|
|
--
|
|
|
|
-- false positives:
|
|
|
|
-- * IDE's
|
|
|
|
--
|
|
|
|
-- references:
|
|
|
|
-- * https://attack.mitre.org/techniques/T1059/ (Command and Scripting Interpreter)
|
|
|
|
-- * https://attack.mitre.org/techniques/T1204/002/ (User Execution: Malicious File)
|
|
|
|
--
|
|
|
|
-- tags: process events
|
|
|
|
-- interval: 300
|
|
|
|
-- platform: posix
|
2023-01-06 15:18:19 +00:00
|
|
|
SELECT
|
2023-01-26 16:40:54 +00:00
|
|
|
-- Child
|
|
|
|
pe.path AS p0_path,
|
|
|
|
REGEX_MATCH (pe.path, '.*/(.*)', 1) AS p0_name,
|
|
|
|
TRIM(pe.cmdline) AS p0_cmd,
|
|
|
|
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,
|
|
|
|
REGEX_MATCH(COALESCE(p1.path, pe1.path), '.*/(.*)', 1) AS p1_name,
|
|
|
|
-- Grandparent
|
|
|
|
COALESCE(p1.parent, pe1.parent) AS p2_pid,
|
|
|
|
COALESCE(p1_p2.cgroup_path, pe1_p2.cgroup_path) AS p2_cgroup,
|
|
|
|
TRIM(COALESCE(p1_p2.cmdline, pe1_p2.cmdline, pe1_pe2.cmdline)) AS p2_cmd,
|
|
|
|
COALESCE(p1_p2.path, pe1_p2.path, pe1_pe2.path) AS p2_path,
|
|
|
|
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,
|
|
|
|
-- Exception key
|
|
|
|
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-06 15:18:19 +00:00
|
|
|
FROM
|
|
|
|
process_events pe
|
|
|
|
LEFT JOIN processes p ON pe.pid = p.pid
|
2023-01-26 16:40:54 +00:00
|
|
|
-- Parents (via two paths)
|
|
|
|
LEFT JOIN processes p1 ON pe.parent = p1.pid
|
|
|
|
LEFT JOIN hash p_hash1 ON p1.path = p_hash1.path
|
|
|
|
LEFT JOIN process_events pe1 ON pe.parent = pe1.pid AND pe1.cmdline != ''
|
|
|
|
LEFT JOIN hash pe_hash1 ON pe1.path = pe_hash1.path
|
|
|
|
|
|
|
|
-- Grandparents (via 3 paths)
|
|
|
|
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
|
|
|
|
LEFT JOIN process_events pe1_pe2 ON pe1.parent = pe1_p2.pid AND pe1_pe2.cmdline != '' -- Past grandparent via parent events
|
|
|
|
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-01-06 15:18:19 +00:00
|
|
|
WHERE
|
2023-01-26 16:40:54 +00:00
|
|
|
pe.time > (strftime('%s', 'now') -300)
|
|
|
|
AND pe.cmdline != ''
|
|
|
|
AND pe.parent > 0
|
|
|
|
AND p0_name IN ('sh', 'fish', 'zsh', 'bash', 'dash')
|
2023-01-06 15:18:19 +00:00
|
|
|
AND NOT (
|
2023-01-26 16:40:54 +00:00
|
|
|
p1_name IN (
|
2023-01-06 15:18:19 +00:00
|
|
|
'abrt-handle-eve',
|
|
|
|
'alacritty',
|
2023-01-30 19:58:47 +00:00
|
|
|
'at-spi-bus-launcher',
|
2023-01-06 15:18:19 +00:00
|
|
|
'bash',
|
|
|
|
'build-script-build',
|
2023-01-23 13:13:04 +00:00
|
|
|
'chainctl',
|
2023-01-06 15:18:19 +00:00
|
|
|
'chezmoi',
|
|
|
|
'clang-11',
|
|
|
|
'code',
|
|
|
|
'Code Helper (Renderer)',
|
|
|
|
'Code - Insiders Helper (Renderer)',
|
|
|
|
'collect2',
|
2023-01-27 01:40:47 +00:00
|
|
|
'com.docker.backend',
|
2023-01-06 15:18:19 +00:00
|
|
|
'conmon',
|
|
|
|
'containerd-shim',
|
2023-01-26 21:30:14 +00:00
|
|
|
'cpptools',
|
2023-01-06 15:18:19 +00:00
|
|
|
'dash',
|
|
|
|
'demoit',
|
|
|
|
'direnv',
|
|
|
|
'doas',
|
2023-01-06 15:36:48 +00:00
|
|
|
'docker-credential-desktop',
|
2023-01-23 13:13:04 +00:00
|
|
|
'docker-credential-gcr',
|
2023-01-30 19:58:47 +00:00
|
|
|
'Docker Desktop',
|
|
|
|
'Emacs-arm64-11',
|
2023-01-06 15:36:48 +00:00
|
|
|
'env',
|
2023-01-06 15:18:19 +00:00
|
|
|
'erl_child_setup',
|
|
|
|
'find',
|
|
|
|
'FinderSyncExtension',
|
|
|
|
'fish',
|
2023-01-26 21:30:14 +00:00
|
|
|
'gatherheaderdoc',
|
2023-01-30 19:58:47 +00:00
|
|
|
'gdm3',
|
2023-01-23 13:13:04 +00:00
|
|
|
'gdm-session-worker',
|
2023-01-27 01:40:47 +00:00
|
|
|
'gdm-x-session',
|
2023-01-06 15:18:19 +00:00
|
|
|
'git',
|
2023-01-23 13:13:04 +00:00
|
|
|
'gke-gcloud-auth-plugin',
|
2023-01-30 19:58:47 +00:00
|
|
|
'gnome-session-binary',
|
|
|
|
'gnome-shell',
|
2023-01-26 21:30:14 +00:00
|
|
|
'gnome-terminal-server',
|
2023-01-06 15:36:48 +00:00
|
|
|
'go',
|
2023-01-06 15:18:19 +00:00
|
|
|
'goland',
|
2023-01-26 21:30:14 +00:00
|
|
|
'gopls',
|
2023-01-06 15:18:19 +00:00
|
|
|
'helm',
|
2023-01-26 21:30:14 +00:00
|
|
|
'HP Diagnose & Fix',
|
2023-01-06 15:18:19 +00:00
|
|
|
'i3bar',
|
|
|
|
'i3blocks',
|
|
|
|
'java',
|
|
|
|
'kitty',
|
|
|
|
'ko',
|
|
|
|
'kubectl',
|
|
|
|
'lightdm',
|
2023-01-23 13:13:04 +00:00
|
|
|
'local-path-provisioner',
|
2023-01-06 15:36:48 +00:00
|
|
|
'login',
|
2023-01-06 15:18:19 +00:00
|
|
|
'make',
|
|
|
|
'monorail',
|
2023-01-23 13:13:04 +00:00
|
|
|
'my_print_defaults',
|
2023-01-06 15:18:19 +00:00
|
|
|
'ninja',
|
|
|
|
'nix',
|
|
|
|
'nix-build',
|
|
|
|
'nix-daemon',
|
2023-01-26 21:30:14 +00:00
|
|
|
'nm-dispatcher',
|
2023-01-06 15:18:19 +00:00
|
|
|
'node',
|
|
|
|
'nvim',
|
|
|
|
'package_script_service',
|
|
|
|
'perl',
|
|
|
|
'PK-Backend',
|
2023-01-27 01:40:47 +00:00
|
|
|
'pulumi',
|
2023-01-23 13:13:04 +00:00
|
|
|
-- 'python' - do not include this, or you won't detect supply-chain attacks.
|
2023-01-06 15:18:19 +00:00
|
|
|
'roxterm',
|
|
|
|
'sdk',
|
|
|
|
'sdzoomplugin',
|
|
|
|
'sh',
|
2023-01-23 13:13:04 +00:00
|
|
|
'ShellLauncher',
|
2023-01-06 15:18:19 +00:00
|
|
|
'skhd',
|
2023-01-06 15:36:48 +00:00
|
|
|
'snyk',
|
2023-01-06 15:18:19 +00:00
|
|
|
'sshd',
|
2023-01-27 01:40:47 +00:00
|
|
|
'Stream Deck',
|
2023-01-06 15:18:19 +00:00
|
|
|
'sudo',
|
|
|
|
'swift',
|
|
|
|
'systemd',
|
2023-01-06 15:36:48 +00:00
|
|
|
'systemd-sleep',
|
2023-01-06 15:18:19 +00:00
|
|
|
'terminator',
|
2023-01-30 19:58:47 +00:00
|
|
|
'terraform-ls',
|
2023-01-06 15:18:19 +00:00
|
|
|
'test2json',
|
|
|
|
'tmux',
|
|
|
|
'tmux:server',
|
2023-01-23 13:13:04 +00:00
|
|
|
'update-notifier',
|
2023-01-06 15:18:19 +00:00
|
|
|
'vi',
|
|
|
|
'vim',
|
2023-01-27 01:40:47 +00:00
|
|
|
'Vim',
|
2023-01-06 15:18:19 +00:00
|
|
|
'watch',
|
|
|
|
'wezterm-gui',
|
|
|
|
'xargs',
|
|
|
|
'xcrun',
|
|
|
|
'xfce4-terminal',
|
2023-01-30 19:58:47 +00:00
|
|
|
'Xorg',
|
2023-01-27 01:40:47 +00:00
|
|
|
'yay',
|
2023-01-06 15:18:19 +00:00
|
|
|
'yum',
|
2023-01-30 19:58:47 +00:00
|
|
|
'zed'','
|
2023-01-06 15:18:19 +00:00
|
|
|
'zellij',
|
|
|
|
'zsh'
|
2023-01-04 16:58:54 +00:00
|
|
|
)
|
2023-01-26 16:40:54 +00:00
|
|
|
OR p1_name LIKE 'terraform-provider-%'
|
2023-01-06 15:18:19 +00:00
|
|
|
-- Do not add shells to this list if you want your query to detect
|
|
|
|
-- bad programs that were started from a shell.
|
2023-01-26 16:40:54 +00:00
|
|
|
OR p2_name IN ('env', 'git')
|
2023-01-06 15:18:19 +00:00
|
|
|
-- Homebrew, except we don't want to allow all of ruby
|
2023-01-26 16:40:54 +00:00
|
|
|
OR p0_cmd IN (
|
2023-01-30 19:58:47 +00:00
|
|
|
'/bin/bash /usr/bin/xdg-settings set default-url-scheme-handler slack Slack.desktop',
|
2023-01-27 01:40:47 +00:00
|
|
|
'/bin/sh -c lsb_release -a --short',
|
2023-01-26 21:30:14 +00:00
|
|
|
'/bin/sh -c ps ax -ww -o pid,ppid,uid,gid,args',
|
2023-01-30 19:58:47 +00:00
|
|
|
'/bin/sh /usr/bin/lsb_release -a --short',
|
|
|
|
'/bin/zsh -c ls',
|
|
|
|
'sh -c /bin/stty size 2>/dev/null',
|
2023-01-26 21:30:14 +00:00
|
|
|
"sh -c osascript -e 'user locale of (get system info)'",
|
2023-01-30 19:58:47 +00:00
|
|
|
'sh -c python3.7 --version 2>&1',
|
2023-01-06 15:18:19 +00:00
|
|
|
'sh -c xcode-select --print-path >/dev/null 2>&1 && xcrun --sdk macosx --show-sdk-path 2>/dev/null'
|
|
|
|
)
|
2023-01-27 13:46:48 +00:00
|
|
|
OR (
|
|
|
|
p1_name = 'WhatsApp'
|
|
|
|
-- WhatsApp grabs the serial number from people's machines :(
|
|
|
|
AND p0_cmd = '/bin/sh -c ioreg -c IOPlatformExpertDevice -d 2'
|
|
|
|
)
|
2023-01-27 15:38:01 +00:00
|
|
|
OR p1_cmd IN (
|
|
|
|
'/usr/bin/python3 /usr/share/apport/apport-gtk',
|
|
|
|
'php ./autodocs update images'
|
|
|
|
)
|
|
|
|
OR (
|
|
|
|
p1_cmd LIKE '%Python% /opt/homebrew/bin/jupyter%'
|
2023-01-30 19:58:47 +00:00
|
|
|
AND p0_cmd = '/bin/sh -c osascript'
|
|
|
|
)
|
|
|
|
OR (
|
|
|
|
p1_name = 'osqueryd'
|
|
|
|
AND p0_cmd LIKE '/bin/sh /etc/NetworkManager/dispatcher.d/%'
|
|
|
|
)
|
|
|
|
OR (
|
|
|
|
p1_name = 'ssh'
|
|
|
|
AND p0_cmd LIKE 'gcloud.py compute start-iap-tunnel%'
|
|
|
|
)
|
|
|
|
|
|
|
|
OR exception_key IN (
|
|
|
|
'bash,0,pia-daemon,launchd',
|
|
|
|
'zsh,500,python3.10,gnome-shell'
|
2023-01-27 15:38:01 +00:00
|
|
|
)
|
2023-01-26 21:30:14 +00:00
|
|
|
OR p0_cmd LIKE '%/bash -e%/bin/as -arch%'
|
|
|
|
OR p0_cmd LIKE '/bin/bash /usr/local/Homebrew/%'
|
|
|
|
OR p0_cmd LIKE '/bin/bash /opt/homebrew/%'
|
2023-01-26 16:40:54 +00:00
|
|
|
OR p0_cmd LIKE '/bin/sh -c pkg-config %'
|
|
|
|
OR p0_cmd LIKE '/bin/sh %/docker-credential-gcloud get'
|
2023-01-30 19:58:47 +00:00
|
|
|
OR p0_cmd LIKE '%/google-chrome% --flag-switches-begin % --product-version'
|
2023-01-26 21:30:14 +00:00
|
|
|
OR p0_cmd LIKE '/bin/sh /usr/bin/xdg-open %'
|
2023-01-27 13:46:48 +00:00
|
|
|
OR p0_cmd LIKE '/bin/bash /usr/bin/xdg-settings check %'
|
2023-01-26 21:30:14 +00:00
|
|
|
OR p0_cmd LIKE '/bin/sh /usr/bin/xdg-settings set %'
|
|
|
|
OR p0_cmd LIKE '/bin/sh /usr/bin/xdg-settings check %'
|
|
|
|
OR p0_cmd LIKE '%gcloud config config-helper --format=json'
|
2023-01-30 19:58:47 +00:00
|
|
|
OR p0_cmd LIKE '%gcloud config get-value%'
|
2023-01-26 21:30:14 +00:00
|
|
|
OR p1_cmd LIKE '%Python /opt/homebrew/bin/aws configure sso'
|
2023-01-26 16:40:54 +00:00
|
|
|
OR p2_cmd LIKE '/bin/bash /usr/local/bin/brew%'
|
|
|
|
OR p2_cmd LIKE '/usr/bin/python3 -m py_compile %'
|
2023-01-06 15:18:19 +00:00
|
|
|
)
|
|
|
|
GROUP BY
|
|
|
|
pe.pid
|