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

117 lines
2.9 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,
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,
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 (
'bash,~/.local/share',
'bash,~/go/src',
'Electron,~/.vscode/extensions',
'fish,~/.local/share',
'git,~/.local/share',
'makepkg,~/.cache/yay',
'zsh,~/.Trash',
'bash,~/.Trash',
'fish,~/.Trash',
'make,~/.cache/yay',
'java,~/.gradle/daemon',
'java,~/.local/share',
'rust-analyzer-p,~/.cargo/registry',
'as,~/.cache/yay',
'c++,~/.cache/yay',
'cc1plus,~/.cache/yay',
2022-10-19 21:07:52 +00:00
'npm install,~/.npm/_cacache',
'mysqld,~/.local/share'
)
OR exception_key LIKE '%sh,~/.Trash/%'
OR dir IN (
'~/.config',
'~/.local/bin',
'~/.vim',
'~/.terraform.d',
'~/.cache/yay',
'~/.local/share/chezmoi',
'~/.local/share/Steam',
'~/.local/share/nvim',
'~/.gmailctl',
'~/.oh-my-zsh',
'~/.zsh'
)
OR p.name IN (
'bindfs',
'vim',
'nvim',
'code',
'updatedb',
'git',
'gitsign',
'Code Helper'
)
OR dir LIKE '~/.cache/yay/%'
OR dir LIKE '~/.cargo/%'
OR dir LIKE '~/code/%'
OR dir LIKE '~/.dotfiles/%'
OR dir LIKE '~/%/.github%'
OR dir LIKE '~/go/src/%'
OR dir LIKE '~/.gradle/%'
OR dir LIKE '/Library/Apple/System/Library/InstallerSandboxes/.PKInstallSandboxManager-SystemSoftware/%'
OR dir LIKE '~/.local/share/fish/%'
OR dir LIKE '~/.local/share/JetBrains/%'
OR dir LIKE '~/.local/share/kotlin/%'
OR dir LIKE '~/.local/share/nvim/%'
2022-10-30 14:19:33 +00:00
OR dir LIKE '~/.local/share/Steam/%'
2022-10-18 15:44:03 +00:00
OR dir LIKE '~/.provisio%'
OR dir LIKE '~/src/%'
OR dir LIKE '~/%/.terraform%'
OR dir LIKE '~/.vscode/extensions/%'
OR dir LIKE '~/.zsh/%'
OR dir LIKE '~/%/.git'
-- For sudo calls to other things
OR (dir LIKE '/home/.terraform.d/%' AND p.euid = 0)
)