90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
import json, os, sys, ssl, urllib.parse, urllib.request, urllib.error
|
|
|
|
BASE = "https://endpoints.office.com/endpoints/"
|
|
# A fixed client request id is acceptable for this public, unauthenticated API.
|
|
CLIENT_ID = "b10c5ed1-bad1-445f-b386-b919946339a7"
|
|
|
|
|
|
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 _get(url, cfg):
|
|
req = urllib.request.Request(
|
|
url, headers={"User-Agent": "Riposte-SOAR", "Accept": "application/json"}
|
|
)
|
|
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 main(cfg, inputs):
|
|
instance = str(inputs.get("instance") or cfg.get("instance") or "Worldwide")
|
|
include_urls = inputs.get("include_urls")
|
|
if include_urls is None:
|
|
include_urls = cfg.get("include_urls")
|
|
if include_urls is None:
|
|
include_urls = True
|
|
|
|
url = BASE + urllib.parse.quote(instance, safe="") + "?" + urllib.parse.urlencode({"clientrequestid": CLIENT_ID})
|
|
raw = _get(url, cfg)
|
|
data = json.loads(raw) if raw else []
|
|
if not isinstance(data, list):
|
|
data = []
|
|
|
|
maxn = int(inputs.get("max_indicators") or 0)
|
|
seen = set()
|
|
out = []
|
|
|
|
def add(value, typ, area):
|
|
key = (typ, value)
|
|
if key in seen:
|
|
return False
|
|
seen.add(key)
|
|
out.append({"value": value, "type": typ, "service_area": area, "provider": "microsoft365"})
|
|
return not (maxn and len(out) >= maxn)
|
|
|
|
for ep in data:
|
|
if not isinstance(ep, dict):
|
|
continue
|
|
area = ep.get("serviceArea")
|
|
for cidr in ep.get("ips", []) or []:
|
|
typ = "cidr" if "/" in str(cidr) else "ip"
|
|
if not add(str(cidr), typ, area):
|
|
return {"source": "microsoft365:" + instance, "count": len(out), "indicators": out}
|
|
if include_urls:
|
|
for u in ep.get("urls", []) or []:
|
|
dom = str(u).lstrip("*.").strip()
|
|
if not dom:
|
|
continue
|
|
if not add(dom, "domain", area):
|
|
return {"source": "microsoft365:" + instance, "count": len(out), "indicators": out}
|
|
|
|
return {"source": "microsoft365:" + instance, "count": len(out), "indicators": out}
|
|
|
|
|
|
_run(main)
|