Class: Arachni::Plugins::Metrics

Inherits:
Arachni::Plugin::Base show all
Defined in:
components/plugins/metrics.rb

Overview

Author:

Constant Summary

Constants included from Arachni

BANNER, Cookie, Form, Header, JSON, Link, LinkTemplate, Severity, UIForm, UIInput, VERSION, WEBSITE, WIKI, XML

Instance Attribute Summary

Attributes inherited from Arachni::Plugin::Base

#framework, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Arachni::Plugin::Base

#browser_cluster, #clean_up, distributable, distributable?, #framework_abort, #framework_pause, #framework_resume, gems, #http, #info, #initialize, is_distributable, merge, #register_results, #restore, #session, #suspend, #wait_while_framework_running, #with_browser

Methods inherited from Component::Base

author, description, fullname, #shortname, shortname, shortname=, version

Methods included from Component::Output

#depersonalize_output, #depersonalize_output?, #intercept_print_message

Methods included from UI::Output

#caller_location, #debug?, #debug_level, #debug_level_1?, #debug_level_2?, #debug_level_3?, #debug_level_4?, #debug_off, #debug_on, #disable_only_positives, #error_buffer, #error_log_fd, #error_logfile, #has_error_log?, #included, #log_error, #mute, #muted?, #only_positives, #only_positives?, #print_bad, #print_debug, #print_debug_backtrace, #print_debug_exception, #print_debug_level_1, #print_debug_level_2, #print_debug_level_3, #print_debug_level_4, #print_error, #print_error_backtrace, #print_exception, #print_info, #print_line, #print_ok, #print_status, #print_verbose, #reroute_to_file, #reroute_to_file?, reset_output_options, #set_error_logfile, #unmute, #verbose?, #verbose_off, #verbose_on

Methods included from Component::Utilities

#read_file

Methods included from Utilities

#available_port, available_port_mutex, #bytes_to_kilobytes, #bytes_to_megabytes, #caller_name, #caller_path, #cookie_decode, #cookie_encode, #cookies_from_file, #cookies_from_parser, #cookies_from_response, #exception_jail, #exclude_path?, #follow_protocol?, #form_decode, #form_encode, #forms_from_parser, #forms_from_response, #full_and_absolute_url?, #generate_token, #get_path, #hms_to_seconds, #html_decode, #html_encode, #include_path?, #links_from_parser, #links_from_response, #normalize_url, #page_from_response, #page_from_url, #parse_set_cookie, #path_in_domain?, #path_too_deep?, #port_available?, #rand_port, #random_seed, #redundant_path?, #regexp_array_match, #remove_constants, #request_parse_body, #seconds_to_hms, #skip_page?, #skip_path?, #skip_resource?, #skip_response?, #to_absolute, #uri_decode, #uri_encode, #uri_parse, #uri_parse_query, #uri_parser, #uri_rewrite

Methods included from Arachni

URI, collect_young_objects, #get_long_win32_filename, jruby?, null_device, profile?, windows?

Constructor Details

This class inherits a constructor from Arachni::Plugin::Base

Class Method Details

.infoObject


250
251
252
253
254
255
256
257
258
259
# File 'components/plugins/metrics.rb', line 250

def self.info
    {
        name:        'Metrics',
        description: %q{
Captures metrics about multiple aspects of the scan and the web application.
},
        author:      'Tasos "Zapotek" Laskos <[email protected]>',
        version:     '0.1.2'
    }
end

Instance Method Details

#find_swf(page) ⇒ Object


226
227
228
# File 'components/plugins/metrics.rb', line 226

def find_swf( page )
    page.body.scan( /(?:data|src)=['"]?(.*)\.swf['"]?>/ )[0]
end

#prepareObject


12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'components/plugins/metrics.rb', line 12

def prepare
    @metrics = {
        'general'   => {
            'egress_traffic'  => 0,
            # Approximation, may differ from the real value depending on
            # compression and other factors.
            'ingress_traffic' => 0,
            'uses_http'       => false,
            'uses_https'      => false
        },
        'scan'      => {
            'duration'      => 0,
            'authenticated' => false
        },
        'http'      => {
            'requests'              => 0,
            'request_time_outs'     => 0,
            'request_size_min'      => 0,
            'request_size_max'      => 0,
            'request_size_average'  => 0,
            'responses_per_second'  => 0.0,
            'response_time_min'     => 0,
            'response_time_max'     => 0,
            'response_time_average' => 0,
            'response_size_min'     => 0,
            'response_size_max'     => 0,
            'response_size_average' => 0
        },
        'browser_cluster' => {
            'seconds_per_job' => 0.0,
            'total_job_time'  => 0.0,
            'job_time_outs'   => 0.0,
            'job_count'       => 0
        },
        'resource'  => {
            'binary'             => Arachni::Support::LookUp::HashSet.new,
            'without_parameters' => Arachni::Support::LookUp::HashSet.new,
            'with_parameters'    => Arachni::Support::LookUp::HashSet.new
        },
        'element'   => {
            'links'                    => 0,
            'forms'                    => 0,
            'cookies'                  => 0,
            'jsons'                    => 0,
            'xmls'                     => 0,
            'headers'                  => 0,
            'has_forms_with_nonces'    => false,
            'has_forms_with_passwords' => false,
            'input_names_total'        => 0,
            'input_names_unique'       => Arachni::Support::LookUp::HashSet.new
        },
        'dom'       => {
            'event_listeners' => Arachni::Support::LookUp::HashSet.new,
            'swf_objects'     => Arachni::Support::LookUp::HashSet.new
        },
        'platforms' => Arachni::Platform::Manager::TYPES.keys.
            inject({}) { |h, t| h[t.to_s] = Set.new; h }
    }
end

#process(hash) ⇒ Object


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'components/plugins/metrics.rb', line 230

def process( hash )
    h = {}
    hash.each do |k, v|
        case v
            when Hash
                v = process( v )

            when Set
                v = v.to_a

            when Arachni::Support::LookUp::HashSet
                v = v.size

        end

        h[k] = v
    end
    h
end

#runObject


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'components/plugins/metrics.rb', line 72

def run
    http_response_time_total = 0

    http.on_complete do |response|
        response.platforms.to_a.each do |platform|
            @metrics['platforms'][Arachni::Platform::Manager.find_type( platform ).to_s] << platform.to_s
        end

        @metrics['general']['egress_traffic']  += response.request.to_s.size
        @metrics['general']['ingress_traffic'] += response.to_s.size

        if @metrics['http']['response_time_min'].is_a?( Integer ) ||
            response.time < @metrics['http']['response_time_min']

            @metrics['http']['response_time_min'] = response.time
        end
        if response.time > @metrics['http']['response_time_max']
            @metrics['http']['response_time_max'] = response.time
        end

        response_size = response.to_s.size
        if @metrics['http']['response_size_min'].is_a?( Integer ) ||
            response_size < @metrics['http']['response_size_min']

            @metrics['http']['response_size_min'] = response_size
        end
        if response_size > @metrics['http']['response_size_max']
            @metrics['http']['response_size_max'] = response_size
        end

        request_size = response.request.to_s.size
        if @metrics['http']['request_size_min'].is_a?( Integer ) ||
            request_size < @metrics['http']['request_size_min']

            @metrics['http']['request_size_min'] = request_size
        end
        if request_size > @metrics['http']['request_size_max']
            @metrics['http']['request_size_max'] = request_size
        end

        # Only track OK codes, otherwise discovery checks will muck with the
        # data.
        if response.code == 200
            if response.request.body.is_a?( Hash ) ||
                response.request.parameters.any? ||
                response.request.url.include?( '?' )

                if response.request.body.is_a? Hash
                    body = response.request.body.keys.sort
                else
                    body = nil
                end

                @metrics['resource']['with_parameters'] <<
                    "#{response.parsed_url.up_to_path}#{response.request.parameters.keys.sort}:#{body}"
            else
                @metrics['resource']['without_parameters'] << response.url
            end
        end

        @metrics['general']['uses_http']  ||=
            (response.parsed_url.scheme == 'http')
        @metrics['general']['uses_https'] ||=
            (response.parsed_url.scheme == 'https')

        if !response.text?
            @metrics['resource']['binary'] << response.url
        end

        http_response_time_total += response.time
    end

    framework.on_page_audit do |page|
        %w(links forms cookies headers jsons xmls).each do |type|
            page.send( type ).each do |e|
                next if e.inputs.empty?

                @metrics['element'][type]                += 1
                @metrics['element']['input_names_total'] += e.inputs.size

                e.inputs.keys.each do |name|
                    @metrics['element']['input_names_unique'] << name
                end

                if e.is_a? Arachni::Element::Form
                    # Probably not a real form, just a request with inputs
                    # captured by the browsers and fed back to the system.
                    if !e.source
                        @metrics['element'][type] -= 1
                    end

                    @metrics['element']['has_forms_with_nonces']    ||= !!e.has_nonce?
                    @metrics['element']['has_forms_with_passwords'] ||= !!e.requires_password?
                end
            end
        end

        if (swf = find_swf( page ))
            @metrics['dom']['swf_objects'] << swf
        end

        if Arachni::Options.scope.dom_depth_limit.to_i < page.dom.depth + 1 &&
            browser_cluster && page.has_script?

            with_browser do |browser|
                browser.load( page ).each_element_with_events do |locator, event_data|
                    event_data.each do |data|
                        @metrics['dom']['event_listeners'] << "#{locator}:#{data}"
                    end
                end
            end
        end
    end

    wait_while_framework_running

    metrics = process( @metrics )

    statistics = framework.statistics

    metrics['browser_cluster']['job_time_outs'] =
        statistics[:browser_cluster][:time_out_count]

    metrics['browser_cluster']['seconds_per_job'] =
        statistics[:browser_cluster][:seconds_per_job]

    metrics['browser_cluster']['total_job_time'] =
        statistics[:browser_cluster][:total_job_time]

    metrics['browser_cluster']['job_count'] =
        statistics[:browser_cluster][:queued_job_count]

    metrics['http']['requests'] = statistics[:http][:response_count]

    metrics['http']['request_time_outs']    = statistics[:http][:time_out_count]
    metrics['http']['responses_per_second'] = statistics[:http][:total_responses_per_second]

    if metrics['http']['requests'] > 0
        metrics['http']['response_time_average'] =
            http_response_time_total / metrics['http']['requests']

        metrics['http']['response_size_average'] =
            metrics['general']['ingress_traffic'] / metrics['http']['requests']

        metrics['http']['request_size_average'] =
            metrics['general']['egress_traffic'] / metrics['http']['requests']
    end

    metrics['scan']['duration']      = statistics[:runtime]
    metrics['scan']['authenticated'] = !!Arachni::Options.session.check_url

    register_results metrics
end