-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration.js
306 lines (262 loc) · 9.44 KB
/
integration.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
const async = require('async');
const request = require('postman-request');
const config = require('./config/config');
const Transformer = require('./transformer');
const includes = require('lodash.includes');
let previousDomainRegexAsString = '';
let previousIpRegexAsString = '';
let domainBlocklistRegex = null;
let ipBlocklistRegex = null;
let transformer;
let Logger;
let requestWithDefaults;
const risk = {
high: 3,
medium: 2,
low: 1,
'': 0
};
const DATA_TYPE_ERROR =
'if this error occurs it means you added data ' + 'types in config.js without updating the code in integration.js';
function _setupRegexBlocklists(options) {
if (options.domainBlocklistRegex !== previousDomainRegexAsString && options.domainBlocklistRegex.length === 0) {
Logger.debug('Removing Domain Blocklist Regex Filtering');
previousDomainRegexAsString = '';
domainBlocklistRegex = null;
} else {
if (options.domainBlocklistRegex !== previousDomainRegexAsString) {
previousDomainRegexAsString = options.domainBlocklistRegex;
Logger.debug({ domainBlocklistRegex: previousDomainRegexAsString }, 'Modifying Domain Blocklist Regex');
domainBlocklistRegex = new RegExp(options.domainBlocklistRegex, 'i');
}
}
if (options.ipBlocklistRegex !== previousIpRegexAsString && options.ipBlocklistRegex.length === 0) {
Logger.debug('Removing IP Blocklist Regex Filtering');
previousIpRegexAsString = '';
ipBlocklistRegex = null;
} else {
if (options.ipBlocklistRegex !== previousIpRegexAsString) {
previousIpRegexAsString = options.ipBlocklistRegex;
Logger.debug({ ipBlocklistRegex: previousIpRegexAsString }, 'Modifying IP Blocklist Regex');
ipBlocklistRegex = new RegExp(options.ipBlocklistRegex, 'i');
}
}
}
function _isEntityBlocklisted(entityObj, options) {
const blocklist = options.blocklist;
Logger.trace({ blocklist: blocklist }, 'checking to see what blocklist looks like');
if (includes(blocklist, entityObj.value.toLowerCase())) {
return true;
}
if (entityObj.isIPv4 && !entityObj.isPrivateIP) {
if (ipBlocklistRegex !== null) {
if (ipBlocklistRegex.test(entityObj.value)) {
Logger.debug({ ip: entityObj.value }, 'Blocked BlockListed IP Lookup');
return true;
}
}
}
if (entityObj.isDomain) {
if (domainBlocklistRegex !== null) {
if (domainBlocklistRegex.test(entityObj.value)) {
Logger.debug({ domain: entityObj.value }, 'Blocked BlockListed Domain Lookup');
return true;
}
}
}
return false;
}
function doLookup(entities, options, callback) {
Logger.trace({ entities: entities, options: options }, 'Entities received');
let results = [];
let minimumScore = options.minimumScore;
let minimumRisk = options.minimumRisk.value ? options.minimumRisk.value : 'low';
_setupRegexBlocklists(options);
Logger.trace({ minimumScore: minimumScore, minimumRisk: minimumRisk });
async.each(
entities,
(entity, done) => {
Logger.trace({ entity: entity }, 'Looking up entity in x-force exchange');
if (
(entity.isIP && entity.isPrivateIP) ||
// skip blocklisted domains or IPs
_isEntityBlocklisted(entity, options) ||
// skip invalid URLs (must start with http or https)
(entity.isURL && !_isValidUrl(entity.value))
) {
Logger.debug(`Ignoring entity '${entity.value}'`);
return done(null);
}
let requestOptions = {
auth: {
user: options.apikey,
password: options.password
}
};
if (entity.isIP) {
requestOptions.url = options.host + '/ipr/' + entity.value;
} else if (entity.isURL || entity.isDomain) {
requestOptions.url = options.host + '/url/' + entity.value;
} else if (entity.isHash) {
requestOptions.url = options.host + '/malware/' + entity.value;
} else if (entity.type === 'cve') {
requestOptions.url = options.host + '/vulnerabilities/search/' + entity.value;
} else {
Logger.error({ entity: entity }, DATA_TYPE_ERROR);
throw new Error(DATA_TYPE_ERROR);
}
requestWithDefaults(requestOptions, function (err, resp, body) {
Logger.trace({ error: err, body: body }, 'Results of lookup');
if (err) {
done({
detail: 'Error executing HTTPS request',
debug: err
});
return;
}
if (resp.statusCode !== 200) {
if (resp.statusCode === 404) {
Logger.trace({ id: entity.value }, 'Entity not in x-force exchange');
results.push({
entity: entity,
data: null
});
done();
} else if (resp.statusCode === 400) {
Logger.error({ error: body }, 'Bad Request');
done({
detail: 'Unexpected HTTP Status Code Received',
debug: body,
entityValue: entity.value
});
} else if (resp.statusCode === 401) {
Logger.error({ error: body }, 'Invalid Credentials');
done({
detail: 'Invalid authentication credentials',
debug: body,
entityValue: entity.value
});
} else {
Logger.error({ error: body }, 'Error looking up entity in x-force exchange');
done({
detail: 'Unexpected HTTP Status Code Received',
debug: body,
response: resp
});
}
return;
}
if (entity.isHash) {
let riskLevel = body.malware.risk;
Logger.trace({ risk: riskLevel, minimumRisk: minimumRisk }, 'Checking minimum risk');
if (risk[riskLevel] < risk[minimumRisk]) {
done();
return;
}
} else {
let score = body.score ? body.score : body.result ? body.result.score : body.score;
Logger.trace({ score: score, minimumScore: minimumScore }, 'Checking minimum score');
if (score < minimumScore) {
done();
return;
}
}
const details = transformer.transform(entity, body);
let result;
if (details.hasData && details.summary.length) {
result = {
entity: entity,
data: {
summary: details.summary,
details
}
};
} else {
result = {
entity,
data: null
};
}
Logger.trace({ result: result }, 'Result added to list');
results.push(result);
done();
});
},
(err) => {
Logger.trace({ results: results }, 'All entity lookups completed, returning results to client');
callback(err, results);
}
);
}
// x-force only seems to accept URLs that start with http or https
function _isValidUrl(url) {
if (url.startsWith('http') || url.startsWith('https')) {
return true;
}
return false;
}
function startup(logger) {
Logger = logger;
transformer = new Transformer(logger);
let defaults = {};
if (typeof config.request.cert === 'string' && config.request.cert.length > 0) {
defaults.cert = fs.readFileSync(config.request.cert);
}
if (typeof config.request.key === 'string' && config.request.key.length > 0) {
defaults.key = fs.readFileSync(config.request.key);
}
if (typeof config.request.passphrase === 'string' && config.request.passphrase.length > 0) {
defaults.passphrase = config.request.passphrase;
}
if (typeof config.request.ca === 'string' && config.request.ca.length > 0) {
defaults.ca = fs.readFileSync(config.request.ca);
}
if (typeof config.request.proxy === 'string' && config.request.proxy.length > 0) {
defaults.proxy = config.request.proxy;
}
if (typeof config.request.rejectUnauthorized === 'boolean') {
defaults.rejectUnauthorized = config.request.rejectUnauthorized;
}
defaults.json = true;
requestWithDefaults = request.defaults(defaults);
}
function validateStringOption(errors, options, optionName, errMessage) {
if (
typeof options[optionName].value !== 'string' ||
(typeof options[optionName].value === 'string' && options[optionName].value.length === 0)
) {
errors.push({
key: optionName,
message: errMessage
});
}
}
function validateOptions(options, callback) {
let errors = [];
validateStringOption(errors, options, 'host', 'You must provide a valid host for the IBM X-Force Exchange server.');
validateStringOption(
errors,
options,
'apikey',
'You must provide a valid apikey for authentication with the IBM X-Force Exchange server.'
);
validateStringOption(
errors,
options,
'password',
'You must provide a valid password for authentication with the IBM X-Force Exchange server.'
);
let minimumScore = Number(options.minimumScore.value);
if (options.minimumScore.value.length === 0 || !Number.isInteger(minimumScore)) {
errors.push({
key: 'minimumScore',
message: 'You must provide a valid, numeric, minimum score for Polarity to display.'
});
}
callback(null, errors);
}
module.exports = {
doLookup: doLookup,
startup: startup,
validateOptions: validateOptions
};