-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpropertyMapper.js
290 lines (275 loc) · 9.68 KB
/
propertyMapper.js
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
286
287
288
289
290
var threatData;
var allThreats;
var rows = [];
var idFromUrl;
var urlParams = new URLSearchParams(window.location.search);
var categoryList = [];
var selectedThreats = [];
//Load the json file that contains the threat and device properties mapping
fetch("../_data/properties_threat_mappings.json")
.then((res) => {
if (!res.ok) {
throw new Error(`HTTP error! Status: ${res.status}`);
}
return res.json();
})
.then((json) => {
threatData = json;
if (urlParams.size > 0) {
// get clicked id from url
idFromUrl = urlParams.get("id");
var propID = document.getElementById(idFromUrl);
propID.checked = true;
id_found = false;
// below code is run if the parent property is selected
for (let i = 0; i < threatData.properties.length; i++) {
if (
threatData.properties[i].isparentProp &&
threatData.properties[i].id === idFromUrl
) {
updateList(threatData.properties[i].id);
id_found = true;
}
}
// below code is run if any of the sub properties is selected
if (!id_found) {
has_parent(idFromUrl);
}
}
});
fetch("../_data/threats.json")
.then((res) => {
if (!res.ok) {
throw new Error(`HTTP error! Status: ${res.status}`);
}
return res.json();
})
.then((json) => {
allThreats = json;
createThreatColumn();
});
// Displays threats depending on device property checkbox selected
function updatePropertiesNew() {
var selectedProperties = [];
var propertiesDiv = document.getElementById("props");
var propertiesDiv_inner = "";
selectedThreats = [];
rows = [];
for (let i = 0; i < threatData.properties.length; i++) {
var propID = threatData.properties[i].id;
var propText = threatData.properties[i].text;
if (document.getElementById(propID).checked) {
selectedProperties = selectedProperties.concat(
threatData.properties[i]["threats"]
);
if (selectedProperties.length != 0) {
propertiesDiv_inner += "<br><b>" + propID + "</b>";
for (var j = 0; j < selectedProperties.length; j++) {
selectedThreats.push(selectedProperties[j].id);
var tid = selectedProperties[j].id;
var tdesc = selectedProperties[j].text;
propertiesDiv_inner +=
"<li><a href='/threats/" +
tid +
".html'>" +
tid +
"</a> - " +
tdesc +
"</li>";
rows.push({ tid: tid, tdesc: tdesc, pid: propID, pdesc: propText });
}
}
}
selectedProperties = [];
}
propertiesDiv.innerHTML = "<ul>" + propertiesDiv_inner + "</ul>";
}
function createThreatColumn() {
dict = [];
var categoryDict = {};
for (let threat of allThreats.threats) {
category = threat.category;
if (!(category in categoryDict)) {
categoryDict[category] = [];
}
categoryDict[category].push({
text: threat.id + " - " + threat.text,
hyperlink: "https://emb3d.mitre.org/threats/" + threat.id + ".html"
});
}
for (let category in categoryDict) {
categoryList.push({ id: category, threats: categoryDict[category] });
}
}
// Downloads the current device property-threat selection as a CSV
async function downloadCSV() {
let columnNames = [];
for (let i = 65; i <= 90; i++) {
columnNames.push(String.fromCharCode(i));
}
const workbook = new ExcelJS.Workbook();
const threatSheet = workbook.addWorksheet("Selected Threats");
var columnIncrement = 0;
for (let i = 0; i < categoryList.length; i++) {
threatList = categoryList[i].threats;
threatList.unshift({
text: categoryList[i].id,
hyperlink:
"https://emb3d.mitre.org/threats/" +
categoryList[i].id.replace(/\s+/g, "-").toLowerCase() +
".html",
});
threatSheet.getColumn(columnNames[columnIncrement]).values = threatList;
columnIncrement = columnIncrement + 1;
}
// highlight the invoked threats
threatSheet.eachRow((row) => {
row.eachCell((cell) => {
if (selectedThreats.includes(cell.value.text.substring(0, 7))) {
cell.fill = {
type: "pattern",
pattern: "solid",
fgColor: { argb: "CC66FF" }
};
}
cell.font = { underline: true };
});
});
// sheet formatting
threatSheet.getColumn(1).width = 58;
threatSheet.getColumn(2).width = 54;
threatSheet.getColumn(3).width = 57;
threatSheet.getColumn(4).width = 43;
threatSheet.getCell('A1').alignment = { vertical: 'middle', horizontal: 'center' };
threatSheet.getCell('A1').font = { name: 'Arial Black', family: 2, size: 13, bold: true };
threatSheet.getCell('B1').alignment = { vertical: 'middle', horizontal: 'center' };
threatSheet.getCell('B1').font = { name: 'Arial Black', family: 2, size: 13, bold: true };
threatSheet.getCell('C1').alignment = { vertical: 'middle', horizontal: 'center' };
threatSheet.getCell('C1').font = { name: 'Arial Black', family: 2, size: 13, bold: true };
threatSheet.getCell('D1').alignment = { vertical: 'middle', horizontal: 'center' };
threatSheet.getCell('D1').font = { name: 'Arial Black', family: 2, size: 13, bold: true };
const propertySheet = workbook.addWorksheet("Selected Properties");
propertySheet.columns = [
{ header: "Threat ID", key: "tid", width: 10 },
{ header: "Threat Description", key: "tdesc", width: 51 },
{ header: "Device Property ID", key: "pid", width: 18 },
{ header: "Device Property Description", key: "pdesc", width: 95 }
];
rows.forEach(function (rowArray) {
propertySheet.addRow({
tid: {
text: rowArray.tid,
hyperlink: "https://emb3d.mitre.org/threats/" + rowArray.tid + ".html"
},
tdesc: rowArray.tdesc,
pid: {
text: rowArray.pid,
hyperlink:
"https://emb3d.mitre.org/properties-mapper/?id=" + rowArray.pid
},
pdesc: rowArray.pdesc
});
});
let websiteColumn = propertySheet.getColumn("tid");
// Iterate over the cells in the column
websiteColumn.eachCell((cell, rowNumber) => {
// Skip the header row
if (rowNumber === 1) return;
// Set the font style to underline
cell.font = { underline: true };
});
let propertiesColumn = propertySheet.getColumn("pid");
propertiesColumn.eachCell((cell, rowNumber) => {
// Skip the header row
if (rowNumber === 1) return;
// Set the font style to underline
cell.font = { underline: true };
});
// sheet formatting
propertySheet.getCell('A1').font = { size: 13, bold: true}
propertySheet.getCell('B1').font = { size: 13, bold: true}
propertySheet.getCell('C1').font = { size: 13, bold: true}
propertySheet.getCell('D1').font = { size: 13, bold: true}
const buffer = await workbook.xlsx.writeBuffer();
// Create a blob from the buffer and create a link to download it
const blob = new Blob([buffer], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "heat_map.xlsx";
a.click();
}
// Uses the updateList_helper() function to accurately hide/display sub properties until their parent property is checked/unchecked
function updateList(parent) {
for (let i = 0; i < threatData.properties.length; i++) {
if (
parent == threatData.properties[i].id &&
threatData.properties[i].subProps.length != 0
) {
has_subprop = true;
updateList_helper(parent, threatData.properties[i].subProps, has_subprop);
}
if (threatData.properties[i].subProps.length == 0) {
has_subprop = false;
updateList_helper(parent, threatData.properties[i].subProps, has_subprop);
}
}
}
// this function is run when there is a property id in the url
// recursively checks the parent checkbox so that the checkboxes are accurately selected and displayed
function has_parent(child) {
for (let i = 0; i < threatData.properties.length; i++) {
for (let j = 0; j < threatData.properties[i].subProps.length; j++) {
if (threatData.properties[i].subProps[j] === child) {
const propID = document.getElementById(threatData.properties[i].id);
if (propID) {
propID.checked = true;
// Recursively check the parent
has_parent(threatData.properties[i].id);
updateList(threatData.properties[i].id);
}
return; // Exit the loop
}
}
}
}
// Helps the updateList() function to accurately hide/display sub properties until their parent property is checked/unchecked
// Depending on the selction, the updatePropertiesNew() function displays the corresponding threats
function updateList_helper(parent, child, has_subprop) {
// if id is present in url then run below code
if (urlParams.size > 0) {
has_parent(parent);
has_subprop = true;
urlParams = [];
}
if (has_subprop) {
for (let i = 0; i < child.length; i++) {
const el = document.getElementById(parent);
parent_margin = document.getElementById(parent).style.marginLeft;
if (parent_margin == "") {
parent_margin = "0px";
}
if (el.checked) {
className = document.getElementsByClassName(child[i]);
className[0].style.marginLeft =
parseInt(parent_margin.replace(/px/, "")) + 20 + "px";
for (let j = 0; j < className.length; j++) {
className[j].style.display = "inline";
}
updatePropertiesNew();
} else {
className = document.getElementsByClassName(child[i]);
for (let j = 0; j < className.length; j++) {
className[j].checked = false;
className[j].style.display = "none";
}
updatePropertiesNew();
}
updateList(child[i]);
}
} else {
updatePropertiesNew();
}
}