-
Notifications
You must be signed in to change notification settings - Fork 1
/
merger.js
160 lines (141 loc) · 3.45 KB
/
merger.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
let piper = require("./piper2.0");
let util = require("util")
let promise = util.promisify;
let custom = util.promisify.custom
let errors = {};
async function callbacker(func,arg,callback){
try{
result = func(arg);
callback(undefined,result)
}catch(err){
callback(err,undefined);
}
}
let prm = promise(callbacker)
let type = process.argv[2]
let ip = process.argv[3] || "0.0.0.0"
let url = process.argv[4]
function validate(argv){
return argv.length == 5
}
if(!validate(process.argv)){
errors.COMMON="Incomplete Argument, Length of Argument must be equals to 5";
console.log(errors)
return;
}
//console.log(type)
//console.log(ip)
//console.log(url)
let fetched_res = {}
let ready_signal = 0;
async function runProcess(func,arg,cb){
const res = await prm(func,arg).then((res) => {
// Handle the error.
cb(res)
}).catch((err)=>{
console.log(err)
});
}
let Event = require("events")
class Emitter extends Event{}
let status_em = new Emitter();
let avarice_ev = new Emitter();
let prejudice_ev = new Emitter();
let source_ev = new Emitter();
runProcess(piper.avarice,ip,(res)=>{
res((result)=>{
if(result.errors) errors.AV_ERRORS = result.stackTrace;
avarice_ev.emit("ready",result.result);
})
});
runProcess(piper.getSource,`JSON_SOURCE/${type}.json`,(res)=>{
//console.log(`${res} is the result`)
//if(res.error_reports) errors._ERRORS = res.error_reports
source_ev.emit("ready",res);
});
runProcess(piper.detectPR,ip+url,(res)=>{
//console.log(final)
//since the variable is a promise, it has to be handled this way
prejudice_ev.emit("ready",res.result);
// if(res.error_reports) {
// errors.PR_ERRORS = res.error_reports;
// let {spawn} = require("child_process");
// insert = spawn("path_apis/insertpath",[url,"0","0"]);
// insert.stdout.on("data",(err,data)=>{
// //console.log(err);console.log(data);
// })
// insert.stderr.on('data',(data)=>{
// //console.log(data.toString())
// })
// }
})
function readyCheck(){
//console.log(ready_signal)
if(ready_signal == 3){
//console.log("READY")
status_em.emit("ready",fetched_res);
}
}
avarice_ev.on("ready",(data)=>{
//console.log("AVR")
fetched_res['AV'] = `AV:${data}`;
ready_signal+=1;
readyCheck();
})
prejudice_ev.on('ready',(data)=>{
//console.log("A")
fetched_res['PR'] = `PR:${data}`;
ready_signal+=1;
readyCheck();
})
source_ev.on('ready',(data)=>{
// console.log("SOUCE RE")
// console.log(data)
// data = JSON.parse(data);
fetched_res['STRING'] = data.vector;
fetched_res['likelyhood'] = data.final
ready_signal+=1;
readyCheck();
})
function modifyAV(string,av){
return string.replace(/AV:\S/,av);
}
function modifyPR(string,pr){
return string.replace(/PR:\S/,pr);
}
function sev(sev){
if(sev>9.0){
return "CRITICAL"
}
if(sev>7.0){
return "HIGH"
}
if(sev>4.0){
return "MEDIUM"
}
if(sev>0.1){
return "LOW"
}
return "NONE"
}
status_em.on("ready",(data)=>{
//console.log(data);
let av_mod = modifyAV(data.STRING,data.AV)
let final = modifyPR(av_mod,data.PR);
let likelyhood = data.likelyhood;
//console.log(final)
//CVSS:3.0/"+def.replace(/.$/,"")
let score = piper.calculator("CVSS:3.0/"+final.replace(/.$/,"")).trim()
let stat_score = piper.calculator("CVSS:3.0/"+data.STRING.replace(/.$/,"")).trim()
let result = {
stat_vector:data.STRING,
vector:final,
stat_score:stat_score,
score:score,
stat_severity:sev(stat_score),
severity:sev(score),
errors:errors,
likelyhood:`${likelyhood}`
}
console.log(JSON.stringify(result))
})