AppSecBench / scripts /cvss.py
ismailtasdelen's picture
Upload folder using huggingface_hub
8df6aa0 verified
Raw
History Blame Contribute Delete
7.06 kB
#!/usr/bin/env python3
"""CVSS 3.1 base-score computation + vector builder.
Implements the official FIRST CVSS v3.1 specification formulas so every
benchmark record carries a mathematically consistent vector and score.
Reference: https://www.first.org/cvss/v3.1/specification-document
"""
from __future__ import annotations
AV = {"N": 0.85, "A": 0.62, "L": 0.55, "P": 0.20}
AC = {"L": 0.77, "H": 0.44}
PR_U = {"N": 0.85, "L": 0.62, "H": 0.27} # scope unchanged
PR_C = {"N": 0.85, "L": 0.68, "H": 0.50} # scope changed
UI = {"N": 0.85, "R": 0.62}
CIA = {"H": 0.56, "L": 0.22, "N": 0.0}
def _roundup(value: float) -> float:
"""CVSS roundup: ceil(value * 10) / 10, with a 0-floor."""
int_input = round(value * 100000)
if int_input % 10000 == 0:
return int_input / 100000.0
return (round((int_input / 10000)) + 1) / 10.0
def severity_from_score(score: float) -> str:
if score == 0.0:
return "None"
if score < 4.0:
return "Low"
if score < 7.0:
return "Medium"
if score < 9.0:
return "High"
return "Critical"
def compute_base_vector(
*,
av: str = "N",
ac: str = "L",
pr: str = "N",
ui: str = "N",
s: str = "U",
c: str = "H",
i: str = "H",
a: str = "H",
) -> tuple[str, float]:
"""Return (CVSS:3.1/... vector string, base_score float)."""
av, ac, pr, ui, s, c, i, a = (x.upper() for x in (av, ac, pr, ui, s, c, i, a))
vector = f"CVSS:3.1/AV:{av}/AC:{ac}/PR:{pr}/UI:{ui}/S:{s}/C:{c}/I:{i}/A:{a}"
if s == "C":
pr_val = PR_C[pr]
else:
pr_val = PR_U[pr]
iss = 1 - ((1 - CIA[c]) * (1 - CIA[i]) * (1 - CIA[a]))
if s == "U":
impact = 6.42 * iss
else:
impact = 7.52 * (iss - 0.029) - 3.25 * (iss * 0.9731 - 0.02) ** 13
exploitability = 8.22 * AV[av] * AC[ac] * pr_val * UI[ui]
if impact <= 0:
score = 0.0
elif s == "U":
score = _roundup(min(impact + exploitability, 10.0))
else:
score = _roundup(min(1.08 * (impact + exploitability), 10.0))
return vector, round(score, 1)
def cvss_for(vuln_key: str, difficulty: str) -> tuple[str, float]:
"""Hand-tuned default CVSS vectors per vulnerability archetype.
'difficulty' nudges PR/UI slightly: enterprise examples tend to require
a foothold or user interaction, so the score is realistic not maximal.
Returns (vector, score).
"""
enterprise = difficulty in ("Real-world enterprise", "Expert")
base = _DEFAULT_VECTORS.get(vuln_key, _DEFAULT_VECTORS["__default__"])
av, ac, pr, ui, s, c, i, a = [base[f"AV"], base["AC"], base["PR"], base["UI"],
base["S"], base["C"], base["I"], base["A"]]
# Real-world enterprise scenarios often require low-priv/UI foothold.
if enterprise and pr == "N":
pr = "L"
if enterprise and ui == "N":
ui = "R"
return compute_base_vector(av=av, ac=ac, pr=pr, ui=ui, s=s, c=c, i=i, a=a)
# Default vectors keyed by vulnerability_name (normalized lower). Each maps to
# the dominant realistic exploitability/impact profile for that weakness.
_DEFAULT_VECTORS = {
"__default__": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "H"},
"sql injection": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "H"},
"cross-site scripting": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "C", "C": "L", "I": "L", "A": "N"},
"ssrf": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "C", "C": "H", "I": "L", "A": "L"},
"command injection": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "H"},
"path traversal": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "N", "A": "N"},
"xml external entity": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "U", "C": "H", "I": "L", "A": "L"},
"insecure direct object reference": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "N"},
"broken access control": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "N"},
"broken authentication": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "N"},
"broken authorization": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "N"},
"jwt vulnerabilities": {"AV": "N", "AC": "H", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "N"},
"oauth vulnerabilities": {"AV": "N", "AC": "H", "PR": "N", "UI": "R", "S": "U", "C": "H", "I": "H", "A": "N"},
"session management": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "U", "C": "H", "I": "H", "A": "N"},
"cross-site request forgery": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "U", "C": "H", "I": "H", "A": "N"},
"insecure file upload": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "H"},
"insecure deserialization": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "H"},
"open redirect": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "C", "C": "L", "I": "L", "A": "N"},
"race condition": {"AV": "N", "AC": "H", "PR": "L", "UI": "N", "S": "U", "C": "L", "I": "H", "A": "L"},
"insecure cryptography": {"AV": "N", "AC": "H", "PR": "N", "UI": "N", "S": "U", "C": "H", "I": "N", "A": "N"},
"hardcoded secrets": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "U", "C": "H", "I": "N", "A": "N"},
"business logic": {"AV": "N", "AC": "L", "PR": "L", "UI": "R", "S": "U", "C": "H", "I": "H", "A": "N"},
"missing rate limiting": {"AV": "N", "AC": "L", "PR": "N", "UI": "N", "S": "U", "C": "N", "I": "L", "A": "H"},
"sensitive data logging": {"AV": "L", "AC": "L", "PR": "L", "UI": "N", "S": "U", "C": "H", "I": "N", "A": "N"},
"header injection": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "C", "C": "L", "I": "L", "A": "N"},
"prompt injection": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "C", "C": "H", "I": "H", "A": "L"},
"rag security": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "C", "C": "H", "I": "L", "A": "L"},
"mcp security": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "C", "C": "H", "I": "H", "A": "L"},
"ai agent security": {"AV": "N", "AC": "L", "PR": "N", "UI": "R", "S": "C", "C": "H", "I": "H", "A": "H"},
"graphql security": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "N"},
"rest api security": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "N"},
"grpc security": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "U", "C": "H", "I": "H", "A": "N"},
"cloud misconfiguration": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "C", "C": "H", "I": "H", "A": "H"},
"kubernetes security": {"AV": "N", "AC": "L", "PR": "L", "UI": "N", "S": "C", "C": "H", "I": "H", "A": "H"},
"docker security": {"AV": "L", "AC": "L", "PR": "L", "UI": "N", "S": "C", "C": "H", "I": "H", "A": "H"},
}