Class: Injectcss

Inherits:
BetterCap::Proxy::Module show all
Defined in:
lib/bettercap/proxy/modules/injectcss.rb

Overview

This proxy module will take care of CSS code injection.

Constant Summary collapse

@@cssdata =

CSS data to be injected.

nil
@@cssurl =

CSS file URL to be injected.

nil

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BetterCap::Proxy::Module

available, #enabled?, is_builtin?, load, modules, register_modules, register_options

Constructor Details

#initializeInjectcss

Create an instance of this module and raise a BetterCap::Error if command line arguments weren't correctly specified.

Raises:


49
50
51
# File 'lib/bettercap/proxy/modules/injectcss.rb', line 49

def initialize
  raise BetterCap::Error, "No --css-file, --css-url or --css-data options specified for the proxy module." if @@cssdata.nil? and @@cssurl.nil?
end

Class Method Details

.on_options(opts) ⇒ Object

Add custom command line arguments to the opts OptionParser instance.


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
# File 'lib/bettercap/proxy/modules/injectcss.rb', line 21

def self.on_options(opts)
  opts.separator ""
  opts.separator "Inject CSS Proxy Module Options:"
  opts.separator ""

  opts.on( '--css-data STRING', 'CSS code to be injected.' ) do |v|
    @@cssdata = v
    unless @@cssdata.include?("<style>")
      @@cssdata = "<style>\n#{@@cssdata}\n</style>"
    end
  end

  opts.on( '--css-file PATH', 'Path of the CSS file to be injected.' ) do |v|
    filename = File.expand_path v
    raise BetterCap::Error, "#{filename} invalid file." unless File.exists?(filename)
    @@cssdata = File.read( filename )
    unless @@cssdata.include?("<style>")
      @@cssdata = "<style>\n#{@@cssdata}\n</style>"
    end
  end

  opts.on( '--css-url URL', 'URL the CSS file to be injected.' ) do |v|
    @@cssurl = v
  end
end

Instance Method Details

#on_request(request, response) ⇒ Object

Called by the BetterCap::Proxy::Proxy processor on each HTTP request and response.


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bettercap/proxy/modules/injectcss.rb', line 55

def on_request( request, response )
  # is it a html page?
  if response.content_type =~ /^text\/html.*/
    BetterCap::Logger.info "Injecting CSS #{if @@cssdata.nil? then "URL" else "file" end} into http://#{request.host}#{request.url}"
    # inject URL
    if @@cssdata.nil?
      response.body.sub!( '</head>', "  <link rel=\"stylesheet\" href=\"#{@cssurl}\"></script></head>" )
    # inject data
    else
      response.body.sub!( '</head>', "#{@@cssdata}</head>" )
    end
  end
end