Datasets:
Download build_scripts/refcheck.py from VertexAGI/cyberdata-2-medium: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/datasets/VertexAGI/cyberdata-2-medium/resolve/main/build_scripts/refcheck.py
- Command line
-
hf download hf://datasets/VertexAGI/cyberdata-2-medium/build_scripts/refcheck.py
-
curl -L -o refcheck.py https://huggingface.co/datasets/VertexAGI/cyberdata-2-medium/resolve/main/build_scripts/refcheck.py
2.42 kB
| """Reference checker: does a piece of text cite CWE / ATT&CK ids whose names match the official MITRE data? | |
| Used to FILTER third-party AI-written Q&A (Trendyol, Fenrir, Heimdall, ...): a row is dropped if it cites a CWE id that does not exist, or names an | |
| enterprise ATT&CK technique / CWE with a name that does not match. (We cannot check every fact, but we can remove rows that are provably wrong.)""" | |
| import difflib, json, os, re, zipfile | |
| import xml.etree.ElementTree as ET | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| _z = zipfile.ZipFile(os.path.join(HERE, "raw", "cwec.xml.zip")); _root = ET.fromstring(_z.read(_z.namelist()[0])); _ns = {"c": _root.tag.split("}")[0][1:]} | |
| CWE = {"CWE-" + w.get("ID"): w.get("Name") for w in _root.findall(".//c:Weakness", _ns)} | |
| CWE.update({"CWE-" + w.get("ID"): w.get("Name") for w in _root.findall(".//c:Category", _ns)}) | |
| ATT = {} | |
| for _o in json.load(open(os.path.join(HERE, "raw", "enterprise-attack.json")))["objects"]: | |
| if _o["type"] == "attack-pattern": | |
| for _r in _o.get("external_references", []): | |
| if _r.get("source_name") == "mitre-attack": ATT[_r["external_id"]] = _o["name"] | |
| P_CWE = re.compile(r"\b(CWE-\d+)\b\s*[\(:–—-]\s*([A-Z][A-Za-z0-9 ,'/\-]{4,70})") | |
| P_CWE_ID = re.compile(r"\bCWE-(\d+)\b") | |
| P_ATT = re.compile(r"\b(T\d{4}(?:\.\d{3})?)\b\s*[\(:–—-]\s*([A-Z][A-Za-z0-9 ,'/\-&:]{3,70})") | |
| NONENT = re.compile(r"T0\d{3}|T14\d{2}|T15\d{2}") # ICS / Mobile technique ids: not in the enterprise file, so we do not judge them | |
| def _sim(a, b): return difflib.SequenceMatcher(None, a.lower(), b.lower()).ratio() | |
| def ref_ok(text): | |
| """True if no citation in `text` is provably wrong.""" | |
| text = text or "" | |
| for m in P_CWE_ID.finditer(text): | |
| if "CWE-" + m.group(1) not in CWE: return False | |
| for m in P_CWE.finditer(text): | |
| i, name = m.group(1), m.group(2).strip() | |
| off = CWE[i] | |
| if _sim(name[: len(off) + 6], off) < 0.55 and off.lower() not in name.lower() and name.lower().split(" (")[0] not in off.lower(): return False | |
| for m in P_ATT.finditer(text): | |
| i, name = m.group(1), m.group(2).strip() | |
| if i not in ATT: | |
| if NONENT.match(i) or i.split(".")[0] in ATT: continue | |
| return False | |
| off = ATT[i] | |
| if _sim(name[: len(off) + 6], off) < 0.55 and off.lower() not in name.lower() and name.lower() not in off.lower(): return False | |
| return True | |