Class: Msf::RPC::JSON::RpcCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/core/rpc/json/rpc_command.rb

Direct Known Subclasses

V1_0::RpcCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(framework, execute_timeout: 7200) ⇒ RpcCommand

Instantiate an RpcCommand.

Parameters:

  • framework (Msf::Simple::Framework)

    Framework wrapper instance

  • execute_timeout (Integer) (defaults to: 7200)

    execute timeout duration in seconds



9
10
11
12
13
# File 'lib/msf/core/rpc/json/rpc_command.rb', line 9

def initialize(framework, execute_timeout: 7200)
  @framework = framework
  @execute_timeout = execute_timeout
  @methods = {}
end

Instance Attribute Details

#execute_timeoutObject

Returns the value of attribute execute_timeout.



4
5
6
# File 'lib/msf/core/rpc/json/rpc_command.rb', line 4

def execute_timeout
  @execute_timeout
end

#frameworkObject (readonly)

Returns the value of attribute framework.



3
4
5
# File 'lib/msf/core/rpc/json/rpc_command.rb', line 3

def framework
  @framework
end

Instance Method Details

#execute(method, params) ⇒ Object

Invokes the method on the receiver object with the specified params, returning the method’s return value.

Parameters:

  • method (String)

    the RPC method name

  • params (Array, Hash)

    parameters for the RPC call

Returns:

  • (Object)

    the method’s return value.

Raises:

  • (MethodNotFound)

    The method does not exist

  • (Timeout::Error)

    The method failed to terminate in @execute_timeout seconds



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/msf/core/rpc/json/rpc_command.rb', line 37

def execute(method, params)
  unless @methods.key?(method)
    raise MethodNotFound.new(method)
  end

  ::Timeout.timeout(@execute_timeout) do
    if params.nil?
      return @methods[method].call()
    elsif params.is_a?(Array)
      return @methods[method].call(*params)
    else
      return @methods[method].call(**params)
    end
  end
end

#register_method(method, name: nil) ⇒ Method

Add a method to the RPC Command

Parameters:

  • method (Method)

    the Method

  • name (String) (defaults to: nil)

    the name the method is register under. The method name is used if nil.

Returns:

  • (Method)

    the Method.



19
20
21
22
23
24
25
26
27
28
# File 'lib/msf/core/rpc/json/rpc_command.rb', line 19

def register_method(method, name: nil)
  if name.nil?
    if method.is_a?(Method)
      name = method.name.to_s
    else
      name = method.to_s
    end
  end
  @methods[name] = method
end