diff --git a/README.md b/README.md index 57a3312..254a64f 100644 --- a/README.md +++ b/README.md @@ -114,11 +114,16 @@ Legacy entry points (`python3 tools/…`) remain as thin shims and work without emsign-g3 export-indicators emsign-g3 export-indicators --ndjson emsign-g3 export-indicators --verify # PEMs vs CSV + emsign-g3 export-indicators --format sigma --output-dir rules/sigma/ ``` Use host fields (`sha256_fingerprint`) against scanner/MDM output; use network fields (`suricata.sid`, `tls_cert_issuer_match`) against Suricata `eve.json`. + **Sigma stubs** (`--format sigma`) are starter rules mapped to Suricata EVE + (`tls.issuerdn`, `alert.signature_id`). Import into your SIEM and tune field names + before production alerting. + ## Development ```bash diff --git a/src/emsign_g3/cli.py b/src/emsign_g3/cli.py index e57d150..373e883 100644 --- a/src/emsign_g3/cli.py +++ b/src/emsign_g3/cli.py @@ -30,9 +30,24 @@ def main(argv: list[str] | None = None) -> int: exp_p = sub.add_parser("export-indicators", help="SIEM/SOAR indicator bundle") exp_p.add_argument("--csv", default=None, help="path to indicators.csv") - exp_p.add_argument("--ndjson", action="store_true", help="newline-delimited JSON") + exp_p.add_argument( + "--format", + choices=("json", "ndjson", "sigma"), + default="json", + help="output format (default: json)", + ) + exp_p.add_argument( + "--ndjson", + action="store_true", + help="shorthand for --format ndjson (deprecated)", + ) exp_p.add_argument("--verify", action="store_true", help="verify PEMs match CSV") exp_p.add_argument("--certs-dir", default=None, help="PEM directory for --verify") + exp_p.add_argument( + "--output-dir", + default=None, + help="write --format sigma YAML files to this directory", + ) args = parser.parse_args(argv) @@ -43,9 +58,11 @@ def main(argv: list[str] | None = None) -> int: if args.command == "export-indicators": return run_export( csv_path=args.csv or indicators_csv_path(), + fmt=args.format, ndjson=args.ndjson, verify=args.verify, certs_dir=args.certs_dir, + output_dir=args.output_dir, ) parser.error(f"unknown command: {args.command}") diff --git a/src/emsign_g3/export.py b/src/emsign_g3/export.py index 7fc66da..6525145 100644 --- a/src/emsign_g3/export.py +++ b/src/emsign_g3/export.py @@ -17,6 +17,7 @@ load_rows, ) from emsign_g3.paths import default_certs_dir, indicators_csv_path +from emsign_g3.sigma import render_sigma_bundle, write_sigma_rules GENERIC_SURICATA_SID = "92083409" @@ -121,9 +122,11 @@ def to_ndjson(bundle: dict) -> str: def run_export( *, csv_path: str | Path | None = None, + fmt: str = "json", ndjson: bool = False, verify: bool = False, certs_dir: str | Path | None = None, + output_dir: str | Path | None = None, ) -> int: csv_file = Path(csv_path) if csv_path else indicators_csv_path() certs_path = Path(certs_dir) if certs_dir else default_certs_dir() @@ -138,10 +141,25 @@ def run_export( return 0 rows = load_rows(csv_file) + export_fmt = "ndjson" if ndjson else fmt + + if export_fmt == "sigma": + if output_dir: + paths = write_sigma_rules(Path(output_dir), rows) + print(f"Wrote {len(paths)} Sigma rule(s) to {output_dir}") + for p in paths: + print(f" {p.name}") + else: + sys.stdout.write(render_sigma_bundle(rows)) + return 0 + bundle = build_bundle(rows) - if ndjson: + if export_fmt == "ndjson": sys.stdout.write(to_ndjson(bundle)) - else: + elif export_fmt == "json": json.dump(bundle, sys.stdout, indent=2) sys.stdout.write("\n") + else: + print(f"unknown export format: {export_fmt}", file=sys.stderr) + return 2 return 0 diff --git a/src/emsign_g3/sigma.py b/src/emsign_g3/sigma.py new file mode 100644 index 0000000..b71cdf5 --- /dev/null +++ b/src/emsign_g3/sigma.py @@ -0,0 +1,114 @@ +"""Generate Sigma rule stubs from Bug 2083449 indicators (Suricata EVE-oriented).""" +from __future__ import annotations + +import json +import re +from pathlib import Path + +from emsign_g3.indicators import COMPROMISED_TYPES, load_rows + +MOZILLA_BUG = "2083449" +GENERIC_SID = 92083409 + +_SAFE = re.compile(r"[^a-zA-Z0-9_-]+") + + +def _slug(name: str) -> str: + return _SAFE.sub("_", name).strip("_").lower() + + +def _yaml_str(value: str) -> str: + return json.dumps(value) + + +def build_sigma_rule(row: dict) -> str: + """One Sigma YAML document for a compromised intermediate.""" + cn = row["tls_issuer_cn_match"] + sid = int(row["suricata_sid"]) + title = f"eMudhra emSign G3 compromised issuer {row['ca_common_name']}" + lines = [ + "title: " + _yaml_str(title), + "id: " + _yaml_str(f"emsign-g3-{row['pem_basename'].lower()}-2083449"), + "status: experimental", + "description: " + _yaml_str( + "TLS leaf issued under a compromised eMudhra/emSign G3 intermediate " + f"(serial {row.get('serial') or 'unknown'}). Private keys were recoverable; " + "see Mozilla Bug 2083449. Tune field names to your Suricata EVE / SIEM mapping." + ), + "references:", + f" - https://bugzilla.mozilla.org/show_bug.cgi?id={MOZILLA_BUG}", + "author: emsign-g3-distrust", + "tags:", + " - attack.initial_access", + " - attack.t1553.004", + "logsource:", + " category: network", + " product: suricata", + " service: eve", + "detection:", + " selection_issuer:", + f" tls.issuerdn|contains: {_yaml_str(cn)}", + " selection_sid:", + f" alert.signature_id: {sid}", + " condition: selection_issuer or selection_sid", + "falsepositives:", + " - Historical captures before distrust rollout", + "level: high", + ] + return "\n".join(lines) + "\n" + + +def build_sigma_backstop() -> str: + lines = [ + "title: " + _yaml_str("eMudhra emSign G3 compromised-family issuer (generic review)"), + "id: " + _yaml_str("emsign-g3-generic-backstop-2083449"), + "status: experimental", + "description: " + _yaml_str( + "Backstop for emSign G3 EM * CA - G3B/G3C issuer family (Suricata sid " + f"{GENERIC_SID}). Review before blocking; prefer per-CA rules." + ), + "references:", + f" - https://bugzilla.mozilla.org/show_bug.cgi?id={MOZILLA_BUG}", + "author: emsign-g3-distrust", + "logsource:", + " category: network", + " product: suricata", + " service: eve", + "detection:", + " selection:", + f" alert.signature_id: {GENERIC_SID}", + " condition: selection", + "falsepositives:", + " - Requires analyst review", + "level: medium", + ] + return "\n".join(lines) + "\n" + + +def sigma_rules_from_rows(rows: list[dict]) -> list[tuple[str, str]]: + """Return (filename, yaml_content) for each compromised intermediate plus backstop.""" + out: list[tuple[str, str]] = [] + for row in rows: + if row["type"] not in COMPROMISED_TYPES: + continue + base = row.get("pem_basename") or _slug(row["ca_common_name"]) + out.append((f"emsign_g3_{base.lower()}.yml", build_sigma_rule(row))) + out.append(("emsign_g3_generic_backstop.yml", build_sigma_backstop())) + return out + + +def render_sigma_bundle(rows: list[dict] | None = None) -> str: + rows = rows if rows is not None else load_rows() + parts = [content for _, content in sigma_rules_from_rows(rows)] + return "\n---\n".join(parts) + + +def write_sigma_rules(output_dir: Path, rows: list[dict] | None = None) -> list[Path]: + output_dir.mkdir(parents=True, exist_ok=True) + written: list[Path] = [] + rows = rows if rows is not None else load_rows() + for filename, content in sigma_rules_from_rows(rows): + path = output_dir / filename + path.write_text(content, encoding="utf-8") + written.append(path) + return written diff --git a/tests/test_sigma.py b/tests/test_sigma.py new file mode 100644 index 0000000..775dcb4 --- /dev/null +++ b/tests/test_sigma.py @@ -0,0 +1,22 @@ +from emsign_g3.indicators import load_rows +from emsign_g3.sigma import build_sigma_rule, render_sigma_bundle, sigma_rules_from_rows + + +def test_sigma_rule_count(): + rows = load_rows() + files = sigma_rules_from_rows(rows) + assert len(files) == 9 # eight intermediates + generic backstop + + +def test_sigma_rule_contains_sid_and_issuer(): + rows = load_rows() + dv = next(r for r in rows if r["pem_basename"] == "EMDVTLSCAG3B") + yaml = build_sigma_rule(dv) + assert "92083401" in yaml + assert "EM DV TLS CA - G3B" in yaml + assert "2083449" in yaml + + +def test_sigma_bundle_separator(): + bundle = render_sigma_bundle() + assert bundle.count("\n---\n") == 8