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,
|
2023-02-01 18:55:55 +00:00
|
|
|
pe.cwd AS p0_cwd,
|
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-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
|
2023-02-02 22:58:19 +00:00
|
|
|
LEFT JOIN process_events pe1 ON pe.parent = pe1.pid
|
|
|
|
AND pe1.cmdline != ''
|
2023-01-26 16:40:54 +00:00
|
|
|
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
|
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
|
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-02-10 15:21:06 +00:00
|
|
|
'containerd-shim-runc-v2',
|
2023-01-26 21:30:14 +00:00
|
|
|
'cpptools',
|
2023-01-06 15:18:19 +00:00
|
|
|
'dash',
|
2023-02-10 15:21:06 +00:00
|
|
|
'dbus-run-session',
|
2023-01-06 15:18:19 +00:00
|
|
|
'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-02-10 15:21:06 +00:00
|
|
|
'provisio',
|
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-02-10 15:21:06 +00:00
|
|
|
'stable',
|
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 20:01:21 +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-02-10 15:21:06 +00:00
|
|
|
'/bin/bash /usr/local/bin/mount-product-files',
|
|
|
|
'/bin/sh -c black .',
|
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-02-09 01:06:26 +00:00
|
|
|
'/bin/sh -c scutil --get ComputerName',
|
2023-02-10 15:21:06 +00:00
|
|
|
'/bin/sh -c sysctl hw.model kern.osrelease',
|
2023-02-03 02:46:53 +00:00
|
|
|
'/bin/sh /usr/bin/lsb_release -a',
|
2023-02-10 15:21:06 +00:00
|
|
|
'/bin/sh /usr/bin/lsb_release -a --short',
|
|
|
|
'/bin/zsh -c ls',
|
|
|
|
'sh -c /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -find python3 2> /dev/null',
|
2023-01-30 19:58:47 +00:00
|
|
|
'sh -c /bin/stty size 2>/dev/null',
|
2023-02-10 15:21:06 +00:00
|
|
|
'sh -c cat /proc/sys/kernel/pid_max',
|
2023-01-26 21:30:14 +00:00
|
|
|
"sh -c osascript -e 'user locale of (get system info)'",
|
2023-02-10 15:21:06 +00:00
|
|
|
"sh -c pacmd list-sinks |grep 'name:\|module:'",
|
|
|
|
'sh -c pactl --version',
|
2023-01-30 19:58:47 +00:00
|
|
|
'sh -c python3.7 --version 2>&1',
|
2023-02-10 15:21:06 +00:00
|
|
|
'sh -c /usr/bin/xcrun clang 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-02-08 19:37:09 +00:00
|
|
|
OR (
|
|
|
|
p1_name LIKE 'emacs-%'
|
|
|
|
AND p1_path LIKE '%/bin/emacs%'
|
|
|
|
)
|
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'
|
2023-02-03 01:55:46 +00:00
|
|
|
AND p0_cmd LIKE '%gcloud.py compute start-iap-tunnel%'
|
2023-01-30 19:58:47 +00:00
|
|
|
)
|
|
|
|
OR exception_key IN (
|
2023-02-09 22:54:41 +00:00
|
|
|
'bash,0,etcd,containerd-shim-runc-v2',
|
|
|
|
'bash,0,kube-apiserver,containerd-shim-runc-v2',
|
2023-01-30 19:58:47 +00:00
|
|
|
'bash,0,pia-daemon,launchd',
|
2023-02-01 18:55:55 +00:00
|
|
|
'bash,0,udevadm,udevadm',
|
2023-02-09 22:54:41 +00:00
|
|
|
'bash,500,com.docker.dev-envs,com.docker.backend',
|
|
|
|
'bash,500,Foxit PDF Reader,launchd',
|
|
|
|
'bash,500,gnome-session-binary,systemd',
|
|
|
|
'bash,500,gpg-agent,launchd',
|
2023-02-09 01:06:26 +00:00
|
|
|
'bash,500,.man-wrapped,zsh',
|
2023-02-10 15:21:06 +00:00
|
|
|
'dash,0,kube-proxy,containerd-shim-runc-v2',
|
|
|
|
'dash,0,kindnetd,containerd-shim-runc-v2',
|
2023-02-09 22:54:41 +00:00
|
|
|
'bash,500,Private Internet Access,launchd',
|
|
|
|
'dash,0,anacron,systemd',
|
2023-02-08 19:37:09 +00:00
|
|
|
'sh,0,auditd,launchd',
|
2023-02-09 22:54:41 +00:00
|
|
|
'sh,500,cloud_sql_proxy,zsh',
|
|
|
|
'sh,500,docs,zsh',
|
|
|
|
'sh,500,Google Drive,launchd',
|
2023-02-09 01:06:26 +00:00
|
|
|
'sh,500,snyk-macos,snyk',
|
|
|
|
'sh,500,ssh,mosh-client',
|
2023-02-09 22:54:41 +00:00
|
|
|
'sh,500,updater,Foxit PDF Reader',
|
2023-02-09 01:06:26 +00:00
|
|
|
'sh,500,yabai,launchd',
|
|
|
|
'zsh,500,old,launchd',
|
2023-02-09 22:54:41 +00:00
|
|
|
'zsh,500,python3.10,gnome-shell',
|
|
|
|
'zsh,500,stable,launchd'
|
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 /opt/homebrew/%'
|
2023-02-09 01:53:19 +00:00
|
|
|
OR p0_cmd LIKE '/bin/bash /usr/bin/xdg-settings check %'
|
|
|
|
OR p0_cmd LIKE '/bin/bash /usr/local/Homebrew/%'
|
|
|
|
OR p0_cmd LIKE '/bin/sh %/bin/gcloud --format json config config-helper%'
|
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-26 21:30:14 +00:00
|
|
|
OR p0_cmd LIKE '/bin/sh /usr/bin/xdg-open %'
|
2023-02-09 01:53:19 +00:00
|
|
|
OR p0_cmd LIKE '/bin/sh /usr/bin/xdg-settings check %'
|
2023-02-03 02:46:53 +00:00
|
|
|
OR p0_cmd LIKE '/bin/sh /usr/bin/xdg-settings get %'
|
2023-01-26 21:30:14 +00:00
|
|
|
OR p0_cmd LIKE '/bin/sh /usr/bin/xdg-settings set %'
|
|
|
|
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-02-09 01:53:19 +00:00
|
|
|
OR p0_cmd LIKE '%/google-chrome% --flag-switches-begin % --product-version'
|
2023-02-09 01:06:26 +00:00
|
|
|
OR p1_cmd LIKE '%/bin/pipenv shell'
|
2023-02-09 22:54:41 +00:00
|
|
|
OR p1_cmd LIKE 'gcloud% auth%login%'
|
2023-02-10 15:21:06 +00:00
|
|
|
OR (exception_key = 'sh,500,ruby,zsh' AND p1_cmd LIKE '%brew.rb')
|
2023-02-09 01:53:19 +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
|
|
|
)
|
2023-02-10 15:21:06 +00:00
|
|
|
AND NOT p0_cgroup LIKE '/system.slice/docker-%'
|
|
|
|
AND NOT p1_cgroup LIKE '/system.slice/docker-%'
|
|
|
|
AND NOT p2_cgroup LIKE '/system.slice/docker-%'
|
2023-01-06 15:18:19 +00:00
|
|
|
GROUP BY
|
|
|
|
pe.pid
|