-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration.js
423 lines (365 loc) · 13.5 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const config = require('./config/config');
const request = require('postman-request');
const fs = require('fs');
const _ = require('lodash');
const Bottleneck = require('bottleneck');
let Logger;
let requestWithDefaults;
let limiter;
const BASE_URL = 'https://api.recordedfuture.com';
const MAX_NOTES = 20;
let domainBlockList = [];
let previousDomainBlockListAsString = '';
let previousDomainRegexAsString = '';
let previousIpRegexAsString = '';
let domainBlocklistRegex = null;
let ipBlocklistRegex = null;
function _setupLimiter (options) {
limiter = new Bottleneck({
maxConcurrent: Number.parseInt(options.maxConcurrent, 10), // no more than 5 lookups can be running at single time
highWater: 100, // no more than 100 lookups can be queued up
strategy: Bottleneck.strategy.OVERFLOW,
minTime: Number.parseInt(options.minTime, 10) // don't run lookups faster than 1 every 200 ms
});
}
function handleRequestError (request) {
return (options, expectedStatusCode, callback) => {
return request(options, (err, resp, body) => {
if (err || resp.statusCode !== expectedStatusCode) {
const error = err && JSON.parse(JSON.stringify(err, Object.getOwnPropertyNames(err)));
const errorResult = {
error,
statusCode: resp ? resp.statusCode : 'unknown',
body
};
callback(errorResult, body);
} else {
callback(null, body);
}
});
};
}
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.blocklist !== previousDomainBlockListAsString && options.blocklist.length === 0) {
Logger.debug('Removing Domain Blocklist Filtering');
previousDomainBlockListAsString = '';
domainBlockList = null;
} else {
if (options.blocklist !== previousDomainBlockListAsString) {
previousDomainBlockListAsString = options.blocklist;
Logger.debug({ domainBlocklist: previousDomainBlockListAsString }, 'Modifying Domain Blocklist Regex');
domainBlockList = options.blocklist.split(',').map((item) => item.trim());
}
}
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) {
if (domainBlockList.indexOf(entityObj.value) >= 0) {
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 parseErrorToReadableJSON (error) {
return JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)));
}
function doLookup (entities, options, callback) {
const lookupResults = [];
const errors = [];
const blockedEntities = [];
let numConnectionResets = 0;
let numThrottled = 0;
let hasAnyValidEntities = false;
if (!limiter) _setupLimiter(options);
let host = BASE_URL;
if (options.host) {
host = options.host;
}
_setupRegexBlocklists(options);
entities.forEach((entity) => {
if (!_isEntityBlocklisted(entity)) {
hasAnyValidEntities = true;
limiter.submit(_lookupEntity, entity, options, host, (err, result) => {
const maxRequestQueueLimitHit =
(_.isEmpty(err) && _.isEmpty(result) && !err instanceof Error) ||
(err && err.message === 'This job has been dropped by Bottleneck');
const statusCode = _.get(err, 'err.statusCode', '');
const isGatewayTimeout = statusCode === 502 || statusCode === 504 || statusCode === 500;
const isConnectionReset = _.get(err, 'err.error.code', '') === 'ECONNRESET';
Logger.trace({ maxRequestQueueLimitHit, isGatewayTimeout, isConnectionReset }, 'Limiter');
if (maxRequestQueueLimitHit || isConnectionReset || isGatewayTimeout) {
// Tracking for logging purposes
if (isConnectionReset || isGatewayTimeout) numConnectionResets++;
if (maxRequestQueueLimitHit) numThrottled++;
const resultObject = {
entity,
isVolatile: true,
data: {
summary: [' ! Search Limit Reached'],
details: {
errorMessage:
'The search failed due to a temporary Recorded Future API search limit. You can retry your search by pressing the "Retry Search" button.'
}
}
};
resultObject.data.details.general = {
maxRequestQueueLimitHit,
isConnectionReset,
isGatewayTimeout
};
lookupResults.push(resultObject);
} else if (err) {
errors.push(parseErrorToReadableJSON(err));
} else {
lookupResults.push(result);
}
if (lookupResults.length + errors.length + blockedEntities.length === entities.length) {
if (numConnectionResets > 0 || numThrottled > 0) {
Logger.warn(
{
numEntitiesLookedUp: entities.length,
numConnectionResets: numConnectionResets,
numLookupsThrottled: numThrottled
},
'Lookup Limit Error'
);
}
// we got all our results
if (errors.length > 0) {
callback(errors);
} else {
Logger.trace({ lookupResults }, 'Lookup Results');
callback(null, lookupResults);
}
}
});
} else {
blockedEntities.push({ entity, data: null });
}
});
// This can occur if there are no valid entities to lookup so we need a safe guard to make
// sure we still call the callback.
if (!hasAnyValidEntities) {
callback(null, blockedEntities);
}
}
const _lookupEntity = (entity, options, host, callback) => {
let requestOptions = {
qs: {
fields: ['risk', 'intelCard', 'sightings', 'analystnotes']
},
headers: {
'X-RFToken': options.apiKey,
'X-RF-User-Agent': 'polarity'
}
};
if (entity.isIP) {
requestOptions.url = host + '/v2/ip/' + entity.value;
requestOptions.qs.fields.push('location');
} else if (entity.isHash) {
requestOptions.url = host + '/v2/hash/' + entity.value;
} else if (entity.isDomain) {
requestOptions.url = host + '/v2/domain/' + encodeURIComponent(entity.value);
} else if (entity.isURL) {
requestOptions.url = host + '/v2/url/' + encodeURIComponent(entity.value);
} else if (entity.type === 'cve') {
requestOptions.url = host + '/v2/vulnerability/' + entity.value;
requestOptions.qs.fields.push('cvssv3');
} else {
callback({ detail: 'Unknown entity type received', err: new Error('unknown entity type') });
return;
}
requestOptions.qs.fields = requestOptions.qs.fields.join(',');
requestWithDefaults(requestOptions, 200, (err, data) => {
Logger.trace({ data, err }, 'Raw Request Result');
const entityNotFound = err && err.statusCode === 404;
const entityDoesNotHaveMinScore =
(data && data.data && data.data.risk && (data.data.risk.score || data.data.risk.score === 0)
? data.data.risk.score
: options.minimumScore) < options.minimumScore;
if (entityNotFound || entityDoesNotHaveMinScore) return callback(null, { entity, data: null });
if (err && [403, 401].includes(err.statusCode)) {
const baseErrorMessage =
(data && data.error && (data.error.message || data.error.reason)) ||
err.message ||
(err.statusCode === 401 &&
'Unable to authenticate with Recorded Future. The API Key is not valid. Please open your Recorded Future integration settings and change your API key.') ||
(err.statusCode === 403 &&
'You have reached your Recorded Future API Search Quota. Please check your Recorded Future account for details on your quota.') ||
'Unknown Cause';
const optionalStatusCode = err.statusCode || (data && data.status);
const optionalTraceId = data && data.traceId ? `(Trace ID: ${data.traceId})` : '';
Logger.warn(
{ errorMessage: baseErrorMessage, statusCode: optionalStatusCode, traceId: optionalTraceId },
'Search Returned Error '
);
return callback(null, {
entity,
isVolatile: true,
data: {
summary: [err.statusCode === 403 ? '! Search API Quota Exceeded' : '! Auth Failed: Invalid API Key'],
details: {
errorMessage: `${baseErrorMessage}${optionalTraceId}`,
allowRetry: err.statusCode !== 401
}
}
});
}
if (err) {
Logger.error(err, 'Request Error');
callback({
detail: 'Unexpected Error',
err,
data
});
return;
}
let risk = data.data.risk;
// limit the displayed analyst notes to 20
if(data && data.data && data.data.analystNotes.length > MAX_NOTES){
data.data.analystNotesMaxDisplayed = MAX_NOTES;
data.data.analystNotesTotalCount = data.data.analystNotes.length;
data.data.analystNotes = data.data.analystNotes.slice(0, MAX_NOTES);
}
callback(null, {
entity,
data: {
summary: [risk.criticalityLabel, `Risk Score: ${risk.score}`, `Rules: ${risk.riskString}`],
details: data.data
}
});
});
};
function startup (logger) {
Logger = logger;
let requestOptions = {};
if (typeof config.request.cert === 'string' && config.request.cert.length > 0) {
requestOptions.cert = fs.readFileSync(config.request.cert);
}
if (typeof config.request.key === 'string' && config.request.key.length > 0) {
requestOptions.key = fs.readFileSync(config.request.key);
}
if (typeof config.request.passphrase === 'string' && config.request.passphrase.length > 0) {
requestOptions.passphrase = config.request.passphrase;
}
if (typeof config.request.ca === 'string' && config.request.ca.length > 0) {
requestOptions.ca = fs.readFileSync(config.request.ca);
}
if (typeof config.request.proxy === 'string' && config.request.proxy.length > 0) {
requestOptions.proxy = config.request.proxy;
}
if (typeof config.request.rejectUnauthorized === 'boolean') {
requestOptions.rejectUnauthorized = config.request.rejectUnauthorized;
}
requestOptions.json = true;
requestWithDefaults = handleRequestError(request.defaults(requestOptions));
}
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
});
}
if (validateTrailingSlash(errors, options, optionName, 'Must remove trailing slash from input')) {
errors.push({
key: optionName,
message: 'Must remove trailing slash from input'
});
}
}
function validateOptions (options, callback) {
let errors = [];
validateStringOption(errors, options, 'apiKey', 'You must provide an API key.');
if (options.minimumScore.value < 0) {
errors = errors.concat({
key: 'minimumScore',
message: 'Minimum Score must be 0 or higher'
});
}
if (options.maxConcurrent.value < 1) {
errors = errors.concat({
key: 'maxConcurrent',
message: 'Max Concurrent Requests must be 1 or higher'
});
}
if (options.minTime.value < 1) {
errors = errors.concat({
key: 'minTime',
message: 'Minimum Time Between Lookups must be 1 or higher'
});
}
callback(null, errors);
}
const validateTrailingSlash = (errors, options, optionName, errMessage) => {
if (typeof options[optionName].value === 'string' && options[optionName].value.trim().endsWith('/')) {
errors.push({
key: optionName,
message: errMessage
});
}
};
function onMessage (payload, options, callback) {
switch (payload.action) {
case 'RETRY_LOOKUP':
doLookup([payload.entity], options, (err, lookupResults) => {
if (err) {
Logger.error({ err }, 'Error retrying lookup');
callback(err);
} else {
callback(
null,
lookupResults && lookupResults[0] && lookupResults[0].data === null
? { data: { summary: ['No Results Found on Retry'] } }
: lookupResults[0]
);
}
});
break;
}
}
module.exports = {
doLookup,
startup,
onMessage,
validateOptions
};