msfd 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 msfconsole instance. The nice thing about this interface is that
  8. # it allows multiple clients to share one framework instance and thus makes it
  9. # possible for sessions to to be shared from a single vantage point.
  10. #
  11. # $Revision$
  12. #
  13. msfbase = __FILE__
  14. while File.symlink?(msfbase)
  15. msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
  16. end
  17. $:.unshift(File.expand_path(File.join(File.dirname(msfbase), 'lib')))
  18. require 'msfenv'
  19. $:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
  20. require 'msf/base'
  21. require 'msf/ui'
  22. # Declare the argument parser for msfd
  23. arguments = Rex::Parser::Arguments.new(
  24. "-a" => [ true, "Bind to this IP address instead of loopback" ],
  25. "-p" => [ true, "Bind to this port instead of 55554" ],
  26. "-s" => [ false, "Use SSL" ],
  27. "-f" => [ false, "Run the daemon in the foreground" ],
  28. "-A" => [ true, "Specify list of hosts allowed to connect" ],
  29. "-D" => [ true, "Specify list of hosts not allowed to connect" ],
  30. "-h" => [ false, "Help banner" ])
  31. opts = { 'RunInForeground' => true }
  32. foreground = false
  33. # Parse command line arguments.
  34. arguments.parse(ARGV) { |opt, idx, val|
  35. case opt
  36. when "-a"
  37. opts['ServerHost'] = val
  38. when "-p"
  39. opts['ServerPort'] = val
  40. when "-f"
  41. foreground = true
  42. when "-s"
  43. opts['SSL'] = true
  44. when "-A"
  45. begin
  46. opts['HostsAllowed'] = val.split(',').map { |a|
  47. Rex::Socket.resolv_nbo(a)
  48. }
  49. rescue
  50. $stderr.puts "Bad argument for -A: #{$!}"
  51. exit
  52. end
  53. when "-D"
  54. begin
  55. opts['HostsDenied'] = val.split(',').map { |a|
  56. Rex::Socket.resolv_nbo(a)
  57. }
  58. rescue
  59. $stderr.puts "Bad argument for -D: #{$!}"
  60. exit
  61. end
  62. when "-h"
  63. print(
  64. "\nUsage: #{File.basename(__FILE__)} <options>\n" +
  65. arguments.usage)
  66. exit
  67. end
  68. }
  69. $stderr.puts "[*] Initializing msfd..."
  70. $stderr.puts "[*] Running msfd..."
  71. # Fork into the background if requested
  72. begin
  73. if (not foreground)
  74. exit(0) if Process.fork()
  75. end
  76. rescue ::NotImplementedError
  77. $stderr.puts "[-] Background mode is not available on this platform"
  78. end
  79. # Create an instance of the framework
  80. $framework = Msf::Simple::Framework.create
  81. # Run the plugin instance in the foreground.
  82. $framework.plugins.load('msfd', opts).run(opts)