forked from arkime/arkime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.wiseproxy.js
223 lines (202 loc) · 6.62 KB
/
source.wiseproxy.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
/******************************************************************************/
/*
*
* 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 request = require('request');
var wiseSource = require('./wiseSource.js');
var util = require('util');
// ----------------------------------------------------------------------------
function WiseProxySource (api, section) {
WiseProxySource.super_.call(this, api, section);
this.url = api.getConfig(section, 'url');
this.types = api.getConfig(section, 'types');
this.mapping = [];
this.buffer = Buffer.alloc(10000);
this.offset = 0;
this.bufferInfo = [];
if (this.url === undefined) {
console.log(this.section, '- ERROR not loading since no url specified in config file');
return;
}
if (!this.types) {
console.log(this.section, '- ERROR not loading since no types specified in config file');
return;
}
var types = this.types.split(',').map(item => item.trim());
for (var i = 0; i < types.length; i++) {
switch (types[i]) {
case 'domain':
this.getDomain = getDomain;
break;
case 'md5':
this.getMd5 = getMd5;
break;
case 'ip':
this.getIp = getIp;
break;
case 'email':
this.getEmail = getEmail;
break;
case 'url':
this.getURL = getURL;
break;
}
}
this.updateInfo();
setTimeout(this.updateInfo.bind(this), 5 * 60 * 1000);
this.api.addSource(this.section, this);
setInterval(this.performQuery.bind(this), 500);
}
util.inherits(WiseProxySource, wiseSource);
// ----------------------------------------------------------------------------
WiseProxySource.prototype.performQuery = function () {
if (this.bufferInfo.length === 0) {
return;
}
var options = {
url: this.url + '/get',
method: 'POST',
body: this.buffer.slice(0, this.offset)
};
var bufferInfo = this.bufferInfo;
this.bufferInfo = [];
this.offset = 0;
var i;
request(options, (err, response, body) => {
if (err || response.statusCode !== 200) {
console.log(this.section, 'error', this.section, err || response);
for (i = 0; i < bufferInfo.length; i++) {
bufferInfo[i].cb('Error');
}
return;
}
body = Buffer.from(body, 'binary');
var offset = 0;
var fieldsTS = body.readUInt32BE(offset); offset += 4;
if (fieldsTS !== this.fieldsTS) {
this.updateInfo();
}
// var ver = body.readUInt32BE(offset); offset += 4;
for (i = 0; i < bufferInfo.length; i++) {
var num = body[offset]; offset += 1;
var bi = bufferInfo[i];
if (num === 0) {
return bi.cb(null, wiseSource.emptyResult);
}
var args = [];
for (var n = 0; n < num; n++) {
var field = body[offset]; offset += 1;
var len = body[offset]; offset += 1;
var str = body.toString('ascii', offset, offset + len - 1); offset += len;
args.push(this.mapping[field], str);
}
var result = { num: args.length / 2, buffer: wiseSource.encode.apply(null, args) };
return bi.cb(null, result);
}
});
};
// ----------------------------------------------------------------------------
WiseProxySource.prototype.fetch = function (type, item, cb) {
this.buffer[this.offset] = type; this.offset++;
this.buffer.writeUInt16BE(item.length, this.offset); this.offset += 2;
this.buffer.write(item, this.offset); this.offset += item.length;
this.bufferInfo.push({ type: type, item: item, cb: cb });
if (this.bufferInfo.length > 100) {
this.performQuery();
}
};
// ----------------------------------------------------------------------------
WiseProxySource.prototype.updateInfo = function () {
var options = {
url: this.url + '/fields',
method: 'GET'
};
request(options, (err, response, body) => {
if (err) {
console.log(this.section, 'problem fetching /fields', this.section, err || response);
return;
}
var buf = Buffer.from(body, 'binary');
var offset = 0;
this.fieldsTS = buf.readUInt32BE(offset); offset += 4;
// var version = buf.readUInt32BE(offset); offset += 4;
var length = buf[offset]; offset += 1;
for (var i = 0; i < length; i++) {
offset++;
var len = buf[offset]; offset += 1;
var str = buf.toString('ascii', offset, offset + len);
offset += len;
this.mapping[i] = this.api.addField(str);
}
});
options = {
url: this.url + '/views',
method: 'GET',
json: true
};
request(options, (err, response, body) => {
if (err) {
console.log(this.section, 'problem fetching /views', this.section, err || response);
return;
}
for (var name in body) {
this.api.addView(name, body[name]);
}
});
options = {
url: this.url + '/rightClicks',
method: 'GET',
json: true
};
request(options, (err, response, body) => {
if (err) {
console.log(this.section, 'problem fetching /rightClicks', this.section, err || response);
return;
}
for (var name in body) {
this.api.addView(name, body[name]);
}
});
};
// ----------------------------------------------------------------------------
function getIp (item, cb) {
this.fetch(0, item, cb);
}
// ----------------------------------------------------------------------------
function getDomain (item, cb) {
this.fetch(1, item, cb);
}
// ----------------------------------------------------------------------------
function getMd5 (item, cb) {
this.fetch(2, item, cb);
}
// ----------------------------------------------------------------------------
function getEmail (item, cb) {
this.fetch(3, item, cb);
}
// ----------------------------------------------------------------------------
function getURL (item, cb) {
this.fetch(4, item, cb);
}
// ----------------------------------------------------------------------------
exports.initSource = function (api) {
var sections = api.getConfigSections().filter((e) => { return e.match(/^wiseproxy:/); });
sections.forEach((section) => {
return new WiseProxySource(api, section);
});
};
// ----------------------------------------------------------------------------