-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonte.js
330 lines (315 loc) · 13 KB
/
monte.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"use strict";
//////////////////////
//
/**
* A monte carlo simulator for VCDB data. Creates a risk profile using an existing vcdb dataset.
*
* @author Steven Walker-Roberts
* @version v0.0.1
* @copyright Copyright (C) Steven Walker-Roberts 2017. MIT License.
*
* @class vcdbMonteCarlo
*/
///////////////
// Debug Mode
/**
* Debug mode flag.
* @global
*/
var debug = 0;
//////////////
// Requires
//core node modules
/**
* @requires http
* @external http
* @see {@link https://nodejs.org/api/http.html}
*/
const http = require('http');
/**
* @requires fs.promised
* @external fs.promised
* @see {@link https://www.npmjs.com/package/fs.promised.html}
*/
const fs = require('fs.promised');
/**
* @requires assert
* @external assert
* @see {@link https://nodejs.org/api/assert.html}
*/
const assert = require('assert');
/**
* @requires request
* @external request
* @see {@link https://github.com/request/request}
*/
const request = require('request');
/**
* @requires lodash
* @external lodash
* @see {@link https://www.npmjs.com/package/lodash}
*/
const _ = require('lodash');
/**
* @requires mersennetwister
* @external mersennetwister
* @see {@link https://www.npmjs.com/package/mersennetwister}
*/
const MersenneTwister = require('mersennetwister');
/////////
// Main
/**
* Gets the numerical data from VerisDB Analyst.
* @function vcdbMonteCarlo#getData
* @param {string} path - API path on which to make the request.
* @returns {object} - An object containing incident data.
*/
exports.getData = function getData(path) {
return new Promise((resolve, reject) => {
try {
//get the vcdb data
request(process.env.VERIS+path, (err, res, body) => {
//parse raw data
if (err) reject(console.error((new Date()).toISOString()+" Error: "+err.message));
if (!err && res.statusCode == 200) {
var data = JSON.parse(body);
if(debug) console.log((new Date()).toISOString()+" Data received.");
if(debug) console.log(data);
resolve(data);
} //end of if
}) //end of request
} catch(e) { //end of try
reject(console.error((new Date()).toISOString()+" Error: "+err.message));
} //end of catch
}) //end of promise
} //end of getData function
/**
* Gets the numerical data from VerisDB Analyst.
* @function vcdbMonteCarlo#postAggregationQuery
* @param {string} path - The path of the query API.
* @param {object} query - Query objects containing match, group, sort and unwind pattern.
* @returns {object} - An object containing incident data.
*/
exports.postAggregationQuery = function postAggregationQuery(path, query){
return new Promise((resolve, reject) => {
try {
request({
uri: process.env.VERIS+path,
method: 'POST',
body: query,
json: true
},
async (err, res, body) => {
if (err) reject(console.error((new Date()).toISOString()+" Error: "+err.message));
if (!err && res.statusCode == 200) {
if(debug) console.log((new Date()).toISOString()+" Data received.");
if(debug) console.log(body);
resolve(body);
} //end of if
})//end of request
} catch(e) { //end of try
reject(console.error((new Date()).toISOString()+" Error: "+err.message));
} //end of catch
}) //end of request
} //end of postAggregationQuery function
/**
* Creates a probability distribution from aggregated MongoDB data.
* @function vcdbMonteCarlo#computeProbability
* @param {object} data - The object containing aggregated data.
* @returns {object} - An object containing computed probability key, value arrays.
*/
exports.computeProbability = async function computeProbability(keys, values){
//create temporary context variables
var obj = {}
obj.keys = [];
obj.values = [];
obj.p = [];
//calculate probability of each key's value returning
if(values){
obj.values = values;
for(var i in obj.values){
obj.keys.push(keys[i]);
obj.p.push(obj.values[i]/(_.sum(values)));
}//end of for
} else {
for(var i in keys){
obj.keys.push(keys[i]._id);
obj.values.push(keys[i].count);
}
for(var i in obj.keys){
obj.p.push(obj.values[i]/_.sum(obj.values));
}
}
if(debug) console.log((new Date()).toISOString()+" Mapped probabilities.");
return obj;
}//end of computeProbability function
/**
* Carries out a selection from the range of probabilities using Mersenne-Twister 53-bit float random.
* @function vcdbMonteCarlo#doRandom
* @param {array} keys - The array containing the keys mapped by index to probabilities.
* @param {array} p - The array containing the keys mapped by index to probabilities.
* @returns {object} - The key mapped to the probability invocated at random.
*/
exports.doRandom = async function doRandom(keys, p){
//create instance of Mersenne-Twister random number generator
var mt = new MersenneTwister();
var rnd = mt.rndHiRes();
//create counter
var cp = 0;
//create inequalities iteratively to test p against.
for(var i in p){
for(var x in i){
if(i<p.length-1){
cp += p[x];
}//end of if
}//end of for
//return the key matching the index of the value falling into the inequality
if(rnd > ((i>0) ? cp : 0) && rnd <= ((i<p.length-1)?(cp+p[i]):1)){
if(debug) console.log((new Date()).toISOString()+" Random key selected.");
return keys[i];
}
else if(p[i] === p.length-1){
throw new Error("No probability was selected, an unknown error occured.");
}//end of else if
}//end of for
}//end of doRandom function
/**
* Draws a scenario together from possible probabilities.
* @function vcdbMonteCarlo#configureActor
* @returns {object} - The combined draw and blend of actor.
*/
exports.configureActor = async function configureActor(){
var obj = {};
obj.overallP = [];
var p = [];
var pStandard = [];
var types = ['internal', 'external', 'partner'];
var props = ['days', 'months'];
var mt = new MersenneTwister();
//randomly select a day and month
obj.day = Math.floor(mt.rndHiRes()*(31-1)+1);
obj.month = Math.floor(mt.rndHiRes()*(12-1)+1);
//iterate types array
for(var x in props){
obj[props[x]+'P'] = [];
for(var i in types){
//retrieve probability of actor type for each day and month
obj[props[x]+types[i]] = await module.exports.postAggregationQuery('query', {
match: "{\"actor."+types[i]+"\": {\"$exists\": true}}",
group: "$timeline.incident."+props[x].slice(0, -1),
sort: {_id: 1}
});
//remove null values
obj[props[x]+types[i]].splice(0,1);
//compute probabilities of actor types using known frequency of occurence
obj[props[x]+types[i]] = await module.exports.computeProbability(obj[props[x]+types[i]]);
//calculate a probability, given the day and month, of the actor type
var index = obj[props[x]+types[i]].keys.indexOf(obj[props[x].slice(0, -1)]);
obj[props[x]+types[i]].prob = obj[props[x]+types[i]].p[index];
obj[props[x]+'P'].push(obj[props[x]+types[i]].prob);
} //end of for
obj.overallP.push(obj[props[x]+'P']);
} //end of for
//compute combined probabilities
for(var i in obj.overallP[0]){
var pOverall = 1;
for(x in obj.overallP){
var pOverall = 1;
pOverall *= obj.overallP[x][i];
}
p.push(pOverall);
}
//standardise probabilities
for(var i in p){
pStandard.push(p[i]/(_.sum(p)));
}
//return an object with random date and random actor type
if(debug) console.log((new Date()).toISOString()+" Random actor selected.");
return {actor: await module.exports.doRandom(types, pStandard), date: obj.day+'/'+obj.month};
} //end of configureActor
/**
* Draws a scenario together from possible probabilities.
* @function vcdbMonteCarlo#configureScenario
* @param {string} type - Actor type which was randomly derived.
* @returns {object} - The combined draw and blend of scenario.
*/
exports.configureScenario = async function configureScenario(actor, date){
var data = {};
data.type = actor;
data.date = date;
//get random actor attributes
var actors = await module.exports.getData('actors/'+actor);
actors.motives.p = await module.exports.computeProbability(actors.motives.motive, actors.motives.count);
data.motive = await module.exports.doRandom(actors.motives.p.keys, actors.motives.p.p);
data.motiveP = actors.motives.p.p[actors.motives.p.keys.indexOf(data.motive)];
actors.varieties.p = await module.exports.computeProbability(actors.varieties.variety, actors.varieties.count);
data.variety = await module.exports.doRandom(actors.varieties.p.keys, actors.varieties.p.p);
data.varietyP = actors.varieties.p.p[actors.varieties.p.keys.indexOf(data.variety)];
//generate an attack type
var attacksInitial = await module.exports.postAggregationQuery('attacks/misuse/query', {
match: "{\"actor."+actor+"\": {\"$exists\": true}}"
});
attacksInitial.types.p = await module.exports.computeProbability(attacksInitial.types.type, attacksInitial.types.count);
data.attackType = await module.exports.doRandom(attacksInitial.types.p.keys, attacksInitial.types.p.p);
data.attackTypeP = data.motiveP = attacksInitial.types.p.p[attacksInitial.types.p.keys.indexOf(data.attackType)];
//get random attack attributes
var attacks = await module.exports.postAggregationQuery('attacks/'+data.attackType+'/query', {
match: "{\"actor."+actor+"\": {\"$exists\": true}}"
});
attacks.varieties.p = await module.exports.computeProbability(attacks.varieties.variety, attacks.varieties.count);
data.attack = await module.exports.doRandom(attacks.varieties.p.keys, attacks.varieties.p.p);
data.attackP = attacks.varieties.p.p[attacks.varieties.p.keys.indexOf(data.attack)];
attacks.vectors.p = await module.exports.computeProbability(attacks.vectors.vector, attacks.vectors.count);
data.vector = await module.exports.doRandom(attacks.vectors.p.keys, attacks.vectors.p.p);
data.vectorP = attacks.vectors.p.p[attacks.vectors.p.keys.indexOf(data.vector)];
attacks.assets.p = await module.exports.computeProbability(attacks.assets.asset, attacks.assets.count);
data.asset = await module.exports.doRandom(attacks.assets.p.keys, attacks.assets.p.p);
data.assetP = attacks.assets.p.p[attacks.assets.p.keys.indexOf(data.asset)];
//get random impacts attributes
var impacts = await module.exports.postAggregationQuery('impacts/query', {
match: "{\"actor."+actor+"\": {\"$exists\": true}}"
});
impacts.varieties.p = await module.exports.computeProbability(impacts.varieties.variety, impacts.varieties.count);
data.impact = await module.exports.doRandom(impacts.varieties.p.keys, impacts.varieties.p.p);
data.impactP = impacts.varieties.p.p[impacts.varieties.p.keys.indexOf(data.impact)];
impacts.ratings.p = await module.exports.computeProbability(impacts.ratings.rating, impacts.ratings.count);
data.impactRating = await module.exports.doRandom(impacts.ratings.p.keys, impacts.ratings.p.p);
data.impactRatingP = impacts.ratings.p.p[impacts.ratings.p.keys.indexOf(data.impactRating)];
//get random victims attributes
var victims = await module.exports.postAggregationQuery('victims/query', {
match: "{\"actor."+actor+"\": {\"$exists\": true}}"
});
victims.countries.p = await module.exports.computeProbability(victims.countries.country, victims.countries.count);
data.country = await module.exports.doRandom(victims.countries.p.keys, victims.countries.p.p);
data.countryP = victims.countries.p.p[victims.countries.p.keys.indexOf(data.country)];
victims.employeeNumbers.p = await module.exports.computeProbability(victims.employeeNumbers.employee_count, victims.employeeNumbers.count);
data.employee_count = await module.exports.doRandom(victims.employeeNumbers.p.keys, victims.employeeNumbers.p.p);
data.employee_countP = victims.employeeNumbers.p.p[victims.employeeNumbers.p.keys.indexOf(data.employee_count)];
victims.industries.p = await module.exports.computeProbability(victims.industries.industry, victims.industries.count);
//keep trying to populate industry until defined
do{
data.industry = await module.exports.doRandom(victims.industries.p.keys, victims.industries.p.p);
data.industryP = victims.industries.p.p[victims.industries.p.keys.indexOf(data.industry)];
} while(!data.industry);
if(debug) console.log((new Date()).toISOString()+" Successfully blended scenario.");
return data;
}
/**
* Runs trials for the specified number of times.
* @function vcdbMonteCarlo#runTrials
* @param {number} times - The number of times to run the trial.
* @returns {number} - Success status.
*/
exports.runTrials = async function runTrials(times) {
for(var i=0; i<times; i++){
console.log(((i/times)*100).toFixed(2)+" % completed");
var type = await module.exports.configureActor();
var data = await module.exports.configureScenario(type.actor, type.date);
if(i===0) await fs.writeFile("data.json", "["+JSON.stringify(data)+", ");
else if(i===times-1) await fs.appendFile("data.json", JSON.stringify(data)+"]");
else await fs.appendFile("data.json", JSON.stringify(data)+", ");
}
console.log("100% complete");
return 1;
}
module.exports.runTrials(process.argv[2]);