Class: InjectJS

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

Overview

This proxy module will take care of Javascript code injection.

Constant Summary collapse

@@jsdata =

JS data to be injected.

nil
@@jsurl =

JS 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

#initializeInjectJS

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/injectjs.rb', line 50

def initialize
  raise BetterCap::Error, "No --js-file, --js-url or --js-data options specified for the proxy module." if @@jsdata.nil? and @@jsurl.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/injectjs.rb', line 22

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

  opts.on( '--js-data STRING', 'Javascript code to be injected.' ) do |v|
    @@jsdata = v
    unless @@jsdata.include?("<script")
      @@jsdata = "<script type=\"text/javascript\">\n#{@@jsdata}\n</script>"
    end
  end

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

  opts.on( '--js-url URL', 'URL the javascript file to be injected.' ) do |v|
    @@jsurl = 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/injectjs.rb', line 56

def on_request( request, response )
  # is it a html page?
  if response.content_type =~ /^text\/html.*/
    BetterCap::Logger.info "[#{'INJECTJS'.green}] Injecting javascript #{@@jsdata.nil?? "URL" : "file"} into #{request.to_url}"
    # inject URL
    if @@jsdata.nil?
      response.body.sub!( '</head>', "<script src=\"#{@@jsurl}\" type=\"text/javascript\"></script></head>" )
    # inject data
    else
      response.body.sub!( '</head>', "#{@@jsdata}</head>" )
    end
  end
end