Module: Arachni::Utilities
- Extended by:
- Utilities
- Included in:
- Browser, Browser::Javascript, BrowserCluster, Component::Manager, Component::Manager, Component::Utilities, Element::Base, Element::Base, Element::Capabilities::Auditable, Framework, HTTP::Client, HTTP::CookieJar, Page, Parser, Platform::Fingerprinter, Platform::Manager, Platform::Manager, Processes::Dispatchers, Processes::Instances, RPC::Server::Dispatcher, RPC::Server::Framework, RPC::Server::Instance, Report, Session, Support::Mixins::Observable, Trainer, URI, URI, Utilities
- Defined in:
- lib/arachni/utilities.rb
Overview
Includes some useful methods for the system.
Instance Method Summary collapse
-
#available_port ⇒ Fixnum
Random available port number.
-
#caller_name ⇒ String
Filename (without extension) of the caller.
-
#caller_path(offset = 2) ⇒ String
Filepath of the caller.
- #cookie_decode(*args) ⇒ Object
- #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.
-
#follow_protocol?(url, reference = Options.url) ⇒ Bool
Decides whether the given ‘url` has an acceptable protocol.
- #form_decode(*args) ⇒ Object
- #form_encode(*args) ⇒ Object
- #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.).
- #hms_to_seconds(time) ⇒ Object
- #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_set_cookie(*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.
-
#random_seed ⇒ String
Random HEX (SHA2) string.
-
#redundant_path?(url) ⇒ Bool
Checks if the provided URL matches a redundant filter and decreases its counter if so.
- #remove_constants(mod, skip = []) ⇒ Object
- #request_parse_body(*args) ⇒ Object
-
#seconds_to_hms(seconds) ⇒ String
Time in ‘00:00:00` (`hours:minutes:seconds`) format.
-
#skip_page?(page) ⇒ Bool
Determines whether or not the given Page.
-
#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.
-
#skip_response?(response) ⇒ Bool
Determines whether or not the given HTTP::Response should be ignored.
- #to_absolute(relative_url, reference_url = Options.instance.url.to_s) ⇒ Object
- #uri_decode(url) ⇒ Object
- #uri_encode(*args) ⇒ Object
- #uri_parse(url) ⇒ Object
- #uri_parse_query(url) ⇒ Object
-
#uri_parser ⇒ URI::Parser
Cached URI parser.
- #uri_rewrite(*args) ⇒ Object
Instance Method Details
#available_port ⇒ Fixnum
Returns Random available port number.
338 339 340 341 |
# File 'lib/arachni/utilities.rb', line 338 def available_port nil while !port_available?( port = rand_port ) port end |
#caller_name ⇒ String
Returns Filename (without extension) of the caller.
22 23 24 |
# File 'lib/arachni/utilities.rb', line 22 def caller_name File.basename( caller_path( 3 ), '.rb' ) end |
#caller_path(offset = 2) ⇒ String
Returns Filepath of the caller.
28 29 30 |
# File 'lib/arachni/utilities.rb', line 28 def caller_path( offset = 2 ) ::Kernel.caller[offset].split( /:(\d+):in/ ).first end |
#cookie_decode(*args) ⇒ Object
98 99 100 |
# File 'lib/arachni/utilities.rb', line 98 def ( *args ) Cookie.decode( *args ) end |
#cookie_encode(*args) ⇒ Object
93 94 95 |
# File 'lib/arachni/utilities.rb', line 93 def ( *args ) Cookie.encode( *args ) end |
#cookies_from_document(*args) ⇒ Object
78 79 80 |
# File 'lib/arachni/utilities.rb', line 78 def ( *args ) Cookie.from_document( *args ) end |
#cookies_from_file(*args) ⇒ Object
88 89 90 |
# File 'lib/arachni/utilities.rb', line 88 def ( *args ) Cookie.from_file( *args ) end |
#cookies_from_response(*args) ⇒ Object
73 74 75 |
# File 'lib/arachni/utilities.rb', line 73 def ( *args ) Cookie.from_response( *args ) end |
#exception_jail(raise_exception = true, &block) ⇒ Object
Wraps the ‘block` in exception handling code and runs it.
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/arachni/utilities.rb', line 394 def exception_jail( raise_exception = true, &block ) block.call rescue => e if respond_to?( :print_error ) && respond_to?( :print_exception ) print_exception 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 end raise e if raise_exception nil end |
#exclude_path?(url) ⇒ Bool
Decides whether the given ‘url` matches any framework exclusion rules.
203 204 205 |
# File 'lib/arachni/utilities.rb', line 203 def exclude_path?( url ) uri_parse( url ).scope.exclude? end |
#follow_protocol?(url, reference = Options.url) ⇒ Bool
Decides whether the given ‘url` has an acceptable protocol.
245 246 247 |
# File 'lib/arachni/utilities.rb', line 245 def follow_protocol?( url, reference = Options.url ) uri_parse( url ).scope.follow_protocol?( reference ) end |
#form_decode(*args) ⇒ Object
53 54 55 |
# File 'lib/arachni/utilities.rb', line 53 def form_decode( *args ) Form.decode( *args ) end |
#form_encode(*args) ⇒ Object
48 49 50 |
# File 'lib/arachni/utilities.rb', line 48 def form_encode( *args ) Form.encode( *args ) end |
#forms_from_document(*args) ⇒ Object
43 44 45 |
# File 'lib/arachni/utilities.rb', line 43 def forms_from_document( *args ) Form.from_document( *args ) end |
#forms_from_response(*args) ⇒ Object
38 39 40 |
# File 'lib/arachni/utilities.rb', line 38 def forms_from_response( *args ) Form.from_response( *args ) end |
#generate_token ⇒ Object
354 355 356 |
# File 'lib/arachni/utilities.rb', line 354 def generate_token SecureRandom.hex 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 |
#hms_to_seconds(time) ⇒ Object
382 383 384 385 386 387 |
# File 'lib/arachni/utilities.rb', line 382 def hms_to_seconds( time ) a = [1, 60, 3600] * 2 time.split( /[:\.]/ ).map { |t| t.to_i * a.pop }.inject(&:+) rescue 0 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.
215 216 217 |
# File 'lib/arachni/utilities.rb', line 215 def include_path?( url ) uri_parse( url ).scope.include? end |
#links_from_document(*args) ⇒ Object
68 69 70 |
# File 'lib/arachni/utilities.rb', line 68 def links_from_document( *args ) Link.from_document( *args ) end |
#links_from_response(*args) ⇒ Object
63 64 65 |
# File 'lib/arachni/utilities.rb', line 63 def links_from_response( *args ) Link.from_response( *args ) end |
#normalize_url(url) ⇒ Object
157 158 159 |
# File 'lib/arachni/utilities.rb', line 157 def normalize_url( url ) URI.normalize( url ) end |
#page_from_response(*args) ⇒ Object
103 104 105 |
# File 'lib/arachni/utilities.rb', line 103 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_set_cookie(*args) ⇒ Object
83 84 85 |
# File 'lib/arachni/utilities.rb', line 83 def ( *args ) Cookie.( *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.
191 192 193 |
# File 'lib/arachni/utilities.rb', line 191 def path_in_domain?( url, reference = Options.url ) uri_parse( url ).scope.in_domain?( reference ) end |
#path_too_deep?(url) ⇒ Bool
Returns ‘true` is the path exceeds the framework limit, `false` otherwise.
177 178 179 |
# File 'lib/arachni/utilities.rb', line 177 def path_too_deep?( url ) uri_parse( url ).scope.too_deep? end |
#port_available?(port) ⇒ Bool
Checks whether the port number is available.
363 364 365 366 367 368 369 370 |
# File 'lib/arachni/utilities.rb', line 363 def port_available?( port ) begin TCPServer.new( '127.0.0.1', port ).close true rescue false end end |
#rand_port ⇒ Integer
Returns Random port within the user specified range.
347 348 349 350 351 352 |
# File 'lib/arachni/utilities.rb', line 347 def rand_port first, last = Options.dispatcher.instance_port_range range = (first..last).to_a range[ rand( range.last - range.first ) ] end |
#random_seed ⇒ String
Returns random HEX (SHA2) string.
33 34 35 |
# File 'lib/arachni/utilities.rb', line 33 def random_seed @@random_seed ||= generate_token end |
#redundant_path?(url) ⇒ 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.
230 231 232 |
# File 'lib/arachni/utilities.rb', line 230 def redundant_path?( url ) uri_parse( url ).scope.redundant? end |
#remove_constants(mod, skip = []) ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/arachni/utilities.rb', line 416 def remove_constants( mod, skip = [] ) 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 ) } nil end |
#request_parse_body(*args) ⇒ Object
58 59 60 |
# File 'lib/arachni/utilities.rb', line 58 def request_parse_body( *args ) HTTP::Request.parse_body( *args ) end |
#seconds_to_hms(seconds) ⇒ String
Returns Time in ‘00:00:00` (`hours:minutes:seconds`) format.
376 377 378 379 380 |
# File 'lib/arachni/utilities.rb', line 376 def seconds_to_hms( seconds ) seconds = seconds.to_i [seconds / 3600, seconds / 60 % 60, seconds % 60]. map { |t| t.to_s.rjust( 2, '0' ) }.join( ':' ) end |
#skip_page?(page) ⇒ Bool
Determines whether or not the given Page.
299 300 301 |
# File 'lib/arachni/utilities.rb', line 299 def skip_page?( page ) page.scope.out? end |
#skip_path?(path) ⇒ Bool
Does not call #redundant_path?.
Decides whether or not the provided ‘path` should be skipped based on:
261 262 263 264 265 266 267 268 |
# File 'lib/arachni/utilities.rb', line 261 def skip_path?( path ) return true if !path parsed = uri_parse( path.to_s ) return true if !parsed parsed.scope.out? end |
#skip_resource?(resource) ⇒ Bool
Determines whether or not the given ‘resource` should be ignored depending on its type and content.
323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/arachni/utilities.rb', line 323 def skip_resource?( resource ) case resource when Page skip_page?( resource ) when Arachni::HTTP::Response skip_response?( resource ) else skip_path? resource.to_s end end |
#skip_response?(response) ⇒ Bool
Determines whether or not the given HTTP::Response should be ignored.
282 283 284 |
# File 'lib/arachni/utilities.rb', line 282 def skip_response?( response ) response.scope.out? end |
#to_absolute(relative_url, reference_url = Options.instance.url.to_s) ⇒ Object
152 153 154 |
# File 'lib/arachni/utilities.rb', line 152 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(*args) ⇒ Object
133 134 135 |
# File 'lib/arachni/utilities.rb', line 133 def uri_encode( *args ) URI.encode( *args ) end |
#uri_parse(url) ⇒ Object
128 129 130 |
# File 'lib/arachni/utilities.rb', line 128 def uri_parse( url ) URI.parse( url ) end |
#uri_parse_query(url) ⇒ Object
147 148 149 |
# File 'lib/arachni/utilities.rb', line 147 def uri_parse_query( url ) URI.parse_query( url ) end |