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

Methods inherited from BetterCap::Pluggable

meta, metadata

Constructor Details

#initializeInjectJS

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

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.



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

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.



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

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