osquery-defense-kit/detection/execution/recently-created-executable...

136 lines
5.7 KiB
SQL

-- Programs who were recently added to disk, based on btime/ctime
--
-- false-positives:
-- * many
--
-- tags: transient process state often
-- platform: darwin
SELECT
f.ctime,
f.btime,
f.mtime,
p0.start_time,
-- Child
p0.pid AS p0_pid,
p0.path AS p0_path,
s.authority AS p0_sauth,
s.identifier AS p0_sid,
p0.name AS p0_name,
p0.cmdline AS p0_cmd,
p0.cwd AS p0_cwd,
p0.cgroup_path AS p0_cgroup,
p0.euid AS p0_euid,
p0_hash.sha256 AS p0_sha256,
-- Parent
p0.parent AS p1_pid,
p1.path AS p1_path,
p1.name AS p1_name,
p1.euid AS p1_euid,
p1.cmdline AS p1_cmd,
p1_hash.sha256 AS p1_sha256,
-- Grandparent
p1.parent AS p2_pid,
p2.name AS p2_name,
p2.path AS p2_path,
p2.cmdline AS p2_cmd,
p2_hash.sha256 AS p2_sha256
FROM
processes p0
LEFT JOIN signature s ON p0.path = s.path
LEFT JOIN file f ON p0.path = f.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
p0.start_time > 0
AND f.ctime > 0
AND p0.start_time > (strftime('%s', 'now') - 7200)
AND (p0.start_time - MAX(f.ctime, f.btime)) < 180
AND p0.start_time >= MAX(f.ctime, f.ctime)
AND s.authority NOT IN (
'Apple Mac OS Application Signing',
'Developer ID Application: SUSE LLC (2Q6FHJR3H3)',
'Developer ID Application: Adobe Inc. (JQ525L2MZD)',
'Developer ID Application: Brave Software, Inc. (KL8N8XSYF4)',
'Developer ID Application: Brother Industries, LTD. (5HCL85FLGW)',
'Developer ID Application: Bryan Jones (49EYHPJ4Q3)',
'Developer ID Application: CodeWeavers Inc. (9C6B7X7Z8E)',
'Developer ID Application: Corsair Memory, Inc. (Y93VXCB8Q5)',
'Developer ID Application: Objective-See, LLC (VBG97UB4TA)',
'Developer ID Application: Microsoft Corporation (UBF8T346G9)',
'Developer ID Application: Docker Inc (9BNSXJN65R)',
'Developer ID Application: Dropbox, Inc. (G7HH3F8CAK)',
'Developer ID Application: Emmanouil Konstantinidis (3YP8SXP3BF)',
'Developer ID Application: Galvanix (5BRAQAFB8B)',
'Developer ID Application: Sublime HQ Pty Ltd (Z6D26JE4Y4)',
'Developer ID Application: General Arcade (Pte. Ltd.) (S8JLSG5ES7)',
'Developer ID Application: GEORGE NACHMAN (H7V7XYVQ7D)',
'Developer ID Application: GitHub (VEKTX9H2N7)',
'Developer ID Application: Google LLC (EQHXZ8M8AV)',
'Developer ID Application: GPGTools GmbH (PKV8ZPD836)',
'Developer ID Application: JetBrains s.r.o. (2ZEFAR8TH3)',
'Developer ID Application: Kolide Inc (YZ3EM74M78)',
'Developer ID Application: Logitech Inc. (QED4VVPZWA)',
'Developer ID Application: Michael Jones (YD6LEYT6WZ)',
'Developer ID Application: Opal Camera Inc (97Z3HJWCRT)',
'Developer ID Application: OSQUERY A Series of LF Projects, LLC (3522FA9PXF)',
'Developer ID Application: RescueTime, Inc (FSY4RB8H39)',
'Developer ID Application: Seiko Epson Corporation (TXAEAV5RN4)',
'Developer ID Application: Yubico Limited (LQA3CS5MM7)',
'Developer ID Application: Zoom Video Communications, Inc. (BJ4HAAB9B3)',
'Software Signing'
)
AND NOT p0.path LIKE '/Applications/%.app/%'
AND NOT p0.path LIKE '%-go-build%'
AND NOT p0.path LIKE '/Library/Apple/System/%'
AND NOT p0.path LIKE '/Library/Application Support/Adobe/Adobe Desktop Common/%'
AND NOT p0.path LIKE '%/Library/Application Support/com.elgato.StreamDeck%' -- Known parent processes, typically GUI shells and updaters
AND NOT p0.path LIKE '/Library/Application Support/Logitech.localized/%'
AND NOT p0.path LIKE '/nix/store/%/bin/%'
AND NOT p0.path LIKE '/opt/homebrew/bin/%'
AND NOT p0.path LIKE '/opt/homebrew/Cellar/%'
AND NOT p0.path LIKE '/private/tmp/%/Creative Cloud Installer.app/Contents/MacOS/Install'
AND NOT p0.path LIKE '/private/tmp/go-build%'
AND NOT p0.path LIKE '/private/tmp/go-%/go/pkg/%'
AND NOT p0.path LIKE '/private/tmp/nix-build-%'
AND NOT p0.path LIKE '/private/var/db/com.apple.xpc.roleaccountd.staging/%'
AND NOT p0.path LIKE '/private/var/folders/%/bin/%'
AND NOT p0.path LIKE '/private/var/folders/%/go-build%'
AND NOT p0.path LIKE '/private/var/folders/%/T/download/ARMDCHammer'
AND NOT p0.path LIKE '/private/var/folders/%/GoLand/%'
AND NOT p0.path LIKE '/private/var/folders/%/T/pulumi-go.%'
AND NOT p0.path LIKE '/Users/%/bin/%'
AND NOT p0.path LIKE '/Users/%/code/%'
AND NOT p0.path LIKE '/Users/%/src/%'
AND NOT p0.path LIKE '/Users/%/Library/Application Support/%/Contents/MacOS/%'
AND NOT p0.path LIKE '/Users/%/Library/Application Support/iTerm2/iTermServer-%'
AND NOT p0.path LIKE '/Users/%/Library/Caches/%/Contents/MacOS/%'
AND NOT p0.path LIKE '/Users/%/Library/Google/%.bundle/Contents/Helpers/%'
AND NOT p0.path LIKE '/Users/%/Library/Mobile Documents/%/Contents/Frameworks%'
AND NOT p0.path LIKE '/Users/%/terraform-provider-%'
AND NOT p0.path LIKE '/Users/%/%.test'
AND NOT p0.path LIKE '/usr/local/bin/%'
AND NOT p0.path LIKE '/usr/local/Cellar/%'
AND NOT p0.path LIKE '/usr/local/kolide-k2/bin/osqueryd-updates/%/osqueryd'
AND NOT p0.path LIKE '%/.vscode/extensions/%'
AND NOT p0.path LIKE '/Users/%/Library/Caches/snyk/%/snyk-macos'
AND NOT p0.path LIKE '/Users/%/Parallels/%/Contents/MacOS/WinAppHelper'
AND NOT p0.path LIKE '/Users/%/Applications (Parallels)/%/Contents/MacOS/WinAppHelper'
AND NOT (
p0.path LIKE '/Users/%'
AND p0.uid > 499
AND f.ctime = f.mtime
AND f.uid = p0.uid
AND p0.cmdline LIKE './%'
)
AND NOT (
p0.path LIKE '/Users/%/Library/Printers/EPSON%/Contents/MacOS/PrinterProxy'
AND s.identifier = 'com.apple.print.PrinterProxy'
AND s.authority = ''
)
GROUP BY
p0.pid