2022-10-12 01:53:36 +00:00
|
|
|
-- This query looks for exotic open file/process combinations
|
|
|
|
--
|
|
|
|
-- It's unfortunately of limited use, as the query is slow (250ms)
|
|
|
|
-- and it requires catching a program at the exact moment it has
|
|
|
|
-- the file open.
|
2022-10-13 18:59:32 +00:00
|
|
|
|
|
|
|
-- tags: ephemeral critical access
|
2022-10-12 01:53:36 +00:00
|
|
|
SELECT
|
|
|
|
pof.pid,
|
|
|
|
pof.fd,
|
|
|
|
pof.path,
|
|
|
|
f.uid AS file_uid,
|
|
|
|
p.cwd AS cwd,
|
|
|
|
p.euid,
|
|
|
|
p.uid AS process_uid,
|
|
|
|
p.name AS program_name,
|
|
|
|
p.cmdline AS cmdline,
|
|
|
|
pp.name AS parent_name,
|
|
|
|
pp.cwd AS parent_cwd,
|
|
|
|
pp.path AS parent_path,
|
|
|
|
hp.sha256 AS parent_sha256,
|
|
|
|
pf.filename AS program_base,
|
|
|
|
hash.sha256,
|
2022-10-13 18:59:32 +00:00
|
|
|
REPLACE(f.directory, u.directory, '~') AS dir,
|
2022-10-12 01:53:36 +00:00
|
|
|
CONCAT (
|
|
|
|
pf.filename,
|
2022-10-13 18:59:32 +00:00
|
|
|
',',
|
2022-10-12 01:53:36 +00:00
|
|
|
p.name,
|
2022-10-13 18:59:32 +00:00
|
|
|
',',
|
2022-10-12 01:53:36 +00:00
|
|
|
IIF(
|
|
|
|
REGEX_MATCH (
|
2022-10-13 18:59:32 +00:00
|
|
|
REPLACE(f.directory, u.directory, '~'),
|
|
|
|
'([/~].*?/.*?/.*?)/',
|
2022-10-12 01:53:36 +00:00
|
|
|
1
|
2022-10-13 18:59:32 +00:00
|
|
|
) != '',
|
2022-10-12 01:53:36 +00:00
|
|
|
REGEX_MATCH (
|
2022-10-13 18:59:32 +00:00
|
|
|
REPLACE(f.directory, u.directory, '~'),
|
|
|
|
'([/~].*?/.*?/.*?)/',
|
2022-10-12 01:53:36 +00:00
|
|
|
1
|
|
|
|
),
|
2022-10-13 18:59:32 +00:00
|
|
|
REPLACE(f.directory, u.directory, '~')
|
2022-10-12 01:53:36 +00:00
|
|
|
)
|
|
|
|
) AS exception_key
|
|
|
|
FROM
|
|
|
|
process_open_files pof
|
|
|
|
LEFT JOIN processes p ON pof.pid = p.pid
|
|
|
|
LEFT JOIN processes pp ON p.parent = pp.pid
|
|
|
|
LEFT JOIN file f ON pof.path = f.path
|
|
|
|
LEFT JOIN file pf ON p.path = pf.path
|
|
|
|
LEFT JOIN users u ON p.uid = u.uid
|
|
|
|
LEFT JOIN hash ON p.path = hash.path
|
|
|
|
LEFT JOIN hash hp ON pp.path = hp.path
|
|
|
|
WHERE
|
2022-10-13 18:59:32 +00:00
|
|
|
f.uid != ''
|
|
|
|
AND pf.filename != ''
|
2022-10-12 01:53:36 +00:00
|
|
|
AND (
|
2022-10-13 18:59:32 +00:00
|
|
|
pof.path LIKE '/home/%/.ssh/%'
|
|
|
|
OR pof.path LIKE '/home/%/.mozilla/firefox/%'
|
|
|
|
OR pof.path LIKE '/home/%/.config/google-chrome/%'
|
|
|
|
OR pof.path LIKE '/root/.ssh/%'
|
|
|
|
OR pof.path LIKE '/root/.bash_history'
|
|
|
|
OR pof.path LIKE '/home/%/.config/gcloud/%'
|
|
|
|
OR pof.path LIKE '/home/%/.config/Slack/%'
|
|
|
|
OR pof.path LIKE '/home/%/.bash_history'
|
|
|
|
OR pof.path LIKE '/home/%/.cache/mozilla/firefox%'
|
|
|
|
OR pof.path LIKE '/home/%/.config/mozilla/firefox%'
|
|
|
|
OR pof.path LIKE '/home/%/.aws%'
|
2022-10-12 01:53:36 +00:00
|
|
|
)
|
|
|
|
AND NOT (
|
|
|
|
file_uid == process_uid
|
|
|
|
AND exception_key IN (
|
2022-10-13 18:59:32 +00:00
|
|
|
'aws,aws,~/.aws',
|
|
|
|
'chrome_crashpad_handler,chrome_crashpad,',
|
|
|
|
'chrome_crashpad_handler,chrome_crashpad,~/.config/google-chrome',
|
|
|
|
'chrome,chrome,~/.config/google-chrome',
|
|
|
|
'firefox,.firefox-wrappe,~/.cache/mozilla',
|
|
|
|
'firefox,.firefox-wrappe,~/.mozilla/firefox',
|
|
|
|
'firefox,file:// Content,~/.mozilla/firefox',
|
|
|
|
'firefox,firefox,~/.cache/mozilla',
|
|
|
|
'firefox,firefox,~/.mozilla/firefox',
|
|
|
|
'firefox,file:// Content,~/.cache/mozilla',
|
|
|
|
'firefox,firefox,~/snap/firefox',
|
|
|
|
'firefox,Isolated Servic,~/.cache/mozilla',
|
|
|
|
'firefox,Isolated Servic,~/snap/firefox',
|
|
|
|
'firefox,Isolated Web Co,~/.cache/mozilla',
|
|
|
|
'firefox,Isolated Web Co,~/.mozilla/firefox',
|
|
|
|
'firefox,Isolated Web Co,~/snap/firefox',
|
|
|
|
'firefox,Privileged Cont,~/.cache/mozilla',
|
|
|
|
'firefox,Privileged Cont,~/.mozilla/firefox',
|
|
|
|
'firefox,Privileged Cont,~/snap/firefox',
|
|
|
|
'firefox,Web Content,~/.cache/mozilla',
|
|
|
|
'firefox,Web Content,~/snap/firefox',
|
|
|
|
'firefox,WebExtensions,~/.cache/mozilla',
|
|
|
|
'firefox,WebExtensions,~/.mozilla/firefox',
|
|
|
|
'firefox,WebExtensions,~/snap/firefox',
|
|
|
|
'plugin-container,MainThread,~/.mozilla/firefox',
|
|
|
|
'slack,slack,~/.config/Slack',
|
|
|
|
'slack,slack,~/snap/slack'
|
2022-10-12 01:53:36 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
GROUP BY
|
|
|
|
pof.pid,
|
|
|
|
pof.path
|