f34c87f0c8
search_detections and search_ngsiem_detections now return a query block (FQL sent, Falcon's total, errors) next to resources, so an empty run can be told apart from a wrong region or an API client that sees no alerts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
3.2 KiB
Python
75 lines
3.2 KiB
Python
import json, os, sys, urllib.request, urllib.parse, urllib.error
|
|
|
|
SECRETS = json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
|
INPUTS = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
|
BASE = SECRETS.get("base_url", "https://api.crowdstrike.com").rstrip("/")
|
|
|
|
|
|
def token():
|
|
data = urllib.parse.urlencode({"client_id": SECRETS.get("client_id", ""), "client_secret": SECRETS.get("client_secret", "")}).encode()
|
|
req = urllib.request.Request(BASE + "/oauth2/token", data=data, headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
return json.loads(r.read()).get("access_token", "")
|
|
|
|
|
|
def call(method, path, tok, params=None, body=None):
|
|
url = BASE + path
|
|
if params:
|
|
# params is a list of (key, value) tuples to allow repeated keys (e.g. ids)
|
|
url += "?" + urllib.parse.urlencode(params)
|
|
data = json.dumps(body).encode() if body is not None else None
|
|
headers = {"Authorization": "Bearer " + tok, "Accept": "application/json"}
|
|
if data is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
with urllib.request.urlopen(req, timeout=90) as r:
|
|
raw = r.read()
|
|
return json.loads(raw) if raw else {}
|
|
|
|
|
|
def csv(v):
|
|
return [x.strip() for x in str(v or "").split(",") if x.strip()]
|
|
|
|
|
|
def main():
|
|
tok = token()
|
|
# Next-Gen SIEM correlation rules raise their detections in the same unified
|
|
# Alerts API as the EDR, under product 'ngsiem'. The product clause is always
|
|
# kept, so a filter narrows the NG-SIEM detections and never widens past them.
|
|
fql = "product:'ngsiem'"
|
|
extra = str(INPUTS.get("filter") or "").strip()
|
|
if extra:
|
|
fql += "+(" + extra + ")"
|
|
# The alert rule's watermark lands in created_from, folded into the FQL as
|
|
# created_timestamp (see search_detections for why it is not the filter input).
|
|
since = str(INPUTS.get("created_from") or "").strip().strip("'\"")
|
|
if since:
|
|
fql += "+created_timestamp:>'" + since + "'"
|
|
limit = INPUTS.get("limit", 50)
|
|
params = [("filter", fql), ("limit", limit), ("sort", "created_timestamp.desc")]
|
|
found = call("GET", "/alerts/queries/alerts/v2", tok, params=params)
|
|
ids = found.get("resources", []) or []
|
|
if ids:
|
|
result = call("POST", "/alerts/entities/alerts/v2", tok, body={"composite_ids": ids})
|
|
else:
|
|
result = {"resources": []}
|
|
# An empty page says nothing on its own: keep what the query answered — the
|
|
# FQL actually sent, Falcon's total for it and any errors — so a run with no
|
|
# alerts reads as "matched nothing" rather than as a silent failure.
|
|
result["query"] = {
|
|
"filter": fql,
|
|
"total": ((found.get("meta") or {}).get("pagination") or {}).get("total"),
|
|
"errors": found.get("errors") or [],
|
|
}
|
|
print(json.dumps(result))
|
|
|
|
|
|
try:
|
|
main()
|
|
except urllib.error.HTTPError as e:
|
|
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(json.dumps({"error": str(e)}))
|
|
sys.exit(1)
|