繁体   English   中英

Node.js:侦听器出现问题

[英]Node.js: trouble with listener

我正在用Node.js开发我的第一个应用程序,但我认为我还不能正确理解侦听器的工作原理,因为我编写的示例并未显示我认为它将显示的内容。

我想使用一个模块(msfnode),它是一个metasploit RPC客户端,因此它通过websockets连接。

我做了一个类,构造函数具有以下代码:

this.clientmsf = new MetasploitClient({
            login : options.login || 'myLogin',
            password : options.password || 'myPassword'
        });
        this.clientmsf.on('connected',function(err,token) {
            if (err) throw err; 
        });

因此,我认为可以在该类的其他函数中使用对象“ clientmsf”,并且显示错误:error_message:'无效的身份验证令牌'。 这是代码:

this.clientmsf.exec(['console.create'], function(err,r){
            consoleID = r.id;
            console.log(r);
        });

我想我有这个错误,因为我不知道node.js的所有概念,所以如果有人帮助我,我将非常感激。

非常感谢你。

PD。 这是msfnode库的示例:

var metasploitClient = require('metasploitJSClient');
var onConnect = function(err,token) {
    if (err) {
        console.log(err.error_message);
        process.exit(0);
    }
    metasploitVersion();
}
var metasploitVersion = function() {
    // Do not care about token, it will automaticaly
    // be added as the second arguments
    // The first value of the array is the function
    // you want to call. Full list is available
    // in metasploit remote api documentation
    var args = ['core.version'];
    client.exec(args,function(err,r) {
        if (err) return console.log('Error: '+err);
        console.log('-> Version: '+r);
    });
}
var client = new metasploitClient({
    login:'myLogin',
    password:'myPassword',
});
client.on('connected',onConnect);

错误:

{ error: true,
  error_class: 'Msf::RPC::Exception',
  error_string: 'Msf::RPC::Exception',
  error_backtrace: 
   [ 'lib/msf/core/rpc/v10/service.rb:148:in `process\'',
     'lib/msf/core/rpc/v10/service.rb:90:in `on_request_uri\'',
     'lib/msf/core/rpc/v10/service.rb:72:in `block in start\'',
     'lib/rex/proto/http/handler/proc.rb:38:in `call\'',
     'lib/rex/proto/http/handler/proc.rb:38:in `on_request\'',
     'lib/rex/proto/http/server.rb:363:in `dispatch_request\'',
     'lib/rex/proto/http/server.rb:297:in `on_client_data\'',
     'lib/rex/proto/http/server.rb:157:in `block in start\'',
     'lib/rex/io/stream_server.rb:48:in `call\'',
     'lib/rex/io/stream_server.rb:48:in `on_client_data\'',
     'lib/rex/io/stream_server.rb:192:in `block in monitor_clients\'',
     'lib/rex/io/stream_server.rb:190:in `each\'',
     'lib/rex/io/stream_server.rb:190:in `monitor_clients\'',
     'lib/rex/io/stream_server.rb:73:in `block in start\'',
     'lib/rex/thread_factory.rb:22:in `call\'',
     'lib/rex/thread_factory.rb:22:in `block in spawn\'',
     'lib/msf/core/thread_manager.rb:100:in `call\'',
     'lib/msf/core/thread_manager.rb:100:in `block in spawn\'' ],
  error_message: 'Invalid Authentication Token',
  error_code: 401 }

编辑2:

这是我检查过的代码:

clientmsf.on('connected',function(err,token) {
    if (err) throw err; 
    var consoleID;
    console.log('token:' + token);
    // should have connected by now
    clientmsf.exec(['console.list'], function(err,r){
        consoleID = r;
        console.log(r);
    });
    console.log (consoleID);
});

这就是它所显示的:

token:[object Object]
undefined
{ consoles: [ { id: '0', prompt: 'msf > ', busy: false } ] }

请注意,只有在连接并获得令牌后,它们才能在示例代码中完成工作(metasploitVersion)。

var onConnect = function(err,token) {
    if (err) {
        console.log(err.error_message);
        process.exit(0);
    }
    // in the connect callback here - you have a token
    // only then call to do the work
    metasploitVersion();
}

尝试将“ this.clientmsf.exec”代码移至connect回调函数中。 如果您在回调之外,它将在连接完成之前执行。

我还建议您注销该回调内的令牌,以确保已正确连接。

我建议类似的东西:

var clientmsf = new MetasploitClient({
    login : options.login || 'myLogin',
    password : options.password || 'myPassword'
});

clientmsf.on('connected',function(err,token) {
    if (err) throw err; 

    console.log('token:' + token);
    // should have connected by now
    clientmsf.exec(['console.create'], function(err,r, token){
        consoleID = r.id;
        console.log(r);
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM