osquery-defense-kit/detection/evasion/hidden-cwd.sql

137 lines
3.4 KiB
MySQL
Raw Normal View History

-- Programs running with a hidden current working directory (state-based)
2022-10-14 18:19:13 +00:00
--
2022-10-19 20:56:32 +00:00
-- false positives:
2022-10-14 18:19:13 +00:00
-- * Users rummaging through their configuration files
--
2022-10-19 20:56:32 +00:00
-- references:
-- * https://attack.mitre.org/techniques/T1564/001/ (Hide Artifacts: Hidden Files and Directories)
--
2022-10-14 18:19:13 +00:00
-- tags: transient often
2022-10-19 21:07:52 +00:00
-- platform: posix
SELECT
p.pid,
p.path,
p.name,
p.cmdline,
p.cwd,
p.euid,
p.parent,
2023-01-13 20:24:18 +00:00
p.cgroup_path,
pp.path AS parent_path,
pp.name AS parent_name,
pp.cmdline AS parent_cmdline,
pp.cwd AS parent_cwd,
pp.euid AS parent_euid,
2022-10-05 12:36:35 +00:00
hash.sha256,
REPLACE(p.cwd, u.directory, '~') AS dir,
REGEX_MATCH (
REPLACE(p.cwd, u.directory, '~'),
'([/~].*?/.*?)/',
1
) AS top_dir,
CONCAT (
2022-10-05 12:36:35 +00:00
p.name,
',',
2022-10-05 12:36:35 +00:00
IIF(
REGEX_MATCH (
REPLACE(p.cwd, u.directory, '~'),
'([/~].*?/.*?/.*?)/',
2022-10-05 12:36:35 +00:00
1
) != '',
REGEX_MATCH (
REPLACE(p.cwd, u.directory, '~'),
'([/~].*?/.*?/.*?)/',
2022-10-05 12:36:35 +00:00
1
),
REPLACE(p.cwd, u.directory, '~')
2022-10-05 12:36:35 +00:00
)
) AS exception_key
FROM
processes p
2022-09-30 18:12:24 +00:00
LEFT JOIN processes pp ON p.parent = pp.pid
2022-10-05 12:36:35 +00:00
LEFT JOIN users u ON p.uid = u.uid
LEFT JOIN hash ON p.path = hash.path
WHERE
dir LIKE '%/.%'
AND NOT (
exception_key IN (
'as,~/.cache/yay',
'bash,~/go/src',
'bash,~/.local/share',
'bash,~/.Trash',
'cc1plus,~/.cache/yay',
'c++,~/.cache/yay',
'cgo,~/.gimme/versions',
'dirhelper,/private/var/folders',
'Electron,~/.vscode/extensions',
'fish,~/.local/share',
'fish,~/.Trash',
'cc1,/home/build/.cache',
'git,~/.local/share',
'java,~/.gradle/daemon',
'java,~/.local/share',
'make,~/.cache/yay',
2023-01-13 20:24:18 +00:00
'vet,/home/build/.cache',
'makepkg,~/.cache/yay',
'mysqld,~/.local/share',
2022-10-19 21:07:52 +00:00
'npm install,~/.npm/_cacache',
'rust-analyzer-p,~/.cargo/registry',
'zsh,~/.Trash'
)
OR exception_key LIKE '%sh,~/.Trash/%'
OR exception_key LIKE '%sh,~/dev/%'
OR dir IN (
'~/.config',
'~/.local/bin',
'~/.vim',
'~/.terraform.d',
'~/.cache/yay',
'~/.emacs.d',
'~/.local/share/chezmoi',
'~/.local/share/Steam',
'~/.local/share/nvim',
'~/.gmailctl',
'~/.oh-my-zsh',
'~/.hunter/_Base',
'~/.zsh'
)
2023-01-14 13:19:26 +00:00
OR top_dir IN ('~/Sync')
OR p.name IN (
'bindfs',
'vim',
'find',
'nvim',
'code',
'updatedb',
'git',
'gitsign',
'Code Helper'
)
2022-11-08 19:22:12 +00:00
OR dir LIKE '~/.%'
OR dir LIKE '~/code/%'
OR dir LIKE '~/%/.config/nvim'
OR dir LIKE '~/dev/%/dots/%/.config%'
OR dir LIKE '~/%/.git'
OR dir LIKE '~/%/.github%'
OR dir LIKE '~/%/.docker%'
OR dir LIKE '~/%/.vercel%'
2022-11-08 17:59:11 +00:00
OR dir LIKE '~/%/github.com/%'
OR dir LIKE '~/%google-cloud-sdk/.install/.backup%'
OR dir LIKE '~/.gradle/%'
OR dir LIKE '/Library/Apple/System/Library/InstallerSandboxes/.PKInstallSandboxManager-SystemSoftware/%'
OR dir LIKE '~/%/.modcache/%'
OR dir LIKE '~/%/node_modules/.pnpm/%'
OR dir LIKE '/opt/homebrew/%/.cache/%'
OR dir LIKE '~/%/src/%'
OR dir LIKE '~/src/%'
OR dir LIKE '~/%/.terraform%'
OR dir LIKE '/tmp/%/.github/workflows'
OR dir LIKE '/tmp/.mount_%'
-- For sudo calls to other things
OR (
dir LIKE '/home/.terraform.d/%'
AND p.euid = 0
)
)
AND NOT p.cgroup_path LIKE '/system.slice/docker-%'