-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathgen_graphml.py
More file actions
285 lines (237 loc) · 8.76 KB
/
gen_graphml.py
File metadata and controls
285 lines (237 loc) · 8.76 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
#!/usr/bin/env python3
"""Generate a GraphML export for all MISP galaxies and clusters.
The graph contains:
- One node per galaxy definition (`galaxies/*.json`)
- One node per cluster value (`clusters/*.json`)
- Optional explicit edges from `cluster.related`
- Optional inferred edges across galaxy types when values/synonyms match
"""
from __future__ import annotations
import argparse
import itertools
import json
import re
from dataclasses import dataclass
from pathlib import Path
import xml.etree.ElementTree as ET
@dataclass(frozen=True)
class Galaxy:
uuid: str
type: str
name: str
description: str
@dataclass(frozen=True)
class Cluster:
node_id: str
uuid: str
value: str
galaxy_type: str
description: str
meta: dict
def normalize(text: str) -> str:
return re.sub(r"\s+", " ", text.strip().lower())
def load_galaxies(galaxies_dir: Path) -> dict[str, Galaxy]:
galaxies: dict[str, Galaxy] = {}
for path in sorted(galaxies_dir.glob("*.json")):
with path.open(encoding="utf-8") as handle:
data = json.load(handle)
uuid = data.get("uuid")
gtype = data.get("type")
if not uuid or not gtype:
continue
galaxies[gtype] = Galaxy(
uuid=uuid,
type=gtype,
name=data.get("name", gtype),
description=data.get("description", ""),
)
return galaxies
def load_clusters(clusters_dir: Path) -> tuple[dict[str, Cluster], list[tuple[str, str, str]]]:
clusters: dict[str, Cluster] = {}
explicit_edges: list[tuple[str, str, str]] = []
for path in sorted(clusters_dir.glob("*.json")):
with path.open(encoding="utf-8") as handle:
data = json.load(handle)
galaxy_type = data.get("type", "unknown")
for index, raw_cluster in enumerate(data.get("values", []), start=1):
uuid = raw_cluster.get("uuid") or f"{galaxy_type}:{index}"
node_id = f"cluster:{uuid}"
cluster = Cluster(
node_id=node_id,
uuid=uuid,
value=raw_cluster.get("value", ""),
galaxy_type=galaxy_type,
description=raw_cluster.get("description", ""),
meta=raw_cluster.get("meta", {}),
)
clusters[uuid] = cluster
for relation in raw_cluster.get("related", []) or []:
dest_uuid = relation.get("dest-uuid")
relation_type = relation.get("type", "related-to")
if dest_uuid:
explicit_edges.append((uuid, dest_uuid, relation_type))
return clusters, explicit_edges
def cluster_terms(cluster: Cluster, include_synonyms: bool) -> set[str]:
terms = set()
if cluster.value:
terms.add(normalize(cluster.value))
if include_synonyms:
synonyms = cluster.meta.get("synonyms", [])
for synonym in synonyms:
if isinstance(synonym, str) and synonym.strip():
terms.add(normalize(synonym))
return terms
def add_graphml_keys(root: ET.Element) -> None:
keys = [
("d0", "node", "kind"),
("d1", "node", "uuid"),
("d2", "node", "name"),
("d3", "node", "galaxy_type"),
("d4", "node", "description"),
("d5", "edge", "relation_type"),
("d6", "edge", "relation_source"),
]
for key_id, attr_for, attr_name in keys:
ET.SubElement(
root,
"key",
id=key_id,
**{"for": attr_for, "attr.name": attr_name, "attr.type": "string"},
)
def add_data(parent: ET.Element, key: str, value: str) -> None:
data = ET.SubElement(parent, "data", key=key)
data.text = value
def build_graphml(
galaxies: dict[str, Galaxy],
clusters: dict[str, Cluster],
explicit_edges: list[tuple[str, str, str]],
include_explicit_edges: bool,
inferred_mode: str,
) -> ET.ElementTree:
root = ET.Element("graphml", xmlns="http://graphml.graphdrawing.org/xmlns")
add_graphml_keys(root)
graph = ET.SubElement(root, "graph", id="misp_galaxies", edgedefault="directed")
for galaxy in galaxies.values():
node = ET.SubElement(graph, "node", id=f"galaxy:{galaxy.uuid}")
add_data(node, "d0", "galaxy")
add_data(node, "d1", galaxy.uuid)
add_data(node, "d2", galaxy.name)
add_data(node, "d3", galaxy.type)
add_data(node, "d4", galaxy.description)
for cluster in clusters.values():
node = ET.SubElement(graph, "node", id=cluster.node_id)
add_data(node, "d0", "cluster")
add_data(node, "d1", cluster.uuid)
add_data(node, "d2", cluster.value)
add_data(node, "d3", cluster.galaxy_type)
add_data(node, "d4", cluster.description)
galaxy = galaxies.get(cluster.galaxy_type)
if galaxy:
edge = ET.SubElement(
graph,
"edge",
source=f"galaxy:{galaxy.uuid}",
target=cluster.node_id,
)
add_data(edge, "d5", "contains")
add_data(edge, "d6", "membership")
edge_counter = itertools.count(1)
if include_explicit_edges:
for source_uuid, target_uuid, relation_type in explicit_edges:
source_cluster = clusters.get(source_uuid)
target_cluster = clusters.get(target_uuid)
if not source_cluster or not target_cluster:
continue
edge = ET.SubElement(
graph,
"edge",
id=f"e{next(edge_counter)}",
source=source_cluster.node_id,
target=target_cluster.node_id,
)
add_data(edge, "d5", relation_type)
add_data(edge, "d6", "explicit")
if inferred_mode != "none":
include_synonyms = inferred_mode == "value-or-synonyms"
term_index: dict[str, list[Cluster]] = {}
for cluster in clusters.values():
for term in cluster_terms(cluster, include_synonyms=include_synonyms):
term_index.setdefault(term, []).append(cluster)
seen_pairs: set[tuple[str, str]] = set()
for term, matching_clusters in term_index.items():
if len(matching_clusters) < 2:
continue
for left, right in itertools.combinations(matching_clusters, 2):
if left.galaxy_type == right.galaxy_type:
continue
pair = tuple(sorted((left.uuid, right.uuid)))
if pair in seen_pairs:
continue
seen_pairs.add(pair)
edge = ET.SubElement(
graph,
"edge",
id=f"e{next(edge_counter)}",
source=left.node_id,
target=right.node_id,
)
add_data(edge, "d5", "same-value")
add_data(edge, "d6", f"inferred:{term}")
return ET.ElementTree(root)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Generate a GraphML graph from galaxies and clusters JSON files."
)
parser.add_argument(
"--clusters-dir",
type=Path,
default=Path("clusters"),
help="Directory containing cluster JSON files.",
)
parser.add_argument(
"--galaxies-dir",
type=Path,
default=Path("galaxies"),
help="Directory containing galaxy JSON files.",
)
parser.add_argument(
"-o",
"--output",
type=Path,
default=Path("misp-galaxies.graphml"),
help="Output GraphML file path.",
)
parser.add_argument(
"--no-existing-relationships",
action="store_true",
help="Disable explicit relationships from cluster related[] entries.",
)
parser.add_argument(
"--cross-cluster-matching",
choices=["none", "value", "value-or-synonyms"],
default="none",
help=(
"Create inferred edges across different galaxy types by matching cluster values "
"or values+synonyms."
),
)
return parser.parse_args()
def main() -> int:
args = parse_args()
galaxies = load_galaxies(args.galaxies_dir)
clusters, explicit_edges = load_clusters(args.clusters_dir)
graphml = build_graphml(
galaxies=galaxies,
clusters=clusters,
explicit_edges=explicit_edges,
include_explicit_edges=not args.no_existing_relationships,
inferred_mode=args.cross_cluster_matching,
)
args.output.parent.mkdir(parents=True, exist_ok=True)
graphml.write(args.output, encoding="utf-8", xml_declaration=True)
print(
f"GraphML written to {args.output} with {len(galaxies)} galaxies and {len(clusters)} clusters."
)
return 0
if __name__ == "__main__":
raise SystemExit(main())