msfdb_ws 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env ruby
  2. # -*- coding: binary -*-
  3. #
  4. # Starts the HTTP DB Service interface
  5. # TODO: This functionality exists within the top level msfdb.rb, and should be merged.
  6. # Note that this file is currently called by RSpec when REMOTE_DB is set
  7. require 'optparse'
  8. class HelpError < StandardError; end
  9. class SwitchError < StandardError
  10. def initialize(msg="Missing required switch.")
  11. super(msg)
  12. end
  13. end
  14. def require_deps
  15. require 'pathname'
  16. require Pathname.new(__FILE__).realpath.expand_path.parent.parent.parent.join('config', 'boot')
  17. require 'msfenv'
  18. # require 'msf/core/web_services/http_db_manager_service'
  19. end
  20. def parse_args(args)
  21. opts = {}
  22. opt = OptionParser.new
  23. banner = "msfdb_ws - Metasploit data store as a web service.\n"
  24. banner << "Usage: #{$0} [options] <var=val>"
  25. opt.banner = banner
  26. opt.separator('')
  27. opt.separator('Options:')
  28. # Defaults:
  29. opts[:interface] = '0.0.0.0'
  30. opts[:port] = 8080
  31. opts[:ssl] = false
  32. opts[:ssl_cert] = nil
  33. opts[:ssl_key] = nil
  34. opt.on('-i', '--interface <interface>', String, 'Interface to listen on') do |p|
  35. opts[:interface] = p
  36. end
  37. opt.on('-p', '--port <port number>', Integer, 'Port to listen on') do |p|
  38. opts[:port] = p
  39. end
  40. opt.on('-s', '--ssl', 'Enable SSL on the server') do |p|
  41. opts[:ssl] = true
  42. end
  43. opt.on('-c', '--cert <path/to/cert.pem>', String, 'Path to SSL Certificate file') do |p|
  44. opts[:ssl_cert] = p
  45. end
  46. opt.on('-k', '--key <path/to/key.pem>', String, 'Path to SSL Key file') do |p|
  47. opts[:ssl_key] = p
  48. end
  49. opt.on_tail('-h', '--help', 'Show this message') do
  50. raise HelpError, "#{opt}"
  51. end
  52. begin
  53. opt.parse!(args)
  54. rescue OptionParser::InvalidOption => e
  55. raise UsageError, "Invalid option\n#{opt}"
  56. rescue OptionParser::MissingArgument => e
  57. raise UsageError, "Missing required argument for option\n#{opt}"
  58. end
  59. opts
  60. end
  61. begin
  62. opts = parse_args(ARGV)
  63. raise SwitchError.new("certificate file must be specified when using -s") if opts[:ssl] && (opts[:ssl_cert].nil?)
  64. require_deps
  65. Msf::WebServices::HttpDBManagerService.new.start(:Port => opts[:port],
  66. :Host => opts[:interface],
  67. :ssl => opts[:ssl],
  68. :ssl_cert => opts[:ssl_cert],
  69. :ssl_key => opts[:ssl_key])
  70. rescue HelpError => e
  71. $stderr.puts e.message
  72. end