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

Methods inherited from BetterCap::Pluggable

meta, metadata

Constructor Details

#initializeInjectCSS

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

Raises:



58
59
60
# File 'lib/bettercap/proxy/http/modules/injectcss.rb', line 58

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.



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

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bettercap/proxy/http/modules/injectcss.rb', line 64

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