Create cvss-new.js
Browse files- cvss-new.js +121 -0
cvss-new.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* CVSS Calculator
|
| 3 |
+
* Version 0.1 beta
|
| 4 |
+
* Usage:
|
| 5 |
+
* Create an HTML element with an id:
|
| 6 |
+
* <div id="cvssboard"></div>
|
| 7 |
+
* Instantiate the CVSS calculator:
|
| 8 |
+
* let c = new CVSS("cvssboard");
|
| 9 |
+
* or with optional callbacks:
|
| 10 |
+
* let c = new CVSS("cvssboard", {
|
| 11 |
+
* onchange: function() {...},
|
| 12 |
+
* onsubmit: function() {...}
|
| 13 |
+
* });
|
| 14 |
+
*
|
| 15 |
+
* Set a vector:
|
| 16 |
+
* c.set('AV:L/AC:L/PR:N/UI:N/S:C/C:N/I:N/A:L');
|
| 17 |
+
* Retrieve the result:
|
| 18 |
+
* c.get() -> { score: 4.3, vector: 'AV:L/AC:L/PR:N/UI:N/S:C/C:N/I:N/A:L' }
|
| 19 |
+
*/
|
| 20 |
+
|
| 21 |
+
// CVSS Class
|
| 22 |
+
class CVSS {
|
| 23 |
+
constructor(id, options = {}) {
|
| 24 |
+
this.options = options;
|
| 25 |
+
this.containerId = id;
|
| 26 |
+
this.container = document.getElementById(id);
|
| 27 |
+
this.baseMetrics = this.getBaseMetrics();
|
| 28 |
+
this.metricElements = {};
|
| 29 |
+
this.setupUI();
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
getBaseMetrics() {
|
| 33 |
+
return {
|
| 34 |
+
AV: { label: 'Attack Vector', values: { N: 'Network', A: 'Adjacent', L: 'Local', P: 'Physical' }},
|
| 35 |
+
AC: { label: 'Attack Complexity', values: { L: 'Low', H: 'High' }},
|
| 36 |
+
PR: { label: 'Privileges Required', values: { N: 'None', L: 'Low', H: 'High' }},
|
| 37 |
+
UI: { label: 'User Interaction', values: { N: 'None', R: 'Required' }},
|
| 38 |
+
S: { label: 'Scope', values: { C: 'Changed', U: 'Unchanged' }},
|
| 39 |
+
C: { label: 'Confidentiality', values: { H: 'High', L: 'Low', N: 'None' }},
|
| 40 |
+
I: { label: 'Integrity', values: { H: 'High', L: 'Low', N: 'None' }},
|
| 41 |
+
A: { label: 'Availability', values: { H: 'High', L: 'Low', N: 'None' }}
|
| 42 |
+
};
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
setupUI() {
|
| 46 |
+
if (!this.container) return;
|
| 47 |
+
|
| 48 |
+
const form = document.createElement('form');
|
| 49 |
+
form.className = 'cvss-form';
|
| 50 |
+
|
| 51 |
+
for (let metric in this.baseMetrics) {
|
| 52 |
+
const metricData = this.baseMetrics[metric];
|
| 53 |
+
const dl = document.createElement('dl');
|
| 54 |
+
dl.className = metric;
|
| 55 |
+
|
| 56 |
+
const dt = document.createElement('dt');
|
| 57 |
+
dt.textContent = metricData.label;
|
| 58 |
+
dl.appendChild(dt);
|
| 59 |
+
|
| 60 |
+
for (let value in metricData.values) {
|
| 61 |
+
const dd = document.createElement('dd');
|
| 62 |
+
dl.appendChild(dd);
|
| 63 |
+
|
| 64 |
+
const input = document.createElement('input');
|
| 65 |
+
input.type = 'radio';
|
| 66 |
+
input.name = metric;
|
| 67 |
+
input.value = value;
|
| 68 |
+
input.id = `${this.containerId}-${metric}-${value}`;
|
| 69 |
+
input.className = `${metric}-${value}`;
|
| 70 |
+
input.addEventListener('change', () => this.updateMetric(metric, value));
|
| 71 |
+
|
| 72 |
+
this.metricElements[`${metric}-${value}`] = input;
|
| 73 |
+
dd.appendChild(input);
|
| 74 |
+
|
| 75 |
+
const label = document.createElement('label');
|
| 76 |
+
label.setAttribute('for', input.id);
|
| 77 |
+
label.textContent = metricData.values[value];
|
| 78 |
+
dd.appendChild(label);
|
| 79 |
+
}
|
| 80 |
+
form.appendChild(dl);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
this.container.appendChild(form);
|
| 84 |
+
this.form = form;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
updateMetric(metric, value) {
|
| 88 |
+
// You can implement the metric change logic here if needed
|
| 89 |
+
console.log(`Metric ${metric} changed to ${value}`);
|
| 90 |
+
if (this.options.onchange) this.options.onchange();
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
set(vector) {
|
| 94 |
+
const metrics = vector.split('/');
|
| 95 |
+
metrics.forEach(pair => {
|
| 96 |
+
const [metric, value] = pair.split(':');
|
| 97 |
+
if (this.metricElements[`${metric}-${value}`]) {
|
| 98 |
+
this.metricElements[`${metric}-${value}`].checked = true;
|
| 99 |
+
}
|
| 100 |
+
});
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
get() {
|
| 104 |
+
const result = { score: 0, vector: [] };
|
| 105 |
+
for (let metric in this.baseMetrics) {
|
| 106 |
+
const checkedInput = this.form.querySelector(`input[name=${metric}]:checked`);
|
| 107 |
+
if (checkedInput) {
|
| 108 |
+
result.vector.push(`${metric}:${checkedInput.value}`);
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
result.vector = result.vector.join('/');
|
| 112 |
+
// Calculations for the score would go here. This is just a placeholder.
|
| 113 |
+
result.score = this.calculateScore(result.vector);
|
| 114 |
+
return result;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
calculateScore(vector) {
|
| 118 |
+
// Placeholder scoring function; implement CVSS scoring here if needed
|
| 119 |
+
return Math.random() * 10; // Example score calculation
|
| 120 |
+
}
|
| 121 |
+
}
|