msfrpcd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env ruby
  2. # -*- coding: binary -*-
  3. #
  4. # $Id$
  5. #
  6. # This user interface listens on a port and provides clients that connect to
  7. # it with an RPC interface to the Metasploit Framework.
  8. #
  9. # $Revision$
  10. #
  11. msfbase = __FILE__
  12. while File.symlink?(msfbase)
  13. msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
  14. end
  15. $:.unshift(File.expand_path(File.join(File.dirname(msfbase), 'lib')))
  16. require 'msfenv'
  17. $:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
  18. require 'rex/parser/arguments'
  19. # Declare the argument parser for msfrpcd
  20. arguments = Rex::Parser::Arguments.new(
  21. "-a" => [ true, "Bind to this IP address" ],
  22. "-p" => [ true, "Bind to this port instead of 55553" ],
  23. "-U" => [ true, "Specify the username to access msfrpcd" ],
  24. "-P" => [ true, "Specify the password to access msfrpcd" ],
  25. "-u" => [ true, "URI for Web server" ],
  26. "-S" => [ false, "Disable SSL on the RPC socket" ],
  27. "-f" => [ false, "Run the daemon in the foreground" ],
  28. "-n" => [ false, "Disable database" ],
  29. "-h" => [ false, "Help banner" ])
  30. opts = {
  31. 'RunInForeground' => true,
  32. 'SSL' => true,
  33. 'ServerHost' => '0.0.0.0',
  34. 'ServerPort' => 55553,
  35. 'ServerType' => 'Msg'
  36. }
  37. foreground = false
  38. frameworkOpts = {}
  39. # Parse command line arguments.
  40. arguments.parse(ARGV) { |opt, idx, val|
  41. case opt
  42. when "-a"
  43. opts['ServerHost'] = val
  44. when "-S"
  45. opts['SSL'] = false
  46. when "-p"
  47. opts['ServerPort'] = val
  48. when '-U'
  49. opts['User'] = val
  50. when '-P'
  51. opts['Pass'] = val
  52. when "-f"
  53. foreground = true
  54. when "-u"
  55. opts['URI'] = val
  56. when "-n"
  57. frameworkOpts['DisableDatabase'] = true
  58. when "-h"
  59. print("\nUsage: #{File.basename(__FILE__)} <options>\n" + arguments.usage)
  60. exit
  61. end
  62. }
  63. unless opts['Pass']
  64. $stderr.puts "[-] Error: a password must be specified (-P)"
  65. exit(0)
  66. end
  67. $0 = "msfrpcd"
  68. rpctype = 'MSG'
  69. $stderr.puts "[*] #{rpctype}RPC starting on #{opts['ServerHost']}:#{opts['ServerPort']} (#{opts['SSL'] ? "SSL" : "NO SSL"}):#{opts['ServerType']}..."
  70. $stderr.puts "[*] URI: #{opts['URI']}" if opts['URI']
  71. require 'msf/base'
  72. require 'msf/ui'
  73. # Fork into the background if requested
  74. begin
  75. if foreground
  76. $stdout.puts "[*] #{rpctype}RPC ready at #{Time.now}."
  77. else
  78. $stderr.puts "[*] #{rpctype}RPC backgrounding at #{Time.now}..."
  79. exit(0) if Process.fork()
  80. end
  81. rescue ::NotImplementedError
  82. $stderr.puts "[-] Background mode is not available on this platform"
  83. end
  84. # Create an instance of the framework
  85. $framework = Msf::Simple::Framework.create(frameworkOpts)
  86. # Run the plugin instance in the foreground.
  87. begin
  88. $framework.plugins.load("#{rpctype.downcase}rpc", opts).run
  89. rescue ::Interrupt
  90. $stderr.puts "[*] Shutting down"
  91. end