forked from arkime/arkime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.reversedns.js
103 lines (96 loc) · 3.53 KB
/
source.reversedns.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
/******************************************************************************/
/*
*
* Copyright 2012-2016 AOL Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this Software except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var Dns = require('dns');
var iptrie = require('iptrie');
var wiseSource = require('./wiseSource.js');
var util = require('util');
var resolver = Dns;
// ----------------------------------------------------------------------------
function removeArray (arr, value) {
var pos = 0;
while ((pos = arr.indexOf(value, pos)) !== -1) {
arr.splice(pos, 1);
}
return arr;
}
// ----------------------------------------------------------------------------
function ReverseDNSSource (api, section) {
ReverseDNSSource.super_.call(this, api, section);
this.field = api.getConfig('reversedns', 'field');
this.ips = api.getConfig('reversedns', 'ips');
this.servers = api.getConfig('reversedns', 'servers');
if (this.servers !== undefined) {
resolver = new Dns();
resolver.setServers(this.servers.split(';'));
}
this.stripDomains = removeArray(api.getConfig('reversedns', 'stripDomains', '').split(';').map(item => item.trim()), '');
if (this.field === undefined) {
console.log(this.section, '- No field defined');
return;
}
if (this.ips === undefined) {
console.log(this.section, '- No ips defined');
return;
}
this.theField = this.api.addField(`field:${this.field}`);
this.trie = new iptrie.IPTrie();
this.ips.split(';').forEach((item) => {
if (item === '') {
return;
}
var parts = item.split('/');
this.trie.add(parts[0], +parts[1] || (parts[0].includes(':') ? 128 : 32), true);
});
this.api.addSource('reversedns', this);
}
util.inherits(ReverseDNSSource, wiseSource);
// ----------------------------------------------------------------------------
ReverseDNSSource.prototype.getIp = function (ip, cb) {
if (!this.trie.find(ip)) {
return cb(null, undefined);
}
resolver.reverse(ip, (err, domains) => {
// console.log("answer", ip, err, domains);
if (err || domains.length === 0) {
return cb(null, wiseSource.emptyResult);
}
var args = [];
for (var i = 0; i < domains.length; i++) {
var domain = domains[i];
if (this.stripDomains.length === 0) {
var parts = domain.split('.');
args.push(this.theField, parts[0].toLowerCase());
} else {
for (var j = 0; j < this.stripDomains.length; j++) {
var stripDomain = this.stripDomains[j];
if (domain.indexOf(stripDomain, domain.length - stripDomain.length) !== -1) {
args.push(this.theField, domain.slice(0, domain.length - stripDomain.length));
}
}
}
}
var wiseResult = { num: args.length / 2, buffer: wiseSource.encode.apply(null, args) };
cb(null, wiseResult);
});
};
// ----------------------------------------------------------------------------
exports.initSource = function (api) {
return new ReverseDNSSource(api, 'reversedns');
};
// ----------------------------------------------------------------------------