1c3936992b
Cybereason API, 7 commands: query Malops, list sensors, get machine details, isolate/un-isolate machine (containment), block file hash. Session (login) auth, stdlib-only. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
87 lines
2.8 KiB
Python
87 lines
2.8 KiB
Python
import json, os, sys, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl
|
|
|
|
|
|
def _cfg():
|
|
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
|
|
|
|
|
def _inputs():
|
|
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
|
|
|
|
|
def _ctx(cfg):
|
|
if cfg.get("insecure"):
|
|
c = ssl.create_default_context()
|
|
c.check_hostname = False
|
|
c.verify_mode = ssl.CERT_NONE
|
|
return c
|
|
return None
|
|
|
|
|
|
class Client:
|
|
def __init__(self, cfg):
|
|
self.cfg = cfg
|
|
self.base = str(cfg.get("server_url", "")).rstrip("/")
|
|
ctx = _ctx(cfg)
|
|
self.opener = urllib.request.build_opener(
|
|
urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()),
|
|
urllib.request.HTTPSHandler(context=ctx) if ctx else urllib.request.HTTPSHandler(),
|
|
)
|
|
|
|
def login(self):
|
|
form = urllib.parse.urlencode({
|
|
"username": self.cfg.get("username", ""),
|
|
"password": self.cfg.get("password", ""),
|
|
}).encode("utf-8")
|
|
req = urllib.request.Request(self.base + "/login.html", data=form,
|
|
headers={"Content-Type": "application/x-www-form-urlencoded"}, method="POST")
|
|
with self.opener.open(req, timeout=60) as r:
|
|
r.read()
|
|
|
|
def call(self, method, path, body=None):
|
|
url = self.base + path
|
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
headers = {"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 self.opener.open(req, timeout=90) as r:
|
|
raw = r.read()
|
|
try:
|
|
return json.loads(raw) if raw else {}
|
|
except Exception:
|
|
return {"raw": raw.decode("utf-8", "replace")}
|
|
|
|
|
|
def _run(fn):
|
|
try:
|
|
cfg = _cfg()
|
|
inputs = _inputs()
|
|
client = Client(cfg)
|
|
client.login()
|
|
print(json.dumps(fn(client, inputs)))
|
|
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)
|
|
|
|
|
|
def main(client, inputs):
|
|
sensor_ids = inputs.get("sensor_ids")
|
|
if not sensor_ids:
|
|
raise Exception("sensor_ids is required")
|
|
|
|
sensor_ids_list = [s.strip() for s in str(sensor_ids).split(",") if s.strip()]
|
|
if not sensor_ids_list:
|
|
raise Exception("sensor_ids is required")
|
|
|
|
resp = client.call("POST", "/rest/sensors/actions/isolate", {"sensorsIds": sensor_ids_list})
|
|
if not resp:
|
|
return {"ok": True, "isolated": sensor_ids_list}
|
|
return resp
|
|
|
|
|
|
_run(main)
|