{"status":"ok","result":{"name":"huntbase-hub","title":"Huntbase Hub","desc":"Ingest open threat hunts from the Huntbase Hub (https://hub.huntbase.io) as\nplan:procedure graphs, with their queries, ATT&CK techniques, CVEs and\nsource reports.\n","modules":[{"name":"huntbase.hub","storm":"// huntbase.hub — map Huntbase Hub hunts onto the Synapse plan:* model.\n//\n//   hunt            -> plan:procedure        (:body = the hunt.md source)\n//   definition node -> plan:procedure:step   (query steps -(uses)> meta:rule)\n//   parents[]       -> plan:procedure:link   (checkpoint branches -> :condition)\n//   parameters      -> plan:procedure:variable (procedure :inputs)\n//   techniques      -> meta:technique        (procedure -(refs)>)\n//   cves            -> risk:vuln             (procedure -(refs)>)\n//   source report   -> doc:report            (procedure -(refs)>)\n//\n// Every guid is derived from the hub slug and step id, so re-ingesting a hunt\n// updates the same nodes rather than creating copies.\n\n$system = $lib.guid(huntbase, hub, plan, system, hunt.md)\n\nfunction fetch(url, json=(true)) {\n    $resp = $lib.inet.http.get($url)\n    if ($resp.code != 200) {\n        $lib.warn(`huntbase.hub: GET {$url} returned {$resp.code}`)\n        return((null))\n    }\n    if $json { return($resp.json()) }\n    return($resp.body.decode())\n}\n\nfunction tagPart(text) {\n    if ($text = (null)) { return(\"unknown\") }\n    return($lib.regex.replace(\"[^a-z0-9_]+\", \"_\", $text.lower()))\n}\n\nfunction getSource(url) {\n    [ meta:source=({\"name\": \"huntbase hub\", \"url\": $url}) :url=$url :type=intel.feed.hunts ]\n    return($node)\n}\n\nfunction setCursor(url, latest) {\n    [ meta:source=({\"name\": \"huntbase hub\", \"url\": $url}) :url=$url :ingest:latest=$latest ]\n    return()\n}\n\n// When the hub last changed a hunt: its `updated` time where the index has\n// one, otherwise when it was published. The sync cursor follows this, so an\n// edited hunt is pulled again.\nfunction entryTime(entry) {\n    if $entry.updated { return($entry.updated) }\n    return($entry.generated.at)\n}\n\nfunction stepGuid(slug, stepid) {\n    return($lib.guid(huntbase, hub, step, $slug, $stepid))\n}\n\n// A hub branch name on a parent edge -> plan:procedure:link :condition.\nfunction branchCondition(branch) {\n    if ($branch = \"on_supports\") { return((true)) }\n    if ($branch = \"on_refutes\") { return((false)) }\n    return((null))\n}\n\nfunction ingestHunt(url, entry, defn, text) {\n\n    // Storm streams nodes: anything that loops over the procedure node is\n    // wrapped in its own { } subquery, which drains before the node moves on,\n    // so each loop runs to completion exactly once.\n\n    $slug = $entry.slug\n    $proc = $lib.guid(huntbase, hub, procedure, $slug)\n    $page = `{$url}/{$entry.path}`\n    $meta = $defn.hunt.meta\n\n    [ plan:procedure=$proc\n        :id=$slug\n        :title?=$entry.title\n        :desc?=$entry.hypothesis\n        :body?=$text\n        :url=$page\n        :type=huntbase.hunt\n        :system={[ plan:system=$system :name=hunt.md :url=https://github.com/huntbase-io/hunt-md ]}\n        :creator:name=huntbase\n        :created?=$entry.generated.at\n        :updated?=$entryTime($entry)\n        +#rep.huntbase.hunt\n        <(seen)+ {[ meta:source=({\"name\": \"huntbase hub\", \"url\": $url}) ]}\n    ]\n\n    $node.data.set(huntbase:hub:entry, $entry)\n    $node.data.set(huntbase:hub:definition, $defn)\n\n    // classification tags\n    {\n        $tags = ([])\n        if $entry.severity { $tags.append(`rep.huntbase.severity.{$tagPart($entry.severity)}`) }\n        if $entry.tlp { $tags.append(`rep.huntbase.tlp.{$tagPart($entry.tlp)}`) }\n        if $entry.series { $tags.append(`rep.huntbase.series.{$tagPart($entry.series.slug)}`) }\n        for $tactic in $entry.tactics { $tags.append(`rep.huntbase.tactic.{$tagPart($tactic)}`) }\n        for $product in $entry.products { $tags.append(`rep.huntbase.product.{$tagPart($product)}`) }\n        for $tag in $tags { [ +#$tag ] }\n    }\n\n    // what the hunt is about\n    { for $tid in $entry.techniques { [ +(refs)> {[ meta:technique=({\"id\": $tid}) ]} ] } }\n    { for $cve in $entry.cves { [ +(refs)> {[ risk:vuln=({\"id\": $cve}) ]} ] } }\n    {\n        if $entry.source.url {\n            [ +(refs)> {[ doc:report=({\"url\": $entry.source.url})\n                :title?=$entry.source.title\n                :publisher:name?=$entry.source.vendor\n                :public=(true)\n            ]} ]\n        }\n    }\n\n    // parameters -> procedure inputs\n    {\n        for ($name, $param) in $meta.parameters {\n            $var = $lib.guid(huntbase, hub, variable, $slug, $name)\n            { spin | [ plan:procedure:variable=$var\n                :procedure={plan:procedure=$proc}\n                :name=$name\n                :type?=$param.type\n                :default?=$param.default\n            ] }\n            [ :inputs += {plan:procedure:variable=$var} ]\n        }\n    }\n\n    // steps, then the links between them\n    {\n        for $step in $defn.nodes {\n            $sg = $stepGuid($slug, $step.id)\n            $cfg = $step.config\n            $steptag = `rep.huntbase.step.{$tagPart($step.type)}`\n            { spin | [ plan:procedure:step=$sg\n                :procedure={plan:procedure=$proc}\n                :title?=$step.label\n                :desc?=$cfg.description\n                +#$steptag\n            ] }\n            if ($step.parents = (null)) { [ :firststep={plan:procedure:step=$sg} ] }\n\n            if ($step.type = \"query\") {\n                $rule = $lib.guid(huntbase, hub, query, $slug, $step.id)\n                $ruletags = ([\"rep.huntbase.query\"])\n                if $cfg.surface { $ruletags.append(`rep.huntbase.surface.{$tagPart($cfg.surface)}`) }\n                { spin | [ meta:rule=$rule\n                    :id=`{$slug}/{$step.id}`\n                    :name?=$step.label\n                    :desc?=$cfg.description\n                    :text?=$cfg.content\n                    :type=`huntbase.query.{$tagPart($cfg.dsl)}`\n                    :url=`{$page}#{$step.id}`\n                    :severity?=$entry.severity\n                    <(uses)+ { plan:procedure:step=$sg }\n                ]\n                for $tag in $ruletags { [ +#$tag ] } }\n            }\n        }\n    }\n    {\n        for $step in $defn.nodes {\n            $sg = $stepGuid($slug, $step.id)\n            for $parent in $step.parents {\n                $branch = $parent.branch\n                if ($branch = (null)) { $branch = next }\n                $link = $lib.guid(huntbase, hub, link, $slug, $parent.id, $step.id, $branch)\n                $cond = $branchCondition($parent.branch)\n                { spin | [ plan:procedure:link=$link\n                    :procedure={plan:procedure=$proc}\n                    :next={plan:procedure:step=$sg}\n                    :condition?=$cond\n                ] }\n                $psg = $stepGuid($slug, $parent.id)\n                { spin | plan:procedure:step=$psg [ :links += {plan:procedure:link=$link} ] }\n            }\n        }\n    }\n\n    return($node)\n}\n\n// Fetch and ingest one hub entry. Returns the plan:procedure node or (null).\nfunction syncEntry(url, entry) {\n    $base = `{$url}/{$entry.path}`\n    $defn = $fetch(`{$base}definition.json`)\n    if ($defn = (null)) { return((null)) }\n    $text = $fetch(`{$base}hunt.md`, json=(false))\n    // one malformed hunt must not stop a sync\n    try {\n        return($ingestHunt($url, $entry, $defn, $text))\n    } catch * as err {\n        $lib.warn(`huntbase.hub: skipped {$entry.slug}: {$err.mesg}`)\n        return((null))\n    }\n}\n"}],"commands":[{"name":"huntbase.hub.sync","desc":"Pull new and changed hunts from the Huntbase Hub into the current view.\n\nExamples:\n  // pull everything published or changed since the last sync\n  huntbase.hub.sync\n  // first ten hunts only, ignoring the cursor\n  huntbase.hub.sync --all --size 10\n","cmdargs":[["--all",{"type":"bool","default":false,"action":"store_true","help":"Ignore the sync cursor and re-ingest every hunt."}],["--size",{"type":"int","default":0,"help":"Stop after this many hunts (0 = no limit)."}],["--url",{"type":"str","default":"https://hub.huntbase.io","help":"Base URL of the hub."}]],"storm":"init {\n    $hub = $lib.import(huntbase.hub)\n    $url = $cmdopts.url.rstrip(\"/\")\n\n    $index = $hub.fetch(`{$url}/index.json`)\n    if ($index = (null)) { $lib.exit(\"huntbase.hub.sync: could not read the hub index\") }\n\n    $source = $hub.getSource($url)\n    $cursor = $source.props.\"ingest:latest\"\n    if $cmdopts.all { $cursor = (null) }\n\n    // a hunt is due when it changed after the cursor: its `updated` time,\n    // or its publication time on a hub that does not list one.\n    // oldest first, so a --size limited run advances the cursor without\n    // skipping the hunts it did not reach\n    $pending = ([])\n    for $i in $lib.range($index.size()) {\n        $at = $lib.cast(time, $hub.entryTime($index.$i))\n        if ($cursor = (null) or $at > $cursor) { $pending.append(($at, $i)) }\n    }\n\n    $count = (0)\n    $newest = $cursor\n    for ($at, $i) in $lib.sorted($pending) {\n        if ($cmdopts.size > 0 and $count >= $cmdopts.size) { break }\n        $entry = $index.$i\n        $proc = $hub.syncEntry($url, $entry)\n        if ($proc = (null)) { continue }\n        $count = ($count + 1)\n        if ($newest = (null) or $at > $newest) { $newest = $at }\n    }\n\n    if ($newest != (null)) {\n        $hub.setCursor($url, $newest)\n    }\n    $lib.print(`huntbase.hub.sync: ingested {$count} hunt(s) from {$url}`)\n}\n"},{"name":"huntbase.hub.import","desc":"Ingest one or more hunts from the Huntbase Hub by slug and yield the procedures.\n\nExample:\n  huntbase.hub.import activemq-lateral-movement-lockbit\n","cmdargs":[["slugs",{"type":"str","nargs":"+","help":"Hub hunt slugs (the last path segment of a hunt URL)."}],["--url",{"type":"str","default":"https://hub.huntbase.io","help":"Base URL of the hub."}]],"storm":"init {\n    $hub = $lib.import(huntbase.hub)\n    $url = $cmdopts.url.rstrip(\"/\")\n    $index = $hub.fetch(`{$url}/index.json`)\n    $wanted = $lib.set()\n    for $slug in $cmdopts.slugs { $wanted.add($slug) }\n}\nfor $entry in $index {\n    if $wanted.has($entry.slug) {\n        $proc = $hub.syncEntry($url, $entry)\n        if ($proc != (null)) { yield $proc }\n    }\n}\n"}],"version":"0.2.0","build":{"time":1795112232453949}}}