Class: Arachni::Page
- Includes:
- Utilities
- Defined in:
- lib/arachni/page.rb,
lib/arachni/page/dom.rb,
lib/arachni/page/scope.rb,
lib/arachni/page/dom/transition.rb
Overview
It holds page data like elements, cookies, headers, etc…
Defined Under Namespace
Constant Summary collapse
- ELEMENTS =
[ :links, :forms, :cookies, :nested_cookies, :headers, :link_templates, :jsons, :xmls, :ui_inputs, :ui_forms ]
- METADATA =
[ :nonce_name, :skip_dom ]
Instance Attribute Summary collapse
- #cache ⇒ Hash readonly
-
#dom ⇒ DOM
DOM snapshot.
-
#element_audit_whitelist ⇒ Set<Integer>
readonly
Audit whitelist based on Element::Capabilities::Auditable#coverage_hash.
-
#metadata ⇒ Hash
readonly
Holds page data that will need to persist between #clear_cache calls and other utility data.
-
#response ⇒ HTTP::Response
readonly
HTTP response.
Class Method Summary collapse
- ._load(data) ⇒ Object
- .from_data(data) ⇒ Object
- .from_response(response) ⇒ Page
- .from_rpc_data(data) ⇒ Page
- .from_url(url, opts = {}, &block) ⇒ Page
Instance Method Summary collapse
- #==(other) ⇒ Object
- #_dump(_) ⇒ Object
-
#audit_element?(element) ⇒ Bool
`true` if the element should be audited, `false` otherwise.
-
#body ⇒ String
HTTP response body.
- #body=(string) ⇒ Object
-
#clear_cache ⇒ Page
`self` with caches cleared.
-
#code ⇒ String
URL of the page.
-
#cookie_jar ⇒ Array<Element::Cookie>
Cookies extracted from the supplied cookie-jar.
-
#do_not_audit_elements ⇒ Object
It forces #audit_element? to always returns false.
-
#document ⇒ Arachni::Parser::Document
Parsed HTML document.
- #dup ⇒ Object
-
#elements ⇒ Array<Element::Base>
All page elements.
-
#elements_within_scope ⇒ Array<Element::Base>
All page elements that are within the scope of the scan.
- #eql?(other) ⇒ Boolean
-
#has_elements?(*tags) ⇒ Boolean
`true` if the page contains any of the given elements, `false` otherwise.
-
#has_script? ⇒ Boolean
`true` if the page contains client-side code, `false` otherwise.
- #hash ⇒ Object
- #import_metadata(other, metas = METADATA) ⇒ Object
-
#initialize(options) ⇒ Page
constructor
Needs either a `:parser` or a `:response` or user provided data.
-
#method(*args) ⇒ String
The request method that returned the page.
- #parsed_url ⇒ Arachni::URI
- #parser ⇒ Parser
- #parser=(p) ⇒ Object
-
#paths ⇒ Array<String>
Paths contained in this page.
-
#performer ⇒ Object
Object which performed the #request which lead to this page.
- #persistent_hash ⇒ Object
-
#platforms ⇒ Platform
Applicable platforms for the page.
- #prepare_for_report ⇒ Object
-
#query_vars ⇒ Hash
URL query parameters.
- #reload_metadata ⇒ Object
-
#request ⇒ HTTP::Request
HTTP request.
- #scope ⇒ Scope
-
#text? ⇒ Boolean
`true` if the body of the page is text-base, `false` otherwise.
-
#title ⇒ String
Title of the page.
-
#to_h ⇒ Hash
(also: #to_hash)
Converts the page data to a hash.
- #to_initialization_options(deep = true) ⇒ Object
-
#to_rpc_data ⇒ Hash
Data representing this instance that are suitable the RPC transmission.
- #to_s ⇒ Object (also: #inspect)
- #update_element_audit_whitelist(list) ⇒ Set
- #update_metadata ⇒ Object
-
#url ⇒ String
URL of the page.
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
Constructor Details
#initialize(options) ⇒ Page
Needs either a `:parser` or a `:response` or user provided data.
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 |
# File 'lib/arachni/page.rb', line 143 def initialize( ) fail ArgumentError, 'Options cannot be empty.' if .empty? = .dup @cache = {} @do_not_audit_elements = .delete(:do_not_audit_elements) @cache[:parser] = .delete(:parser) @response = @cache[:parser].response if @cache[:parser] # We need to know whether or not the page has been dynamically updated # with elements, in order to optimize #dup and #hash operations. @has_custom_elements = Set.new @metadata ||= {} .each do |k, v| send( "#{k}=", try_dup( v ) ) end @dom = DOM.new( ([:dom] || {}).merge( page: self ) ) fail ArgumentError, 'No URL given!' if !url Platform::Manager.fingerprint( self ) @element_audit_whitelist ||= [] @element_audit_whitelist = Set.new( @element_audit_whitelist ) end |
Instance Attribute Details
#dom ⇒ DOM
Returns DOM snapshot.
110 111 112 |
# File 'lib/arachni/page.rb', line 110 def dom @dom end |
#element_audit_whitelist ⇒ Set<Integer> (readonly)
Returns Audit whitelist based on Element::Capabilities::Auditable#coverage_hash.
132 133 134 |
# File 'lib/arachni/page.rb', line 132 def element_audit_whitelist @element_audit_whitelist end |
#metadata ⇒ Hash (readonly)
Returns Holds page data that will need to persist between #clear_cache calls and other utility data.
124 125 126 |
# File 'lib/arachni/page.rb', line 124 def @metadata end |
#response ⇒ HTTP::Response (readonly)
Returns HTTP response.
114 115 116 |
# File 'lib/arachni/page.rb', line 114 def response @response end |
Class Method Details
._load(data) ⇒ Object
605 606 607 |
# File 'lib/arachni/page.rb', line 605 def self._load( data ) new( Marshal.load( data ) ) end |
.from_data(data) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/arachni/page.rb', line 82 def self.from_data( data ) data = data.dup data[:response] ||= {} data[:response][:code] ||= 200 data[:response][:url] ||= data.delete( :url ) data[:response][:body] ||= data.delete( :body ) || '' data[:response][:request] ||= {} data[:response][:request][:url] ||= data[:response][:url] data[:cookie_jar] ||= [] data[:response][:request] = Arachni::HTTP::Request.new( data[:response][:request] ) data[:response] = Arachni::HTTP::Response.new( data[:response] ) new data end |
.from_response(response) ⇒ Page
59 60 61 |
# File 'lib/arachni/page.rb', line 59 def self.from_response( response ) Parser.new( response ).page end |
.from_rpc_data(data) ⇒ Page
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
# File 'lib/arachni/page.rb', line 573 def self.from_rpc_data( data ) dom = data.delete('dom') normalized_data = {} data.each do |name, value| value = case name when 'response' HTTP::Response.from_rpc_data( value ) when *ELEMENTS.map(&:to_s) value.map do |e| Element.type_to_class( name[0...-1].to_sym ).from_rpc_data( e ) end.to_a else value end normalized_data[name.to_sym] = value end instance = new( normalized_data ) instance.instance_variable_set( '@dom', DOM.from_rpc_data( dom.merge( page: instance ) ) ) instance end |
.from_url(url, opts = {}, &block) ⇒ Page
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/arachni/page.rb', line 37 def self.from_url( url, opts = {}, &block ) responses = [] opts[:precision] ||= 2 opts[:precision].times do HTTP::Client.get( url, opts[:http] || {} ) do |res| responses << res next if responses.size != opts[:precision] block.call( from_response( responses ) ) if block_given? end end if !block_given? HTTP::Client.run from_response( responses ) end end |
Instance Method Details
#==(other) ⇒ Object
467 468 469 |
# File 'lib/arachni/page.rb', line 467 def ==( other ) hash == other.hash end |
#_dump(_) ⇒ Object
601 602 603 |
# File 'lib/arachni/page.rb', line 601 def _dump( _ ) Marshal.dump( ( false ) ) end |
#audit_element?(element) ⇒ Bool
Returns `true` if the element should be audited, `false` otherwise.
229 230 231 232 233 234 235 |
# File 'lib/arachni/page.rb', line 229 def audit_element?( element ) return if @do_not_audit_elements return true if @element_audit_whitelist.empty? @element_audit_whitelist.include?( element.is_a?( Integer ) ? element : element.coverage_hash ) end |
#body ⇒ String
Returns HTTP response body.
269 270 271 272 |
# File 'lib/arachni/page.rb', line 269 def body return '' if !@body && !@response @body ||= response.body end |
#body=(string) ⇒ Object
276 277 278 279 280 281 |
# File 'lib/arachni/page.rb', line 276 def body=( string ) @has_javascript = nil clear_cache @body = string.to_s.freeze end |
#clear_cache ⇒ Page
Will preserve caches for elements which have been externally modified.
Returns `self` with caches cleared.
352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/arachni/page.rb', line 352 def clear_cache ELEMENTS.each do |type| next if @has_custom_elements.include? type # Remove the association to this page before clearing the elements # from cache to make it easier on the GC. (@cache[type] || []).each { |e| e.page = nil } end @cache.delete_if { |k, _| !@has_custom_elements.include? k } self end |
#code ⇒ String
Returns URL of the page.
256 257 258 259 |
# File 'lib/arachni/page.rb', line 256 def code return 0 if !@code && !response @code ||= response.code end |
#cookie_jar ⇒ Array<Element::Cookie>
Returns Cookies extracted from the supplied cookie-jar.
300 301 302 |
# File 'lib/arachni/page.rb', line 300 def @cookie_jar ||= (parser ? parser. : []) end |
#do_not_audit_elements ⇒ Object
It forces #audit_element? to always returns false.
238 239 240 |
# File 'lib/arachni/page.rb', line 238 def do_not_audit_elements @do_not_audit_elements = true end |
#document ⇒ Arachni::Parser::Document
Returns Parsed HTML document.
342 343 344 345 346 |
# File 'lib/arachni/page.rb', line 342 def document @cache[:document] ||= (parser.nil? ? Arachni::Parser.parse( body ) : parser.document) end |
#dup ⇒ Object
475 476 477 |
# File 'lib/arachni/page.rb', line 475 def dup self.class.new end |
#elements ⇒ Array<Element::Base>
Returns All page elements.
320 321 322 |
# File 'lib/arachni/page.rb', line 320 def elements ELEMENTS.map { |type| send( type ) }.flatten end |
#elements_within_scope ⇒ Array<Element::Base>
Returns All page elements that are within the scope of the scan.
326 327 328 329 330 331 |
# File 'lib/arachni/page.rb', line 326 def elements_within_scope ELEMENTS.map do |type| next if !Options.audit.element? type send( type ).select { |e| e.scope.in? } end.flatten.compact end |
#eql?(other) ⇒ Boolean
471 472 473 |
# File 'lib/arachni/page.rb', line 471 def eql?( other ) self == other end |
#has_elements?(*tags) ⇒ Boolean
Returns `true` if the page contains any of the given elements, `false` otherwise.
412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/arachni/page.rb', line 412 def has_elements?( * ) return if !text? .flatten.each do |tag| tag = tag.to_s next if !body.has_html_tag?( tag ) return false if !document return true if document.nodes_by_name( tag ).any? end false end |
#has_script? ⇒ Boolean
Returns `true` if the page contains client-side code, `false` otherwise.
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/arachni/page.rb', line 384 def has_script? return @has_javascript if !@has_javascript.nil? if !response.headers.content_type.to_s.start_with?( 'text/html' ) || !text? return @has_javascript = false end dbody = body.downcase # First check, quick and simple. if dbody.include?( '<script' ) || dbody.include?( 'javascript:' ) return @has_javascript = true end # Check for event attributes, if there are any then there's JS to be # executed. Browser::Javascript.events.flatten.each do |event| return @has_javascript = true if dbody.include?( "#{event}=" ) end @has_javascript = false end |
#hash ⇒ Object
463 464 465 |
# File 'lib/arachni/page.rb', line 463 def hash digest.hash end |
#import_metadata(other, metas = METADATA) ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/arachni/page.rb', line 495 def ( other, = METADATA ) [].flatten.each do || other..each do |element_type, data| @metadata[element_type] ||= {} @metadata[element_type][.to_s] ||= {} @metadata[element_type][.to_s].merge!( data[.to_s] ) end end self end |
#method(*args) ⇒ String
Returns The request method that returned the page.
335 336 337 338 |
# File 'lib/arachni/page.rb', line 335 def method( *args ) return super( *args ) if args.any? response.request.method end |
#parsed_url ⇒ Arachni::URI
186 187 188 |
# File 'lib/arachni/page.rb', line 186 def parsed_url Arachni::URI( url ) end |
#parser ⇒ Parser
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/arachni/page.rb', line 191 def parser return if !@response return @cache[:parser] if @cache[:parser] @cache[:parser] = Parser.new( @response ) # The page may have a browser-assigned body, set it as the one to parse. @cache[:parser].body = body @cache[:parser] end |
#parser=(p) ⇒ Object
202 203 204 |
# File 'lib/arachni/page.rb', line 202 def parser=( p ) @cache[:parser] = p end |
#paths ⇒ Array<String>
Returns Paths contained in this page.
308 309 310 |
# File 'lib/arachni/page.rb', line 308 def paths @cache[:paths] ||= (parser ? parser.paths : []) end |
#performer ⇒ Object
Returns Object which performed the #request which lead to this page.
181 182 183 |
# File 'lib/arachni/page.rb', line 181 def performer request.performer end |
#persistent_hash ⇒ Object
459 460 461 |
# File 'lib/arachni/page.rb', line 459 def persistent_hash digest.persistent_hash end |
#platforms ⇒ Platform
Returns Applicable platforms for the page.
314 315 316 |
# File 'lib/arachni/page.rb', line 314 def platforms Platform::Manager[url] end |
#prepare_for_report ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/arachni/page.rb', line 364 def prepare_for_report # We want a hard clear, that's why we don't call #clear_cache. @cache.clear # If we're dealing with binary data remove it before storing. if !text? response.body = nil self.body = nil end @cookie_jar.clear if @cookie_jar @dom.digest = nil @dom.skip_states = nil self end |
#query_vars ⇒ Hash
Returns URL query parameters.
263 264 265 |
# File 'lib/arachni/page.rb', line 263 def query_vars @cache[:query_vars] ||= uri_parse_query( url ) end |
#reload_metadata ⇒ Object
487 488 489 490 491 492 493 |
# File 'lib/arachni/page.rb', line 487 def ELEMENTS.each do |type| next if !@cache[type] @cache[type].each { |e| e } end end |
#request ⇒ HTTP::Request
Returns HTTP request.
244 245 246 |
# File 'lib/arachni/page.rb', line 244 def request response.request end |
#scope ⇒ Scope
175 176 177 |
# File 'lib/arachni/page.rb', line 175 def scope @scope = Scope.new( self ) end |
#text? ⇒ Boolean
Returns `true` if the body of the page is text-base, `false` otherwise.
429 430 431 432 |
# File 'lib/arachni/page.rb', line 429 def text? return false if !response response.text? end |
#title ⇒ String
Returns Title of the page.
436 437 438 |
# File 'lib/arachni/page.rb', line 436 def title document.nodes_by_name( 'title' ).first.text rescue nil end |
#to_h ⇒ Hash Also known as: to_hash
Returns Converts the page data to a hash.
442 443 444 445 446 447 448 449 450 451 |
# File 'lib/arachni/page.rb', line 442 def to_h skip = [:@document, :@do_not_audit_elements, :@has_custom_elements, :@scope] instance_variables.inject({}) do |h, iv| next h if skip.include? iv h[iv.to_s.gsub( '@', '').to_sym] = try_dup( instance_variable_get( iv ) ) h end.merge(@cache).tap { |h| h.delete :parser } end |
#to_initialization_options(deep = true) ⇒ Object
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/arachni/page.rb', line 509 def ( deep = true ) h = {} h[:body] = @body if @body [:cookie_jar, :element_audit_whitelist, :metadata].each do |m| h[m] = instance_variable_get( "@#{m}".to_sym ) if deep h[m] = try_dup( h[m] ) end h.delete( m ) if !h[m] end ELEMENTS.each do |type| next if !@has_custom_elements.include?( type ) h[type] = @cache[type] if !h[type] || h[type].empty? h.delete( type ) next end h[type] = h[type].map { |e| c = e.dup; c.page = nil; c } end h[:response] = response h[:do_not_audit_elements] = @do_not_audit_elements h[:dom] = dom.to_h.keys.inject({}) do |dh, k| dh[k] = dom.send( k ) if deep dh[k] = try_dup( dh[k] ) end dh end h end |
#to_rpc_data ⇒ Hash
Returns Data representing this instance that are suitable the RPC transmission.
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/arachni/page.rb', line 553 def to_rpc_data data = ( false ).my_stringify_keys(false) data['dom'] = dom.to_rpc_data data['element_audit_whitelist'] = element_audit_whitelist.to_a data['response'] = data['response'].to_rpc_data (ELEMENTS - [:headers]).map(&:to_s).each do |e| next if !data[e] data[e] = send(e).map(&:to_rpc_data) end data.delete 'cookie_jar' data end |
#to_s ⇒ Object Also known as: inspect
454 455 456 |
# File 'lib/arachni/page.rb', line 454 def to_s "#<#{self.class}:#{object_id} @url=#{@url.inspect} @dom=#{@dom}>" end |
#update_element_audit_whitelist(list) ⇒ Set
Returns #element_audit_whitelist.
214 215 216 217 218 219 |
# File 'lib/arachni/page.rb', line 214 def update_element_audit_whitelist( list ) [list].flatten.each do |e| @element_audit_whitelist << (e.is_a?( Integer ) ? e : e.coverage_hash ) end end |
#update_metadata ⇒ Object
479 480 481 482 483 484 485 |
# File 'lib/arachni/page.rb', line 479 def ELEMENTS.each do |type| next if !@cache[type] @cache[type].each { |e| e } end end |
#url ⇒ String
Returns URL of the page.
250 251 252 |
# File 'lib/arachni/page.rb', line 250 def url @url ||= @response.url end |