Class: Msf::Aggregator::Http::Responder

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

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.



13
14
15
16
17
18
19
# File 'lib/msf/aggregator/http/responder.rb', line 13

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

Instance Attribute Details

#log_messagesObject

Returns the value of attribute log_messages.



10
11
12
# File 'lib/msf/aggregator/http/responder.rb', line 10

def log_messages
  @log_messages
end

#queueObject

Returns the value of attribute queue.



8
9
10
# File 'lib/msf/aggregator/http/responder.rb', line 8

def queue
  @queue
end

#timeObject

Returns the value of attribute time.



9
10
11
# File 'lib/msf/aggregator/http/responder.rb', line 9

def time
  @time
end

#uriObject (readonly)

Returns the value of attribute uri.



11
12
13
# File 'lib/msf/aggregator/http/responder.rb', line 11

def uri
  @uri
end

Class Method Details

.get_data(connection, guaranteed_length) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/msf/aggregator/http/responder.rb', line 105

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



138
139
140
# File 'lib/msf/aggregator/http/responder.rb', line 138

def close_connection(connection)
  connection.close
end

#get_connection(host, port) ⇒ Object



134
135
136
# File 'lib/msf/aggregator/http/responder.rb', line 134

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

#process_requestsObject



21
22
23
24
25
26
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
73
74
75
76
77
78
79
80
81
82
# File 'lib/msf/aggregator/http/responder.rb', line 21

def process_requests

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

      # peer_addr = connection.io.peeraddr[3]

      host, port = @router.get_forward(@uri)
      if host.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

      client = nil

      begin
        client = get_connection(host, port)
      rescue StandardError => e
        log 'error on console connect ' + e.to_s
        send_parked_response(connection)
        next
      end

      log 'connected to console'

      request_task.headers.each do |line|
        client.write line
      end
      unless request_task.body.nil?
        client.write request_task.body
      end
      client.flush
      # log "From victim: \n" + request_lines.join()

      begin
        response = ''
        request_obj = Responder.get_data(client, true)
        request_obj.headers.each do |line|
          connection.write line
          response += line
        end
        unless request_obj.body.nil?
          connection.write request_obj.body
        end
        connection.flush
          # log "From console: \n" + response
      rescue
        log $!
      end
      close_connection(client)
      close_connection(connection)
    rescue Exception => e
      log "an error occurred processing request from #{@uri}"
    end
  end

end

#stop_processingObject



84
85
86
# File 'lib/msf/aggregator/http/responder.rb', line 84

def stop_processing
  @thread.exit
end