-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathbuild_marketplace.py
More file actions
647 lines (564 loc) · 21.7 KB
/
Copy pathbuild_marketplace.py
File metadata and controls
647 lines (564 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/usr/bin/env python3
"""Build the AiSOC marketplace index from on-disk content.
This script walks the canonical content directories and emits a single
authoritative ``marketplace/index.json`` describing every detection,
playbook, and plugin shipped with this repo.
Sources walked:
- ``detections/<category>/*.yaml`` - curated AiSOC detection rules
- ``playbooks/packs/v1/<category>/*.json`` - production playbook pack v1
- ``plugins/<plugin-id>/plugin.yaml`` - reference plugin manifests
The output schema is consumed by:
- ``apps/web/public/marketplace/index.json`` (UI fetches this directly)
- ``apps/web/src/components/marketplace/MarketplaceView.tsx``
- The "Sync Marketplace Index" CI workflow
The schema deliberately captures MITRE ATT&CK technique IDs so the
marketplace UI can offer a real coverage filter (the plan calls for a
"MITRE filter" specifically).
Usage:
python3 scripts/build_marketplace.py # build & write
python3 scripts/build_marketplace.py --check # fail if drift
python3 scripts/build_marketplace.py --print # write to stdout
"""
from __future__ import annotations
import argparse
import datetime as dt
import json
import re
import sys
from collections.abc import Iterable
from pathlib import Path
from typing import Any
import yaml
REPO_ROOT = Path(__file__).resolve().parent.parent
DETECTIONS_DIR = REPO_ROOT / "detections"
PLAYBOOKS_PACKS_DIR = REPO_ROOT / "playbooks" / "packs"
PLUGINS_DIR = REPO_ROOT / "plugins"
COMMUNITY_DETECTIONS_DIR = REPO_ROOT / "detections" / "community"
COMMUNITY_PLAYBOOKS_DIR = REPO_ROOT / "playbooks" / "community"
COMMUNITY_PLUGINS_DIR = REPO_ROOT / "plugins" / "community"
OUTPUT_PRIMARY = REPO_ROOT / "marketplace" / "index.json"
OUTPUT_PUBLIC = REPO_ROOT / "apps" / "web" / "public" / "marketplace" / "index.json"
DETECTION_CATEGORIES = {
"cloud",
"identity",
"endpoint",
"network",
"application",
"data-exfil",
}
# Top-level dirs under detections/ that are NOT native rule directories
# but contain rules in some tier (we walk these separately).
DETECTION_NATIVE_SKIP = {
"fixtures",
"community",
"sigma-imports",
"car-imports",
"splunk-imports",
"chronicle-imports",
}
# Imported detection tiers: directory name -> source name used in the
# `provenance.source` field of the rule (and the `source` we project
# into the marketplace item).
IMPORTED_TIER_DIRS: dict[str, str] = {
"sigma-imports": "sigmahq",
"car-imports": "mitre-car",
"splunk-imports": "splunk-security-content",
"chronicle-imports": "chronicle-detection-rules",
}
MITRE_RE = re.compile(r"mitre\.attack\.(t\d{4}(?:\.\d{3})?)", re.IGNORECASE)
MITRE_LOOSE_RE = re.compile(r"mitre\.(t\d{4}(?:\.\d{3})?)", re.IGNORECASE)
MITRE_BARE_RE = re.compile(r"^t\d{4}(?:\.\d{3})?$", re.IGNORECASE)
def normalise_tags(raw: Any) -> list[str]:
"""Flatten the ``tags`` block into a list of dotted strings.
Native rules emit ``['mitre.attack.t1234', 'tlp.white']``. The detection
importers emit a dict shape: ``{'mitre': ['T1234'], 'categories': ['endpoint']}``.
Downstream code expects strings, so we project the dict shape into the
same dotted form (``mitre.attack.t1234``, ``categories.endpoint``).
"""
if raw is None:
return []
if isinstance(raw, list):
return [str(t) for t in raw if isinstance(t, str)]
if isinstance(raw, dict):
out: list[str] = []
for key, values in raw.items():
if not isinstance(values, list):
continue
for v in values:
if not isinstance(v, str):
continue
if str(key).lower() == "mitre":
out.append(f"mitre.attack.{v.lower()}")
else:
out.append(f"{key}.{v}")
return out
return []
def extract_mitre(tags: Iterable[Any]) -> list[str]:
"""Extract uppercase MITRE technique IDs from a tag block.
Accepts:
* the strict ``mitre.attack.tXXXX[.YYY]`` form,
* the looser ``mitre.tXXXX[.YYY]`` form that some playbooks use,
* bare ``T1234`` IDs as emitted by the detection importers under
``tags.mitre``.
The argument may be a list of strings *or* a dict like
``{'mitre': ['T1234'], 'categories': ['endpoint']}``.
"""
out: list[str] = []
def _add(tid: str) -> None:
u = tid.upper()
if u not in out:
out.append(u)
if isinstance(tags, dict):
# Fast path for the importer shape — just consume tags['mitre'].
for v in tags.get("mitre") or []:
if isinstance(v, str) and MITRE_BARE_RE.match(v):
_add(v)
iterable = tags if isinstance(tags, (list, tuple)) else normalise_tags(tags)
for tag in iterable or []:
if not isinstance(tag, str):
continue
m = MITRE_RE.search(tag) or MITRE_LOOSE_RE.search(tag)
if m:
_add(m.group(1))
continue
if MITRE_BARE_RE.match(tag):
_add(tag)
return out
def detection_files() -> list[Path]:
"""Walk only the native detection tier (``detections/<category>/``)."""
files: list[Path] = []
if not DETECTIONS_DIR.exists():
return files
for child in sorted(DETECTIONS_DIR.iterdir()):
if not child.is_dir() or child.name in DETECTION_NATIVE_SKIP:
continue
for f in sorted(child.rglob("*.yaml")):
files.append(f)
return files
def imported_detection_files() -> list[tuple[Path, str, bool]]:
"""Return (path, source_name, is_quarantined) for every imported rule.
Walks the tier directories declared in :data:`IMPORTED_TIER_DIRS`.
Rules nested under a ``_quarantine/`` directory are returned with
``is_quarantined=True`` so the marketplace can surface them as
"imported, requires translation" instead of pretending they execute.
"""
out: list[tuple[Path, str, bool]] = []
if not DETECTIONS_DIR.exists():
return out
for tier_dir, source_name in IMPORTED_TIER_DIRS.items():
root = DETECTIONS_DIR / tier_dir
if not root.exists():
continue
for f in sorted(root.rglob("*.yaml")):
try:
rel = f.relative_to(root).parts
except ValueError:
continue
quarantined = bool(rel) and rel[0] == "_quarantine"
out.append((f, source_name, quarantined))
return out
def playbook_files() -> list[Path]:
if not PLAYBOOKS_PACKS_DIR.exists():
return []
return sorted(PLAYBOOKS_PACKS_DIR.rglob("*.playbook.json"))
def plugin_manifests() -> list[Path]:
if not PLUGINS_DIR.exists():
return []
out: list[Path] = []
for child in sorted(PLUGINS_DIR.iterdir()):
if not child.is_dir() or child.name == "community":
continue
manifest = child / "plugin.yaml"
if manifest.exists():
out.append(manifest)
return out
def community_detection_files() -> list[Path]:
if not COMMUNITY_DETECTIONS_DIR.exists():
return []
return sorted(COMMUNITY_DETECTIONS_DIR.rglob("*.yaml"))
def community_playbook_files() -> list[Path]:
if not COMMUNITY_PLAYBOOKS_DIR.exists():
return []
return sorted(COMMUNITY_PLAYBOOKS_DIR.rglob("*.playbook.json"))
def community_plugin_manifests() -> list[Path]:
if not COMMUNITY_PLUGINS_DIR.exists():
return []
out: list[Path] = []
for child in sorted(COMMUNITY_PLUGINS_DIR.iterdir()):
if not child.is_dir():
continue
manifest = child / "plugin.yaml"
if manifest.exists():
out.append(manifest)
return out
def build_detection_item(
path: Path,
*,
source: str,
tier: str,
quarantined: bool = False,
) -> dict[str, Any] | None:
try:
data = yaml.safe_load(path.read_text(encoding="utf-8"))
except Exception as exc:
print(f"WARN: could not parse {path}: {exc}", file=sys.stderr)
return None
if not isinstance(data, dict):
return None
raw_tags = data.get("tags")
mitre = extract_mitre(raw_tags or [])
tags = normalise_tags(raw_tags)
category = data.get("category") or path.parent.name
enabled = data.get("enabled")
if quarantined:
# Quarantine directory layout has the category two levels below the
# tier root (e.g. sigma-imports/_quarantine/cloud/foo.yaml).
if not data.get("category"):
try:
rel = path.relative_to(DETECTIONS_DIR).parts
if len(rel) >= 4 and rel[1] == "_quarantine":
category = rel[2]
except ValueError:
pass # path is not relative to DETECTIONS_DIR; category stays None
item: dict[str, Any] = {
"id": data.get("id") or path.stem,
"type": "detection",
"name": data.get("name") or data.get("id") or path.stem,
"description": (data.get("description") or "").strip(),
"version": data.get("version", "1.0.0"),
"author": data.get("author", "AiSOC"),
"tags": [t for t in tags if not t.lower().startswith("mitre.")],
"severity": data.get("severity"),
"category": category,
"mitre_techniques": mitre,
"log_source": (data.get("log_source") or {}).get("product"),
"playbook": data.get("playbook"),
"verified": tier == "stable",
"source": source,
"tier": tier,
"enabled": False if (quarantined or enabled is False) else True,
"path": str(path.relative_to(REPO_ROOT)),
}
if quarantined:
item["quarantine_reason"] = data.get("quarantine_reason") or (
"imported rule; upstream query language not directly executable "
"by the AiSOC engine yet"
)
provenance = data.get("provenance")
if isinstance(provenance, dict):
item["provenance"] = {
"source": provenance.get("source"),
"source_id": provenance.get("source_id"),
"source_commit": provenance.get("source_commit"),
"license": provenance.get("license"),
"license_url": provenance.get("license_url"),
"imported_at": provenance.get("imported_at"),
"upstream_path": provenance.get("upstream_path"),
}
return item
def build_playbook_item(
path: Path, *, source: str, tier: str
) -> dict[str, Any] | None:
try:
data = json.loads(path.read_text(encoding="utf-8"))
except Exception as exc:
print(f"WARN: could not parse {path}: {exc}", file=sys.stderr)
return None
if not isinstance(data, dict):
return None
raw_tags = data.get("tags")
mitre = extract_mitre(raw_tags or [])
tags = normalise_tags(raw_tags)
trigger_block = data.get("trigger") or {}
trigger = trigger_block.get("on") if isinstance(trigger_block, dict) else None
severities = (
trigger_block.get("severity") if isinstance(trigger_block, dict) else None
)
severity: str | None = None
if isinstance(severities, list) and severities:
# Pick the highest declared severity for display.
order = {"critical": 4, "high": 3, "medium": 2, "low": 1}
severity = max(
severities,
key=lambda s: order.get(str(s).lower(), 0),
)
return {
"id": data.get("id") or path.stem,
"type": "playbook",
"name": data.get("name") or path.stem,
"description": (data.get("description") or "").strip(),
"version": data.get("version", "1.0.0"),
"author": data.get("author", "AiSOC"),
"tags": [t for t in tags if not t.lower().startswith("mitre.")],
"severity": severity,
"trigger": trigger,
"steps": len(data.get("steps") or []),
"category": path.parent.name,
"mitre_techniques": mitre,
"verified": tier == "stable",
"source": source,
"tier": tier,
"enabled": True,
"path": str(path.relative_to(REPO_ROOT)),
}
def build_plugin_item(
path: Path, *, source: str, tier: str | None = None
) -> dict[str, Any] | None:
try:
data = yaml.safe_load(path.read_text(encoding="utf-8"))
except Exception as exc:
print(f"WARN: could not parse {path}: {exc}", file=sys.stderr)
return None
if not isinstance(data, dict):
return None
tags = normalise_tags(data.get("tags"))
plugin_dir = path.parent
has_python = (plugin_dir / "plugin.py").exists()
has_go = (plugin_dir / "go" / "main.go").exists()
sdks: list[str] = []
if has_python:
sdks.append("python")
if has_go:
sdks.append("go")
# Resolve tier. A plugin manifest can self-declare ``tier:`` to mark
# itself as ``beta`` or ``community``. Otherwise we infer from source
# and how complete the implementation is. Manifest-only plugins (no
# plugin.py + no go/main.go) get demoted to ``beta`` so we don't pass
# off scaffolds as production-ready.
declared_tier = (data.get("tier") or "").strip().lower() or None
if declared_tier in {"stable", "beta", "community"}:
resolved_tier = declared_tier
elif tier is not None:
resolved_tier = tier
elif source == "community":
resolved_tier = "community"
elif not (has_python or has_go):
resolved_tier = "beta"
else:
resolved_tier = "stable"
return {
"id": data.get("id") or plugin_dir.name,
"type": "plugin",
"name": data.get("name") or plugin_dir.name,
"description": (data.get("description") or "").strip(),
"version": data.get("version", "1.0.0"),
"author": data.get("author", "AiSOC"),
"tags": tags,
"plugin_type": data.get("plugin_type"),
"license": data.get("license"),
"homepage": data.get("homepage"),
"min_aisoc_version": data.get("min_aisoc_version"),
"sdks": sdks,
"mitre_techniques": [],
"verified": resolved_tier == "stable",
"source": source,
"tier": resolved_tier,
"path": str(path.relative_to(REPO_ROOT)),
}
def collect_items() -> list[dict[str, Any]]:
items: list[dict[str, Any]] = []
# Detections — native (stable) tier
for f in detection_files():
item = build_detection_item(f, source="core", tier="stable")
if item:
items.append(item)
# Detections — imported tiers (one per upstream corpus)
for f, src_name, quarantined in imported_detection_files():
item = build_detection_item(
f, source=src_name, tier="imported", quarantined=quarantined
)
if item:
items.append(item)
# Detections — community tier
for f in community_detection_files():
item = build_detection_item(f, source="community", tier="community")
if item:
items.append(item)
# Playbooks
for f in playbook_files():
item = build_playbook_item(f, source="core", tier="stable")
if item:
items.append(item)
for f in community_playbook_files():
item = build_playbook_item(f, source="community", tier="community")
if item:
items.append(item)
# Plugins
for f in plugin_manifests():
item = build_plugin_item(f, source="core")
if item:
items.append(item)
for f in community_plugin_manifests():
item = build_plugin_item(f, source="community", tier="community")
if item:
items.append(item)
return items
def categories_block(items: list[dict[str, Any]]) -> list[dict[str, Any]]:
return [
{
"id": "playbooks",
"label": "Response Playbooks",
"description": (
"Automated incident-response workflows triggered by "
"alerts or manual invocation."
),
},
{
"id": "detections",
"label": "Detection Rules",
"description": (
"Curated YAML rules for identifying malicious or "
"suspicious activity across cloud, identity, endpoint, "
"network, application, and data-exfil categories."
),
},
{
"id": "plugins",
"label": "Plugins",
"description": (
"Reference connectors, enrichers, actions, and "
"widgets shipped with both Python and Go SDK "
"implementations for cross-language parity."
),
},
]
def _tier_breakdown(items: list[dict[str, Any]]) -> dict[str, int]:
"""Count items per tier (stable, beta, imported, community)."""
counts: dict[str, int] = {}
for item in items:
tier = item.get("tier") or "stable"
counts[tier] = counts.get(tier, 0) + 1
return dict(sorted(counts.items()))
def _detection_tier_breakdown(items: list[dict[str, Any]]) -> dict[str, int]:
"""Count detection items per tier — the main 'are we Wazuh-scale' headline."""
counts: dict[str, int] = {}
for item in items:
if item.get("type") != "detection":
continue
tier = item.get("tier") or "stable"
counts[tier] = counts.get(tier, 0) + 1
return dict(sorted(counts.items()))
def coverage_block(items: list[dict[str, Any]]) -> dict[str, Any]:
"""Compute MITRE ATT&CK coverage across detections + playbooks.
Returns aggregate counts per technique plus a per-tier breakdown so the
UI can render a stacked coverage matrix (native vs imported vs community)
rather than a single flat number.
"""
techniques: dict[str, int] = {}
by_tier: dict[str, dict[str, int]] = {}
for item in items:
tier = item.get("tier") or "stable"
for tid in item.get("mitre_techniques") or []:
techniques[tid] = techniques.get(tid, 0) + 1
tier_map = by_tier.setdefault(tier, {})
tier_map[tid] = tier_map.get(tid, 0) + 1
return {
"techniques": dict(sorted(techniques.items())),
"unique_techniques": len(techniques),
"total_with_mitre": sum(
1 for i in items if i.get("mitre_techniques")
),
"by_tier": {
tier: dict(sorted(tids.items())) for tier, tids in by_tier.items()
},
}
def build_index() -> dict[str, Any]:
items = collect_items()
items.sort(key=lambda i: (i["type"], i.get("id", "")))
return {
"$schema": "https://example.com/schemas/marketplace/v1.json",
"version": "1.0.0",
"generated": dt.datetime.now(dt.UTC)
.replace(microsecond=0)
.isoformat()
.replace("+00:00", "Z"),
"categories": categories_block(items),
"stats": {
"total": len(items),
"playbooks": sum(1 for i in items if i["type"] == "playbook"),
"detections": sum(1 for i in items if i["type"] == "detection"),
"plugins": sum(1 for i in items if i["type"] == "plugin"),
"verified": sum(1 for i in items if i.get("verified")),
"community": sum(1 for i in items if i.get("source") == "community"),
"by_tier": _tier_breakdown(items),
"detections_by_tier": _detection_tier_breakdown(items),
"quarantined": sum(
1 for i in items if i.get("quarantine_reason")
),
},
"mitre_coverage": coverage_block(items),
"items": items,
}
def write_index(index: dict[str, Any]) -> None:
payload = json.dumps(index, indent=2, sort_keys=False) + "\n"
OUTPUT_PRIMARY.parent.mkdir(parents=True, exist_ok=True)
OUTPUT_PUBLIC.parent.mkdir(parents=True, exist_ok=True)
OUTPUT_PRIMARY.write_text(payload, encoding="utf-8")
OUTPUT_PUBLIC.write_text(payload, encoding="utf-8")
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--check",
action="store_true",
help="Exit non-zero if the on-disk index does not match the build.",
)
parser.add_argument(
"--print",
dest="print_only",
action="store_true",
help="Print built index to stdout instead of writing files.",
)
args = parser.parse_args()
index = build_index()
serialised = json.dumps(index, indent=2, sort_keys=False) + "\n"
if args.print_only:
sys.stdout.write(serialised)
return 0
if args.check:
existing_primary = (
OUTPUT_PRIMARY.read_text(encoding="utf-8")
if OUTPUT_PRIMARY.exists()
else ""
)
existing_public = (
OUTPUT_PUBLIC.read_text(encoding="utf-8")
if OUTPUT_PUBLIC.exists()
else ""
)
# Compare ignoring `generated` timestamp.
def _strip_generated(s: str) -> str:
if not s:
return s
try:
obj = json.loads(s)
except Exception:
return s
obj.pop("generated", None)
return json.dumps(obj, indent=2, sort_keys=False) + "\n"
rebuilt_no_ts = _strip_generated(serialised)
if (
_strip_generated(existing_primary) != rebuilt_no_ts
or _strip_generated(existing_public) != rebuilt_no_ts
):
print(
"marketplace/index.json is stale. Run: "
"pnpm marketplace:build",
file=sys.stderr,
)
return 1
print(
f"marketplace/index.json is up to date "
f"({index['stats']['total']} items)."
)
return 0
write_index(index)
print(
f"Wrote marketplace index: total={index['stats']['total']} "
f"detections={index['stats']['detections']} "
f"playbooks={index['stats']['playbooks']} "
f"plugins={index['stats']['plugins']} "
f"mitre_techniques={index['mitre_coverage']['unique_techniques']}"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())