osquery-defense-kit/detection/execution/sketchy-fetcher.sql

165 lines
4.6 KiB
MySQL
Raw Normal View History

2022-10-14 18:19:13 +00:00
-- Suspicious URL requests by built-in fetching tools (state-based)
--
-- refs:
-- * https://attack.mitre.org/techniques/T1105/ (Ingress Tool Transfer)
-- * https://attack.mitre.org/techniques/T1571/ (Non-Standard Port)
--
2022-10-14 18:19:13 +00:00
-- tags: transient process state
-- platform: posix
SELECT
REGEX_MATCH (p0.cmdline, '(\w+:\/\/.*)\b', 1) AS url,
REGEX_MATCH (p0.cmdline, '//(\d+\.\d+\.\d+\.\d+)[:/]', 1) AS ip,
REGEX_MATCH (p0.cmdline, ':(\d+)', 1) AS port,
REGEX_MATCH (p0.cmdline, '//([\w\-\.]+)[:/]', 1) AS addr,
REGEX_MATCH (p0.cmdline, '//[\w\-\.]+\.(\w+)[:/]', 1) AS tld,
-- Child
p0.pid AS p0_pid,
p0.path AS p0_path,
p0.name AS p0_name,
p0.start_time AS p0_start,
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.start_time AS p1_start,
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.start_time AS p2_start,
p2.path AS p2_path,
p2.cmdline AS p2_cmd,
p2_hash.sha256 AS p2_sha256
FROM
processes p0
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
2022-09-09 14:16:28 +00:00
WHERE
-- NOTE: Sync remaining portion with sketchy-fetcher-events
(
INSTR(p0.cmdline, 'wget ') > 0
OR INSTR(p0.cmdline, 'curl ') > 0
)
-- Sketchy fetcher events always seem to contain a switch
AND p0.cmdline LIKE '%-%'
AND p0.cmdline LIKE '%/%'
AND (
ip NOT IN ('', '127.0.0.1', '::1')
OR port != ''
OR tld NOT IN (
'',
'app',
'ca',
'cloud',
'com',
'de',
'dev',
'edu',
'fun',
'gov',
'io',
'md',
'mil',
'net',
'org',
'se',
'sh',
'so',
'uk'
)
OR p0.cmdline LIKE '%chmod%'
OR p0.cmdline LIKE '%.onion%'
OR p0.cmdline LIKE '%tor2web%'
OR p0.cmdline LIKE '%aliyun%'
OR p0.cmdline LIKE '%pastebin%'
OR p0.cmdline LIKE '%curl %--user-agent%'
OR p0.cmdline LIKE '%curl -k%'
OR p0.cmdline LIKE '%curl -sL %'
OR p0.cmdline LIKE '%curl%-o-%'
OR p0.cmdline LIKE '%curl%--insecure%'
OR p0.cmdline LIKE '%wget %--user-agent%'
OR p0.cmdline LIKE '%wget %--no-check-certificate%'
OR p0.cmdline LIKE '%curl%--connect-timeout%'
OR p0.cmdline LIKE '%wget -nc%'
OR p0.cmdline LIKE '%wget -t%'
OR p0.cmdline LIKE '%wget -q%'
OR (
p0.cmdline LIKE '%wget %'
AND p0.euid < 500
-- TODO: Update this query to understand containers
AND p1.path NOT IN (
"/usr/bin/bwrap",
"/bin/busybox",
"/usr/bin/melange"
)
2022-09-15 13:34:45 +00:00
)
OR (
p0.cmdline LIKE '%curl %'
AND p0.euid < 500
AND p0.cmdline NOT LIKE "%./configure %--with-curl%"
)
)
-- Exceptions for all calls
AND p1.name NOT IN ('makepkg') -- Exceptions for non-privileged calls
AND NOT (
p0.euid > 500
AND (
p0.cmdline LIKE '%--dump-header%'
OR p0.cmdline LIKE '%/api/v%'
OR p0.cmdline LIKE '%curl -X %'
OR p0.cmdline LIKE '%go mod %'
OR p0.cmdline LIKE '%application/json%'
OR p0.cmdline LIKE '%grpcurl%'
OR p0.cmdline LIKE '%Homebrew%'
OR p0.cmdline LIKE '%Nixpkgs/%'
OR p0.cmdline LIKE '%If-None-Match%'
OR p0.cmdline LIKE '%ctlog%'
OR p0.cmdline LIKE '%.well-known/openid-configuration%'
OR p0.cmdline LIKE '%/openid/v1/jwks%'
OR p0.cmdline LIKE '%--progress-bar%'
OR p1.cmdline LIKE '%brew.rb%'
OR p1.cmdline LIKE '%brew.sh%'
OR p1.cmdline LIKE '/nix/store/%-builder.sh'
OR p0.cmdline LIKE 'git %'
OR p0.cmdline LIKE '%LICENSES/vendor/%'
OR p0.cmdline LIKE 'curl -sL wttr.in%'
OR p0.cmdline LIKE '%localhost:%'
OR p0.cmdline LIKE '%127.0.0.1:%'
OR p0.name IN ('apko')
)
)
-- These are typically curl -k calls
-- We need the addr "IS NOT NULL" to avoid filtering out
-- NULL entries
AND NOT (
addr IS NOT NULL
2023-01-14 13:19:26 +00:00
AND (
2023-01-20 14:24:24 +00:00
addr IN (
'releases.hashicorp.com',
'github.com',
'dl.enforce.dev'
)
2023-01-14 13:19:26 +00:00
-- Ignore local addresses (Docker development)
OR addr NOT LIKE '%.%'
OR ip LIKE '172.21.%'
OR ip LIKE '192.168.%'
2023-01-14 13:19:26 +00:00
)
)
-- Qualys Cloud Agent
AND NOT (
addr = "169.254.169.254"
AND p2.path = "/usr/local/qualys/cloud-agent/bin/qualys-scan-util"
)
-- Elastic Agent
AND NOT p0.path LIKE '/Library/Elastic/Agent/%'
AND NOt p0.cmdline LIKE '%/osqueryd %'