Unexpected Osascript Calls
osquery
Detect unusual calls to osascript
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
-- Detect unusual calls to osascript
--
-- This query does some gymnastics, as the information on the parent process may be
-- found in the 'process' (currently running) or 'process_events' table (recently started).
--
-- In the case of a long-running process that was recently terminated, parent information
-- may not be in either.
--
-- false positives:
-- * none observed, but they are expected
--
-- interval: 300
-- platform: darwin
-- tags: process events extra
SELECT
-- Child
AS p0_path,
pe.path '.*/(.*)', 1) AS p0_name,
REGEX_MATCH (pe.path, TRIM(pe.cmdline) AS p0_cmd,
AS p0_cwd,
pe.cwd time AS p0_time,
pe.AS p0_pid,
pe.pid AS p0_euid,
pe.euid AS p0_authority,
s.authority -- Parent
parent AS p1_pid,
pe.TRIM(COALESCE(p1.cmdline, pe1.cmdline)) AS p1_cmd,
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 (AS p1_authority,
pe_sig1.authority -- Grandparent
COALESCE(p1.parent, pe1.parent) AS p2_pid,
TRIM(
COALESCE(p1_p2.cmdline, pe1_p2.cmdline, pe1_pe2.cmdline)
AS p2_cmd,
) COALESCE(p1_p2.path, pe1_p2.path, pe1_pe2.path) AS p2_path,
COALESCE(
p1_p2_hash.path,
pe1_p2_hash.path,
pe1_pe2_hash.pathAS 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.authorityAS p2_authority
) FROM
process_events peLEFT JOIN processes p ON pe.pid = p.pid
LEFT JOIN signature s ON pe.path = s.path
-- 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.cmdline != ''
LEFT JOIN hash pe_hash1 ON pe1.path = pe_hash1.path
LEFT JOIN signature pe_sig1 ON pe1.path = pe_sig1.path
-- Grandparents (via 3 paths)
LEFT JOIN processes p1_p2 ON p1.parent = p1_p2.pid -- Current grandparent via parent processes
LEFT JOIN processes pe1_p2 ON pe1.parent = pe1_p2.pid -- Current grandparent via parent events
LEFT JOIN process_events pe1_pe2 ON pe1.parent = pe1_p2.pid
AND pe1_pe2.cmdline != '' -- Past grandparent via parent events
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
WHERE
IN ('/usr/bin/osascript', '/usr/bin/osacompile')
pe.path AND pe.time > (strftime('%s', 'now') -300)
AND pe.cmdline != ''
-- Only include successful executions: On macOS, process_events includes unsuccessful path lookups!
AND pe.status = 0
AND NOT (
> 500
pe.euid AND (
IN (
p0_cmd 'osascript -e get POSIX path of (path to application id "com.garmin.antagent")',
'osascript -e get POSIX path of (path to application id "com.garmin.expressfit")',
'osascript -e get POSIX path of (path to application id "com.garmin.LifetimeMapUpdate")',
'osascript -e tell application "Finder" to reveal application file id "com.garmin.renu.client"',
'osascript -e user locale of (get system info)',
'osascript ./ExpressLoginItem.scpt'
)OR p0_cmd LIKE '/usr/bin/osascript /Applications/Amazon Photos.app/Contents/Resources/quit_and_restart_app.scpt /Applications/Amazon Photos.app com.amazon.clouddrive.mac%'
OR p0_cmd LIKE '/usr/bin/osascript /Users/%/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/%'
OR p0_cmd LIKE '/usr/bin/osascript /Users/%/osx-trash/trashfile.AppleScript %'
OR p0_cmd LIKE '/usr/bin/osascript %com.docker.docker%'
OR p0_cmd LIKE '%"CFBundleName" of property list file (app_path & ":Contents:Info.plist")'
OR p0_cmd LIKE '%osacompile -o /Users/%/Applications/Home Manager Trampolines/%'
OR p0_cmd LIKE 'osascript -e set zoomStatus to "closed"%'
OR p0_cmd LIKE 'osascript -e tell application "zoom.us"%'
OR p0_cmd LIKE 'osascript -e%tell application "System Preferences"%reveal anchor "shortcutsTab"%"com.apple.preference.keyboard"'
OR p0_cmd LIKE 'osascript -l JavaScript /tmp/PKInstallSandbox.%/Scripts/org.gpgtools.gpgmailloader.pkg.%/mailbundle-enabled.jxa -- GPGMailLoader.mailbundle'
OR p0_cmd LIKE 'osascript -l JavaScript%com.elgato.StreamDeck%'
OR p0_cmd LIKE 'osascript openChrome.applescript http://127.0.0.1:%'
OR p0_cmd LIKE 'osascript openChrome.applescript http%://localhost%'
OR p1_cmd LIKE '/bin/sh %/opt/homebrew/bin/git-gui%'
OR p1_cmd LIKE '% /opt/homebrew/bin/jupyter%notebook'
OR p1_cmd LIKE '%auth login'
OR p1_cmd LIKE '%aws %sso%'
OR p1_cmd LIKE '%gcloud% auth %login%'
OR p1_cmd LIKE '%gcloud% init'
OR p1_cmd LIKE '%jupyter_mac.command'
OR p1_cmd LIKE '%tell application "Finder" to empty trash%'
OR p1_cmd LIKE '%tell application "System Events" to sleep%'
OR p1_authority = 'Developer ID Application: Docker Inc (9BNSXJN65R)'
OR p1_name IN ('yubikey-agent')
OR (
= 'Developer ID Application: VNG ONLINE CO.,LTD (CVB6BX97VM)'
p1_authority AND p0_cmd = 'osascript -ss'
)OR (
= 'Developer ID Application: Dropbox, Inc. (G7HH3F8CAK)'
p1_authority AND p0_cmd = 'osascript'
)
)
)-- The following apply to all uids
AND NOT p0_cmd IN (
'/usr/bin/osascript -e do shell script "/bin/rm -Rf /opt/vagrant /usr/local/bin/vagrant" with administrator privileges',
'osascript -e do shell script "/bin/rm -Rf /opt/vagrant /usr/local/bin/vagrant" with administrator privileges',
'osascript -e user locale of (get system info)'
)GROUP BY
pe.pid
tags: SStagSS