File: generate_test_vectors.py

package info (click to toggle)
python-cvss 3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 107,152 kB
  • sloc: python: 3,235; makefile: 18
file content (69 lines) | stat: -rw-r--r-- 2,513 bytes parent folder | download
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
"""
Generates test vectors for testing of CVSS library. All of them are created using cvsslib
from https://pypi.python.org/pypi/cvsslib .

Simple vectors are all vector combinations without any optional values.
Random vectors are 100,000 randomly generated vectors.
Runs only with Python 3 because cvsslib does not support Python 2.
"""

from itertools import product
from random import choice

import cvsslib
from generate_constants import METRICS, NAMES, VALUES, build_constants

NR_OF_VECTORS = 100000


for cvss_version in range(2):
    # CVSS version selections
    if cvss_version == 0:
        cvsslib_module = cvsslib.cvss2
        cvss_name = "2"
        not_defined = "ND"
        prefix = ""
    else:
        cvsslib_module = cvsslib.cvss3
        cvss_name = "3"
        not_defined = "X"
        prefix = "CVSS:3.0/"

    # Generate constants
    constants = build_constants(METRICS[cvss_version], NAMES[cvss_version], VALUES[cvss_version])
    metrics_abbreviations, metrics_mandatory, metrics_values, metrics_value_names = constants

    # Generate all simple vectors
    list_of_iterables = []
    for metric in metrics_mandatory:
        metric_options = [metric + ":" + value for value in metrics_values[metric]]
        list_of_iterables.append(metric_options)

    with open("vectors_simple{}".format(cvss_name), "w") as f:
        for combo in product(*list_of_iterables):
            combo = [a for a in combo if a != ""]
            vector = "/".join(combo)
            v = cvsslib.vector.calculate_vector(vector, module=cvsslib_module)
            print("{}{} - {}".format(prefix, vector, v), file=f)

    # Generate random complex vectors
    list_of_iterables = []
    for metric in metrics_abbreviations:
        metric_options = [metric + ":" + value for value in metrics_values[metric]]
        if not_defined in metrics_values[metric]:  # not defined value can also be missing
            metric_options.append("")
        list_of_iterables.append(metric_options)

    with open("vectors_random{}".format(cvss_name), "w") as f:
        i = 0
        while i < NR_OF_VECTORS:
            combo = [choice(a) for a in list_of_iterables]
            combo = [a for a in combo if a != ""]
            vector = "/".join(combo)
            try:
                v = cvsslib.vector.calculate_vector(vector, module=cvsslib_module)
            except TypeError:
                pass
            else:
                print("{}{} - {}".format(prefix, vector, v), file=f)
                i += 1