-
Notifications
You must be signed in to change notification settings - Fork 0
/
nessus-parse.js
174 lines (143 loc) · 3.7 KB
/
nessus-parse.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
/* eslint-disable no-cond-assign */
/* eslint-disable no-restricted-syntax */
const csvParse = require('csv-parse');
/**
* Получает уровень уязвимости по CVSS
* @param {number} number - CVSS
* @returns {number} - Уровень уязвимости
*/
function getLevel(number) {
if (number >= 9) {
return 5;
}
if (number >= 7) {
return 4;
}
if (number >= 5) {
return 3;
}
if (number >= 3) {
return 2;
}
return 1;
}
/**
* Парсит имя источника из URL
* @param {String} url - URL
* @returns {String} Источник
*/
function getSourceFromUrl(url) {
// eslint-disable-next-line no-useless-escape
return url.match(/^\w+:\/\/([^\/]+)/)[1];
}
/**
* Парсит CSV отчет из Nessus
* @param {Stream} stream - readable поток с отчетом
* @param {Function} cb - callback. вызывается после завершения работы функции
*/
module.exports = function nessusParse(stream, cb) {
const hosts = {};
const parser = csvParse({
from_line: 2,
});
stream.pipe(parser);
parser.on('error', err => cb(err));
parser.on('end', () => {
const result = [];
for (const item of Object.values(hosts)) {
const vulns = [];
for (const vuln of Object.values(item.vulnerabilities)) {
vulns.push(vuln);
}
item.vulnerabilities = vulns;
result.push(item);
}
// errors пока не используются, но нужно чтобы совпадал формат с результатом парсинга MP
cb(null, { hosts: result, errors: [] });
});
function processRecord(record) {
const {
0: pluginId,
1: cve,
2: cvss,
4: address,
5: protocol,
6: port,
7: name,
9: description,
10: remediation,
11: seeAlso,
12: result,
} = record;
if (!address || !pluginId) {
return;
}
if (!hosts[address]) {
hosts[address] = {
ip: address,
hostname: address,
vulnerabilities: {},
};
}
if (!hosts[address].vulnerabilities[`${pluginId}-${port}-${protocol}`]) {
let reference = [];
if (seeAlso) {
const urls = seeAlso.split('\n');
reference = urls
.map(url => url.trim())
.map(url => ({
ref_id: url,
source: getSourceFromUrl(url),
ref_url: url,
}));
}
if (cve) {
reference.push({
ref_id: cve,
source: 'NVD',
ref_url: `http://cve.mitre.org/cgi-bin/cvename.cgi?name=${cve}`,
});
}
// default level is 1
let level = 1;
if (cvss) {
level = getLevel(parseInt(cvss, 10));
}
const item = {
name,
description,
level_id: level,
uid: pluginId,
remediation,
result,
};
if (port > 0) {
item.port = port;
item.protocol = protocol;
item.isNetworkVulnerability = true;
}
if (reference) {
item.reference = reference;
}
const cvssV2BaseScore = parseFloat(cvss);
if (!Number.isNaN(cvssV2BaseScore)) {
item.cvss_v2_base_score = cvssV2BaseScore;
}
hosts[address].vulnerabilities[`${pluginId}-${port}-${protocol}`] = item;
} else if (cve) {
hosts[address].vulnerabilities[
`${pluginId}-${port}-${protocol}`
].reference.push({
ref_id: cve,
source: 'NVD',
ref_url: `http://cve.mitre.org/cgi-bin/cvename.cgi?name=${cve}`,
});
}
}
parser.on('readable', () => {
let record;
while ((record = parser.read())) {
processRecord(record);
}
});
};