Class: Metasploit::Aggregator::Http::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/metasploit/aggregator/http/responder.rb

Overview

a Responder acts a a gateway to convert data from a port to into a Request object used in the aggregator. It also reverses this process as a gateway for sending Request object back as responses to the original Request.

Direct Known Subclasses

SslResponder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Responder

Returns a new instance of Responder.



17
18
19
20
21
22
23
24
25
# File 'lib/metasploit/aggregator/http/responder.rb', line 17

def initialize(uri)
  @uri = uri
  @queue = Queue.new
  @thread = Thread.new { process_requests }
  @time = Time.now
  @router = Router.instance
  @session_service = SessionDetailService.instance
  @pending_request = nil
end

Instance Attribute Details

#log_messagesObject

Returns the value of attribute log_messages.



14
15
16
# File 'lib/metasploit/aggregator/http/responder.rb', line 14

def log_messages
  @log_messages
end

#queueObject

Returns the value of attribute queue.



12
13
14
# File 'lib/metasploit/aggregator/http/responder.rb', line 12

def queue
  @queue
end

#timeObject

Returns the value of attribute time.



13
14
15
# File 'lib/metasploit/aggregator/http/responder.rb', line 13

def time
  @time
end

#uriObject (readonly)

Returns the value of attribute uri.



15
16
17
# File 'lib/metasploit/aggregator/http/responder.rb', line 15

def uri
  @uri
end

Class Method Details

.get_data(connection, guaranteed_length) ⇒ Object



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
117
118
# File 'lib/metasploit/aggregator/http/responder.rb', line 91

def self.get_data(connection, guaranteed_length)
  checked_first = has_length = guaranteed_length
  content_length = 0
  request_lines = []

  while (input = connection.gets)
    request_lines << input
    # break for body read
    break if (input.inspect.gsub /^"|"$/, '').eql? '\r\n'

    if !checked_first && !has_length
      has_length = input.include?('POST')
      checked_first = true
    end

    if has_length && input.include?('Content-Length')
      content_length = input[(input.index(':') + 1)..input.length].to_i
    end

  end
  body = ''
  if has_length
    while body.length < content_length
      body += connection.read(content_length - body.length)
    end
  end
  Request.new request_lines, body, connection
end

Instance Method Details

#close_connection(connection) ⇒ Object



124
125
126
# File 'lib/metasploit/aggregator/http/responder.rb', line 124

def close_connection(connection)
  connection.close
end

#get_connection(host, port) ⇒ Object



120
121
122
# File 'lib/metasploit/aggregator/http/responder.rb', line 120

def get_connection(host, port)
  TCPSocket.new host, port
end

#process_requestsObject



27
28
29
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/metasploit/aggregator/http/responder.rb', line 27

def process_requests

  while true do
    begin
      request_task = @queue.pop
      connection = request_task.socket
      request_task.headers

      send, recv = @router.get_forward(@uri)
      if send.nil?
        # when no forward found park the connection for now
        # in the future this may get smarter and return a 404 or something
        send_parked_response(connection)
        next
      end

      # response from get_forward will be a queue to push messages onto and a response queue to retrieve result from
      @session_service.add_request(request_task, @uri)
      send << request_task
      @pending_request = connection

      log 'queued to console'

      # now get the response once available and send back using this connection
      begin
        request_obj = recv.pop
        @session_service.add_request(request_task, @uri)
        @pending_request = nil
        request_obj.headers.each do |line|
          connection.write line
        end
        unless request_obj.body.nil?
          connection.write request_obj.body
        end
        connection.flush
        log 'message delivered from console'
      rescue
        log $!
      end
      close_connection(connection)
    rescue Exception => e
      log "an error occurred processing request from #{@uri}"
    end
  end

end

#stop_processingObject



74
75
76
77
78
79
80
# File 'lib/metasploit/aggregator/http/responder.rb', line 74

def stop_processing
  @thread.exit
  if @pending_request
    send_parked_response(@pending_request)
    close_connection(@pending_request)
  end
end