Class: BetterCap::Proxy::HTTP::Streamer

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/proxy/http/streamer.rb

Overview

Handle data streaming between clients and servers for the BetterCap::Proxy::HTTP::Proxy.

Instance Method Summary collapse

Constructor Details

#initialize(sslstrip) ⇒ Streamer

Initialize the class.



21
22
23
24
# File 'lib/bettercap/proxy/http/streamer.rb', line 21

def initialize( sslstrip )
  @ctx       = Context.get
  @sslstrip  = SSLStrip::Strip.new( @ctx ) if sslstrip
end

Instance Method Details

#handle(request, client, redirects = 0) ⇒ Object

Handle the HTTP request from client.



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
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bettercap/proxy/http/streamer.rb', line 45

def handle( request, client, redirects = 0 )
  response = Response.new
  request.client, _ = get_client_details( client )

  Logger.debug "Handling #{request.method} request from #{request.client} ..."

  begin
    r = nil
    if @sslstrip
      r = @sslstrip.preprocess( request )
    end

    if r.nil?
      # call modules on_pre_request
      process( request )

      self.send( "do_#{request.method}", request, response )
    else
      response = r
    end

    if response.textual? or request.method == 'DELETE'
      StreamLogger.log_http( request, response )
    else
      Logger.debug "[#{request.client}] -> #{request.to_url} [#{response.code}]"
    end

    if @sslstrip
      # do we need to retry the request?
      if @sslstrip.process( request, response ) == true
        # https redirect loop?
        if redirects < SSLStrip::Strip::MAX_REDIRECTS
          return self.handle( request, client, redirects + 1 )
        else
          Logger.info "[#{'SSLSTRIP'.red} #{request.client}] Detected HTTPS redirect loop for '#{request.host}'."
        end
      end
    end

    # Strip out a few security headers.
    strip_security( response )

    # call modules on_request
    process( request, response )

    client.write response.to_s
  rescue NoMethodError => e
    Logger.warn "Could not handle #{request.method} request from #{request.client} ..."
    Logger.exception e
  end
end

#rickroll(client) ⇒ Object

Redirect the client to a funny video.



36
37
38
39
40
41
42
# File 'lib/bettercap/proxy/http/streamer.rb', line 36

def rickroll( client )
  client_ip, client_port = get_client_details( client )

  Logger.warn "#{client_ip}:#{client_port} is connecting to us directly."

  client.write Response.redirect( "https://www.youtube.com/watch?v=dQw4w9WgXcQ" ).to_s
end

#was_stripped?(request, client) ⇒ Boolean

Return true if the request was stripped.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/bettercap/proxy/http/streamer.rb', line 27

def was_stripped?(request, client)
  if @sslstrip
    request.client, _ = get_client_details( client )
    return @sslstrip.was_stripped?(request)
  end
  false
end