-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
332 lines (284 loc) · 9.77 KB
/
index.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
'use strict';
const request = require('request');
const fs = require('fs');
/**
* Create a new ATD connection object
* @param {string} host
* @param {string} username
* @param {string} password
* @return {Object} atd
*/
module.exports = function(host, ssl, username, password) {
const baseUrl = 'https://' + host + '/php/';
const _header = {
'Accept': 'application/vnd.ve.v1.0+json',
'VE-API-Version': '1.5.0',
'user-agent': 'Javascript ATD Client',
'Content-Type': 'application/json'
};
const userpass = new Buffer(username + ':' + password);
const userpass_enc = userpass.toString('base64');
/**
* parseResponse
*
* @param {*} error
* @param {*} response
* @param {*} body
* @param {*} callback (err)
* @returns parsed response, or null if error and callback called
*/
function parseResponse(error, response, body, callback) {
//console.log('parse** ', error, response, body);
if (error) {
callback(error);
return null;
}
var info = null;
try {
info = JSON.parse(body);
} catch (e) {
// NOP
}
if (response.statusCode != 200) {
callback(new Error(info && info.errorMessage ?
response.statusCode + ":" + info.errorMessage :
'HTTP Code: ' + response.statusCode));
return null;
}
if (!info.success) {
callback(new Error(info.errorMessage || 'Unknown Error'));
return null;
}
return info;
}
/**
* try and "log in"
*
* @param {*} callback (err, results from atd)
*/
function createSession(callback) {
// set the header with Auth
const headers = Object.assign({}, _header, {
'VE-SDK-API': userpass_enc
});
// get a session ID
const url = baseUrl + "session.php";
console.log('connect', url, headers);
request({
method: 'GET',
uri: url,
headers: headers,
timeout: 2000,
rejectUnauthorized: false // todo: make this default true and override
}, function(error, response, body) {
const info = parseResponse(error, response, body, callback);
if (!info) { return; }
console.log(info);
callback(null, info.results);
});
}
var atd = {
host: host
};
/**
* try and "log in"
*
* @param {*} callback (err)
*/
atd.connect = function(callback) {
createSession(function(err, results) {
atd.session = results && results.session;
atd.userId = results && results.userId;
atd.isAdmin = results && results.isAdmin;
atd.serverTZ = results && results.serverTZ;
atd.apiVersion = results && results.apiVersion;
atd.matdVersion = results && results.matdVersion;
const auth = atd.session && atd.userId && new Buffer(atd.session + ':' + atd.userId);
atd.auth = auth && auth.toString('base64');
callback(err);
});
}
/**
* close any open session (log out)
*
* @param {*} callback (err)
*/
atd.close = function(callback) {
if (!atd.session) {
return callback(null, true);
}
const headers = Object.assign({}, _header, {
'VE-SDK-API': userpass_enc
});
const url = baseUrl + '/session.php'
request({
method: 'DELETE',
uri: url,
headers: headers,
timeout: 2000,
rejectUnauthorized: false // todo: make this default true and override
}, function(error, response, body) {
const info = parseResponse(error, response, body, callback);
if (!info) { return }
console.log(info);
atd.auth = null;
atd.session = null;
atd.userId = null;
atd.isAdmin = null;
atd.serverTZ = null;
atd.apiVersion = null;
atd.matdVersion = null;
callback(null, info.results);
});
}
/**
* Uploads file to ATD for analysis.
* filename - absolute or relative path to the file being analyzed,
* srcip - string representing source IP address for reporting purposes.
* reanalyze - boolean, whether to forcibly reanalyze previously submitted sample.
* callback(error, jobId) - for completion, returns analysis job id
* stream - IF SUPPLIED, then this is the stream for file content.
*
*/
atd.upload = function(filename, srcip, reanalyze, callback, stream, suppressAutoconnect) {
// ensure connected (session)
if (!atd.session || !atd.auth) {
if (suppressAutoconnect) {
return callback(new Error('Invalid Session'));
}
atd.connect(function(err) {
return atd.upload(filename, srcip, reanalyze, callback, stream, true);
});
}
// url = baseUrl + 'atdHashLookup.php';
const uploadUrl = baseUrl + 'fileupload.php';
const headers = Object.assign({}, _header, {
'VE-SDK-API': atd.auth,
"Content-Type": "multipart/form-data"
});
const data = {
submitType: 0, // regular file upload
srcIp: srcip,
// destIp: '1.2.3.4',
analyzeAgain: reanalyze === true ? '1' : '0'
};
// pipe the supplied stream or fall back to the file from the local file system
const formData = {
amas_filename: stream || fs.createReadStream(filename),
data: JSON.stringify({ data: data})
};
var options = {
method: 'POST',
uri: uploadUrl,
headers: headers,
timeout: 2000,
formData: formData,
//data: JSON.stringify(data),
rejectUnauthorized: false // todo: make this default true and override
}
console.log('submit to ATD', options);
request(options, function(error, response, body) {
// todo: auto reconnect if session is no longer valid?
const info = parseResponse(error, response, body, callback);
if (!info) { return }
console.log(info);
const fileData = info.results && info.results[0];
const meta = {
success: info.success,
jobId: info.subId,
md5: fileData && fileData.md5,
sha1: fileData && fileData.sha1,
sha256: fileData && fileData.sha256
}
return callback(error, meta);
});
}
/**
* ATD report types.
*/
atd.REPORT = {
HTML: 'html',
TXT: 'txt',
XML: 'xml',
ZIP: 'zip',
JSON: 'json',
IOC: 'ioc',
STIX: 'stix',
PDF: 'pdf',
SAMPLE: 'sample'
};
/**
* ATD report lookup options.
*/
const lookupKeys = {
JOBID: 'jobId',
TASKID: 'iTaskId',
MD5: 'md5'
// todo: any others?
};
atd.LOOKUP = lookupKeys;
/**
* Uploads file to ATD for analysis.
* filename - absolute or relative path to the file being analyzed,
* srcip - string representing source IP address for reporting purposes.
* reanalyze - boolean, whether to forcibly reanalyze previously submitted sample.
* callback(error, jobId) - for completion, returns analysis job id
* stream - IF SUPPLIED, then this is the stream for file content.
*
*/
atd.getReport = function(lookupOption, lookupValue, type, callback, suppressAutoconnect) {
// ensure connected (session)
if (!atd.session || !atd.auth) {
if (suppressAutoconnect) {
return callback(new Error('Invalid Session'));
}
atd.connect(function(err) {
return atd.getReport(lookupOption, lookupValue, type, callback, true);
});
}
const validOptions = Object.values(lookupKeys);
if (validOptions.indexOf(lookupOption) < 0) {
return callback(new Error('Invalid Report Lookup Option: ' + Object.keys(lookupKeys).join(', ')));
}
const reportUrl = baseUrl + 'showreport.php';
const headers = Object.assign({}, _header, {
'VE-SDK-API': atd.auth
// should be "Expert"? : "Content-Type": "multipart/form-data"
});
delete headers['Content-Type'];
var options = {
method: 'GET',
uri: reportUrl,
headers: headers,
timeout: 2000,
qs: {
iType: type,
[lookupOption]: lookupValue
},
rejectUnauthorized: false // todo: make this default true and override
}
console.log('show report', options);
request(options, function(error, response, body) {
// todo: auto reconnect if session is no longer valid?
console.log(error, response, body);
if (error) {
return callback(error);
}
var info = null;
if (type === atd.REPORT.JSON) {
try {
info = JSON.parse(body);
} catch (e) {
// NOP
}
}
if (response.statusCode != 200) {
return callback(new Error(info && info.errorMessage ?
response.statusCode + ":" + info.errorMessage :
'HTTP Code: ' + response.statusCode));
}
return callback(error, !error && body);
});
}
return atd;
};