Class: Msf::RPC::Simple::Client

Inherits:
Object
  • Object
show all
Includes:
Features::Framework, Features::Pro
Defined in:
lib/msfrpc-simple/client.rb

Instance Method Summary collapse

Methods included from Features::Pro

#start_bruteforce, #start_discover, #start_report

Methods included from Features::Framework

#bruteforce_range, #discover_range, #execute_module, #exploit_single, #nmap_range

Constructor Details

#initialize(user_options) ⇒ Client

Public: Create a simple client object.

user_options - hash of options to include in our initial connection. project - project name we want to use for this connection.

Returns nothing.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/msfrpc-simple/client.rb', line 23

def initialize(user_options)

  # configure default options
  @options = {
    :project => user_options[:project] || "default",
    :port => user_options[:project] || 55553,
    :user => user_options[:rpc_user],
    :pass => user_options[:rpc_pass],
    :db_host => user_options[:db_host] || "localhost",
    :db_user => user_options[:db_user],
    :db_pass => user_options[:db_pass],
    :db_name => user_options[:db_name] || "msf"
  }

  @options.merge!(user_options)

  #
  # Connect to the RPC daemon using the default client
  #
  @client = Msf::RPC::Client.new(@options)

  # connect to the database 
  _connect_database
end

Instance Method Details

#cleanupObject

Public: clean up after ourselves

Returns nothing



51
52
53
54
55
# File 'lib/msfrpc-simple/client.rb', line 51

def cleanup
  _send_command("hosts -d")
  _send_command("services -d")
  _send_command("creds -d")
end

#create_reportObject

Public: Creates and retuns an xml report

This method is ugly for a number of reasons, but there doesn’t appear to be a way to be notified when the command is completed

returns a valid xml string



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/msfrpc-simple/client.rb', line 86

def create_report
  report_path = "/tmp/metasploit_#{@workspace_name}.xml"

  # Create the report using the db_export command
  _send_command("db_export #{report_path}\n")

  # We've sent the command, so let's sit back and wait for th
  # output to hit the disk.
  begin
    xml_string = ""
    status = Timeout::timeout(240) {
      # We don't know when the file is going to show up, so
      # wait for it...
      until File.exists? report_path do
      sleep 1
      end

    # Read and clean up the file when it exists...
    until xml_string.include? "</MetasploitV4>" do
      sleep 5
      xml_string = File.read(report_path)
    end

    File.delete(report_path)
    }
  rescue Timeout::Error
    xml_string = "<MetasploitV4></MetasploitV4>"
  end

  xml_string
end

#db_connected?Boolean

Public: determine if we’re connected to the RPC server

returns true/false

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/msfrpc-simple/client.rb', line 75

def db_connected?
  return false unless _send_command("db_status") =~ /connected/
true
end

#list_threadsObject

Public: list all running threads

Returns a hash of running threads



60
61
62
# File 'lib/msfrpc-simple/client.rb', line 60

def list_threads
  @client.call("core.thread_list")
end

#rpc_connected?Boolean

Public: determine if we’re connected to the RPC server

returns true/false

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/msfrpc-simple/client.rb', line 67

def rpc_connected?
  return false unless @client.call("core.version")
true
end