Module: Arachni::Utilities
- Extended by:
- Utilities
- Included in:
- Element::Base, Element::Capabilities::Auditable, ElementFilter, Framework, HTTP, HTTP::CookieJar, Mixins::Observable, Module::Base, Module::Base, Module::Manager, Module::Utilities, Parser, Platform::Fingerprinter, Platform::Manager, Platform::Manager, Plugin::Manager, Plugin::Manager, Processes::Dispatchers, Processes::Instances, RPC::Server::Dispatcher, RPC::Server::Framework, RPC::Server::Instance, Report::Manager, Report::Manager, Session, Spider, Trainer, Arachni::UI::CLI, Arachni::UI::CLI::Utilities, URI, URI, Utilities
- Defined in:
- lib/arachni/utilities.rb
Overview
Includes some useful methods for the system, the modules etc.
Instance Method Summary collapse
-
#available_port ⇒ Fixnum
Random available port number.
- #cookie_encode(*args) ⇒ Object
- #cookies_from_document(*args) ⇒ Object
- #cookies_from_file(*args) ⇒ Object
- #cookies_from_response(*args) ⇒ Object
-
#exception_jail(raise_exception = true, &block) ⇒ Object
Wraps the “block” in exception handling code and runs it.
-
#exclude_path?(url) ⇒ Bool
Decides whether the given ‘url` matches any framework exclusion rules.
-
#extract_domain(url) ⇒ String
Domain name.
-
#follow_protocol?(url, reference = Options.url) ⇒ Bool
Decides whether the given ‘url` has an acceptable protocol.
- #form_decode(*args) ⇒ Object
- #form_encode(*args) ⇒ Object
- #form_parse_request_body(*args) ⇒ Object (also: #parse_request_body)
- #forms_from_document(*args) ⇒ Object
- #forms_from_response(*args) ⇒ Object
- #generate_token ⇒ Object
-
#get_path(url) ⇒ String
Path Full URL up to the path component (no resource, query etc.).
- #html_decode(str) ⇒ Object (also: #html_unescape)
- #html_encode(str) ⇒ Object (also: #html_escape)
-
#include_path?(url) ⇒ Bool
Decides whether the given ‘url` matches any framework inclusion rules.
- #links_from_document(*args) ⇒ Object
- #links_from_response(*args) ⇒ Object
- #normalize_url(url) ⇒ Object
- #page_from_response(*args) ⇒ Object
- #page_from_url(*args, &block) ⇒ Object
- #parse_query(*args) ⇒ Object
- #parse_set_cookie(*args) ⇒ Object
- #parse_url_vars(*args) ⇒ Object
-
#path_in_domain?(url, reference = Options.url) ⇒ Bool
Compares 2 urls in order to decide whether or not they belong to the same domain.
-
#path_too_deep?(url) ⇒ Bool
‘true` is the path exceeds the framework limit, `false` otherwise.
-
#port_available?(port) ⇒ Bool
Checks whether the port number is available.
-
#rand_port ⇒ Integer
Random port within the user specified range.
-
#redundant_path?(url, &block) ⇒ Bool
Checks if the provided URL matches a redundant filter and decreases its counter if so.
- #remove_constants(mod, skip = [], children_only = true) ⇒ Object
-
#seed ⇒ String
Random HEX (SHA2) string.
-
#skip_page?(page_or_response) ⇒ Bool
(also: #skip_response?)
Determines whether or not a given Page or Typhoeus::Response should be ignored.
-
#skip_path?(path) ⇒ Bool
Decides whether or not the provided ‘path` should be skipped based on:.
-
#skip_resource?(resource) ⇒ Bool
Determines whether or not the given ‘resource` should be ignored depending on its type and content.
- #to_absolute(relative_url, reference_url = Options.instance.url.to_s) ⇒ Object
- #uri_decode(url) ⇒ Object
- #uri_encode(string, bad_characters = nil) ⇒ Object
- #uri_parse(url) ⇒ Object
-
#uri_parser ⇒ URI::Parser
Cached URI parser.
- #url_sanitize(url) ⇒ Object
Instance Method Details
#available_port ⇒ Fixnum
Returns Random available port number.
371 372 373 374 |
# File 'lib/arachni/utilities.rb', line 371 def available_port nil while !port_available?( port = rand_port ) port end |
#cookie_encode(*args) ⇒ Object
100 101 102 |
# File 'lib/arachni/utilities.rb', line 100 def ( *args ) Cookie.encode( *args ) end |
#cookies_from_document(*args) ⇒ Object
85 86 87 |
# File 'lib/arachni/utilities.rb', line 85 def ( *args ) Cookie.from_document( *args ) end |
#cookies_from_file(*args) ⇒ Object
95 96 97 |
# File 'lib/arachni/utilities.rb', line 95 def ( *args ) Cookie.from_file( *args ) end |
#cookies_from_response(*args) ⇒ Object
80 81 82 |
# File 'lib/arachni/utilities.rb', line 80 def ( *args ) Cookie.from_response( *args ) end |
#exception_jail(raise_exception = true, &block) ⇒ Object
Wraps the “block” in exception handling code and runs it.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/arachni/utilities.rb', line 415 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.
221 222 223 |
# File 'lib/arachni/utilities.rb', line 221 def exclude_path?( url ) uri_parse( url ).exclude?( Options.exclude ) end |
#extract_domain(url) ⇒ String
Returns Domain name.
178 179 180 |
# File 'lib/arachni/utilities.rb', line 178 def extract_domain( url ) uri_parse( url ).domain end |
#follow_protocol?(url, reference = Options.url) ⇒ Bool
Decides whether the given ‘url` has an acceptable protocol.
266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/arachni/utilities.rb', line 266 def follow_protocol?( url, reference = Options.url ) return true if !reference check_scheme = uri_parse( url ).scheme return false if !%(http https).include?( check_scheme.to_s.downcase ) ref_scheme = uri_parse( reference ).scheme return true if ref_scheme && ref_scheme != 'https' return true if ref_scheme == check_scheme !Options.https_only? 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 |
#generate_token ⇒ Object
385 386 387 388 389 |
# File 'lib/arachni/utilities.rb', line 385 def generate_token secret = '' 1000.times { secret << rand( 9999 ).to_s } Digest::SHA2.hexdigest( secret ) end |
#get_path(url) ⇒ String
Returns path Full URL up to the path component (no resource, query etc.).
167 168 169 |
# File 'lib/arachni/utilities.rb', line 167 def get_path( url ) uri_parse( url ).up_to_path end |
#html_decode(str) ⇒ Object Also known as: html_unescape
114 115 116 |
# File 'lib/arachni/utilities.rb', line 114 def html_decode( str ) ::CGI.unescapeHTML( str.to_s ) end |
#html_encode(str) ⇒ Object Also known as: html_escape
119 120 121 |
# File 'lib/arachni/utilities.rb', line 119 def html_encode( str ) ::CGI.escapeHTML( str.to_s ) end |
#include_path?(url) ⇒ Bool
Decides whether the given ‘url` matches any framework inclusion rules.
235 236 237 |
# File 'lib/arachni/utilities.rb', line 235 def include_path?( url ) uri_parse( url ).include?( Options.include ) end |
#links_from_document(*args) ⇒ Object
67 68 69 |
# File 'lib/arachni/utilities.rb', line 67 def links_from_document( *args ) Link.from_document( *args ) end |
#links_from_response(*args) ⇒ Object
62 63 64 |
# File 'lib/arachni/utilities.rb', line 62 def links_from_response( *args ) Link.from_response( *args ) end |
#normalize_url(url) ⇒ Object
150 151 152 |
# File 'lib/arachni/utilities.rb', line 150 def normalize_url( url ) URI.normalize( url ) end |
#page_from_response(*args) ⇒ Object
105 106 107 |
# File 'lib/arachni/utilities.rb', line 105 def page_from_response( *args ) Page.from_response( *args ) end |
#page_from_url(*args, &block) ⇒ Object
110 111 112 |
# File 'lib/arachni/utilities.rb', line 110 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 |
#parse_set_cookie(*args) ⇒ Object
90 91 92 |
# File 'lib/arachni/utilities.rb', line 90 def ( *args ) 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.
207 208 209 |
# File 'lib/arachni/utilities.rb', line 207 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.
191 192 193 |
# File 'lib/arachni/utilities.rb', line 191 def path_too_deep?( url ) uri_parse( url ).too_deep?( Options.depth_limit ) end |
#port_available?(port) ⇒ Bool
Checks whether the port number is available.
398 399 400 401 402 403 404 405 406 407 |
# File 'lib/arachni/utilities.rb', line 398 def port_available?( port ) begin socket = Socket.new( :INET, :STREAM, 0 ) socket.bind( Addrinfo.tcp( '127.0.0.1', port ) ) socket.close true rescue false end end |
#rand_port ⇒ Integer
Returns Random port within the user specified range.
378 379 380 381 382 383 |
# File 'lib/arachni/utilities.rb', line 378 def rand_port first, last = Options.rpc_instance_port_range range = (first..last).to_a range[ rand( range.last - range.first ) ] end |
#redundant_path?(url, &block) ⇒ Bool
Checks if the provided URL matches a redundant filter and decreases its counter if so.
If a filter’s counter has reached 0 the method returns true.
251 252 253 |
# File 'lib/arachni/utilities.rb', line 251 def redundant_path?( url, &block ) Options.redundant?( url, &block ) end |
#remove_constants(mod, skip = [], children_only = true) ⇒ Object
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/arachni/utilities.rb', line 436 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 |
#seed ⇒ String
Returns 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_page?(page_or_response) ⇒ Bool Also known as: skip_response?
Determines whether or not a given Page or Typhoeus::Response should be ignored.
326 327 328 329 330 |
# File 'lib/arachni/utilities.rb', line 326 def skip_page?( page_or_response ) (Options.exclude_binaries? && !page_or_response.text?) || skip_path?( page_or_response.url ) || Options.exclude_page?( page_or_response.body ) end |
#skip_path?(path) ⇒ Bool
Does not call #redundant_path?.
Decides whether or not the provided ‘path` should be skipped based on:
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/arachni/utilities.rb', line 293 def skip_path?( path ) return true if !path parsed = uri_parse( path.to_s ) begin return true if !follow_protocol?( parsed ) return true if !path_in_domain?( parsed ) return true if path_too_deep?( parsed ) return true if !include_path?( parsed ) return true if exclude_path?( parsed ) false rescue => e ap e ap e.backtrace true end end |
#skip_resource?(resource) ⇒ Bool
Determines whether or not the given ‘resource` should be ignored depending on its type and content.
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/arachni/utilities.rb', line 353 def skip_resource?( resource ) case resource when Page skip_page?( resource ) when Typhoeus::Response skip_response?( resource ) else if (s = resource.to_s) =~ /[\r\n]/ Options.exclude_page? s else skip_path? s end end end |
#to_absolute(relative_url, reference_url = Options.instance.url.to_s) ⇒ Object
145 146 147 |
# File 'lib/arachni/utilities.rb', line 145 def to_absolute( relative_url, reference_url = Options.instance.url.to_s ) URI.to_absolute( relative_url, reference_url ) end |
#uri_decode(url) ⇒ Object
140 141 142 |
# File 'lib/arachni/utilities.rb', line 140 def uri_decode( url ) URI.decode( url ) end |
#uri_encode(string, bad_characters = nil) ⇒ Object
135 136 137 |
# File 'lib/arachni/utilities.rb', line 135 def uri_encode( string, bad_characters = nil ) URI.encode( string, bad_characters ) end |
#uri_parse(url) ⇒ Object
130 131 132 |
# File 'lib/arachni/utilities.rb', line 130 def uri_parse( url ) URI.parse( url ) end |
#uri_parser ⇒ URI::Parser
Returns cached URI parser.
125 126 127 |
# File 'lib/arachni/utilities.rb', line 125 def uri_parser URI.parser end |
#url_sanitize(url) ⇒ Object
155 156 157 |
# File 'lib/arachni/utilities.rb', line 155 def url_sanitize( url ) normalize_url( url ) end |