Class: InjectHTML

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

Overview

This proxy module will take care of HTML code injection.

Constant Summary collapse

@@iframe =

URL of the iframe if –html-iframe-url was specified.

nil
@@data =

HTML data to be injected.

nil
@@position =

Position of the injection, 0 = just after <body>, 1 = before </body>

0

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

#initializeInjectHTML

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

Raises:



64
65
66
# File 'lib/bettercap/proxy/http/modules/injecthtml.rb', line 64

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

Class Method Details

.on_options(opts) ⇒ Object

Add custom command line arguments to the opts OptionParser instance.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bettercap/proxy/http/modules/injecthtml.rb', line 32

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

  opts.on( '--html-data STRING', 'HTML code to be injected.' ) do |v|
    @@data = v
  end

  opts.on( '--html-file PATH', 'Path of the html file to be injected.' ) do |v|
    filename = File.expand_path v
    raise BetterCap::Error, "#{filename} invalid file." unless File.exists?(filename)
    @@data = File.read( filename )
  end

  opts.on( '--html-iframe-url URL', 'URL of the iframe that will be injected, if this option is specified an "iframe" tag will be injected.' ) do |v|
    @@iframe = v
  end

  opts.on( '--html-position POSITION', 'Position of the injection, valid values are START for injecting after the <body> tag and END to inject just before </body>.' ) do |v|
    if v == 'START'
      @@position = 0
    elsif v == 'END'
      @@position = 1
    else
      raise BetterCap::Error, "#{v} invalid position, only START or END values are accepted."
    end
  end
end

Instance Method Details

#on_request(request, response) ⇒ Object

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bettercap/proxy/http/modules/injecthtml.rb', line 70

def on_request( request, response )
  # is it a html page?
  if response.content_type =~ /^text\/html.*/
    BetterCap::Logger.info "[#{'INJECTHTML'.green}] Injecting HTML code into #{request.to_url}"

    if @@data.nil?
      replacement = "<iframe src=\"#{@@iframe}\" frameborder=\"0\" height=\"0\" width=\"0\"></iframe>"
    else
      replacement = "#{@@data}"
    end

    if @@position == 0
      response.body.sub!( /<body([^>]*)>/i ) { "<body#{$1}>#{replacement}" }
    else
      response.body.sub!( /<\/body>/i ) { "#{replacement}</body>" }
    end
  end
end