Class: InjectCSS

Inherits:
BetterCap::Proxy::HTTP::Module show all
Defined in:
lib/bettercap/proxy/http/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::HTTP::Module

available, #enabled?, is_builtin?, load, modules, #on_pre_request, 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:



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

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.



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

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::HTTP::Proxy processor on each HTTP request and response.



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

def on_request( request, response )
  # is it a html page?
  if response.content_type =~ /^text\/html.*/
    BetterCap::Logger.info "[#{'INJECTCSS'.green}] Injecting CSS #{@@cssdata.nil?? "URL" : "file"} into #{request.to_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