Class: Watobo::HTTPSocket::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/watobo/http_socket/connection.rb

Constant Summary

Constants included from Constants

Constants::AC_GROUP_APACHE, Constants::AC_GROUP_DOMINO, Constants::AC_GROUP_ENUMERATION, Constants::AC_GROUP_FILE_INCLUSION, Constants::AC_GROUP_FLASH, Constants::AC_GROUP_GENERIC, Constants::AC_GROUP_JBOSS, Constants::AC_GROUP_JOOMLA, Constants::AC_GROUP_SAP, Constants::AC_GROUP_SQL, Constants::AC_GROUP_TYPO3, Constants::AC_GROUP_XSS, Constants::AUTH_TYPE_BASIC, Constants::AUTH_TYPE_DIGEST, Constants::AUTH_TYPE_NONE, Constants::AUTH_TYPE_NTLM, Constants::CHAT_SOURCE_AUTO_SCAN, Constants::CHAT_SOURCE_FUZZER, Constants::CHAT_SOURCE_INTERCEPT, Constants::CHAT_SOURCE_MANUAL, Constants::CHAT_SOURCE_MANUAL_SCAN, Constants::CHAT_SOURCE_PROXY, Constants::CHAT_SOURCE_UNDEF, Constants::DEFAULT_PORT_HTTP, Constants::DEFAULT_PORT_HTTPS, Constants::FINDING_TYPE_HINT, Constants::FINDING_TYPE_INFO, Constants::FINDING_TYPE_UNDEFINED, Constants::FINDING_TYPE_VULN, Constants::FIRST_TIME_FILE, Constants::GUI_REGULAR_FONT_SIZE, Constants::GUI_SMALL_FONT_SIZE, Constants::ICON_PATH, Constants::LOG_DEBUG, Constants::LOG_INFO, Constants::SCAN_CANCELED, Constants::SCAN_FINISHED, Constants::SCAN_PAUSED, Constants::SCAN_STARTED, Constants::TE_CHUNKED, Constants::TE_COMPRESS, Constants::TE_DEFLATE, Constants::TE_GZIP, Constants::TE_IDENTITY, Constants::TE_NONE, Constants::VULN_RATING_CRITICAL, Constants::VULN_RATING_HIGH, Constants::VULN_RATING_INFO, Constants::VULN_RATING_LOW, Constants::VULN_RATING_MEDIUM, Constants::VULN_RATING_UNDEFINED

Instance Method Summary collapse

Constructor Details

#initialize(request, prefs) ⇒ Connection

Returns a new instance of Connection.


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/watobo/http_socket/connection.rb', line 32

def initialize(request, prefs)
  @request = request
  @response = nil
  
  @proxy = Watobo::ForwardingProxy.get(site)

    unless @proxy.nil?
      host = @proxy.host
      port = @proxy.port
    else
      host = @request.host
      port = @request.port
    end
    # check if hostname is valid and can be resolved
    #hostip = IPSocket.getaddress(host)
  
end

Instance Method Details

#doProxyAuthObject

doProxyAuth


408
409
410
411
412
413
414
415
416
417
418
# File 'lib/watobo/http_socket/connection.rb', line 408

def  doProxyAuth()
 # puts "DO PROXY AUTH"
 # puts proxy.to_yaml
  response_headers = nil
  case @proxy.auth_type
  when AUTH_TYPE_NTLM
    return proxyAuthNTLM()

  end # END OF NTLM

end

#proxyAuthNTLMObject

proxyAuthNTLM returns: ResponseHeaders


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/watobo/http_socket/connection.rb', line 313

def proxyAuthNTLM()
  
  request = @request.copy
  
  
  request.removeHeader("Proxy-Authorization")
  request.removeHeader("Proxy-Connection")

  response_header = []

  ntlm_challenge = nil
  t1 = Net::NTLM::Message::Type1.new()
  msg = "NTLM " + t1.encode64

  request.addHeader("Proxy-Authorization", msg)
  request.addHeader("Proxy-Connection", "Keep-Alive")

  #   puts "============= T1 ======================="
  #    puts auth_request
  data = request.join + "\r\n"

  @socket.print data
  #  puts "-----------------"
  response_header = readHTTPHeader(@socket)
  rcode = nil
  rmsg = nil
  ntlm_challenge = nil
  clen = 0
  response_header.each do |line|
    # puts line
    if line =~ /^HTTP\/\d\.\d (\d+) (.*)/ then
      rcode = $1.to_i
      rmsg = $2
    end
    if line =~ /^Proxy-Authenticate: (NTLM) (.+)\r\n/
      ntlm_challenge = $2
    end
    if line =~ /^Content-Length: (\d{1,})\r\n/
      clen = $1.to_i
    end
    break if line.strip.empty?
  end

  #puts "* reading #{clen} bytes"

  if rcode == 407 # ProxyAuthentication Required
    return response_header if ntlm_challenge.nil? or ntlm_challenge == ""
  else
    puts "* no proxy authentication required!"
    return response_header
  end

  Watobo::HTTPSocket.read_body(@socket, :max_bytes => clen){ |d|
    #puts d
  }

  t2 = Net::NTLM::Message.decode64(ntlm_challenge)
  t3 = t2.response({:user => proxy.username, :password => proxy.password, :workstation => proxy.workstation, :domain => proxy.domain}, {:ntlmv2 => true})
  request.removeHeader("Proxy-Authorization")
  #  request.removeHeader("Proxy-Connection")

  #  request.addHeader("Proxy-Connection", "Close")
  #  request.addHeader("Pragma", "no-cache")
  msg = "NTLM " + t3.encode64
  request.addHeader("Proxy-Authorization", msg)
  # puts "============= T3 ======================="
  # puts request
  # puts "------------------------"
  data = request.join + "\r\n"
  @socket.print data

  response_header = readHTTPHeader(@socket)
  response_header.each do |line|
    #  puts line
    if line =~ /^HTTP\/\d\.\d (\d+) (.*)/ then
      rcode = $1.to_i
      rmsg = $2
    end
    if line =~ /^Proxy-Authenticate: (NTLM) (.+)\r\n/
      ntlm_challenge = $2
    end
    if line =~ /^Content-Length: (\d{1,})\r\n/
      clen = $1.to_i
    end
    break if line.strip.empty?
  end
  #  Watobo::HTTPSocket.read_body(tcp_socket, :max_bytes => clen){ |d|
  #puts d
  #  }
  return response_header
end

#read_body(prefs = {}) ⇒ Object


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
83
84
85
86
87
88
89
# File 'lib/watobo/http_socket/connection.rb', line 50

def read_body( prefs={} )
  clen = @response.content_length
  data = ""

  begin
    if @response.is_chunked?
      Watobo::HTTPSocket.readChunkedBody(@socket) { |c|
        data += c
      }
    elsif  clen > 0
      #  puts "* read #{clen} bytes for body"
      Watobo::HTTPSocket.read_body(@socket, :max_bytes => clen) { |c|
        data += c
        break if data.length == clen
      }
    elsif clen < 0
      # puts "* no content-length information ... mmmmmpf"
     # eofcount = 0
      Watobo::HTTPSocket.read_body(@socket) do |c|
        data += c
      end

    end
    
    response.push data unless data.empty?
    unless prefs[:ignore_logout]==true  or @session[:logout_signatures].empty?
      notify(:logout, self) if loggedOut?(response)
    end

    update_sids(request.host, response) if prefs[:update_sids] == true
    return true
   
  rescue => e
    puts "! Could not read response"
    puts e
    # puts e.backtrace
  end

  return false
end

#read_header(prefs = {}) ⇒ 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
119
120
121
122
# File 'lib/watobo/http_socket/connection.rb', line 91

def read_header( prefs={} )
  
  header = []
  msg = nil
  begin
      Watobo::HTTPSocket.read_header(@socket) do |line|
      #puts line
      # puts line.unpack("H*")
      header << line
    end
    rescue Errno::ECONNRESET
      msg = "<html><head><title>WATOBO</title></head><body>WATOBO: Connection Reset By Peer</body></html>"           
    rescue Timeout::Error
      msg = "<html><head><title>WATOBO</title></head><body>WATOBO: Timeout</body></html>"
  rescue => bang
    puts "!ERROR: read_header"
    return nil
  end
   
   header = [ "HTTP/1.1 200 OK\r\n", "Server: WATOBO\r\n", "Content-Length: #{msg.length.to_i}\r\n", "Content-Type: text/html\r\n", "\r\n", "#{msg}" ] unless msg.nil?

  response = Watobo::Response.new header
  #  update_sids(header)

  #  update_sids(request.site, response) if prefs[:update_sids] == true

  unless prefs[:ignore_logout]==true or @session[:logout_signatures].empty?
     notify(:logout, self) if loggedOut?(response)
  end

  return response
end

#sslConnect(tcp_socket, current_prefs = {}) ⇒ Object


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/watobo/http_socket/connection.rb', line 124

def sslConnect(tcp_socket, current_prefs = {} )
  begin
  
    ctx = OpenSSL::SSL::SSLContext.new()
    ctx.ciphers = current_prefs[:ssl_cipher] if current_prefs.has_key? :ssl_cipher

    if current_prefs.has_key? :ssl_client_cert and current_prefs.has_key? :ssl_client_key
    
      ctx.cert = current_prefs[:ssl_client_cert]
      ctx.key = current_prefs[:ssl_client_key]
      if $DEBUG
                  puts "[SSLconnect] Client Certificates"
                  puts "= CERT ="
                 # puts @ctx.cert.methods.sort
        puts ctx.cert.display
        puts "---"
                  puts "= KEY ="
                  puts ctx.key.display
                  puts "---"
                end    
                end
    
    @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ctx)
    @socket.sync_close = true

    @socket.connect
    @socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
    puts "[SSLconnect]: #{@socket.state}" if $DEBUG
    return socket
  rescue => bang
    if current_prefs[:ssl_cipher].nil?
      puts "[SSLconnect] ... gr#!..*peep*.. "
      puts bang
      puts bang.backtrace if $DEBUG
    end
  end
end

#sslProxyConnect(prefs) ⇒ Object

SSLProxyConnect return SSLSocket, ResponseHeader of ConnectionSetup On error SSLSocket is nil


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/watobo/http_socket/connection.rb', line 165

def sslProxyConnect( prefs )
  begin
    tcp_socket = nil
    response_header = []

    request = @request.copy
   
    #  timeout(6) do

    tcp_socket = TCPSocket.new( @proxy.host, @proxy.port)
    tcp_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
    tcp_socket.sync = true
   
    # setup request
    dummy = "CONNECT #{request.host}:#{request.port} HTTP/1.0\r\n"
    request.shift
    request.unshift dummy

    request.removeHeader("Proxy-Connection")
    request.removeHeader("Connection")
    request.removeHeader("Content-Length")
    request.removeBody()
    request.addHeader("Proxy-Connection", "Keep-Alive")
    request.addHeader("Pragma", "no-cache")

    #  puts "=== sslProxyConnect ==="
    #  puts request

    if proxy.has_login?
      case proxy.auth_type
      when AUTH_TYPE_NTLM

        t1 = Net::NTLM::Message::Type1.new()
        msg = "NTLM " + t1.encode64
        request.addHeader("Proxy-Authorization", msg)

        if $DEBUG
        puts "============= PROXY NTLM: T1 ======================="
        puts request
        puts "---"
        end
        data = request.join + "\r\n"

        tcp_socket.print data
        #  puts "-----------------"
        cl = 0
        ntlm_challenge = nil
        while (line = tcp_socket.gets)
          response_header.push line
          puts line if $DEBUG
          if line =~ /^HTTP\/\d\.\d (\d+) (.*)/ then
            rcode = $1.to_i
            rmsg = $2
          end
          if line =~ /^Proxy-Authenticate: (NTLM) (.+)\r\n/
            ntlm_challenge = $2
          end
          if line =~ /^Content-Length: (\d*)/i
            cl = $1.to_i
          end
          break if line.strip.empty?
        end

    
        if cl > 0
        Watobo::HTTPSocket.read_body(tcp_socket) { |d|
          # puts d
        }
        end

        if rcode == 200 # Ok
          puts "* seems proxy doesn't require authentication"
          socket = sslConnect(tcp_socket, prefs)
          return socket, response_header
        end

        return socket, response_header if ntlm_challenge.nil? or ntlm_challenge == ""

        t2 = Net::NTLM::Message.decode64(ntlm_challenge)
        t3 = t2.response( { :user => proxy.username,
          :password => proxy.password,
          :domain => proxy.domain },
        { :workstation => proxy.workstation, :ntlmv2 => true } )
        request.removeHeader("Proxy-Authorization")

        msg = "NTLM " + t3.encode64
        request.addHeader("Proxy-Authorization", msg)
        
        data = request.join + "\r\n"
        if $DEBUG
         puts "============= T3 ======================="
         puts data
         puts "---"
        end

        tcp_socket.print data
        #  puts "-----------------"

        response_header = []
        rcode = 0
        response_header = read_header(@socket)
        rcode = response_header.status
        if rcode =~/^200/ # Ok
          puts "[ProxyAuth-NTLM] Authorization Successful" if $DEBUG
          socket = sslConnect(tcp_socket, prefs)
          return socket, response_header
        elsif rcode =~ /^407/ # ProxyAuthentication Required
          # if rcode is still 407 authentication didn't work -> break
          msg = "NTLM-Authentication failed!"
          puts "[ProxyAuth-NTLM] #{msg}" if $DEBUG
          return nil, msg
        else
          puts "[SSLconnect] NTLM Authentication"
          puts ">  #{rcode} <"
          return nil, response_header
        end              
      end
    end # END OF PROXY AUTH

    # Start ProxyConnect Without Authentication
    data = request.join + "\r\n"
    tcp_socket.print data
    # puts "-----------------"

    response_header = []
    response_header = readHTTPHeader(@socket)
    rcode = response_header.status
    if rcode =~ /^200/ # Ok
      # puts "* proxy connection successfull"
    elsif rcode =~ /^407/ # ProxyAuthentication Required
      # if rcode is still 407 authentication didn't work -> break

    else
      puts "[SSLconnect] Response Status"
      puts ">  #{rcode} <"
    end

    socket = sslConnect(@socket, prefs)
    return socket, response_header
  rescue => bang
    puts bang
    return nil, error_response(bang)
  end
  # return nil, nil
end