Unexpected Long Running Security Framework Macos
osquery
Find programs that use the Security Framework on macOS - popular among malware authors
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
-- Find programs that use the Security Framework on macOS - popular among malware authors
--
-- platform: darwin
-- tags: persistent state process seldom
SELECT
s.authority,identifier,
s.CONCAT (
MIN(p0.euid, 500),
',',
p0.name,',',
identifier,
s.',',
s.authorityAS exception_key,
) AS lib_path,
pmm.path -- Child
AS p0_pid,
p0.pid AS p0_start,
p0.start_time AS p0_path,
p0.path AS p0_name,
p0.name AS p0_cmd,
p0.cmdline AS p0_cwd,
p0.cwd AS p0_euid,
p0.euid AS p0_sha256,
p0_hash.sha256 -- Parent
parent AS p1_pid,
p0.AS p1_start,
p1.start_time AS p1_path,
p1.path AS p1_name,
p1.name AS p1_euid,
p1.euid AS p1_cmd,
p1.cmdline AS p1_sha256,
p1_hash.sha256 -- Grandparent
parent AS p2_pid,
p1.AS p2_start,
p2.start_time AS p2_name,
p2.name AS p2_path,
p2.path AS p2_cmd,
p2.cmdline AS p2_sha256
p2_hash.sha256 FROM
processes p0JOIN process_memory_map pmm ON p0.pid = pmm.pid
LEFT JOIN signature s ON p0.path = s.path
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 -- Focus on longer-running programs
IN (
p0.pid SELECT
pidFROM
processesWHERE
< (strftime('%s', 'now') - 25200)
start_time AND parent != 0 -- Assume STP
AND NOT path LIKE '/Applications/%.app/%' -- Other oddball binary paths
AND NOT path LIKE '/nix/store/%'
AND NOT path LIKE '/opt/%'
AND NOT path LIKE '/private/var/folders%/T/go-build%/exe/%'
AND NOT path LIKE '/System/%'
AND NOT path LIKE '/Users/%/.terraform/providers/%'
AND NOT path LIKE '/Users/%/bin/%'
AND NOT path LIKE '/Users/%/dev/%'
AND NOT path LIKE '/Users/%/go/%'
AND NOT path LIKE '/Users/%/Library/Application Support/com.elgato.StreamDeck/Plugins/%'
AND NOT path LIKE '/Users/%/Library/Application Support/Zed/extensions/%'
AND NOT path LIKE '/Users/%/Library/Application Support/Zed/supermaven/%'
AND NOT path LIKE '/Users/%/src/%'
AND NOT path LIKE '/usr/libexec/%'
AND NOT path LIKE '/usr/sbin/%' -- Regular apps
AND NOT REGEX_MATCH (path, '(.*)/', 1) LIKE '%/bin'
AND NOT (
LIKE '/Users/%/Library/Application Support/com.elgato.StreamDeck/Plugins/com.elgato.cpu.sdPlugin/cpu'
path AND name = 'cpu'
-- Takes arguments
) AND NOT (
>= 500
euid AND cmdline LIKE "% --%"
)
)AND pmm.path LIKE '%Security.framework%'
AND NOT s.authority IN (
'Developer ID Application: Corsair Memory, Inc. (Y93VXCB8Q5)',
'Developer ID Application: Google, Inc. (EQHXZ8M8AV)',
'Developer ID Application: OSQUERY A Series of LF Projects, LLC (3522FA9PXF)',
'Developer ID Application: Valve Corporation (MXGJJ98X76)'
)AND exception_key NOT IN (
'0,velociraptor,a.out,',
'500,___1go_build_main_go,a.out,',
'500,circleci-yaml-language-server,a.out,',
'500,cloud_sql_proxy,a.out,',
'500,docker,docker,',
'500,gopls,a.out,',
'500,python3,python.exe,',
'500,sdaudioswitch,,',
'500,sdaudioswitch,sdaudioswitch,',
'500,sdmicmute,sdmicmute,',
'500,sdzoomplugin,,',
'500,serial-discovery,a.out,'
)AND NOT exception_key LIKE '500,___%go_build_%,a.out,'
AND NOT exception_key LIKE '500,___2go_build_main_go,a.out,'
AND NOT exception_key LIKE '500,___Test%.test,a.out'
AND NOT exception_key LIKE '500,lifx-streamdeck,lifx-streamdeck-%'
AND NOT exception_key LIKE '500,marksman-macos,marksman-%,'
AND NOT exception_key LIKE '500,nvim,bob-%,'
AND NOT exception_key LIKE '500,package-version-server-v%,package_version_server-%,'
AND NOT exception_key LIKE '500,rust-analyzer,rust_analyzer-%,'
AND NOT exception_key LIKE '500,sm-agent,sm_agent-%'
GROUP BY
p0.pid
tags: SStagSS