Hidden Cwd Events Linux
osquery
Programs running with a hidden current working directory (event-based)
Description
ODK (osquery-defense-kit) is unique in that the queries are designed to be used as part of a production detection & response pipeline. The detection queries are formulated to return zero rows during normal expected behavior, so that they may be configured to generate alerts when rows are returned.
Query
-- Programs running with a hidden current working directory (event-based)
--
-- false positives:
-- * Users rummaging through their configuration files
--
-- NOTES:
-- * Disabled on macOS, as the cwd field as NULL there
--
-- references:
-- * https://attack.mitre.org/techniques/T1564/001/ (Hide Artifacts: Hidden Files and Directories)
--
-- tags: transient extra events
-- platform: linux
-- interval: 600
SELECT
COALESCE(
TRIM(pe.cwd, '"'), "/(\\..*?)\\/", 1),
REGEX_MATCH (TRIM(pe.cwd, '"'), "/(\\..*)", 1)
REGEX_MATCH (AS hidden_base,
) TRIM(pe.cwd, '"'), "/(\\..*)", 1) AS hidden_part,
REGEX_MATCH (COALESCE(
TRIM(pe.cwd, '"'), '.*/(.*)', 1),
REGEX_MATCH (
pe.cwdAS basename,
) CONCAT (
COALESCE(
TRIM(pe.path, '"'), '.*/(.*)', 1),
REGEX_MATCH (
pe.path
),',',
TRIM(pe.cwd, '"'), "/(\\..*?)\\/", 1),
REGEX_MATCH (TRIM(pe.cwd, '"'), "/(\\..*)", 1)
REGEX_MATCH (AS exception_key,
) -- Child
AS p0_path,
pe.path COALESCE(REGEX_MATCH (pe.path, '.*/(.*)', 1), pe.path) AS p0_name,
TRIM(pe.cmdline) AS p0_cmd,
TRIM(pe.cwd, '"') AS p0_cwd,
AS p0_status,
pe.status time AS p0_time,
pe.AS p0_pid,
pe.pid AS p0_euid,
pe.euid -- Parent
parent AS p1_pid,
pe.TRIM(COALESCE(p1.cmdline, pe1.cmdline)) AS p1_cmd,
AS p1_cwd,
p1.cwd COALESCE(p1.path, pe1.path) AS p1_path,
COALESCE(p_hash1.sha256, pe_hash1.sha256) AS p1_hash,
COALESCE(p1.path, pe1.path), '.*/(.*)', 1) AS p1_name
REGEX_MATCH (FROM
process_events peLEFT JOIN users u ON pe.uid = u.uid
LEFT JOIN processes p ON pe.pid = p.pid -- 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.time > (strftime('%s', 'now') -60660)
AND pe1.cmdline != ''
LEFT JOIN hash pe_hash1 ON pe1.path = pe_hash1.path
WHERE
time > (strftime('%s', 'now') -60600)
pe.AND pe.cwd LIKE '%/.%'
AND NOT (
IN (
hidden_base '.cache',
'.cargo',
'.config',
'.docker',
'.emacs.d',
'.gimme',
'.git',
'.github',
'.gmailctl',
'.gradle',
'.kotlin',
'.linuxbrew',
'.local',
'.npm',
'.oh-my-zsh',
'.provisio',
'.terraform.d',
'.vim',
'.vscode-oss',
'.vscode',
'.zsh'
)OR exception_key LIKE '%sh,~/.Trash'
OR exception_key IN ('git,.test')
)AND NOT pe.cwd LIKE '%/build/%'
AND NOT pe.cwd LIKE '%/out/%'
GROUP BY
p.cmdline, p.cwd;
tags: SStagSS