2022-10-19 20:56:32 +00:00
|
|
|
-- Find processes that run with a lower effective UID than their parent (event-based)
|
2022-10-14 18:19:13 +00:00
|
|
|
--
|
2022-10-19 20:56:32 +00:00
|
|
|
-- references:
|
|
|
|
-- * https://attack.mitre.org/techniques/T1548/001/ (Setuid and Setgid)
|
|
|
|
-- * https://cybersecurity.att.com/blogs/labs-research/shikitega-new-stealthy-malware-targeting-linux
|
|
|
|
--
|
|
|
|
-- related:
|
|
|
|
-- * unexpected-privilege-escalation.sql
|
|
|
|
--
|
|
|
|
-- tags: events process escalation
|
2022-11-08 01:36:37 +00:00
|
|
|
-- platform: darwin
|
2023-02-01 21:17:36 +00:00
|
|
|
-- interval: 300
|
2023-03-20 21:05:02 +00:00
|
|
|
SELECT -- Child
|
2023-01-26 16:40:54 +00:00
|
|
|
pe.path AS p0_path,
|
|
|
|
REGEX_MATCH (pe.path, '.*/(.*)', 1) AS p0_name,
|
|
|
|
TRIM(pe.cmdline) AS p0_cmd,
|
2023-02-01 18:55:55 +00:00
|
|
|
pe.cwd AS p0_cwd,
|
2023-01-26 16:40:54 +00:00
|
|
|
pe.pid AS p0_pid,
|
2023-05-02 21:49:53 +00:00
|
|
|
pe.uid AS p0_uid,
|
2023-01-26 16:40:54 +00:00
|
|
|
pe.euid AS p0_euid,
|
|
|
|
s.authority AS p0_authority,
|
|
|
|
-- Parent
|
|
|
|
pe.parent AS p1_pid,
|
|
|
|
TRIM(COALESCE(p1.cmdline, pe1.cmdline)) AS p1_cmd,
|
|
|
|
COALESCE(p1.path, pe1.path) AS p1_path,
|
|
|
|
COALESCE(p1.euid, pe1.euid) AS p1_euid,
|
|
|
|
COALESCE(p_hash1.sha256, pe_hash1.sha256) AS p1_hash,
|
2023-02-02 22:58:19 +00:00
|
|
|
REGEX_MATCH (COALESCE(p1.path, pe1.path), '.*/(.*)', 1) AS p1_name,
|
2023-01-26 16:40:54 +00:00
|
|
|
pe_sig1.authority AS p1_authority,
|
|
|
|
-- Grandparent
|
|
|
|
COALESCE(p1.parent, pe1.parent) AS p2_pid,
|
2023-02-02 22:58:19 +00:00
|
|
|
TRIM(
|
|
|
|
COALESCE(p1_p2.cmdline, pe1_p2.cmdline, pe1_pe2.cmdline)
|
|
|
|
) AS p2_cmd,
|
2023-01-26 16:40:54 +00:00
|
|
|
COALESCE(p1_p2.path, pe1_p2.path, pe1_pe2.path) AS p2_path,
|
2023-02-02 22:58:19 +00:00
|
|
|
COALESCE(
|
|
|
|
p1_p2_hash.path,
|
|
|
|
pe1_p2_hash.path,
|
|
|
|
pe1_pe2_hash.path
|
|
|
|
) AS p2_hash,
|
|
|
|
REGEX_MATCH (
|
|
|
|
COALESCE(p1_p2.path, pe1_p2.path, pe1_pe2.path),
|
|
|
|
'.*/(.*)',
|
|
|
|
1
|
|
|
|
) AS p2_name,
|
|
|
|
COALESCE(
|
|
|
|
p1_p2_sig.authority,
|
|
|
|
pe1_p2_sig.authority,
|
|
|
|
pe1_pe2_sig.authority
|
|
|
|
) AS p2_authority,
|
2023-01-26 16:40:54 +00:00
|
|
|
-- Exception key
|
2023-02-02 22:58:19 +00:00
|
|
|
REGEX_MATCH (pe.path, '.*/(.*)', 1) || ',' || MIN(pe.euid, 500) || ',' || REGEX_MATCH (COALESCE(p1.path, pe1.path), '.*/(.*)', 1) || ',' || REGEX_MATCH (
|
|
|
|
COALESCE(p1_p2.path, pe1_p2.path, pe1_pe2.path),
|
|
|
|
'.*/(.*)',
|
|
|
|
1
|
|
|
|
) AS exception_key
|
2023-03-30 22:44:01 +00:00
|
|
|
FROM
|
|
|
|
process_events pe,
|
2023-02-02 22:58:19 +00:00
|
|
|
uptime
|
2023-01-09 15:46:30 +00:00
|
|
|
LEFT JOIN processes p ON pe.pid = p.pid
|
2023-03-20 21:05:02 +00:00
|
|
|
LEFT JOIN signature s ON pe.path = s.path -- Parents (via two paths)
|
2023-03-14 23:00:44 +00:00
|
|
|
LEFT JOIN processes p1 ON pe.parent = p1.pid
|
|
|
|
AND p1.start_time <= pe.time
|
2023-01-26 16:40:54 +00:00
|
|
|
LEFT JOIN hash p_hash1 ON p1.path = p_hash1.path
|
2023-03-14 23:00:44 +00:00
|
|
|
LEFT JOIN process_events pe1 ON pe.parent = pe1.pid
|
|
|
|
AND pe1.time <= pe.time
|
2023-02-02 22:58:19 +00:00
|
|
|
AND pe1.cmdline != ''
|
2023-01-26 16:40:54 +00:00
|
|
|
LEFT JOIN hash pe_hash1 ON pe1.path = pe_hash1.path
|
2023-03-20 21:05:02 +00:00
|
|
|
LEFT JOIN signature pe_sig1 ON pe1.path = pe_sig1.path -- Grandparents (via 3 paths)
|
2023-03-14 23:00:44 +00:00
|
|
|
LEFT JOIN processes p1_p2 ON p1.parent = p1_p2.pid
|
|
|
|
AND p1_p2.start_time <= p1.start_time
|
|
|
|
LEFT JOIN processes pe1_p2 ON pe1.parent = pe1_p2.pid
|
|
|
|
AND pe1_p2.start_time <= pe1.time
|
2023-02-02 22:58:19 +00:00
|
|
|
LEFT JOIN process_events pe1_pe2 ON pe1.parent = pe1_p2.pid
|
|
|
|
AND pe1_pe2.cmdline != '' -- Past grandparent via parent events
|
2023-01-26 16:40:54 +00:00
|
|
|
LEFT JOIN hash p1_p2_hash ON p1_p2.path = p1_p2_hash.path
|
|
|
|
LEFT JOIN hash pe1_p2_hash ON pe1_p2.path = pe1_p2_hash.path
|
|
|
|
LEFT JOIN hash pe1_pe2_hash ON pe1_pe2.path = pe1_pe2_hash.path
|
|
|
|
LEFT JOIN signature p1_p2_sig ON p1_p2.path = p1_p2_sig.path
|
|
|
|
LEFT JOIN signature pe1_p2_sig ON pe1_p2.path = pe1_p2_sig.path
|
|
|
|
LEFT JOIN signature pe1_pe2_sig ON pe1_pe2.path = pe1_pe2_sig.path
|
2023-03-30 22:44:01 +00:00
|
|
|
WHERE
|
|
|
|
pe.time > (strftime('%s', 'now') -300)
|
2023-01-26 16:40:54 +00:00
|
|
|
AND p0_euid < p1_euid
|
2023-02-17 16:57:23 +00:00
|
|
|
AND pe.status = 0
|
|
|
|
AND pe.parent > 0
|
|
|
|
AND pe.cmdline != ''
|
|
|
|
AND pe.cmdline IS NOT NULL
|
2023-01-26 16:40:54 +00:00
|
|
|
AND p1_path NOT IN (
|
2022-11-28 21:06:07 +00:00
|
|
|
'/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Support/mdworker_shared',
|
2023-02-09 01:06:26 +00:00
|
|
|
'/usr/libexec/PerfPowerServicesExtended',
|
2023-03-03 12:24:42 +00:00
|
|
|
'/System/Library/PrivateFrameworks/AOSKit.framework/Versions/A/XPCServices/com.apple.iCloudHelper.xpc/Contents/MacOS/com.apple.iCloudHelper',
|
2022-10-07 20:19:18 +00:00
|
|
|
'/usr/bin/login',
|
2022-11-08 01:36:37 +00:00
|
|
|
'/usr/bin/su',
|
2022-10-07 20:19:18 +00:00
|
|
|
'/usr/bin/sudo',
|
2023-03-03 12:24:42 +00:00
|
|
|
'/usr/libexec/mdmclient',
|
2022-11-08 01:36:37 +00:00
|
|
|
'/usr/local/bin/doas'
|
2023-03-20 21:05:02 +00:00
|
|
|
) -- Exclude weird bad data we've seen due to badly recorded macOS parent/child relationships, fixable by reboot
|
2023-03-03 12:24:42 +00:00
|
|
|
AND NOT p0_cmd IN (
|
|
|
|
'/usr/sbin/cupsd -l',
|
2023-03-14 23:00:44 +00:00
|
|
|
'/usr/sbin/cfprefsd agent',
|
2023-03-30 22:44:01 +00:00
|
|
|
'/System/Library/CoreServices/iconservicesd',
|
|
|
|
'/System/Library/PrivateFrameworks/InstallCoordination.framework/Support/installcoordinationd',
|
|
|
|
'/System/Library/PrivateFrameworks/CoreSymbolication.framework/coresymbolicationd',
|
2023-03-03 12:24:42 +00:00
|
|
|
'/usr/libexec/PerfPowerServicesExtended',
|
|
|
|
'/usr/libexec/mdmclient daemon',
|
|
|
|
'/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker_shared -s mdworker -c MDSImporterWorker -m com.apple.mdworker.shared'
|
2022-12-15 15:20:16 +00:00
|
|
|
)
|
2023-03-20 21:05:02 +00:00
|
|
|
AND NOT exception_key IN (
|
2023-05-23 15:31:37 +00:00
|
|
|
'amfid,0,com.docker.backend,Docker',
|
2023-05-02 19:25:36 +00:00
|
|
|
'biometrickitd,0,LogiTune,launchd',
|
2023-05-23 15:31:37 +00:00
|
|
|
'bioutil,0,callservicesd,launchd',
|
|
|
|
'CAReportingService,0,LogiTune,launchd',
|
2023-05-08 17:07:57 +00:00
|
|
|
'com.apple.AccountPolicyHelper,0,LogiTune,launchd',
|
2023-05-02 19:25:36 +00:00
|
|
|
'com.apple.geod,262,com.docker.backend,Docker',
|
2023-05-23 15:31:37 +00:00
|
|
|
'com.apple.WebKit.WebContent,200,zsh,Emacs-arm64-11',
|
|
|
|
'containermanagerd,262,com.docker.backend,Docker',
|
|
|
|
'dprivacyd,0,com.docker.backend,Docker',
|
|
|
|
'SCHelper,0,com.docker.backend,Docker',
|
|
|
|
'suhelperd,0,LogiTune,launchd',
|
|
|
|
'sysextd,0,LogiTune,launchd',
|
|
|
|
'system_profiler,0,callservicesd,launchd'
|
2023-03-20 21:05:02 +00:00
|
|
|
)
|
2022-12-15 15:20:16 +00:00
|
|
|
AND NOT (
|
2023-01-09 15:46:30 +00:00
|
|
|
pe.euid = 262 -- core media helper id
|
|
|
|
AND pe.path = '/System/Library/Frameworks/CoreMediaIO.framework/Versions/A/Resources/AppleCamera.plugin/Contents/Resources/AppleCameraAssistant'
|
2023-03-30 22:44:01 +00:00
|
|
|
)
|