osquery-defense-kit/detection/c2/unexpected-https-client-lin...

93 lines
3.0 KiB
MySQL
Raw Normal View History

2022-10-20 11:04:18 +00:00
-- Unexpected programs communicating over HTTPS (state-based)
--
-- This query is a bit awkward and hobbled due to the lack of osquery support
-- for looking up binary signatures in Linux.
--
-- references:
-- * https://attack.mitre.org/techniques/T1071/ (C&C, Application Layer Protocol)
--
-- tags: transient state net rapid
-- platform: linux
2022-10-20 17:50:14 +00:00
SELECT s.remote_address,
2022-10-20 13:11:29 +00:00
p.name,
p.path,
p.cmdline AS child_cmd,
p.cwd,
pp.path AS parent_path,
p.parent AS parent_pid,
pp.cmdline AS parent_cmd,
s.state,
hash.sha256,
-- This intentionally avoids file.path, as it won't join across mount namespaces
CONCAT (
MIN(p.euid, 500),
',',
REPLACE(
2022-10-20 17:50:14 +00:00
REPLACE(
REGEX_MATCH (p.path, '(/.*?)/', 1),
'/nix',
'/usr'
),
'/snap',
'/opt'
) '/',
2022-10-20 13:11:29 +00:00
REGEX_MATCH (p.path, '.*/(.*?)$', 1),
',',
MIN(f.uid, 500),
'u,',
MIN(f.gid, 500),
'g,',
p.name
) AS exception_key
2022-10-20 17:50:14 +00:00
FROM process_open_sockets s
2022-10-20 13:11:29 +00:00
LEFT JOIN processes p ON s.pid = p.pid
LEFT JOIN processes pp ON p.parent = pp.pid
LEFT JOIN file f ON p.path = f.path
LEFT JOIN hash ON p.path = hash.path
2022-10-20 17:50:14 +00:00
WHERE protocol IN (6, 17)
2022-10-20 13:11:29 +00:00
AND s.remote_port = 443
AND s.remote_address NOT IN ('127.0.0.1', '::ffff:127.0.0.1', '::1')
AND s.remote_address NOT LIKE 'fe80:%'
AND s.remote_address NOT LIKE '127.%'
AND s.remote_address NOT LIKE '192.168.%'
AND s.remote_address NOT LIKE '172.1%'
AND s.remote_address NOT LIKE '172.2%'
AND s.remote_address NOT LIKE '172.30.%'
AND s.remote_address NOT LIKE '172.31.%'
AND s.remote_address NOT LIKE '::ffff:172.%'
AND s.remote_address NOT LIKE '10.%'
AND s.remote_address NOT LIKE '::ffff:10.%'
AND s.remote_address NOT LIKE 'fc00:%'
AND p.path != ''
AND NOT exception_key IN (
2022-10-20 17:17:52 +00:00
'0,/usr/dockerd,0u,0g,dockerd',
2022-10-20 17:50:14 +00:00
'0,/usr/flatpak-system-helper,0u,0g,flatpak-system-',
2022-10-20 13:11:29 +00:00
'0,/usr/launcher,0u,0g,launcher',
'0,/usr/packagekitd,0u,0g,packagekitd',
'0,/usr/tailscaled,0u,0g,tailscaled',
2022-10-20 13:11:29 +00:00
'0,/usr/.tailscaled-wrapped,0u,0g,.tailscaled-wra',
'500,/app/slack,u,g,slack',
2022-10-20 17:17:52 +00:00
'500,/app/zoom.real,u,g,zoom.real',
'500,/home/chainctl,500u,500g,chainctl',
'500,/ko-app/chainctl,u,g,chainctl',
2022-10-20 13:11:29 +00:00
'500,/ko-app/controlplane,u,g,controlplane',
'500,/opt/chrome,0u,0g,chrome',
2022-10-20 17:50:14 +00:00
'500,/opt/firefox,0u,0g,firefox',
'500,/opt/slack,0u,0g,slack',
2022-10-20 13:11:29 +00:00
'500,/opt/spotify,0u,0g,spotify',
'500,/usr/chrome,0u,0g,chrome',
'500,/usr/code,0u,0g,code',
2022-10-20 17:17:52 +00:00
'500,/usr/curl,0u,0g,curl',
2022-10-20 17:53:18 +00:00
'500,/usr/electron,0u,0g,electron',
2022-10-20 13:11:29 +00:00
'500,/usr/firefox,0u,0g,firefox',
'500,/usr/firefox,0u,0g,.firefox-wrappe',
2022-10-20 17:50:14 +00:00
'500,/usr/flatpak-oci-authenticator,0u,0g,flatpak-oci-aut',
2022-10-20 13:11:29 +00:00
'500,/usr/geoclue,0u,0g,geoclue',
2022-10-20 17:17:52 +00:00
'500,/usr/gitsign,0u,0g,gitsign',
2022-10-20 13:11:29 +00:00
'500,/usr/gnome-software,0u,0g,gnome-software',
'500,/usr/kubectl,500u,500g,kubectl',
2022-10-20 13:11:29 +00:00
'500,/usr/slack,0u,0g,slack',
'500,/usr/syncthing,0u,0g,syncthing'
2022-10-20 17:50:14 +00:00
) -- stay weird, NixOS (Fastly nix mirror)
AND NOT child_cmd = '/run/current-system/sw/bin/bash'
2022-10-20 17:50:14 +00:00
GROUP BY p.cmdline