osquery-defense-kit/detection/initial_access/unexpected-shell-parents.sql

307 lines
9.1 KiB
MySQL
Raw Permalink Normal View History

-- Unexpected process that spawns shell processes (event based)
2022-10-14 18:19:13 +00:00
--
-- false positives:
-- * IDE's
--
2022-10-19 20:56:32 +00:00
-- references:
-- * https://attack.mitre.org/techniques/T1059/ (Command and Scripting Interpreter)
-- * https://attack.mitre.org/techniques/T1204/002/ (User Execution: Malicious File)
--
2024-10-11 14:38:07 +00:00
-- tags: process events extra
-- interval: 60
2022-10-14 18:19:13 +00:00
-- platform: posix
SELECT
-- Child
p0.pid AS p0_pid,
p0.path AS p0_path,
p0.name AS p0_name,
p0.start_time AS p0_start,
p0.cmdline AS p0_cmd,
p0.cwd AS p0_cwd,
p0.cgroup_path AS p0_cgroup,
p0.euid AS p0_euid,
p0_hash.sha256 AS p0_sha256,
-- Parent
p0.parent AS p1_pid,
p1.path AS p1_path,
p1.name AS p1_name,
p1.start_time AS p1_start,
p1.euid AS p1_euid,
p1.cmdline AS p1_cmd,
p1_hash.sha256 AS p1_sha256,
-- Grandparent
p1.parent AS p2_pid,
p2.name AS p2_name,
p2.start_time AS p2_start,
p2.path AS p2_path,
p2.cmdline AS p2_cmd,
p2_hash.sha256 AS p2_sha256
FROM
processes p0
LEFT JOIN hash p0_hash ON p0.path = p0_hash.path
LEFT JOIN processes p1 ON p0.parent = p1.pid
LEFT JOIN hash p1_hash ON p1.path = p1_hash.path
LEFT JOIN processes p2 ON p1.parent = p2.pid
LEFT JOIN hash p2_hash ON p2.path = p2_hash.path
WHERE
p0.name IN ('sh', 'fish', 'zsh', 'bash', 'dash')
2022-09-29 19:42:27 +00:00
-- Ignore partial table joins
AND p1_path != ''
2022-10-17 23:01:16 +00:00
-- Editors & terminals mostly.
-- I know it's tempting to list "electron" here but please find a more specific exclusion.
AND p1.name NOT IN (
'abrt-action-per',
'abrt-handle-eve',
2024-08-27 22:40:43 +00:00
'AGSService',
'alacritty',
2024-08-27 22:40:43 +00:00
'Alfred',
'anacron',
'arduino-cli',
'auditd',
'bash',
'buildkit-runc',
2024-08-27 22:40:43 +00:00
'build-script-build',
2022-10-17 21:31:47 +00:00
'chezmoi',
'clang-11',
'code',
2024-08-27 22:40:43 +00:00
'Code Helper (Renderer)',
'Code - Insiders Helper',
'Code - Insiders Helper (Renderer)',
'collect2',
'com.docker.back',
'configure',
'conmon',
'containerd-shim',
2024-08-27 22:40:43 +00:00
'Core Sync',
'Cursor',
'Cursor Helper',
'dash',
'demoit',
'direnv',
'dnf',
'dnf-automatic',
2022-10-17 21:31:47 +00:00
'doas',
2024-08-27 22:40:43 +00:00
'Docker Desktop',
'dumb-init',
'elastic-security',
2022-12-16 22:37:32 +00:00
'erl_child_setup',
'find',
2024-08-27 22:40:43 +00:00
'FinderSyncExtension',
'fish',
'flock',
'gopls',
'gdm-wayland-ses',
'gephi',
'git',
2024-08-27 22:40:43 +00:00
'GitKraken Helper (Renderer)',
'git-remote-http',
'git-remote-https',
'gnome-session-b',
'gnome-shell',
'go',
'goland',
2024-08-27 22:40:43 +00:00
'GoogleSoftwareUpdateAgent',
'GoogleUpdater',
2022-10-17 21:31:47 +00:00
'helm',
2024-08-27 22:40:43 +00:00
'Hyper',
2022-11-28 21:06:07 +00:00
'i3bar',
2022-11-16 16:18:45 +00:00
'i3blocks',
2024-08-27 22:40:43 +00:00
'idea',
'incusd',
'inittool2',
'java',
'jetbrains_client',
'just',
'kitty',
'ko',
'konsole',
'kubectl',
'kue',
'ld',
'lightdm',
'linux-sandbox',
2024-08-27 22:40:43 +00:00
'LogiMgrDaemon',
'LogiTune',
'logrotate',
2024-08-27 22:40:43 +00:00
'MacVim',
'make',
2024-08-27 22:40:43 +00:00
'Microsoft.VisualStudio.Reliability.Monitor',
'monorail',
2024-08-27 22:40:43 +00:00
'newgrp',
'ninja',
'nix',
'nix-build',
2022-10-17 21:31:47 +00:00
'nix-daemon',
'node',
'nu',
'nvim',
2024-08-27 22:40:43 +00:00
'OpenLens',
2022-10-17 21:31:47 +00:00
'package_script_service',
'pacman',
'perl',
'pia-daemon',
2024-08-27 22:40:43 +00:00
'PK-Backend',
'provisio',
'ptyxis-agent',
'pycharm',
'qcalc',
2024-08-27 22:40:43 +00:00
'Rancher Desktop',
'roxterm',
'rpmbuild',
2024-08-27 22:40:43 +00:00
'Runner.Listener',
'Runner.Worker',
'screen',
2022-11-28 21:06:07 +00:00
'sdk',
'sdzoomplugin',
'sh',
'skhd',
'ssh',
2022-10-17 21:31:47 +00:00
'sshd',
'steam_osx',
'swift',
'gosec',
'systemd',
'terminator',
'kandji-library-manager',
'terraform',
'terraform-provi',
'test2json',
'timeout',
'tmux',
2022-10-17 21:31:47 +00:00
'tmux:server',
2024-08-27 22:40:43 +00:00
'udev-worker',
'unattended-upgr',
'update-notifier',
'vi',
'vim',
'vim-nox11',
2024-08-27 22:40:43 +00:00
'VisualStudio',
'watch',
'wezterm-gui',
'xargs',
'xcrun',
2023-06-07 12:58:02 +00:00
'xfce4-session',
'xfce4-terminal',
'yum',
2022-10-29 18:11:33 +00:00
'zellij',
'zsh'
)
2024-10-11 14:38:07 +00:00
AND p1_path NOT LIKE '/Applications/%.app/Contents/MacOS/%'
AND p1_path NOT IN (
'/Applications/Docker.app/Contents/MacOS/Docker',
'/Applications/Docker.app/Contents/MacOS/install',
'/Applications/Hyper.app/Contents/MacOS/Hyper',
'/usr/bin/make',
'/Applications/Visual Studio Code.app/Contents/MacOS/Electron',
'/Applications/Docker.app/Contents/Resources/bin/com.docker.cli',
'/Applications/Docker.app/Contents/Resources/bin/docker-credential-desktop',
'/Applications/IntelliJ IDEA.app/Contents/MacOS/idea',
'/Applications/Alfred 5.app/Contents/Preferences/Alfred Preferences.app/Contents/MacOS/Alfred Preferences',
'/Applications/Parallels Desktop.app/Contents/MacOS/Parallels Service',
'/Applications/Parallels Desktop.app/Contents/MacOS/prl_update_helper',
'/Applications/RStudio.app/Contents/Resources/app/bin/rsession-arm64',
'/Applications/Amazon Photos.app/Contents/MacOS/Amazon Photos',
'/bin/dash',
'/usr/bin/less',
'/usr/bin/networksetup',
'/bin/sh',
'/usr/local/qualys/cloud-agent/bin/qualys-cloud-agent',
'/Library/Application Support/Logitech.localized/LogiOptionsPlus/logioptionsplus_agent.app/Contents/MacOS/logioptionsplus_agent',
'/Library/Developer/CommandLineTools/usr/bin/git',
'/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Helpers/GoogleSoftwareUpdateDaemon',
'/Library/Kandji/Kandji Agent.app/Contents/MacOS/kandji-library-manager',
2024-10-16 13:32:00 +00:00
'/Library/Kandji/Kandji Agent.app/Contents/Helpers/Kandji Library Manager.app/Contents/MacOS/kandji-library-manager',
'/opt/X11/libexec/launchd_startx',
'/Applications/DDPM/DDPM.app/Contents/MacOS/DDPM',
'/sbin/launchd',
'/System/Library/Frameworks/Security.framework/authtrampoline',
'/usr/bin/alacritty',
'/usr/bin/apt',
2023-10-24 22:01:36 +00:00
'/usr/sbin/networksetup',
'/usr/bin/apt-get',
'/usr/bin/bash',
'/usr/bin/perl',
'/usr/bin/bwrap',
'/usr/bin/crond',
'/usr/bin/dash',
'/usr/bin/dirname',
'/usr/bin/login',
'/usr/bin/man',
'/usr/bin/su',
'/usr/bin/sudo',
'/usr/bin/sysdiagnose',
'/usr/bin/xargs',
'/usr/bin/zsh',
'/usr/libexec/gdm-x-session',
'/usr/libexec/gnome-terminal-server',
'/usr/libexec/periodic-wrapper',
'/usr/lib/xorg/Xorg'
)
AND NOT p0.cmdline IN (
-- npm run server
'sh -c -- exec-bin node_modules/.bin/hugo/hugo server',
'sh -c /usr/bin/defaults write us.zoom.xos NSQuitAlwaysKeepsWindows -bool false',
'/bin/sh -c ioreg -rd1 -c IOPlatformExpertDevice',
'/bin/sh -c system_profiler SPDisplaysDataType | grep "Chipset Model"',
'/usr/bin/python3 /usr/bin/terminator',
'sh -c echo zoomMute:disabled,zoomVideo:disabled,zoomStatus:closed,zoomShare:disabled,zoomRecord:disabled',
'/bin/sh -c sysctl hw.model kern.osrelease',
'/bin/sh /etc/security/audit_warn soft /var/audit',
'sh -c hugo-installer --version otherDependencies.hugo --extended --destination node_modules/.bin/hugo',
'/bin/bash -c ioreg -l -w 0 | grep SecureInput',
"sh -c acpi -b | grep -v 'unavailable'",
'sh -c xcode-select --print-path >/dev/null 2>&1 && xcrun --sdk macosx --show-sdk-path 2>/dev/null',
-- Brother printer
'sh -c ps -xcocommand,pid | grep "LOGINserver"'
)
AND NOT (
p1.name = 'sshd'
AND p0.cmdline LIKE '%askpass%'
)
AND NOT (
p1.name = '(udev-worker)'
AND p0.cmdline LIKE '/bin/sh -c echo % > /sys/bus/usb/drivers/brcmfmac/new_id'
)
2023-01-13 19:10:43 +00:00
AND NOT (
p1.name = 'steam'
AND p0.cmdline LIKE 'sh -c %steamwebhelper.sh%'
2023-01-13 19:10:43 +00:00
)
2022-09-27 15:54:17 +00:00
AND NOT (
p1.name = 'bash'
AND p0.cmdline LIKE 'sh -s _hostname %'
2022-09-27 15:54:17 +00:00
)
AND NOT (
p1.cmdline LIKE 'perl%/help2man%'
AND p0.cmdline LIKE 'sh -c man/%'
)
AND NOT p0.cmdline LIKE '/bin/sh %/bin/docker-credential-gcloud get'
AND NOT p1_path LIKE '/private/var/folders/%/T/go-build%.test'
AND NOT p1_path LIKE '/Users/%/.vscode/extensions/stateful.runme-%/bin/runme'
AND NOT p1_path LIKE '/private/tmp/PKInstallSandbox.%/tmp/Python/Python3.framework/Versions/%/Resources/Python.app/Contents/MacOS/Python'
AND NOT p0.cmdline LIKE '%/Library/Apple/System/Library/InstallerSandboxes%'
AND NOT p0.cmdline LIKE '%gcloud config config-helper%'
AND NOT p0.cmdline LIKE '%hugo/hugo server%'
AND NOT p1.cmdline LIKE '%/bin/pytest %'
AND NOT p0.cmdline LIKE '%/bin/codeclimate %'
AND NOT p0.cmdline LIKE '%/ChromeRecovery --browser-version=%'
AND NOT p1.cmdline LIKE '/Applications/Warp.app/%'
AND NOT p1.cmdline IN ('npm run start', 'npm install')
AND NOT p1.cmdline LIKE '%brew.rb%'
AND NOT p1.cmdline LIKE '%/Homebrew/build.rb%'
AND NOT p1.cmdline LIKE '%Code Helper%'
AND NOT p1.cmdline LIKE '%Code - Insiders Helper%'
AND NOT p1.cmdline LIKE '%gcloud.py config config-helper%'
AND NOT p1.cmdline LIKE '/usr/lib/electron19/electron /usr/lib/code/out/bootstrap-fork --type=ptyHost --logsPath /home/%/.config/Code - OSS/logs/%'
AND NOT p1.name LIKE '%term%'
AND NOT p1.name LIKE '%Term%'
AND NOT p1.name LIKE 'Emacs%'
AND NOT p1.name LIKE 'terraform-prov%'
AND NOT p1.path LIKE '/Users/%/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Helpers/GoogleSoftwareUpdateAgent.app/Contents/MacOS/GoogleSoftwareUpdateAgent'
-- Oh, NixOS.
AND NOT p1.name LIKE '%/bin/bash'
AND NOT p1.name LIKE '%/bin/direnv'
AND NOT p1_path LIKE '/nix/store/%sh'
AND NOT p1_path LIKE '/opt/homebrew/%'
AND NOT p0.cgroup_path LIKE '/system.slice/docker-%'
AND NOT p0.cgroup_path LIKE '/system.slice/system.slice:docker:%'