Module: Arachni::Utilities

Overview

Includes some useful methods for the system, the modules etc…

Author:

Instance Method Summary collapse

Instance Method Details



99
100
101
# File 'lib/arachni/utilities.rb', line 99

def cookie_encode( *args )
    Cookie.encode( *args )
end

#cookies_from_document(*args) ⇒ Object



85
86
87
# File 'lib/arachni/utilities.rb', line 85

def cookies_from_document( *args )
    Cookie.from_document( *args )
end

#cookies_from_file(*args) ⇒ Object



94
95
96
# File 'lib/arachni/utilities.rb', line 94

def cookies_from_file( *args )
    Cookie.from_file( *args )
end

#cookies_from_response(*args) ⇒ Object



80
81
82
# File 'lib/arachni/utilities.rb', line 80

def cookies_from_response( *args )
    Cookie.from_response( *args )
end

#exception_jail(raise_exception = true, &block) ⇒ Object

Wraps the “block” in exception handling code and runs it.

Parameters:

  • raise_exception (Bool) (defaults to: true)

    re-raise exception

  • block (Block)

    to call



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/arachni/utilities.rb', line 275

def exception_jail( raise_exception = true, &block )
    block.call
rescue Exception => e
    begin
        print_error e.inspect
        print_error_backtrace e
        print_error
        print_error 'Parent:'
        print_error  self.class.to_s
        print_error
        print_error 'Block:'
        print_error block.to_s
        print_error
        print_error 'Caller:'
        ::Kernel.caller.each { |l| print_error l }
        print_error '-' * 80
    rescue
    end
    raise e if raise_exception
end

#exclude_path?(url) ⇒ Bool

Decides whether the given url matches any framework exclusion rules.

Parameters:

Returns:

  • (Bool)


212
213
214
# File 'lib/arachni/utilities.rb', line 212

def exclude_path?( url )
    uri_parse( url ).exclude?( Options.exclude )
end

#extract_domain(url) ⇒ String

Returns domain name.

Parameters:

Returns:

See Also:



175
176
177
# File 'lib/arachni/utilities.rb', line 175

def extract_domain( url )
    uri_parse( url ).domain
end

#form_decode(*args) ⇒ Object



51
52
53
# File 'lib/arachni/utilities.rb', line 51

def form_decode( *args )
    Form.decode( *args )
end

#form_encode(*args) ⇒ Object



46
47
48
# File 'lib/arachni/utilities.rb', line 46

def form_encode( *args )
    Form.encode( *args )
end

#form_parse_request_body(*args) ⇒ Object Also known as: parse_request_body



56
57
58
# File 'lib/arachni/utilities.rb', line 56

def form_parse_request_body( *args )
    Form.parse_request_body( *args )
end

#forms_from_document(*args) ⇒ Object



41
42
43
# File 'lib/arachni/utilities.rb', line 41

def forms_from_document( *args )
    Form.from_document( *args )
end

#forms_from_response(*args) ⇒ Object



36
37
38
# File 'lib/arachni/utilities.rb', line 36

def forms_from_response( *args )
    Form.from_response( *args )
end

#get_path(url) ⇒ String

Returns path full URL up to the path component (no resource, query etc.).

Parameters:

Returns:

  • (String)

    path full URL up to the path component (no resource, query etc.)

See Also:



164
165
166
# File 'lib/arachni/utilities.rb', line 164

def get_path( url )
    uri_parse( url ).up_to_path
end

#hash_keys_to_str(hash) ⇒ Hash

Recursively converts a Hash’s keys to strings

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


260
261
262
263
264
265
266
267
# File 'lib/arachni/utilities.rb', line 260

def hash_keys_to_str( hash )
    nh = {}
    hash.each_pair do |k, v|
        nh[k.to_s] = v
        nh[k.to_s] = hash_keys_to_str( v ) if v.is_a? Hash
    end
    nh
end

#html_decode(str) ⇒ Object Also known as: html_unescape



112
113
114
# File 'lib/arachni/utilities.rb', line 112

def html_decode( str )
    ::CGI.unescapeHTML( str.to_s )
end

#html_encode(str) ⇒ Object Also known as: html_escape



117
118
119
# File 'lib/arachni/utilities.rb', line 117

def html_encode( str )
    ::CGI.escapeHTML( str.to_s )
end

#include_path?(url) ⇒ Bool

Decides whether the given url matches any framework inclusion rules.

Parameters:

Returns:

  • (Bool)


223
224
225
# File 'lib/arachni/utilities.rb', line 223

def include_path?( url )
    uri_parse( url ).include?( Options.include )
end


67
68
69
# File 'lib/arachni/utilities.rb', line 67

def links_from_document( *args )
    Link.from_document( *args )
end


62
63
64
# File 'lib/arachni/utilities.rb', line 62

def links_from_response( *args )
    Link.from_response( *args )
end

#normalize_url(url) ⇒ Object



148
149
150
# File 'lib/arachni/utilities.rb', line 148

def normalize_url( url )
    URI.normalize( url )
end

#page_from_response(*args) ⇒ Object

See Also:



104
105
106
# File 'lib/arachni/utilities.rb', line 104

def page_from_response( *args )
    Page.from_response( *args )
end

#page_from_url(*args, &block) ⇒ Object



108
109
110
# File 'lib/arachni/utilities.rb', line 108

def page_from_url( *args, &block )
    Page.from_url( *args, &block )
end

#parse_query(*args) ⇒ Object



75
76
77
# File 'lib/arachni/utilities.rb', line 75

def parse_query( *args )
    Link.parse_query_vars( *args )
end


89
90
91
# File 'lib/arachni/utilities.rb', line 89

def parse_set_cookie( *args )
    Cookie.parse_set_cookie( *args )
end

#parse_url_vars(*args) ⇒ Object



72
73
74
# File 'lib/arachni/utilities.rb', line 72

def parse_url_vars( *args )
    Link.parse_query_vars( *args )
end

#path_in_domain?(url, reference = Options.url) ⇒ Bool

Compares 2 urls in order to decide whether or not they belong to the same domain.

Parameters:

  • url (String)
  • reference (String) (defaults to: Options.url)

Returns:

  • (Bool)

    true if self is in the same domain as the reference URL, false otherwise

See Also:



201
202
203
# File 'lib/arachni/utilities.rb', line 201

def path_in_domain?( url, reference = Options.url )
    uri_parse( url ).in_domain?( !Options.follow_subdomains, reference )
end

#path_too_deep?(url) ⇒ Bool

Returns true is the path exceeds the framework limit, false otherwise.

Parameters:

Returns:

  • (Bool)

    true is the path exceeds the framework limit, false otherwise

See Also:



186
187
188
# File 'lib/arachni/utilities.rb', line 186

def path_too_deep?( url )
    uri_parse( url ).too_deep?( Options.depth_limit )
end

#remove_constants(mod, skip = [], children_only = true) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/arachni/utilities.rb', line 296

def remove_constants( mod, skip = [], children_only = true )
    return if skip.include?( mod )
    return if !(mod.is_a?( Class ) || !mod.is_a?( Module )) ||
        !mod.to_s.start_with?( 'Arachni' )

    parent = Object
    mod.to_s.split( '::' )[0..-2].each do |ancestor|
        parent = parent.const_get( ancestor.to_sym )
    end

    mod.constants.each { |m| mod.send( :remove_const, m ) }

    return if children_only
    parent.send( :remove_const, mod.to_s.split( ':' ).last.to_sym )
end

#seedString

Returns random HEX (SHA2) string.

Returns:

  • (String)

    random HEX (SHA2) string



31
32
33
# File 'lib/arachni/utilities.rb', line 31

def seed
    @@seed ||= Digest::SHA2.hexdigest( srand( 1000 ).to_s )
end

#skip_path?(path) ⇒ Bool

Decides whether or not the provided path should be skipped based on:

Parameters:

Returns:

  • (Bool)


238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/arachni/utilities.rb', line 238

def skip_path?( path )
    return true if !path

    parsed = uri_parse( path )
    begin
        return true if !include_path?( parsed )
        return true if exclude_path?( parsed )
        return true if path_too_deep?( parsed )
        return true if !path_in_domain?( parsed )
        false
    rescue
        true
    end
end

#to_absolute(relative_url, reference_url = Options.instance.url.to_s) ⇒ Object



143
144
145
# File 'lib/arachni/utilities.rb', line 143

def to_absolute( relative_url, reference_url = Options.instance.url.to_s )
    URI.to_absolute( relative_url, reference_url )
end

#uri_decode(url) ⇒ Object



138
139
140
# File 'lib/arachni/utilities.rb', line 138

def uri_decode( url )
    URI.decode( url )
end

#uri_encode(string, bad_characters = nil) ⇒ Object



133
134
135
# File 'lib/arachni/utilities.rb', line 133

def uri_encode( string, bad_characters = nil )
    URI.encode( string, bad_characters )
end

#uri_parse(url) ⇒ Object

See Also:



128
129
130
# File 'lib/arachni/utilities.rb', line 128

def uri_parse( url )
    URI.parse( url )
end

#uri_parserURI::Parser

Returns cached URI parser.

Returns:

  • (URI::Parser)

    cached URI parser



123
124
125
# File 'lib/arachni/utilities.rb', line 123

def uri_parser
    URI.parser
end

#url_sanitize(url) ⇒ Object

See Also:



153
154
155
# File 'lib/arachni/utilities.rb', line 153

def url_sanitize( url )
    normalize_url( url )
end