diff --git a/integrations/cuckoo/manifest.yaml b/integrations/cuckoo/manifest.yaml new file mode 100644 index 0000000..cdf0413 --- /dev/null +++ b/integrations/cuckoo/manifest.yaml @@ -0,0 +1,87 @@ +id: cuckoo +name: Cuckoo Sandbox +version: 1.0.0 +description: "Cuckoo Sandbox (REST API) — dynamic malware analysis: submit files and URLs for detonation, read task status and reports, list tasks, and delete a task. Optional token authentication; stdlib-only, no extra Python dependencies." +changelog: "1.0.0 — Initial release: submit file/URL, get task, get report, list tasks, delete task." +category: enrichment + +# Per-instance configuration. If the Cuckoo API server requires a token it is +# sent as 'Authorization: Bearer '. +config_schema: + properties: + base_url: + type: string + description: "Cuckoo API URL (e.g. http://cuckoo.example.com:8090)" + api_token: + type: string + description: "API token (leave empty if the API server is unauthenticated)" + x-soar-sensitive: true + insecure: + type: boolean + description: "Trust any TLS certificate (not secure)" + default: false + required: + - base_url + +commands: + - id: submit_file + name: cuckoo-submit-file + description: "Submit a file (base64) for analysis." + inputs_schema: + properties: + file_name: { type: string, description: "File name" } + content_base64: { type: string, description: "File content, base64-encoded" } + required: [file_name, content_base64] + outputs_schema: { properties: {} } + - id: submit_url + name: cuckoo-submit-url + description: "Submit a URL for analysis." + inputs_schema: + properties: + url: { type: string, description: "URL to detonate" } + required: [url] + outputs_schema: { properties: {} } + - id: get_task + name: cuckoo-get-task + description: "Get a task's status and metadata." + risk: read + inputs_schema: + properties: + task_id: { type: string, description: "Task ID" } + required: [task_id] + outputs_schema: { properties: {} } + - id: get_report + name: cuckoo-get-report + description: "Get a task's full analysis report (JSON)." + risk: read + inputs_schema: + properties: + task_id: { type: string, description: "Task ID" } + required: [task_id] + outputs_schema: { properties: {} } + - id: list_tasks + name: cuckoo-list-tasks + description: "List recent tasks." + risk: read + inputs_schema: + properties: + limit: { type: number, description: "Max tasks (default 50)" } + required: [] + outputs_schema: { properties: {} } + - id: delete_task + name: cuckoo-delete-task + description: "Delete a task and its data." + inputs_schema: + properties: + task_id: { type: string, description: "Task ID" } + required: [task_id] + outputs_schema: { properties: {} } + + - id: test_connection + name: cuckoo-test-connection + description: "Verify connectivity (used by the Test button)." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } diff --git a/integrations/cuckoo/scripts/delete_task.py b/integrations/cuckoo/scripts/delete_task.py new file mode 100644 index 0000000..2ce78d0 --- /dev/null +++ b/integrations/cuckoo/scripts/delete_task.py @@ -0,0 +1,91 @@ +import json, os, sys, base64, ssl, uuid, urllib.parse, urllib.request, urllib.error + + +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 + + +def _headers(cfg, extra=None): + h = {"Accept": "application/json"} + if cfg.get("api_token"): + h["Authorization"] = "Bearer " + str(cfg["api_token"]) + if extra: + h.update(extra) + return h + + +def request(method, path, cfg, body=None, form=None): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data = None + extra = {} + if body is not None: + data = json.dumps(body).encode("utf-8") + extra["Content-Type"] = "application/json" + elif form is not None: + data = urllib.parse.urlencode(form).encode("utf-8") + extra["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=_headers(cfg, extra), method=method) + with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def multipart(fields, file_field, file_name, file_bytes): + boundary = "----riposte" + uuid.uuid4().hex + parts = [] + for k, v in (fields or {}).items(): + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + k + '"\r\n\r\n' + str(v) + "\r\n").encode("utf-8")) + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + file_field + '"; filename="' + file_name + '"\r\n' + "Content-Type: application/octet-stream\r\n\r\n").encode("utf-8")) + parts.append(file_bytes) + parts.append(("\r\n--" + boundary + "--\r\n").encode("utf-8")) + return b"".join(parts), "multipart/form-data; boundary=" + boundary + + +def request_multipart(path, cfg, fields, file_field, file_name, file_bytes): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data, content_type = multipart(fields, file_field, file_name, file_bytes) + req = urllib.request.Request(url, data=data, headers=_headers(cfg, {"Content-Type": content_type}), method="POST") + with urllib.request.urlopen(req, timeout=180, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _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) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def main(cfg, inputs): + task_id = inputs.get("task_id") + if not task_id: + raise Exception("task_id is required") + request("GET", "/tasks/delete/" + q(task_id), cfg) + return {"ok": True, "task_id": task_id} + + +_run(main) diff --git a/integrations/cuckoo/scripts/get_report.py b/integrations/cuckoo/scripts/get_report.py new file mode 100644 index 0000000..c916bcb --- /dev/null +++ b/integrations/cuckoo/scripts/get_report.py @@ -0,0 +1,90 @@ +import json, os, sys, base64, ssl, uuid, urllib.parse, urllib.request, urllib.error + + +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 + + +def _headers(cfg, extra=None): + h = {"Accept": "application/json"} + if cfg.get("api_token"): + h["Authorization"] = "Bearer " + str(cfg["api_token"]) + if extra: + h.update(extra) + return h + + +def request(method, path, cfg, body=None, form=None): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data = None + extra = {} + if body is not None: + data = json.dumps(body).encode("utf-8") + extra["Content-Type"] = "application/json" + elif form is not None: + data = urllib.parse.urlencode(form).encode("utf-8") + extra["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=_headers(cfg, extra), method=method) + with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def multipart(fields, file_field, file_name, file_bytes): + boundary = "----riposte" + uuid.uuid4().hex + parts = [] + for k, v in (fields or {}).items(): + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + k + '"\r\n\r\n' + str(v) + "\r\n").encode("utf-8")) + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + file_field + '"; filename="' + file_name + '"\r\n' + "Content-Type: application/octet-stream\r\n\r\n").encode("utf-8")) + parts.append(file_bytes) + parts.append(("\r\n--" + boundary + "--\r\n").encode("utf-8")) + return b"".join(parts), "multipart/form-data; boundary=" + boundary + + +def request_multipart(path, cfg, fields, file_field, file_name, file_bytes): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data, content_type = multipart(fields, file_field, file_name, file_bytes) + req = urllib.request.Request(url, data=data, headers=_headers(cfg, {"Content-Type": content_type}), method="POST") + with urllib.request.urlopen(req, timeout=180, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _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) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def main(cfg, inputs): + task_id = inputs.get("task_id") + if not task_id: + raise Exception("task_id is required") + return request("GET", "/tasks/report/" + q(task_id), cfg) + + +_run(main) diff --git a/integrations/cuckoo/scripts/get_task.py b/integrations/cuckoo/scripts/get_task.py new file mode 100644 index 0000000..511c0df --- /dev/null +++ b/integrations/cuckoo/scripts/get_task.py @@ -0,0 +1,90 @@ +import json, os, sys, base64, ssl, uuid, urllib.parse, urllib.request, urllib.error + + +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 + + +def _headers(cfg, extra=None): + h = {"Accept": "application/json"} + if cfg.get("api_token"): + h["Authorization"] = "Bearer " + str(cfg["api_token"]) + if extra: + h.update(extra) + return h + + +def request(method, path, cfg, body=None, form=None): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data = None + extra = {} + if body is not None: + data = json.dumps(body).encode("utf-8") + extra["Content-Type"] = "application/json" + elif form is not None: + data = urllib.parse.urlencode(form).encode("utf-8") + extra["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=_headers(cfg, extra), method=method) + with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def multipart(fields, file_field, file_name, file_bytes): + boundary = "----riposte" + uuid.uuid4().hex + parts = [] + for k, v in (fields or {}).items(): + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + k + '"\r\n\r\n' + str(v) + "\r\n").encode("utf-8")) + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + file_field + '"; filename="' + file_name + '"\r\n' + "Content-Type: application/octet-stream\r\n\r\n").encode("utf-8")) + parts.append(file_bytes) + parts.append(("\r\n--" + boundary + "--\r\n").encode("utf-8")) + return b"".join(parts), "multipart/form-data; boundary=" + boundary + + +def request_multipart(path, cfg, fields, file_field, file_name, file_bytes): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data, content_type = multipart(fields, file_field, file_name, file_bytes) + req = urllib.request.Request(url, data=data, headers=_headers(cfg, {"Content-Type": content_type}), method="POST") + with urllib.request.urlopen(req, timeout=180, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _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) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def main(cfg, inputs): + task_id = inputs.get("task_id") + if not task_id: + raise Exception("task_id is required") + return request("GET", "/tasks/view/" + q(task_id), cfg) + + +_run(main) diff --git a/integrations/cuckoo/scripts/list_tasks.py b/integrations/cuckoo/scripts/list_tasks.py new file mode 100644 index 0000000..8a518f0 --- /dev/null +++ b/integrations/cuckoo/scripts/list_tasks.py @@ -0,0 +1,86 @@ +import json, os, sys, base64, ssl, uuid, urllib.parse, urllib.request, urllib.error + + +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 + + +def _headers(cfg, extra=None): + h = {"Accept": "application/json"} + if cfg.get("api_token"): + h["Authorization"] = "Bearer " + str(cfg["api_token"]) + if extra: + h.update(extra) + return h + + +def request(method, path, cfg, body=None, form=None): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data = None + extra = {} + if body is not None: + data = json.dumps(body).encode("utf-8") + extra["Content-Type"] = "application/json" + elif form is not None: + data = urllib.parse.urlencode(form).encode("utf-8") + extra["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=_headers(cfg, extra), method=method) + with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def multipart(fields, file_field, file_name, file_bytes): + boundary = "----riposte" + uuid.uuid4().hex + parts = [] + for k, v in (fields or {}).items(): + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + k + '"\r\n\r\n' + str(v) + "\r\n").encode("utf-8")) + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + file_field + '"; filename="' + file_name + '"\r\n' + "Content-Type: application/octet-stream\r\n\r\n").encode("utf-8")) + parts.append(file_bytes) + parts.append(("\r\n--" + boundary + "--\r\n").encode("utf-8")) + return b"".join(parts), "multipart/form-data; boundary=" + boundary + + +def request_multipart(path, cfg, fields, file_field, file_name, file_bytes): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data, content_type = multipart(fields, file_field, file_name, file_bytes) + req = urllib.request.Request(url, data=data, headers=_headers(cfg, {"Content-Type": content_type}), method="POST") + with urllib.request.urlopen(req, timeout=180, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _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(cfg, inputs): + limit = inputs.get("limit") + limit = int(limit) if limit not in (None, "") else 50 + return request("GET", "/tasks/list/" + str(limit), cfg) + + +_run(main) diff --git a/integrations/cuckoo/scripts/submit_file.py b/integrations/cuckoo/scripts/submit_file.py new file mode 100644 index 0000000..d2edf67 --- /dev/null +++ b/integrations/cuckoo/scripts/submit_file.py @@ -0,0 +1,91 @@ +import json, os, sys, base64, ssl, uuid, urllib.parse, urllib.request, urllib.error + + +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 + + +def _headers(cfg, extra=None): + h = {"Accept": "application/json"} + if cfg.get("api_token"): + h["Authorization"] = "Bearer " + str(cfg["api_token"]) + if extra: + h.update(extra) + return h + + +def request(method, path, cfg, body=None, form=None): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data = None + extra = {} + if body is not None: + data = json.dumps(body).encode("utf-8") + extra["Content-Type"] = "application/json" + elif form is not None: + data = urllib.parse.urlencode(form).encode("utf-8") + extra["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=_headers(cfg, extra), method=method) + with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def multipart(fields, file_field, file_name, file_bytes): + boundary = "----riposte" + uuid.uuid4().hex + parts = [] + for k, v in (fields or {}).items(): + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + k + '"\r\n\r\n' + str(v) + "\r\n").encode("utf-8")) + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + file_field + '"; filename="' + file_name + '"\r\n' + "Content-Type: application/octet-stream\r\n\r\n").encode("utf-8")) + parts.append(file_bytes) + parts.append(("\r\n--" + boundary + "--\r\n").encode("utf-8")) + return b"".join(parts), "multipart/form-data; boundary=" + boundary + + +def request_multipart(path, cfg, fields, file_field, file_name, file_bytes): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data, content_type = multipart(fields, file_field, file_name, file_bytes) + req = urllib.request.Request(url, data=data, headers=_headers(cfg, {"Content-Type": content_type}), method="POST") + with urllib.request.urlopen(req, timeout=180, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _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(cfg, inputs): + file_name = inputs.get("file_name") + if not file_name: + raise Exception("file_name is required") + content_base64 = inputs.get("content_base64") + if not content_base64: + raise Exception("content_base64 is required") + file_bytes = base64.b64decode(content_base64) + return request_multipart("/tasks/create/file", cfg, {}, "file", file_name, file_bytes) + + +_run(main) diff --git a/integrations/cuckoo/scripts/submit_url.py b/integrations/cuckoo/scripts/submit_url.py new file mode 100644 index 0000000..a567a24 --- /dev/null +++ b/integrations/cuckoo/scripts/submit_url.py @@ -0,0 +1,87 @@ +import json, os, sys, base64, ssl, uuid, urllib.parse, urllib.request, urllib.error + + +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 + + +def _headers(cfg, extra=None): + h = {"Accept": "application/json"} + if cfg.get("api_token"): + h["Authorization"] = "Bearer " + str(cfg["api_token"]) + if extra: + h.update(extra) + return h + + +def request(method, path, cfg, body=None, form=None): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data = None + extra = {} + if body is not None: + data = json.dumps(body).encode("utf-8") + extra["Content-Type"] = "application/json" + elif form is not None: + data = urllib.parse.urlencode(form).encode("utf-8") + extra["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=_headers(cfg, extra), method=method) + with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def multipart(fields, file_field, file_name, file_bytes): + boundary = "----riposte" + uuid.uuid4().hex + parts = [] + for k, v in (fields or {}).items(): + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + k + '"\r\n\r\n' + str(v) + "\r\n").encode("utf-8")) + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + file_field + '"; filename="' + file_name + '"\r\n' + "Content-Type: application/octet-stream\r\n\r\n").encode("utf-8")) + parts.append(file_bytes) + parts.append(("\r\n--" + boundary + "--\r\n").encode("utf-8")) + return b"".join(parts), "multipart/form-data; boundary=" + boundary + + +def request_multipart(path, cfg, fields, file_field, file_name, file_bytes): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data, content_type = multipart(fields, file_field, file_name, file_bytes) + req = urllib.request.Request(url, data=data, headers=_headers(cfg, {"Content-Type": content_type}), method="POST") + with urllib.request.urlopen(req, timeout=180, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _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(cfg, inputs): + url = inputs.get("url") + if not url: + raise Exception("url is required") + return request("POST", "/tasks/create/url", cfg, form={"url": url}) + + +_run(main) diff --git a/integrations/cuckoo/scripts/test_connection.py b/integrations/cuckoo/scripts/test_connection.py new file mode 100644 index 0000000..91ae7ed --- /dev/null +++ b/integrations/cuckoo/scripts/test_connection.py @@ -0,0 +1,85 @@ +import json, os, sys, base64, ssl, uuid, urllib.parse, urllib.request, urllib.error + + +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 + + +def _headers(cfg, extra=None): + h = {"Accept": "application/json"} + if cfg.get("api_token"): + h["Authorization"] = "Bearer " + str(cfg["api_token"]) + if extra: + h.update(extra) + return h + + +def request(method, path, cfg, body=None, form=None): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data = None + extra = {} + if body is not None: + data = json.dumps(body).encode("utf-8") + extra["Content-Type"] = "application/json" + elif form is not None: + data = urllib.parse.urlencode(form).encode("utf-8") + extra["Content-Type"] = "application/x-www-form-urlencoded" + req = urllib.request.Request(url, data=data, headers=_headers(cfg, extra), method=method) + with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def multipart(fields, file_field, file_name, file_bytes): + boundary = "----riposte" + uuid.uuid4().hex + parts = [] + for k, v in (fields or {}).items(): + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + k + '"\r\n\r\n' + str(v) + "\r\n").encode("utf-8")) + parts.append(("--" + boundary + "\r\n" + 'Content-Disposition: form-data; name="' + file_field + '"; filename="' + file_name + '"\r\n' + "Content-Type: application/octet-stream\r\n\r\n").encode("utf-8")) + parts.append(file_bytes) + parts.append(("\r\n--" + boundary + "--\r\n").encode("utf-8")) + return b"".join(parts), "multipart/form-data; boundary=" + boundary + + +def request_multipart(path, cfg, fields, file_field, file_name, file_bytes): + url = str(cfg.get("base_url", "")).rstrip("/") + path + data, content_type = multipart(fields, file_field, file_name, file_bytes) + req = urllib.request.Request(url, data=data, headers=_headers(cfg, {"Content-Type": content_type}), method="POST") + with urllib.request.urlopen(req, timeout=180, context=_ctx(cfg)) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _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(cfg, inputs): + request("GET", "/cuckoo/status", cfg) + return {"ok": True} + + +_run(main)