-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmetasploitJSClient.js
96 lines (80 loc) · 2.63 KB
/
metasploitJSClient.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
(function (module) {
var EventEmitter = require('events').EventEmitter;
var https = require('https');
var http = require('http');
var msgpack = require('msgpack5')(), encode = msgpack.encode , decode = msgpack.decode
// Constructor
var msapi = function (options,cb) {
this.init(options);
this.cb = cb;
}
msapi.prototype.rpc = function(args,cb) {
var data = encode(args);
var clength = Buffer.byteLength(data.toString('ascii'));
var options = {
hostname: this.host,
port: this.port,
rejectUnauthorized:false,
path: this.apiPath+this.apiVersion,
method: 'POST',
headers:{
'content-type':'binary/message-pack',
'content-length':clength
}
}
var req = https.request(options, function(res) {
var bufs = [];
var data = new Buffer(0);
res.on('data', function(chunk) {
bufs.push(chunk);
});
res.on('end',function() {
var res = decode(Buffer.concat(bufs));
if (res.error) return cb(res.error_message,res);
return cb(null,res);
});
});
req.write(data);
req.end();
req.on('error', function(e) {
e.error_message = e.message;
cb(e);
});
}
msapi.prototype.authLogin = function() {
this.rpc([
'auth.login',
this.login,
this.password
],this.onAuthLogin.bind(this));
}
msapi.prototype.onAuthLogin = function(err,r) {
if (!err && r && r.token) this.token = r.token;
this.emit('connected',err,r);
}
msapi.prototype.init = function(options) {
this.protocol = options.protocol || 'https';
this.host = options.host || 'localhost';
this.port = options.port || '55553';
this.apiVersion = options.apiVersion || '1.0';
this.apiPath = options.apiPath || '/api/';
this.login = options.login || null;
this.password = options.password || null;
this.eventEmitter = new EventEmitter();
msapi.prototype.on = this.eventEmitter.on;
msapi.prototype.emit = this.eventEmitter.emit;
this.authLogin();
}
msapi.prototype.exec = function(args,cb) {
var arr = [];
arr.push(args.shift());
arr.push(this.token);
if (args.length) {
args.forEach(function(arg) {
arr.push(arg);
})
}
this.rpc(arr,cb);
}
module.exports = msapi;
}(module))