2014-03-02 43 views
2

我在Node.js中开发我的第一个应用程序,我认为我听不懂如何工作,因为我写的示例并未显示我认为它是什么去演出。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 } ] } 
+0

这是您收到的通常节点让你在错误发生的行号整个错误消息?你真的在机器上启动了msfrpcd吗? –

+0

不,错误来自Metasploit API。我认为麻烦可能是一种“竞争条件”,因为在连接之前执行“exec”并且对象还不知道该令牌。我要添加完整的错误。 感谢Vasil您的兴趣。 –

回答

1

请注意,在示例代码中,只有在连接并获取令牌后,他们才能完成工作(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'代码。如果您在回调之外拥有它,它将在连接完成之前执行。

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

我的建议是这样的:

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); 
    }); 
}); 
+0

是的,我这样做了,但后来我有一个问题,如果我想多次执行“exec”,我该怎么做?谢谢bryanmac。 –

+0

错误: TypeError:无法调用未定义的 方法'exec'为null。 (/home/dani/proyectos_eclipse/tfg-pentesting/lib/msfManager.js:71:25) at EventEmitter.emit(events.js:98:17) at msapi。在IncomingMessage上的onAuthLogin(/home/dani/proyectos_eclipse/tfg-pentesting/node_modules/msfnode/metasploitJSClient.js:65:14) 。 (/home/dani/proyectos_eclipse/tfg-pentesting/node_modules/msfnode/metasploitJSClient.js:41:24) at IncomingMessage.EventEmitter.emit(events.js:117:20) at _stream_readable.js:920:16 at process._tickCallback(node.js:415:13) –

+0

好的 - 你在下一期。在JS中,'this'就是它的功能。所以,你需要声明clientmsf为var。更新了代码片段。 (记得我看不到你的行号) – bryanmac