Module: Msf::Auxiliary::ManageEngineXnode::Interact

Included in:
Msf::Auxiliary::ManageEngineXnode, BasicChecks
Defined in:
lib/msf/core/auxiliary/manage_engine_xnode/interact.rb

Instance Method Summary collapse

Instance Method Details

#create_socket_for_xnode(rhost, rport) ⇒ Array

Create a socket to connect to an Xnode server and rescue any resulting errors

Parameters:

  • rhost (String)

    Target IP

  • rport (Integer)

    Target port

Returns:

  • (Array)

    Array containing of a response code (Integer) and either a Socket (when a connection is established) or an error message (String)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/msf/core/auxiliary/manage_engine_xnode/interact.rb', line 9

def create_socket_for_xnode(rhost, rport)
  vprint_status('Attempting to establish a connection with the remote server...')
  begin
    sock = Rex::Socket::Tcp.create(
      'PeerHost' => rhost,
      'PeerPort' => rport
    )
  rescue => e
    vprint_status("Encountered the following exception type: #{e.class}")
    return [1, e.message]
  end

  vprint_status('Successfully connected to the remote server')
  [0, sock]
end

#get_response(sock, action_hash, warning_message = nil, expected_response_key = nil) ⇒ Array

Calls send_to_sock and performs basic checks on the received response to ensure it is valid

Parameters:

  • sock (Socket)

    Socket to use for the request

  • action_hash (Hash)

    Hash containing an Xnode-compatible request

  • warning_message (String) (defaults to: nil)

    String to print via print_warning if the server response doesn't match the expected format

  • expected_response_key (String) (defaults to: nil)

    String that should be present as a key in the 'response' hash that is expected to be part of the JSON response

Returns:

  • (Array)

    Array containing a response code and a JSON-parsed Xnode server response hash if interaction with the server succeeded, Array containing a response code and nil otherwise



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/msf/core/auxiliary/manage_engine_xnode/interact.rb', line 67

def get_response(sock, action_hash, warning_message=nil, expected_response_key=nil)
  res = send_to_sock(sock, action_hash)
  return [1, nil] if res.nil?

  unless res.instance_of?(Hash) && res.keys.include?('response') && res['response'].instance_of?(Hash)
    if warning_message
      print_warning(warning_message)
    end
    return [1, res]
  end

  if expected_response_key
    unless res['response'].keys.include?(expected_response_key)
      if warning_message
        print_warning(warning_message)
      end
      return [1, res]
    end
  end

  [0, res]
end

#send_to_sock(sock, action_hash) ⇒ Hash?

Sends a request to an Xnode server

Parameters:

  • sock (Socket)

    Socket to use for the request

  • action_hash (Hash)

    Hash containing an Xnode-compatible request

Returns:

  • (Hash, nil)

    Hash containing a JSON-parsed Xnode server response if interaction with the server succeeded, nil otherwise



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/msf/core/auxiliary/manage_engine_xnode/interact.rb', line 30

def send_to_sock(sock, action_hash)
  unless action_hash.instance_of?(Hash)
    print_error('The provided Xnode action is not a valid Hash. The request will not be performed.')
    return nil
  end
  begin
    vprint_status("Sending request: #{action_hash}")

    sock.put(action_hash.to_json)
    # using sock.get for reading because the server doesn't send newlines so sock.read doesn't work
    # sock.recv won't work either since the message length can be (and often is) larger than the max of 65535
    r = sock.get
  rescue StandardError => e
    print_error("Encountered the following error while trying to interact with the Xnode server:")
    print_error(e.to_s)
    return nil
  end

  vprint_status("Received response: #{r}")

  # attempt to JSON parse the response
  begin
    return JSON.parse(r)
  rescue JSON::ParserError => e
    print_error("Encountered the following error while trying to JSON parse the response from the Xnode server:")
    print_error(e.to_s)
    return nil
  end
end