feat(feed): add generic JSON feed connector (configurable field mapping)
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import json, os, sys, re, ssl, urllib.request, urllib.error
|
||||
|
||||
_IP = re.compile(r"^(?:\d{1,3}\.){3}\d{1,3}$")
|
||||
_HASH = re.compile(r"^[a-fA-F0-9]{32}$|^[a-fA-F0-9]{40}$|^[a-fA-F0-9]{64}$")
|
||||
_EMAIL = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
|
||||
_URL = re.compile(r"^[a-zA-Z][a-zA-Z0-9+.\-]*://")
|
||||
_DOMAIN = re.compile(r"^(?:[a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,}$")
|
||||
|
||||
|
||||
def detect_type(v):
|
||||
v = v.strip()
|
||||
if _IP.match(v):
|
||||
return "ip"
|
||||
if _URL.match(v):
|
||||
return "url"
|
||||
if _EMAIL.match(v):
|
||||
return "email"
|
||||
if _HASH.match(v):
|
||||
return "hash"
|
||||
if _DOMAIN.match(v):
|
||||
return "domain"
|
||||
return "unknown"
|
||||
|
||||
|
||||
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):
|
||||
h = {"User-Agent": "Riposte-SOAR", "Accept": "application/json"}
|
||||
t = cfg.get("api_token")
|
||||
if t:
|
||||
h["Authorization"] = "Bearer " + str(t)
|
||||
return h
|
||||
|
||||
|
||||
def _get(url, cfg):
|
||||
req = urllib.request.Request(url, headers=_headers(cfg))
|
||||
with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r:
|
||||
return r.read()
|
||||
|
||||
|
||||
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 _dig(obj, path):
|
||||
if not path:
|
||||
return obj
|
||||
cur = obj
|
||||
for part in path.split("."):
|
||||
if isinstance(cur, dict):
|
||||
cur = cur.get(part)
|
||||
else:
|
||||
return None
|
||||
return cur
|
||||
|
||||
|
||||
def main(cfg, inputs):
|
||||
url = cfg.get("feed_url")
|
||||
if not url:
|
||||
raise Exception("feed_url is required")
|
||||
|
||||
array_path = inputs.get("array_path") or cfg.get("array_path") or ""
|
||||
value_field = inputs.get("value_field") or cfg.get("value_field") or "value"
|
||||
type_field = inputs.get("type_field") or cfg.get("type_field")
|
||||
ioc_type = str(inputs.get("ioc_type") or "auto").lower()
|
||||
|
||||
raw = _get(url, cfg)
|
||||
data = json.loads(raw) if raw else {}
|
||||
|
||||
arr = _dig(data, array_path) if array_path else data
|
||||
if isinstance(arr, dict):
|
||||
arr = list(arr.values())
|
||||
if not isinstance(arr, list):
|
||||
arr = []
|
||||
|
||||
maxn = int(inputs.get("max_indicators") or 0)
|
||||
out = []
|
||||
for e in arr:
|
||||
if isinstance(e, dict):
|
||||
val = _dig(e, value_field) if "." in value_field else e.get(value_field)
|
||||
t = e.get(type_field) if type_field else None
|
||||
else:
|
||||
val = e
|
||||
t = None
|
||||
if val in (None, ""):
|
||||
continue
|
||||
val = str(val).strip()
|
||||
if not val:
|
||||
continue
|
||||
typ = str(t).lower() if t else (ioc_type if ioc_type != "auto" else detect_type(val))
|
||||
out.append({"value": val, "type": typ})
|
||||
if maxn and len(out) >= maxn:
|
||||
break
|
||||
|
||||
return {"source": url, "count": len(out), "indicators": out}
|
||||
|
||||
|
||||
_run(main)
|
||||
Reference in New Issue
Block a user