| 1 | ####################################################################### | ||
| 2 | # Created on: November 06, 2006 | ||
| 3 | # Package: HoneyClient::Agent::Driver::Browser | ||
| 4 | # File: Browser.pm | ||
| 5 | # Description: A generic driver for automating the link visitation | ||
| 6 | # behavior of a web browser running inside a | ||
| 7 | # HoneyClient VM. | ||
| 8 | # | ||
| 9 | # CVS: $Id: Browser.pm 773 2007-07-26 19:04:55Z kindlund $ | ||
| 10 | # | ||
| 11 | # @author knwang, kindlund, stephenson | ||
| 12 | # | ||
| 13 | # Copyright (C) 2007 The MITRE Corporation. All rights reserved. | ||
| 14 | # | ||
| 15 | # This program is free software; you can redistribute it and/or | ||
| 16 | # modify it under the terms of the GNU General Public License | ||
| 17 | # as published by the Free Software Foundation, using version 2 | ||
| 18 | # of the License. | ||
| 19 | # | ||
| 20 | # This program is distributed in the hope that it will be useful, | ||
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 23 | # GNU General Public License for more details. | ||
| 24 | # | ||
| 25 | # You should have received a copy of the GNU General Public License | ||
| 26 | # along with this program; if not, write to the Free Software | ||
| 27 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
| 28 | # 02110-1301, USA. | ||
| 29 | # | ||
| 30 | # | ||
| 31 | ####################################################################### | ||
| 32 | |||
| 33 | =pod | ||
| 34 | |||
| 35 | =head1 NAME | ||
| 36 | |||
| 37 | HoneyClient::Agent::Driver::Browser - Perl extension to drive a | ||
| 38 | web browser, running inside a HoneyClient VM. | ||
| 39 | |||
| 40 | =head1 VERSION | ||
| 41 | |||
| 42 | This documentation refers to HoneyClient::Agent::Driver::Browser version 0.98. | ||
| 43 | |||
| 44 | =head1 SYNOPSIS | ||
| 45 | |||
| 46 | use HoneyClient::Agent::Driver::Browser; | ||
| 47 | |||
| 48 | # Library used exclusively for debugging complex objects. | ||
| 49 | use Data::Dumper; | ||
| 50 | |||
| 51 | # Create a new Browser object, initialized with a collection | ||
| 52 | # of URLs to visit. | ||
| 53 | my $browser = HoneyClient::Agent::Driver::Browser->new( | ||
| 54 | links_to_visit => { | ||
| 55 | 'http://www.google.com' => 1, | ||
| 56 | 'http://www.cnn.com' => 1, | ||
| 57 | }, | ||
| 58 | ); | ||
| 59 | |||
| 60 | # If you want to see what type of "state information" is physically | ||
| 61 | # inside $browser, try this command at any time. | ||
| 62 | print Dumper($browser); | ||
| 63 | |||
| 64 | # Continue to "drive" the driver, until it is finished. | ||
| 65 | while (!$browser->isFinished()) { | ||
| 66 | |||
| 67 | # Before we drive the application to a new set of resources, | ||
| 68 | # find out where we will be going within the application, first. | ||
| 69 | print "About to contact the following resources:\n"; | ||
| 70 | print Dumper($browser->next()); | ||
| 71 | |||
| 72 | # Now, drive browser for one iteration. | ||
| 73 | $browser->drive(); | ||
| 74 | |||
| 75 | # Get the driver's progress. | ||
| 76 | print "Status:\n"; | ||
| 77 | print Dumper($browser->status()); | ||
| 78 | |||
| 79 | } | ||
| 80 | |||
| 81 | # At this stage, the driver has exhausted its collection of links | ||
| 82 | # to visit. Let's say we want to add the URL "http://www.mitre.org" | ||
| 83 | # to the driver's list. | ||
| 84 | $browser->{links_to_visit}->{'http://www.mitre.org'} = 1; | ||
| 85 | |||
| 86 | # Now, drive the browser for one iteration. | ||
| 87 | $browser->drive(); | ||
| 88 | |||
| 89 | # Or, we can specify the URL as an argument. | ||
| 90 | $browser->drive(url => "http://www.mitre.org"); | ||
| 91 | |||
| 92 | =head1 DESCRIPTION | ||
| 93 | |||
| 94 | This library allows the Agent module to drive an instance of any browser, | ||
| 95 | running inside the HoneyClient VM. The purpose of this module is to | ||
| 96 | programmatically navigate the browser to different websites, in order to | ||
| 97 | become purposefully infected with new malware. | ||
| 98 | |||
| 99 | This module is object-oriented in design, retaining all state information | ||
| 100 | within itself for easy access. A specific browser implementation, such as | ||
| 101 | 'IE' or 'FF', must inherit from this package. | ||
| 102 | |||
| 103 | Fundamentally, the Browser driver is initialized with a set of absolute URLs | ||
| 104 | for the browser to drive to. Upon visiting each URL, the driver collects | ||
| 105 | any B<new> links found and will attempt to drive the browser to each | ||
| 106 | valid URL upon subsequent iterations of work. | ||
| 107 | |||
| 108 | For each top-level URL given, the driver will attempt to process all | ||
| 109 | corresponding links that are hosted on the same server, in order to | ||
| 110 | simulate a complete 'spider' of each server. | ||
| 111 | |||
| 112 | URLs are added and removed from hashtables, as keys. For each URL, a | ||
| 113 | calculated "priority" (a positive integer) of the URL is assigned the | ||
| 114 | value. When the Browser is ready to go to a new link, it will always go | ||
| 115 | to the next link that has the highest priority. If two URLs have the same | ||
| 116 | priority, then the Browser will chose among those two at random. | ||
| 117 | |||
| 118 | Furthermore, the Browser driver will try to visit all links shared by a | ||
| 119 | common server in order before moving on to drive to other, | ||
| 120 | external links in an ordered fashion. B<However>, the Browser may end | ||
| 121 | up re-visiting old sites, if new links were found that the | ||
| 122 | Browser has not visited yet. | ||
| 123 | |||
| 124 | As the Browser driver navigates the browser to each link, it | ||
| 125 | maintains a set of hashtables that record when valid links were | ||
| 126 | visited (see L<links_visited>); when invalid links were found | ||
| 127 | (see L<links_ignored>); and when the browser attempted to visit | ||
| 128 | a link but the operation timed out (see L<links_timed_out>). | ||
| 129 | By maintaining this internal history, the driver will B<never> | ||
| 130 | navigate the browser to the same link twice. | ||
| 131 | |||
| 132 | Lastly, it is highly recommended that for each driver B<$object>, | ||
| 133 | one should call $object->isFinished() prior to making a subsequent | ||
| 134 | call to $object->drive(), in order to verify that the driver has | ||
| 135 | not exhausted its set of links to visit. Otherwise, if | ||
| 136 | $object->drive() is called with an empty set of links to visit, | ||
| 137 | the corresponding operation will B<croak>. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | package HoneyClient::Agent::Driver::Browser; | ||
| 142 | |||
| 143 | # XXX: Disabled version check, Honeywall does not have Perl v5.8 installed. | ||
| 144 | #use 5.008006; | ||
| 145 | use strict; | ||
| 146 | use warnings; | ||
| 147 | use Config; | ||
| 148 | use Carp (); | ||
| 149 | |||
| 150 | # Traps signals, allowing END: blocks to perform cleanup. | ||
| 151 | use sigtrap qw(die untrapped normal-signals error-signals); | ||
| 152 | |||
| 153 | ####################################################################### | ||
| 154 | # Module Initialization # | ||
| 155 | ####################################################################### | ||
| 156 | |||
| 157 | BEGIN { | ||
| 158 | # Defines which functions can be called externally. | ||
| 159 | require Exporter; | ||
| 160 | our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION); | ||
| 161 | |||
| 162 | # Set our package version. | ||
| 163 | $VERSION = 0.98; | ||
| 164 | |||
| 165 | # Define inherited modules. | ||
| 166 | use HoneyClient::Agent::Driver; | ||
| 167 | |||
| 168 | @ISA = qw(Exporter HoneyClient::Agent::Driver); | ||
| 169 | |||
| 170 | # Symbols to export automatically | ||
| 171 | # Note: Since this module is object-oriented, we do *NOT* export | ||
| 172 | # any functions other than "new" to call statically. Each function | ||
| 173 | # for this module *must* be called as a method from a unique | ||
| 174 | # object instance. | ||
| 175 | @EXPORT = qw(); | ||
| 176 | |||
| 177 | # Items to export into callers namespace by default. Note: do not export | ||
| 178 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 179 | # Do not simply export all your public functions/methods/constants. | ||
| 180 | |||
| 181 | # This allows declaration use HoneyClient::Agent::Driver::Browser ':all'; | ||
| 182 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 183 | # will save memory. | ||
| 184 | |||
| 185 | # Note: Since this module is object-oriented, we do *NOT* export | ||
| 186 | # any functions other than "new" to call statically. Each function | ||
| 187 | # for this module *must* be called as a method from a unique | ||
| 188 | # object instance. | ||
| 189 | %EXPORT_TAGS = ( | ||
| 190 | 'all' => [ qw() ], | ||
| 191 | ); | ||
| 192 | |||
| 193 | # Symbols to autoexport (when qw(:all) tag is used) | ||
| 194 | @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 195 | |||
| 196 | # XXX: Fix this! | ||
| 197 | # Check to make sure our OS is Windows-based. | ||
| 198 | #if ($Config{osname} !~ /^MSWin32$/) { | ||
| 199 | # Carp::croak "Error: " . __PACKAGE__ . " will only run on Win32 platforms!\n"; | ||
| 200 | #} | ||
| 201 | |||
| 202 | $SIG{PIPE} = 'IGNORE'; # Do not exit on broken pipes. | ||
| 203 | } | ||
| 204 | our (@EXPORT_OK, $VERSION); | ||
| 205 | |||
| 206 | ####################################################################### | ||
| 207 | |||
| 208 | # Include the Global Configuration Processing Library | ||
| 209 | use HoneyClient::Util::Config qw(getVar); | ||
| 210 | |||
| 211 | # Include the ActiveContent Processing Library | ||
| 212 | # TODO: Need unit testing. | ||
| 213 | use HoneyClient::Agent::Driver::ActiveContent; | ||
| 214 | |||
| 215 | # Include Temp File Library | ||
| 216 | # TODO: Need unit testing. | ||
| 217 | use File::Temp; | ||
| 218 | |||
| 219 | # Use ISO 8601 DateTime Libraries | ||
| 220 | use DateTime::HiRes; | ||
| 221 | |||
| 222 | # Use fractional second sleeping. | ||
| 223 | # TODO: Need unit testing. | ||
| 224 | use Time::HiRes qw(sleep); | ||
| 225 | |||
| 226 | # Use Storable Library | ||
| 227 | # TODO: Need unit testing. | ||
| 228 | use Storable qw(dclone); | ||
| 229 | |||
| 230 | # Use threads Library | ||
| 231 | # TODO: Need unit testing. | ||
| 232 | use threads; | ||
| 233 | # TODO: Need unit testing. | ||
| 234 | use threads::shared; | ||
| 235 | |||
| 236 | # TODO: Need unit testing. | ||
| 237 | use HoneyClient::Util::SOAP qw(getClientHandle); | ||
| 238 | |||
| 239 | # TODO: Need unit testing. | ||
| 240 | use LWP::UserAgent; | ||
| 241 | |||
| 242 | # TODO: Need unit testing. | ||
| 243 | use HTTP::Request::Common; | ||
| 244 | |||
| 245 | # TODO: Need unit testing. | ||
| 246 | use HTML::LinkExtor; | ||
| 247 | |||
| 248 | # TODO: Need unit testing. | ||
| 249 | use HTML::TokeParser; | ||
| 250 | |||
| 251 | # TODO: Need unit testing. | ||
| 252 | use URI::URL; | ||
| 253 | |||
| 254 | # TODO: Need unit testing. | ||
| 255 | use Data::Validate::URI qw(is_uri is_web_uri); | ||
| 256 | |||
| 257 | # Include Logging Library | ||
| 258 | use Log::Log4perl qw(:easy); | ||
| 259 | |||
| 260 | # The global logging object. | ||
| 261 | our $LOG = get_logger(); | ||
| 262 | |||
| 263 | =pod | ||
| 264 | |||
| 265 | =head1 DEFAULT PARAMETER LIST | ||
| 266 | |||
| 267 | When a Browser B<$object> is instantiated using the B<new()> function, | ||
| 268 | the following parameters are supplied default values. Each value | ||
| 269 | can be overridden by specifying the new (key => value) pair into the | ||
| 270 | B<new()> function, as arguments. | ||
| 271 | |||
| 272 | Furthermore, as each parameter is initialized, each can be individually | ||
| 273 | retrieved and set at any time, using the following syntax: | ||
| 274 | |||
| 275 | my $value = $object->{key}; # Gets key's value. | ||
| 276 | $object->{key} = $value; # Sets key's value. | ||
| 277 | |||
| 278 | =head2 links_to_visit | ||
| 279 | |||
| 280 | =over 4 | ||
| 281 | |||
| 282 | This parameter is a hashtable of fully qualified URLs for the browser | ||
| 283 | to visit. Specifically, each 'key' corresponds to an absolute URL | ||
| 284 | and the 'value' is always 1. | ||
| 285 | |||
| 286 | =back | ||
| 287 | |||
| 288 | =head2 links_visited | ||
| 289 | |||
| 290 | =over 4 | ||
| 291 | |||
| 292 | This parameter is a hashtable of fully qualified URLs that the | ||
| 293 | browser has already visited. Specifically, each 'key' corresponds | ||
| 294 | to an absolute URL and the 'value' is a string representing the | ||
| 295 | date and time of when the link was visited. | ||
| 296 | |||
| 297 | B<Note>: See internal documentation of _getTimestamp() for the | ||
| 298 | corresponding date/time format of each value. | ||
| 299 | |||
| 300 | =back | ||
| 301 | |||
| 302 | =head2 links_ignored | ||
| 303 | |||
| 304 | =over 4 | ||
| 305 | |||
| 306 | This parameter is a hashtable of fully qualified URLs that the browser | ||
| 307 | has found during its link traversal process, but the browser could not | ||
| 308 | access the link. | ||
| 309 | |||
| 310 | Links could be added to this list if access requires any type of | ||
| 311 | authentication, or if the link points to a non-HTTP or HTTPS | ||
| 312 | resource (i.e., "javascript:doNetDetect()"). | ||
| 313 | |||
| 314 | Specifically, each 'key' corresponds to an absolute URL and the | ||
| 315 | 'value' is a string representing the date and time of when the link | ||
| 316 | was visited. | ||
| 317 | |||
| 318 | B<Note>: See internal documentation of _getTimestamp() for the | ||
| 319 | corresponding date/time format of each value. | ||
| 320 | |||
| 321 | =back | ||
| 322 | |||
| 323 | =head2 relative_links_to_visit | ||
| 324 | |||
| 325 | =over 4 | ||
| 326 | |||
| 327 | This parameter is a hashtable of fully qualified URLs, such that each | ||
| 328 | URL shares a common B<hostname>. This is an internal hashtable used | ||
| 329 | by the Browser driver that should be initially empty. As the Browser | ||
| 330 | driver extracts and removes new URLs off the B<links_to_visit> hashtable, | ||
| 331 | driving the browser to each URL, any B<relative> links found are | ||
| 332 | added into this hashtable; any B<external> links found are added | ||
| 333 | back into the B<links_to_visit> hashtable. | ||
| 334 | |||
| 335 | When driving to the next link, this hashtable is exhausted prior | ||
| 336 | to the main B<links_to_visit> hashtable. This allows a | ||
| 337 | browser to navigate to all links hosted on the same server, prior | ||
| 338 | to contacting a different server. | ||
| 339 | |||
| 340 | Specifically, each 'key' corresponds to an absolute URL and the | ||
| 341 | 'value' is always 1. | ||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | =head2 next_link_to_visit | ||
| 346 | |||
| 347 | =over 4 | ||
| 348 | |||
| 349 | This parameter is a scalar that contains the next URL to visit. | ||
| 350 | It is updated dynamically, any time $object->getNextLink() is called. | ||
| 351 | |||
| 352 | When the browser is ready to drive to the next link, B<next_link_to_visit> | ||
| 353 | is checked first. If that value is B<undef>, then the B<relative_links_to_visit> | ||
| 354 | hashtable is checked next. If that hashtable is empty, then finally the | ||
| 355 | B<links_to_visit> hashtable is checked last. | ||
| 356 | |||
| 357 | =back | ||
| 358 | |||
| 359 | =head2 links_timed_out | ||
| 360 | |||
| 361 | =over 4 | ||
| 362 | |||
| 363 | This parameter is a hashtable of fully qualified URLs that the browser | ||
| 364 | has found during its link traversal process, but the browser | ||
| 365 | could not access the corresponding resource due to the operation | ||
| 366 | timing out. | ||
| 367 | |||
| 368 | Specifically, each 'key' corresponds to an absolute URL and the | ||
| 369 | 'value' is a string representing the date and time of when access to | ||
| 370 | the resource was attempted. | ||
| 371 | |||
| 372 | B<Note>: See internal documentation of _getTimestamp() for the | ||
| 373 | corresponding date/time format of each value. | ||
| 374 | |||
| 375 | =back | ||
| 376 | |||
| 377 | =head2 ignore_timed_out_links | ||
| 378 | |||
| 379 | =over 4 | ||
| 380 | |||
| 381 | If this parameter is set to 1, then the browser will also | ||
| 382 | never attempt to revisit any links that caused the browser to | ||
| 383 | time out. | ||
| 384 | |||
| 385 | =back | ||
| 386 | |||
| 387 | =head2 process_name | ||
| 388 | |||
| 389 | =over 4 | ||
| 390 | |||
| 391 | A string containing the process name of the browser application, | ||
| 392 | as it appears in the Task Manager. | ||
| 393 | |||
| 394 | =back | ||
| 395 | |||
| 396 | =head2 max_relative_links_to_visit | ||
| 397 | |||
| 398 | =over 4 | ||
| 399 | |||
| 400 | An integer, representing the maximum number of relative links that | ||
| 401 | the browser should visit, before moving onto another website. If | ||
| 402 | negative, then the browser will exhaust all possible relative links | ||
| 403 | found, before moving on. This functionality is best effort; it's | ||
| 404 | possible for the browser to visit new links on previously visited | ||
| 405 | websites. | ||
| 406 | |||
| 407 | =back | ||
| 408 | |||
| 409 | =head2 positive_words | ||
| 410 | |||
| 411 | =over 4 | ||
| 412 | |||
| 413 | An array of positive words, where a link's probability of being | ||
| 414 | visited (its score) will increase, if the link contains any of these | ||
| 415 | words. | ||
| 416 | |||
| 417 | =back | ||
| 418 | |||
| 419 | =head2 negative_words | ||
| 420 | |||
| 421 | =over 4 | ||
| 422 | |||
| 423 | An array of negative words, where a link's probability of being | ||
| 424 | visited (its score) will decrease, if the link contains any of these | ||
| 425 | words. | ||
| 426 | |||
| 427 | =back | ||
| 428 | |||
| 429 | =head2 parse_active_content | ||
| 430 | |||
| 431 | =over 4 | ||
| 432 | |||
| 433 | If set to 1, then the code will attempt to parse and extract links | ||
| 434 | within active content (e.g., Flash animations). Otherwise, the | ||
| 435 | code will ignore all active content. | ||
| 436 | |||
| 437 | =back | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | my %PARAMS = ( | ||
| 442 | |||
| 443 | # This is a hashtable of fully qualified URLs | ||
| 444 | # to visit by the browser. Specifically, the 'key' is | ||
| 445 | # the absolute URL and the 'value' is always 1. | ||
| 446 | links_to_visit => { }, | ||
| 447 | |||
| 448 | # This is a hashtable of fully qualified URLs that the | ||
| 449 | # browser has already visited. Specifically, the | ||
| 450 | # 'key' is the absolute URL and the 'value' is a string | ||
| 451 | # representing the date and time of when the link was visited. | ||
| 452 | # | ||
| 453 | # Note: See _getTimestamp() for the corresponding date/time | ||
| 454 | # format. | ||
| 455 | links_visited => { }, | ||
| 456 | |||
| 457 | # This is a hashtable of URLs that the browser has found | ||
| 458 | # during its traversal process, but the browser could not | ||
| 459 | # access the link. | ||
| 460 | # | ||
| 461 | # Links could be added to this list if access requires any type of | ||
| 462 | # authentication, or if the link points to a non-HTTP or HTTPS | ||
| 463 | # resource (i.e., "javascript:doNetDetect()"). | ||
| 464 | # | ||
| 465 | # The 'key' is the absolute URL and the 'value' is a string | ||
| 466 | # representing the date and time of when the link was visited. | ||
| 467 | # | ||
| 468 | # Note: See _getTimestamp() for the corresponding date/time | ||
| 469 | # format. | ||
| 470 | links_ignored => { }, | ||
| 471 | |||
| 472 | # This is a hashtable of fully qualified URLs | ||
| 473 | # that all share a common *hostname*. This hashtable should be | ||
| 474 | # initially empty. As the driver extracts and removes new URLs | ||
| 475 | # off the 'links_to_visit' hashtable, driving the browser to each URL, | ||
| 476 | # any *relative* links found are added into this hashtable; any | ||
| 477 | # *external* links found are added back into the 'links_to_visit' | ||
| 478 | # hashtable. | ||
| 479 | # | ||
| 480 | # When navigating to the next link, this hashtable is exhausted prior | ||
| 481 | # to the main 'links_to_visit' hashtable. This allows a | ||
| 482 | # browser to navigate to all links hosted on the same server, prior | ||
| 483 | # to contacting a different server. | ||
| 484 | # | ||
| 485 | # Specifically, the 'key' is the absolute URL and the 'value' | ||
| 486 | # is always 1. | ||
| 487 | relative_links_to_visit => { }, | ||
| 488 | |||
| 489 | # This is a scalar that contains the next URL to visit. | ||
| 490 | # It is updated dynamically, any time getNextLink() is called. | ||
| 491 | # When the browser is ready to drive to the next link, | ||
| 492 | # 'next_link_to_visit' is checked. If that value is undef, then | ||
| 493 | # the 'relative_links_to_visit' hashtable is checked next. | ||
| 494 | # If that hashtable is empty, then finally the 'links_to_visit' | ||
| 495 | # hashtable is checked. | ||
| 496 | next_link_to_visit => undef, | ||
| 497 | |||
| 498 | # This is a hashtable of URLs that the browser has found | ||
| 499 | # during its traversal process, but the browser could not | ||
| 500 | # access the resource due to the operation timing out. | ||
| 501 | # | ||
| 502 | # The 'key' is the absolute URL and the 'value' is a string | ||
| 503 | # representing the date and time of when the link was visited. | ||
| 504 | # | ||
| 505 | # Note: See _getTimestamp() for the corresponding date/time | ||
| 506 | # format. | ||
| 507 | links_timed_out => { }, | ||
| 508 | |||
| 509 | # If this parameter is a defined scalar, then the browser | ||
| 510 | # will also never attempt to revisit any links that caused | ||
| 511 | # the browser to time out. | ||
| 512 | ignore_links_timed_out => getVar(name => "ignore_links_timed_out"), | ||
| 513 | |||
| 514 | # A string containing the process name of the browser application, | ||
| 515 | # as it appears in the Task Manager. | ||
| 516 | process_name => getVar(name => "process_name"), | ||
| 517 | |||
| 518 | # An integer, representing how many relative links the browser | ||
| 519 | # should continue to drive to, before moving onto another | ||
| 520 | # website. If negative, then the browser will exhaust all possible | ||
| 521 | # relative links, before moving on. (This internal variable should | ||
| 522 | # never be modified externally.) | ||
| 523 | _remaining_number_of_relative_links_to_visit => getVar(name => "max_relative_links_to_visit"), | ||
| 524 | |||
| 525 | # An integer, representing the maximum number of relative links that | ||
| 526 | # the browser should visit, before moving onto another website. If | ||
| 527 | # negative, then the browser will exhaust all possible relative links | ||
| 528 | # found, before moving on. This functionality is best effort; it's | ||
| 529 | # possible for the browser to visit new links on previously visited | ||
| 530 | # websites. | ||
| 531 | max_relative_links_to_visit => getVar(name => "max_relative_links_to_visit"), | ||
| 532 | |||
| 533 | # An array of positive words, where a link's probability of being | ||
| 534 | # visited (its score) will increase, if the link contains any of these | ||
| 535 | # words. | ||
| 536 | positive_words => getVar(name => "positive_words")->{word}, | ||
| 537 | |||
| 538 | # An array of negative words, where a link's probability of being | ||
| 539 | # visited (its score) will decrease, if the link contains any of these | ||
| 540 | # words. | ||
| 541 | negative_words => getVar(name => "negative_words")->{word}, | ||
| 542 | |||
| 543 | # If set to 1, then the code will attempt to parse and extract links | ||
| 544 | # within active content (e.g., Flash animations). Otherwise, the | ||
| 545 | # code will ignore all active content. | ||
| 546 | parse_active_content => getVar(name => "enable", | ||
| 547 | namespace => "HoneyClient::Agent::Driver::ActiveContent"), | ||
| 548 | ); | ||
| 549 | |||
| 550 | ####################################################################### | ||
| 551 | # Private Methods Implemented # | ||
| 552 | ####################################################################### | ||
| 553 | |||
| 554 | # Helper function designed to retrieve the next link for the browser | ||
| 555 | # to navigate to. | ||
| 556 | # | ||
| 557 | # Note: Calling this function will implicitly remove the next link from | ||
| 558 | # any and all applicable hashtables/scalars. | ||
| 559 | # | ||
| 560 | # When getting the next link, 'next_link_to_visit' is checked first. | ||
| 561 | # If that value is undef, then the 'relative_links_to_visit' hashtable | ||
| 562 | # is checked next. If that hashtable is empty, then finally the | ||
| 563 | # 'links_to_visit' hashtable is checked. | ||
| 564 | # | ||
| 565 | # Inputs: HoneyClient::Agent::Driver::Browser object | ||
| 566 | # Outputs: link, or undef if all applicable scalars/hashtables are empty | ||
| 567 | sub _getNextLink { | ||
| 568 | |||
| 569 | # Get the object state. | ||
| 570 | my $self = shift; | ||
| 571 | |||
| 572 | # Set the link to find as undef, initially. | ||
| 573 | # We use undef to signify that our URL *_links_to_visit hashtables | ||
| 574 | # are empty. If we were to use the empty string instead, as our | ||
| 575 | # signal, then this code would misinterpret an empty link | ||
| 576 | # <a href=""></a> as a signal that our URL hashtables were empty. | ||
| 577 | my $link = undef; | ||
| 578 | |||
| 579 | while (!defined($link) or ($link eq "")) { | ||
| 580 | # Try getting the next link from the next link | ||
| 581 | # scalar. | ||
| 582 | $link = $self->next_link_to_visit; | ||
| 583 | $self->{next_link_to_visit} = undef; | ||
| 584 | |||
| 585 | # If the next link scalar is empty, try | ||
| 586 | # getting a link from the relative hashtable. | ||
| 587 | unless (defined($link)) { | ||
| 588 | $link = _pop($self->relative_links_to_visit); | ||
| 589 | } | ||
| 590 | |||
| 591 | # If the relative hashtable is empty, try getting one | ||
| 592 | # from the external hashtable. | ||
| 593 | unless (defined($link)) { | ||
| 594 | $link = _pop($self->links_to_visit); | ||
| 595 | } | ||
| 596 | |||
| 597 | # If all hashtables/scalars were empty, immediately return an | ||
| 598 | # undef value. | ||
| 599 | unless (defined($link)) { | ||
| 600 | return $link; | ||
| 601 | } | ||
| 602 | |||
| 603 | # Now, make sure the link is valid, before we return | ||
| 604 | # it; if it's not valid, we simply move on to the next | ||
| 605 | # one in our hashtables. Invalid links will cause this | ||
| 606 | # function to return an empty string. | ||
| 607 | $link = $self->_validateLink($link); | ||
| 608 | } | ||
| 609 | |||
| 610 | # Return the next link found. | ||
| 611 | return $link; | ||
| 612 | } | ||
| 613 | |||
| 614 | # Helper function designed to get a current timestamp from | ||
| 615 | # the system OS. | ||
| 616 | # | ||
| 617 | # Note: This timestamp is in ISO 8601 format. | ||
| 618 | # | ||
| 619 | # Inputs: none | ||
| 620 | # Outputs: timestamp | ||
| 621 | sub _getTimestamp { | ||
| 622 | my $dt = DateTime::HiRes->now(); | ||
| 623 | return $dt->ymd('-') . " " . | ||
| 624 | $dt->hms(':') . "." . | ||
| 625 | $dt->nanosecond(); | ||
| 626 | } | ||
| 627 | |||
| 628 | # Helper function designed to "pop" a key off a given hashtable. | ||
| 629 | # When given a hashtable reference, this function will extract a valid key | ||
| 630 | # from the hashtable and delete the (key, value) pair from the | ||
| 631 | # hashtable. The link with the highest score is returned. | ||
| 632 | # | ||
| 633 | # Inputs: hashref | ||
| 634 | # Outputs: valid key, or undef if the hash is empty | ||
| 635 | sub _pop { | ||
| 636 | |||
| 637 | # Get supplied hash reference. | ||
| 638 | my $hash = shift; | ||
| 639 | |||
| 640 | # Get the highest score. | ||
| 641 | my @array = sort {$$hash{$b} <=> $$hash{$a}} keys %{$hash}; | ||
| 642 | my $topkey = $array[0]; | ||
| 643 | |||
| 644 | # Delete the key from the hashtable. | ||
| 645 | if (defined($topkey)) { | ||
| 646 | delete $hash->{$topkey}; | ||
| 647 | } | ||
| 648 | |||
| 649 | # Return the key found. | ||
| 650 | return $topkey; | ||
| 651 | } | ||
| 652 | |||
| 653 | # Helper function, designed to extract the hostname | ||
| 654 | # (and, if it exists, the port number) from a given | ||
| 655 | # URL. | ||
| 656 | # | ||
| 657 | # For example, if "http://hostname.com:8080/path/index.html" | ||
| 658 | # is given, then "hostname:8080" would be returned. | ||
| 659 | # | ||
| 660 | # Inputs: URL | ||
| 661 | # Outputs: hostname[:port] | ||
| 662 | sub _extractHostname { | ||
| 663 | |||
| 664 | # Sanity check. | ||
| 665 | my $arg = shift(); | ||
| 666 | |||
| 667 | if (!defined($arg)) { | ||
| 668 | return ""; | ||
| 669 | } | ||
| 670 | |||
| 671 | # Get the hostname supplied. | ||
| 672 | return URI::URL->new($arg)->authority(); | ||
| 673 | } | ||
| 674 | |||
| 675 | # Helper function, designed to process all links found at a | ||
| 676 | # given URL, once the browser has been driven to that URL | ||
| 677 | # and has collected all corresponding links. The links are | ||
| 678 | # sorted in increasing order as determined by their score. | ||
| 679 | # | ||
| 680 | # When supplied with the array of URL strings, | ||
| 681 | # this function will categorize the corresponding URLs | ||
| 682 | # as follows: | ||
| 683 | # | ||
| 684 | # (Note: The terms "valid" and "invalid" are defined in | ||
| 685 | # the _validateLink() documentation.) | ||
| 686 | # | ||
| 687 | # "New" links are those we've never driven the browser to. | ||
| 688 | # "Old" links are those we've driven the browser to before. | ||
| 689 | # | ||
| 690 | # - If a link is new and "invalid", then it gets added to | ||
| 691 | # the 'links_ignored' hashtable. | ||
| 692 | # | ||
| 693 | # - If a link is old and "invalid", then it gets | ||
| 694 | # ignored. | ||
| 695 | # | ||
| 696 | # - If a link is old and "valid", then it gets ignored. | ||
| 697 | # | ||
| 698 | # - If a link is new and "valid", then we check to see if | ||
| 699 | # the referring URL's hostname[:port] and the link's | ||
| 700 | # hostname[:port] match. If they match, then the link | ||
| 701 | # is added to the 'relative_links_to_visit' hash. | ||
| 702 | # Otherwise, the link is added to the 'links_to_visit' | ||
| 703 | # hash. | ||
| 704 | # | ||
| 705 | # Inputs: HoneyClient::Agent::Driver::Browser object, | ||
| 706 | # hostname[:port] of referring URL, | ||
| 707 | # hash of URL strings and scores, the url is the key | ||
| 708 | # Outputs: HoneyClient::Agent::Driver::Browser object | ||
| 709 | sub _processLinks { | ||
| 710 | |||
| 711 | # Get the object state. | ||
| 712 | my $self = shift; | ||
| 713 | |||
| 714 | # Get the referrer and the corresponding arrays of links and scores. | ||
| 715 | my ($referrer, %links) = @_; | ||
| 716 | |||
| 717 | foreach my $url (keys %links) { | ||
| 718 | my $score = $links{$url}; | ||
| 719 | |||
| 720 | # Skip over any undefined links. | ||
| 721 | unless (defined($url)) { | ||
| 722 | next; | ||
| 723 | } | ||
| 724 | |||
| 725 | # Validate each link. | ||
| 726 | $url = $self->_validateLink($url); | ||
| 727 | |||
| 728 | if (!defined($url) or ($url eq "")) { | ||
| 729 | # If we get here, then the link is either invalid or | ||
| 730 | # already visited. In either case, skip to the next | ||
| 731 | # link. | ||
| 732 | next; | ||
| 733 | } | ||
| 734 | |||
| 735 | # Link is new and valid; go ahead and add to the appropriate | ||
| 736 | # hashtable. | ||
| 737 | |||
| 738 | # Extract the core hostname of the URL to visit. | ||
| 739 | # If $url is undef, then this function will return an empty string. | ||
| 740 | my $hostname = _extractHostname($url); | ||
| 741 | |||
| 742 | # If the referrer's hostname and the URL's hostname match... | ||
| 743 | if ($hostname eq $referrer) { | ||
| 744 | # Then add the URL to the 'relative_links_to_visit' hashtable, | ||
| 745 | # since we're visiting links that share the same hostname. | ||
| 746 | $self->relative_links_to_visit->{$url} = $score; | ||
| 747 | } else { | ||
| 748 | # Else, add the URL to the 'links_to_visit' hashtable, | ||
| 749 | # since we're visiting links that do NOT share the same hostname. | ||
| 750 | $self->links_to_visit->{$url} = $score; | ||
| 751 | } | ||
| 752 | } | ||
| 753 | |||
| 754 | # Return the modified object state. | ||
| 755 | return $self; | ||
| 756 | } | ||
| 757 | |||
| 758 | # Helper function designed to validate supplied links. | ||
| 759 | # | ||
| 760 | # When a link is provided as an argument: | ||
| 761 | # | ||
| 762 | # - The link is checked to make sure it has a valid | ||
| 763 | # HTTP or HTTPS prefix in the URL; any other link | ||
| 764 | # types are considered invalid. | ||
| 765 | # | ||
| 766 | # - The 'links_visited' history is checked; if the link | ||
| 767 | # already exists within the history, then it is considered | ||
| 768 | # invalid. | ||
| 769 | # | ||
| 770 | # If the link is valid, then it is returned. Otherwise, undef | ||
| 771 | # is returned for all invalid links. Also, all invalid links | ||
| 772 | # are added to the 'links_ignored' history -- if they're not | ||
| 773 | # already in the hashtable. | ||
| 774 | # | ||
| 775 | # Inputs: HoneyClient::Agent::Driver::Browser object, url to validate | ||
| 776 | # Outputs: url if valid, empty string if invalid | ||
| 777 | sub _validateLink { | ||
| 778 | |||
| 779 | # Get the object state. | ||
| 780 | my $self = shift; | ||
| 781 | |||
| 782 | # Get the supplied link. | ||
| 783 | my ($link) = @_; | ||
| 784 | |||
| 785 | # Strip off all anchors/fragments/bookmarks from within URLs by default. | ||
| 786 | # Note: RFC 3986 Section 3 guarantees that all fragments | ||
| 787 | # appear at the end of any URL. Keep in mind, that this stripping | ||
| 788 | # assumes we won't have any wierd corner cases, like: | ||
| 789 | # http://www.mitre.org/path/index.html#bookmark?arg=value | ||
| 790 | # ... where we would want to strip the bookmark, but keep the | ||
| 791 | # arg=value piece (which may not be a valid URL syntax, anyway). | ||
| 792 | my $url = URI::URL->new($link); | ||
| 793 | $url->fragment(undef); | ||
| 794 | # XXX: Do we need to clear the query() part, also? | ||
| 795 | $link = $url->canonical()->as_string(); | ||
| 796 | |||
| 797 | # First, check to see if the link is either an | ||
| 798 | # "http://" or "https://" URL. | ||
| 799 | unless (is_uri($link) && is_web_uri($link)) { | ||
| 800 | |||
| 801 | # The link is invalid, so we check to see if it's already | ||
| 802 | # in our 'links_ignored' history. | ||
| 803 | |||
| 804 | # Check if the 'links_ignored' history is not empty and | ||
| 805 | # already has our invalid link recorded. | ||
| 806 | unless (scalar(%{$self->links_ignored}) and | ||
| 807 | exists($self->links_ignored->{$link})) { | ||
| 808 | |||
| 809 | # The invalid link is brand new; add it to our list. | ||
| 810 | $self->links_ignored->{$link} = _getTimestamp(); | ||
| 811 | } | ||
| 812 | |||
| 813 | # The link is invalid, return an empty string. | ||
| 814 | return ""; | ||
| 815 | } | ||
| 816 | |||
| 817 | # Next, we check to see if we've already visited or ignored this | ||
| 818 | # link. Check if the 'links_visited' and 'links_ignored' histories | ||
| 819 | # are not empty and does not already have this valid link recorded. | ||
| 820 | if ((scalar(%{$self->links_visited}) and | ||
| 821 | exists($self->links_visited->{$link})) or | ||
| 822 | (scalar(%{$self->links_ignored}) and | ||
| 823 | exists($self->links_ignored->{$link}))) { | ||
| 824 | |||
| 825 | # Link is valid but already visited, so return undef. | ||
| 826 | return; | ||
| 827 | } | ||
| 828 | |||
| 829 | # If we haven't returned by now, then the link is considered | ||
| 830 | # valid and we need to visit it. | ||
| 831 | return $link; | ||
| 832 | } | ||
| 833 | |||
| 834 | # XXX: Is this really needed? | ||
| 835 | # Helper function designed to kill all instances of the driven | ||
| 836 | # application. | ||
| 837 | # | ||
| 838 | # Inputs: None | ||
| 839 | # Outputs: None | ||
| 840 | sub _killProcess { | ||
| 841 | |||
| 842 | # Get the object state. | ||
| 843 | my $self = shift; | ||
| 844 | |||
| 845 | # TODO: Make this more robust. | ||
| 846 | |||
| 847 | # This function will croak, if it ever tries to return an undefined | ||
| 848 | # object. | ||
| 849 | my $stub = getClientHandle(address => 'localhost', | ||
| 850 | namespace => 'HoneyClient::Agent'); | ||
| 851 | |||
| 852 | my $som = $stub->killProcess($self->process_name); | ||
| 853 | |||
| 854 | if (!$som->result) { | ||
| 855 | $LOG->warn("Failed to kill process: '" . $self->process_name . "'!"); | ||
| 856 | Carp::carp "Failed to kill process: '" . $self->process_name . "'!\n"; | ||
| 857 | } | ||
| 858 | } | ||
| 859 | |||
| 860 | ####################################################################### | ||
| 861 | # Public Methods Implemented # | ||
| 862 | ####################################################################### | ||
| 863 | |||
| 864 | =pod | ||
| 865 | |||
| 866 | =head1 METHODS IMPLEMENTED | ||
| 867 | |||
| 868 | The following functions have been implemented by the Browser driver. Many | ||
| 869 | of these methods were implementations of the parent Driver interface. | ||
| 870 | |||
| 871 | As such, the following code descriptions pertain to this particular | ||
| 872 | Driver implementation. For further information about the generic | ||
| 873 | Driver interface, see the L<HoneyClient::Agent::Driver> documentation. | ||
| 874 | |||
| 875 | =head2 HoneyClient::Agent::Driver::Browser->new($param => $value, ...) | ||
| 876 | |||
| 877 | =over 4 | ||
| 878 | |||
| 879 | Creates a new Browser driver object, which contains a hashtable | ||
| 880 | containing any of the supplied "param => value" arguments. | ||
| 881 | |||
| 882 | I<Inputs>: | ||
| 883 | B<$param> is an optional parameter variable. | ||
| 884 | B<$value> is $param's corresponding value. | ||
| 885 | |||
| 886 | Note: If any $param(s) are supplied, then an equal number of | ||
| 887 | corresponding $value(s) B<must> also be specified. | ||
| 888 | |||
| 889 | I<Output>: The instantiated Browser driver B<$object>, fully initialized. | ||
| 890 | |||
| 891 | =back | ||
| 892 | |||
| 893 | =begin testing | ||
| 894 | |||
| 895 | # XXX: Add this. | ||
| 896 | 1; | ||
| 897 | |||
| 898 | =end testing | ||
| 899 | |||
| 900 | =cut | ||
| 901 | |||
| 902 | sub new { | ||
| 903 | |||
| 904 | # - This function takes in an optional hashtable, | ||
| 905 | # that contains various key => 'value' configuration | ||
| 906 | # parameters. | ||
| 907 | # | ||
| 908 | # - For each parameter given, it overwrites any corresponding | ||
| 909 | # parameters specified within the default hashtable, %PARAMS, | ||
| 910 | # with custom entries that were given as parameters. | ||
| 911 | # | ||
| 912 | # - Finally, it returns a blessed instance of the | ||
| 913 | # merged hashtable, as an 'object'. | ||
| 914 | |||
| 915 | # Get the class name. | ||
| 916 | my $self = shift; | ||
| 917 | |||
| 918 | # Get the rest of the arguments, as a hashtable. | ||
| 919 | # Hash-based arguments are used, since HoneyClient::Util::SOAP is unable to handle | ||
| 920 | # hash references directly. Thus, flat hashtables are used throughout the code | ||
| 921 | # for consistency. | ||
| 922 | my %args = @_; | ||
| 923 | |||
| 924 | # Check to see if the class name is inherited or defined. | ||
| 925 | my $class = ref($self) || $self; | ||
| 926 | |||
| 927 | # Initialize default parameters. | ||
| 928 | my %params = %{dclone(\%PARAMS)}; | ||
| 929 | $self = $class->SUPER::new(); | ||
| 930 | @{$self}{keys %params} = values %params; | ||
| 931 | |||
| 932 | # Now, overwrite any default parameters that were redefined | ||
| 933 | # in the supplied arguments. | ||
| 934 | @{$self}{keys %args} = values %args; | ||
| 935 | |||
| 936 | # Now, assign our object the appropriate namespace. | ||
| 937 | bless $self, $class; | ||
| 938 | |||
| 939 | # Initialize any internal state variables. | ||
| 940 | # Reinitialize '_remaining_number_of_relative_links_to_visit', in case | ||
| 941 | # 'max_relative_links_to_visit' was overloaded. | ||
| 942 | $self->{_remaining_number_of_relative_links_to_visit} = | ||
| 943 | $self->{max_relative_links_to_visit}; | ||
| 944 | |||
| 945 | # Finally, return the blessed object. | ||
| 946 | return $self; | ||
| 947 | } | ||
| 948 | |||
| 949 | =pod | ||
| 950 | |||
| 951 | =head2 $object->drive(url => $url) | ||
| 952 | |||
| 953 | =over 4 | ||
| 954 | |||
| 955 | Drives an instance of the browser for one iteration, | ||
| 956 | navigating to the next URL and updating the driver's corresponding | ||
| 957 | internal hashtables accordingly. | ||
| 958 | |||
| 959 | For a description of which hashtable is consulted upon each | ||
| 960 | iteration of drive(), see the L<next_link_to_visit> documentation, in | ||
| 961 | the "DEFAULT PARAMETER LIST" section. | ||
| 962 | |||
| 963 | Once a drive() iteration has completed, the corresponding browser process | ||
| 964 | is terminated. Thus, each call to drive() invokes a new instance of the | ||
| 965 | browser. | ||
| 966 | |||
| 967 | I<Inputs>: | ||
| 968 | B<$url> is an optional argument, specifying the next immediate URL the browser must drive to. | ||
| 969 | |||
| 970 | I<Output>: The updated Browser driver B<$object>, containing state information | ||
| 971 | from driving the browser for one iteration. | ||
| 972 | |||
| 973 | B<Warning>: This method will B<croak> if the Browser driver object is B<unable> | ||
| 974 | to navigate to a new link, because its list of links to visit is empty and | ||
| 975 | no new URL was supplied. | ||
| 976 | |||
| 977 | =back | ||
| 978 | |||
| 979 | =begin testing | ||
| 980 | |||
| 981 | # XXX: Test this. | ||
| 982 | 1; | ||
| 983 | |||
| 984 | =end testing | ||
| 985 | |||
| 986 | =cut | ||
| 987 | |||
| 988 | sub drive { | ||
| 989 | |||
| 990 | # Extract arguments. | ||
| 991 | my ($self, %args) = @_; | ||
| 992 | |||
| 993 | # Sanity check: Make sure we've been fed an object. | ||
| 994 | unless (ref($self)) { | ||
| 995 | $LOG->error("Error: Function must be called in reference to a " . | ||
| 996 | __PACKAGE__ . "->new() object!"); | ||
| 997 | Carp::croak "Error: Function must be called in reference to a " . | ||
| 998 | __PACKAGE__ . "->new() object!\n"; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | # Sanity check, don't get the next link, if | ||
| 1002 | # we've been fed a url. | ||
| 1003 | my $argsExist = scalar(%args); | ||
| 1004 | if (!$argsExist || | ||
| 1005 | !exists($args{'url'}) || | ||
| 1006 | !defined($args{'url'})) { | ||
| 1007 | # Get the next URL from our hashtables. | ||
| 1008 | $args{'url'} = $self->_getNextLink(); | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | # Sanity check: Make sure our next URL is defined. | ||
| 1012 | unless (defined($args{'url'})) { | ||
| 1013 | $LOG->error("Error: Unable to drive browser - no links left to browse!"); | ||
| 1014 | Carp::croak "Error: Unable to drive browser - no links left to browse!\n"; | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | # Indicates how long we wait for each drive operation to complete, | ||
| 1018 | # before registering attempt as a failure. | ||
| 1019 | my $timeout : shared = $self->timeout(); | ||
| 1020 | |||
| 1021 | # Use LWP::UserAgent to get the desired $args{'url'} and associated content | ||
| 1022 | # TODO: Analyze all the options LWP::UserAgent provides, in case we've | ||
| 1023 | # missed something useful. | ||
| 1024 | # Create a new user agent. | ||
| 1025 | my $ua = LWP::UserAgent->new( | ||
| 1026 | timeout => $timeout, # Fixed timeout. | ||
| 1027 | #max_redirect => 0, # Ignore redirects. | ||
| 1028 | protocols_allowed => [ 'http', 'https' ], # Allow only web protocols. | ||
| 1029 | max_size => 1*1024*1024, # Don't get larger than 1MB for testing | ||
| 1030 | ); | ||
| 1031 | |||
| 1032 | # TODO: Look at the content type "text/html" on the response, to make this | ||
| 1033 | # a little better. | ||
| 1034 | # TODO: Set the default headers, to mimic a regular browser (if need be). | ||
| 1035 | # I'm thinking this could be set by IE/FF and passed via $args{'default_headers'} | ||
| 1036 | # as a HTTP::Headers object. | ||
| 1037 | $ua->default_header( 'Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,application/x-shockwave-flash,*/*;q=0.5' ); | ||
| 1038 | |||
| 1039 | my $response = $ua->request( | ||
| 1040 | HTTP::Request->new( | ||
| 1041 | GET => $args{'url'}, | ||
| 1042 | HTTP::Headers->new( | ||
| 1043 | # TODO: Add custom headers here? | ||
| 1044 | 'Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,application/x-shockwave-flash,*/*;q=0.5', | ||
| 1045 | ), | ||
| 1046 | ) | ||
| 1047 | ); | ||
| 1048 | |||
| 1049 | # Get the base url from the response | ||
| 1050 | my $base = $response->base; | ||
| 1051 | my $content = $response->content; | ||
| 1052 | my $type = $response->header('Content-Type'); | ||
| 1053 | |||
| 1054 | # Get the current time. | ||
| 1055 | my $timestamp = _getTimestamp(); | ||
| 1056 | |||
| 1057 | # Score the new links based on their surrounding HTML context. | ||
| 1058 | # If %scored_links is empty upon return, there are no links | ||
| 1059 | # and we will not perform any of the following code. | ||
| 1060 | my %scored_links; | ||
| 1061 | if ($content) { | ||
| 1062 | |||
| 1063 | # TODO: Need to call ActiveContent::isActiveContent() function. | ||
| 1064 | # Check to see if the content is Flash-based. | ||
| 1065 | if (($type eq "application/x-shockwave-flash") && | ||
| 1066 | ($self->{parse_active_content})) { | ||
| 1067 | |||
| 1068 | # Save content to a temp file on disk. | ||
| 1069 | my $tempFile = new File::Temp(SUFFIX => '.swf'); | ||
| 1070 | print $tempFile $content; | ||
| 1071 | $tempFile->close(); | ||
| 1072 | |||
| 1073 | %scored_links = HoneyClient::Agent::Driver::ActiveContent::process( | ||
| 1074 | file => $tempFile, | ||
| 1075 | url => $args{'url'}, | ||
| 1076 | ); | ||
| 1077 | |||
| 1078 | # Assume that all other content types are HTML-based. | ||
| 1079 | } else { | ||
| 1080 | # Call the link scoring function | ||
| 1081 | %scored_links = $self->_scoreLinks($base, $content); | ||
| 1082 | } | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | # Check to see if the request timed out. | ||
| 1086 | # TODO: Need better error detection. | ||
| 1087 | if (!%scored_links) { | ||
| 1088 | $self->links_timed_out->{$args{'url'}} = $timestamp; | ||
| 1089 | |||
| 1090 | # If we ignore any timed out links, then add them to our ignore | ||
| 1091 | # history as well. | ||
| 1092 | if ($self->ignore_links_timed_out) { | ||
| 1093 | $self->links_ignored->{$args{'url'}} = $timestamp; | ||
| 1094 | } | ||
| 1095 | } else { | ||
| 1096 | # If we've gotten this far, then we've successfully visited the URL. | ||
| 1097 | # Go ahead and add it to our 'links_visited' history. | ||
| 1098 | $self->links_visited->{$args{'url'}} = $timestamp; | ||
| 1099 | |||
| 1100 | # Add all links found on this page to our sorted queues. | ||
| 1101 | # This function modifies the $self object internally and its | ||
| 1102 | # returned content does not need to be checked. | ||
| 1103 | $self->_processLinks(_extractHostname($args{'url'}), %scored_links); | ||
| 1104 | } | ||
| 1105 | |||
| 1106 | # Check our internal relative links counter. | ||
| 1107 | if ($self->_remaining_number_of_relative_links_to_visit == 1) { | ||
| 1108 | |||
| 1109 | # XXX: Do we need this message in here? | ||
| 1110 | $LOG->info("Resetting relative links to visit counter."); | ||
| 1111 | |||
| 1112 | # The counter has reached one, so drop all other relative links | ||
| 1113 | # found, to force the driver to go to a new website. | ||
| 1114 | $self->{relative_links_to_visit} = { }; | ||
| 1115 | |||
| 1116 | # Reset the counter. | ||
| 1117 | $self->{_remaining_number_of_relative_links_to_visit} = | ||
| 1118 | $self->max_relative_links_to_visit; | ||
| 1119 | } elsif ($self->_remaining_number_of_relative_links_to_visit > 1) { | ||
| 1120 | |||
| 1121 | # The counter is positive, so decrement it. | ||
| 1122 | $self->{_remaining_number_of_relative_links_to_visit}--; | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | # Return the modified object state. | ||
| 1126 | return $self; | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | =pod | ||
| 1130 | |||
| 1131 | =head2 $object->getNextLink() | ||
| 1132 | |||
| 1133 | =over 4 | ||
| 1134 | |||
| 1135 | Returns the next URL that the browser will navigate to, upon the next | ||
| 1136 | subsequent call to the B<$object>'s drive() method. | ||
| 1137 | |||
| 1138 | I<Output>: The next URL that the browser will be driven to. The returned | ||
| 1139 | data may be undef, if the Browser driver is finished and there are no links | ||
| 1140 | left to navigate to. | ||
| 1141 | |||
| 1142 | B<Note>: This function is B<deprecated>. $object->next() should be used | ||
| 1143 | instead. | ||
| 1144 | |||
| 1145 | =back | ||
| 1146 | |||
| 1147 | =begin testing | ||
| 1148 | |||
| 1149 | # XXX: Test this. | ||
| 1150 | 1; | ||
| 1151 | |||
| 1152 | =end testing | ||
| 1153 | |||
| 1154 | =cut | ||
| 1155 | |||
| 1156 | sub getNextLink { | ||
| 1157 | |||
| 1158 | # Get the object state. | ||
| 1159 | my $self = shift; | ||
| 1160 | |||
| 1161 | # Sanity check: Make sure we've been fed an object. | ||
| 1162 | unless (ref($self)) { | ||
| 1163 | $LOG->error("Error: Function must be called in reference to a " . | ||
| 1164 | __PACKAGE__ . "->new() object!"); | ||
| 1165 | Carp::croak "Error: Function must be called in reference to a " . | ||
| 1166 | __PACKAGE__ . "->new() object!\n"; | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | # Set the link to find as undef, initially. | ||
| 1170 | my $link = undef; | ||
| 1171 | |||
| 1172 | # Get the next link. | ||
| 1173 | $link = $self->_getNextLink(); | ||
| 1174 | |||
| 1175 | # Before returning the link, be sure to set the | ||
| 1176 | # next link scalar, so that our object consistently | ||
| 1177 | # returns the same next link via getNextLink(). | ||
| 1178 | $self->{next_link_to_visit} = $link; | ||
| 1179 | |||
| 1180 | # Return this link found. | ||
| 1181 | return $link; | ||
| 1182 | } | ||
| 1183 | |||
| 1184 | =pod | ||
| 1185 | |||
| 1186 | =head2 $object->next() | ||
| 1187 | |||
| 1188 | =over 4 | ||
| 1189 | |||
| 1190 | Returns the next set of server hostnames and/or IP addresses that the | ||
| 1191 | browser will contact, upon the next subsequent call to the B<$object>'s | ||
| 1192 | drive() method. | ||
| 1193 | |||
| 1194 | Specifically, the returned data is a reference to a hashtable, containing | ||
| 1195 | detailed information about which resources, hostnames, IPs, protocols, and | ||
| 1196 | ports that the browser will contact upon the next drive() iteration. | ||
| 1197 | |||
| 1198 | Here is an example of such returned data: | ||
| 1199 | |||
| 1200 | $hashref = { | ||
| 1201 | |||
| 1202 | # The set of servers that the driver will contact upon | ||
| 1203 | # the next drive() operation. | ||
| 1204 | targets => { | ||
| 1205 | # The application will contact 'site.com' using | ||
| 1206 | # TCP ports 80 and 81. | ||
| 1207 | 'site.com' => { | ||
| 1208 | 'tcp' => [ 80, 81 ], | ||
| 1209 | }, | ||
| 1210 | |||
| 1211 | # The application will contact '192.168.1.1' using | ||
| 1212 | # UDP ports 53 and 123. | ||
| 1213 | '192.168.1.1' => { | ||
| 1214 | 'udp' => [ 53, 123 ], | ||
| 1215 | }, | ||
| 1216 | |||
| 1217 | # Or, more generically: | ||
| 1218 | 'hostname_or_IP' => { | ||
| 1219 | 'protocol_type' => [ portnumbers_as_list ], | ||
| 1220 | }, | ||
| 1221 | }, | ||
| 1222 | |||
| 1223 | # The set of resources that the driver will operate upon | ||
| 1224 | # the next drive() operation. | ||
| 1225 | resources => { | ||
| 1226 | 'http://www.mitre.org/' => 1, | ||
| 1227 | }, | ||
| 1228 | }; | ||
| 1229 | |||
| 1230 | B<Note>: For this implementation of the Driver interface, | ||
| 1231 | unless getNextLink() returns undef, the returned hashtable | ||
| 1232 | from this method will B<always> contain only B<one> hostname | ||
| 1233 | or IP address. Within this single entry, the protocol type | ||
| 1234 | is B<always guaranteed> to be B<TCP>, specifying a | ||
| 1235 | B<single port>. | ||
| 1236 | |||
| 1237 | I<Output>: The aforementioned B<$hashref> containing the next set of | ||
| 1238 | resources that the back-end application will attempt to contact upon | ||
| 1239 | the next drive() iteration. Returns undef values for both 'targets' | ||
| 1240 | and 'resources' keys, if getNextLink() also returns undef. | ||
| 1241 | |||
| 1242 | # XXX: Resolve this, per parent Driver description. | ||
| 1243 | |||
| 1244 | =back | ||
| 1245 | |||
| 1246 | =begin testing | ||
| 1247 | |||
| 1248 | # XXX: Test this. | ||
| 1249 | 1; | ||
| 1250 | |||
| 1251 | =end testing | ||
| 1252 | |||
| 1253 | =cut | ||
| 1254 | |||
| 1255 | sub next { | ||
| 1256 | # Get the object state. | ||
| 1257 | my $self = shift; | ||
| 1258 | |||
| 1259 | # Sanity check: Make sure we've been fed an object. | ||
| 1260 | unless (ref($self)) { | ||
| 1261 | $LOG->error("Error: Function must be called in reference to a " . | ||
| 1262 | __PACKAGE__ . "->new() object!"); | ||
| 1263 | Carp::croak "Error: Function must be called in reference to a " . | ||
| 1264 | __PACKAGE__ . "->new() object!\n"; | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | # Okay, get the next URL. | ||
| 1268 | my $nextURL = $self->getNextLink(); | ||
| 1269 | |||
| 1270 | # First, construct an empty hashtable. | ||
| 1271 | my $nextSite = { | ||
| 1272 | targets => undef, | ||
| 1273 | resources => undef, | ||
| 1274 | }; | ||
| 1275 | |||
| 1276 | # Sanity check: Make sure our next URL is defined. | ||
| 1277 | unless(defined($nextURL)) { | ||
| 1278 | return $nextSite; | ||
| 1279 | } | ||
| 1280 | |||
| 1281 | # Now, extract the corresponding hostname[:port] | ||
| 1282 | my $hostnamePort = _extractHostname($nextURL); | ||
| 1283 | |||
| 1284 | # Now, find the corresponding hostname or IP address. | ||
| 1285 | my $hostname = $hostnamePort; | ||
| 1286 | $hostname =~ s/:.*$//; | ||
| 1287 | |||
| 1288 | # Check to see if a TCP port number was provided. | ||
| 1289 | my $port = $hostnamePort; | ||
| 1290 | $port =~ s/$hostname:?//; | ||
| 1291 | |||
| 1292 | # If the port was empty, then derive the proper port number | ||
| 1293 | # to use, based upon whether HTTP or HTTPS was supplied by | ||
| 1294 | # the URL. | ||
| 1295 | if ($port eq '') { | ||
| 1296 | if ($nextURL =~ /^https.*/i) { | ||
| 1297 | $port = 443; | ||
| 1298 | } else { # Assume HTTP, since it's a valid URL. | ||
| 1299 | $port = 80; | ||
| 1300 | } | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | # Finally, construct the corresponding hash reference. | ||
| 1304 | $nextSite = { | ||
| 1305 | targets => { | ||
| 1306 | $hostname => { | ||
| 1307 | tcp => [ $port ], | ||
| 1308 | }, | ||
| 1309 | }, | ||
| 1310 | resources => { | ||
| 1311 | $nextURL => 1, | ||
| 1312 | }, | ||
| 1313 | }; | ||
| 1314 | |||
| 1315 | return $nextSite; | ||
| 1316 | } | ||
| 1317 | |||
| 1318 | =pod | ||
| 1319 | |||
| 1320 | =head2 _scoreLinks() | ||
| 1321 | |||
| 1322 | =over 4 | ||
| 1323 | |||
| 1324 | The _scoreLinks helper function takes a scalar which is the base url for | ||
| 1325 | the web page, a scalar which holds the content of the page (HTML), and a | ||
| 1326 | hash which contain the good and bad words. | ||
| 1327 | |||
| 1328 | This function will calculate the "popularity" scores of the links. | ||
| 1329 | The function returns a hash which is keyed on the _absolute_ url | ||
| 1330 | and contains the value of the score. | ||
| 1331 | |||
| 1332 | I<Output>: The populated %scored_links hash if the page is not empty. An empty | ||
| 1333 | hash otherwise. | ||
| 1334 | |||
| 1335 | For example, if your raw HTML content is $content, and the base url is | ||
| 1336 | $base you would use the following call to this function. | ||
| 1337 | |||
| 1338 | if ($content) { | ||
| 1339 | # Call the link scoring function | ||
| 1340 | %scored_links = $self->_scoreLinks($base, $content); | ||
| 1341 | } | ||
| 1342 | |||
| 1343 | =back | ||
| 1344 | |||
| 1345 | =begin testing | ||
| 1346 | |||
| 1347 | # XXX: Test this. | ||
| 1348 | 1; | ||
| 1349 | |||
| 1350 | =end testing | ||
| 1351 | |||
| 1352 | =cut | ||
| 1353 | |||
| 1354 | sub _scoreLinks { | ||
| 1355 | # Extract arguments. | ||
| 1356 | my ($self, $base, $content) = @_; | ||
| 1357 | my %links = (); | ||
| 1358 | my $url = undef; | ||
| 1359 | |||
| 1360 | # If the page is blank, there is no point trying to parse it | ||
| 1361 | if (!$content) { | ||
| 1362 | return %links; | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | # Begin to scour the HTML content for tags, parsing attributes and text. | ||
| 1366 | # Find any tags which could potentially have URLs of interest, either for | ||
| 1367 | # scoring or for punching corresponding holes in the firewall. | ||
| 1368 | |||
| 1369 | # Create a new parser. | ||
| 1370 | my $parser = HTML::TokeParser->new(\$content); | ||
| 1371 | |||
| 1372 | # Don't try to textify anything. | ||
| 1373 | $parser->{textify} = { }; | ||
| 1374 | |||
| 1375 | # List of HTML tags to look for. | ||
| 1376 | # TODO: Expose this list to the global configuration file. | ||
| 1377 | # TODO: Make sure all entries are lowercase. | ||
| 1378 | my $tags = { | ||
| 1379 | 'iframe' => 1, | ||
| 1380 | 'a' => 1, | ||
| 1381 | 'link' => 1, | ||
| 1382 | 'img' => 1, | ||
| 1383 | 'object' => 1, | ||
| 1384 | 'embed' => 1, | ||
| 1385 | 'script' => 1, | ||
| 1386 | }; | ||
| 1387 | |||
| 1388 | # List of HTML attributes to look for. | ||
| 1389 | # TODO: Expose this list to the global configuration file. | ||
| 1390 | # TODO: Make sure all entries are lowercase. | ||
| 1391 | my $attrs = { | ||
| 1392 | 'href' => 1, | ||
| 1393 | 'src' => 1, | ||
| 1394 | 'usemap' => 1, | ||
| 1395 | 'classid' => 1, | ||
| 1396 | 'data' => 1, | ||
| 1397 | }; | ||
| 1398 | |||
| 1399 | # Helper stack (an array), used to determine the URL of the | ||
| 1400 | # currently encapsulated <A></A> block. | ||
| 1401 | my @link_stack = (); | ||
| 1402 | |||
| 1403 | while (my $token = $parser->get_token()) { | ||
| 1404 | |||
| 1405 | # The type of tag found ("S" = starting, "E" = ending). | ||
| 1406 | my $tag_type = $token->[0]; | ||
| 1407 | |||
| 1408 | # The tag name found. | ||
| 1409 | my $tag = $token->[1]; | ||
| 1410 | |||
| 1411 | # The hashtable of attributes found in this tag. | ||
| 1412 | my $attrib_hash = $token->[2]; | ||
| 1413 | |||
| 1414 | # We're only interested in the start and end HTML tags. | ||
| 1415 | # Ignore all others. | ||
| 1416 | unless (($tag_type eq "S") || ($tag_type eq "E")) { | ||
| 1417 | next; | ||
| 1418 | } | ||
| 1419 | |||
| 1420 | # We're only interested in the specified HTML tags. | ||
| 1421 | # Ignore all others. | ||
| 1422 | unless (exists($tags->{$tag})) { | ||
| 1423 | next; | ||
| 1424 | } | ||
| 1425 | |||
| 1426 | # Check if we have an ending tag. | ||
| 1427 | if ($tag_type eq "E") { | ||
| 1428 | |||
| 1429 | # If it's an ending </A> tag, then pop the last | ||
| 1430 | # url off the link_stack. | ||
| 1431 | if ($tag eq "a") { | ||
| 1432 | pop(@link_stack); | ||
| 1433 | } | ||
| 1434 | |||
| 1435 | # Don't bother parsing ending tags any further. | ||
| 1436 | next; | ||
| 1437 | } | ||
| 1438 | |||
| 1439 | # We only pay attention to starting <IMG> tags, if we're inside | ||
| 1440 | # a nested link. | ||
| 1441 | if (($tag eq "img") && (scalar(@link_stack) <= 0)) { | ||
| 1442 | next; | ||
| 1443 | } | ||
| 1444 | |||
| 1445 | # Set the score to zero. | ||
| 1446 | my $score = 0; | ||
| 1447 | |||
| 1448 | # Try to extract any possible URL from any attribute. | ||
| 1449 | # Stop when we first encounter one. | ||
| 1450 | my $counter = 0; | ||
| 1451 | my @attrs_list = keys(%{$attrs}); | ||
| 1452 | while (!defined($url) && ($counter < scalar(@attrs_list))) { | ||
| 1453 | if (exists($attrib_hash->{$attrs_list[$counter]})) { | ||
| 1454 | $url = $attrib_hash->{$attrs_list[$counter]}; | ||
| 1455 | } | ||
| 1456 | $counter++; | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | # If it's a starting <A> tag, then push the URL onto the | ||
| 1460 | # link_stack. | ||
| 1461 | # Also, don't increment, when tag is atomic, like: | ||
| 1462 | # <a href="http://www.foo.com" /> | ||
| 1463 | if (($tag_type eq "S") && ($tag eq "a") && | ||
| 1464 | !exists($attrib_hash->{'/'})) { | ||
| 1465 | push(@link_stack, $url); | ||
| 1466 | } | ||
| 1467 | |||
| 1468 | # Skip if no URL was found. | ||
| 1469 | unless (defined($url)) { | ||
| 1470 | next; | ||
| 1471 | } | ||
| 1472 | |||
| 1473 | # TODO: Expose these values as global options. | ||
| 1474 | # Some programmatic values | ||
| 1475 | my $min_text_length = 6; | ||
| 1476 | my $max_text_length = 20; | ||
| 1477 | my $image_bonus = 50; | ||
| 1478 | my $default_display_size = 1024 * 768; | ||
| 1479 | my $word_value = 6; | ||
| 1480 | |||
| 1481 | # We have to make this an absolute url (if it's not) | ||
| 1482 | # before using it as a key in the %links hash. | ||
| 1483 | $url = url($url, $base)->abs; | ||
| 1484 | |||
| 1485 | # Begin scoring the link based on surrounding context | ||
| 1486 | # This can be improved/customized in many different ways. | ||
| 1487 | # Our implementation is only one possible way to assign | ||
| 1488 | # values to the context elements. | ||
| 1489 | |||
| 1490 | my $width = undef; | ||
| 1491 | my $height = undef; | ||
| 1492 | |||
| 1493 | # Score the size of an object/image based on width and height. | ||
| 1494 | if (exists($attrib_hash->{width})) { | ||
| 1495 | $width = int($attrib_hash->{width}); | ||
| 1496 | } | ||
| 1497 | if (exists($attrib_hash->{height})) { | ||
| 1498 | $height = int($attrib_hash->{height}); | ||
| 1499 | } | ||
| 1500 | if (defined($width) && defined($height)) { | ||
| 1501 | $score += int(($width*$height)/($default_display_size)*100); | ||
| 1502 | } elsif (defined($width)) { | ||
| 1503 | $score += int($width/10); | ||
| 1504 | } elsif (defined($height)) { | ||
| 1505 | $score += int($height/10); | ||
| 1506 | } | ||
| 1507 | |||
| 1508 | # Extract any link text. | ||
| 1509 | my $text = $parser->get_trimmed_text(); | ||
| 1510 | |||
| 1511 | # Score length of link text. These are arbitrary lengths, but | ||
| 1512 | # the reasoning is that really short text links are not too | ||
| 1513 | # visible (we are excluding image links from this criteria), | ||
| 1514 | # and really long text would be weird or abnormal to the human | ||
| 1515 | # web surfer. | ||
| 1516 | if (($tag ne "img") && | ||
| 1517 | (length($text) > $min_text_length) && | ||
| 1518 | (length($text) < $max_text_length)) { | ||
| 1519 | $score += length($text); | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | # Score the image content, if it exists | ||
| 1523 | # We score the size proportional to a 1024 X 768 display | ||
| 1524 | # Image bonus | ||
| 1525 | if ($tag eq "img") { | ||
| 1526 | $score += $image_bonus; | ||
| 1527 | } | ||
| 1528 | |||
| 1529 | # Good word bonus | ||
| 1530 | foreach (@{$self->{'positive_words'}}) { | ||
| 1531 | if ($text =~ /$_/i) { | ||
| 1532 | $score += $word_value; | ||
| 1533 | } | ||
| 1534 | } | ||
| 1535 | |||
| 1536 | # Bad word penalty | ||
| 1537 | foreach (@{$self->{'negative_words'}}) { | ||
| 1538 | if ($text =~ /$_/i) { | ||
| 1539 | $score -= $word_value; | ||
| 1540 | } | ||
| 1541 | } | ||
| 1542 | |||
| 1543 | # Put it in the return value hash and zero the score. | ||
| 1544 | $links{$url} = $score; | ||
| 1545 | |||
| 1546 | # If we're dealing with an <IMG> tag, then we must | ||
| 1547 | # be within a <A></A> tag block. If so, then our | ||
| 1548 | # score should also modify the value of the previously | ||
| 1549 | # found <A HREF="this link">. | ||
| 1550 | |||
| 1551 | # Get the enclosed link's URL. | ||
| 1552 | if (($tag eq "img") && (scalar(@link_stack) > 0)) { | ||
| 1553 | my $parent_url = $link_stack[-1]; | ||
| 1554 | # If defined, then adjust the parent URL's score, too. | ||
| 1555 | if (defined($parent_url)) { | ||
| 1556 | $links{$parent_url} += $score; | ||
| 1557 | } | ||
| 1558 | } | ||
| 1559 | |||
| 1560 | $url = undef; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | return %links; | ||
| 1564 | } | ||
| 1565 | |||
| 1566 | =pod | ||
| 1567 | |||
| 1568 | =head2 $object->isFinished() | ||
| 1569 | |||
| 1570 | =over 4 | ||
| 1571 | |||
| 1572 | Indicates if the Browser driver B<$object> has driven the browser | ||
| 1573 | process to all possible links it has found within its hashtables | ||
| 1574 | and is unable to navigate the browser further without additional, external | ||
| 1575 | input. | ||
| 1576 | |||
| 1577 | I<Output>: True if the Browser driver B<$object> is finished, false otherwise. | ||
| 1578 | |||
| 1579 | B<Note>: Additional links can be fed to this Browser driver at any time, by | ||
| 1580 | simply adding new hashtable entries to the B<links_to_visit> hashtable | ||
| 1581 | within the B<$object>. | ||
| 1582 | |||
| 1583 | For example, if you wanted to add the URL "http://www.mitre.org" | ||
| 1584 | to the Browser driver B<$object>, simply use the following code: | ||
| 1585 | |||
| 1586 | $object->{links_to_visit}->{'http://www.mitre.org'} = 1; | ||
| 1587 | |||
| 1588 | =back | ||
| 1589 | |||
| 1590 | =begin testing | ||
| 1591 | |||
| 1592 | # XXX: Test this. | ||
| 1593 | 1; | ||
| 1594 | |||
| 1595 | =end testing | ||
| 1596 | |||
| 1597 | =cut | ||
| 1598 | |||
| 1599 | sub isFinished { | ||
| 1600 | |||
| 1601 | # Get the object state. | ||
| 1602 | my $self = shift; | ||
| 1603 | |||
| 1604 | # Sanity check: Make sure we've been fed an object. | ||
| 1605 | unless (ref($self)) { | ||
| 1606 | $LOG->error("Error: Function must be called in reference to a " . | ||
| 1607 | __PACKAGE__ . "->new() object!"); | ||
| 1608 | Carp::croak "Error: Function must be called in reference to a " . | ||
| 1609 | __PACKAGE__ . "->new() object!\n"; | ||
| 1610 | } | ||
| 1611 | |||
| 1612 | # Return whether or not all '*_to_visit' variables/hashtables are | ||
| 1613 | # empty. | ||
| 1614 | return (!(defined($self->next_link_to_visit) or | ||
| 1615 | scalar(%{$self->relative_links_to_visit}) or | ||
| 1616 | scalar(%{$self->links_to_visit}))) | ||
| 1617 | |||
| 1618 | } | ||
| 1619 | |||
| 1620 | =pod | ||
| 1621 | |||
| 1622 | =head2 $object->status() | ||
| 1623 | |||
| 1624 | =over 4 | ||
| 1625 | |||
| 1626 | Returns the current status of the Browser driver B<$object>, as it's state | ||
| 1627 | exists, between subsequent calls to $object->driver(). | ||
| 1628 | |||
| 1629 | Specifically, the data returned is a reference to a hashtable, | ||
| 1630 | containing specific statistical information about the status | ||
| 1631 | of the Browser driver's progress, between iterations of driving the | ||
| 1632 | browser process. | ||
| 1633 | |||
| 1634 | The following is an example hashtable, containing all the | ||
| 1635 | (key => value) pairs that would exist in the output. | ||
| 1636 | |||
| 1637 | $hashref = { | ||
| 1638 | 'relative_links_remaining' => 10, # Number of URLs left to | ||
| 1639 | # process, at a given site. | ||
| 1640 | 'links_remaining' => 56, # Number of URLs left to | ||
| 1641 | # process, for all sites. | ||
| 1642 | 'links_processed' => 44, # Number of URLs processed. | ||
| 1643 | 'links_total' => 100, # Total number of URLs given. | ||
| 1644 | 'percent_complete' => '44.00%', # Percent complete, | ||
| 1645 | # (processed / total). | ||
| 1646 | }; | ||
| 1647 | |||
| 1648 | I<Output>: A corresponding B<$hashref>, containing statistical information | ||
| 1649 | about the Browser driver's progress, as previously mentioned. | ||
| 1650 | |||
| 1651 | # XXX: Resolve this, per parent Driver description. | ||
| 1652 | |||
| 1653 | =back | ||
| 1654 | |||
| 1655 | =begin testing | ||
| 1656 | |||
| 1657 | # XXX: Test this. | ||
| 1658 | 1; | ||
| 1659 | |||
| 1660 | =end testing | ||
| 1661 | |||
| 1662 | =cut | ||
| 1663 | |||
| 1664 | sub status { | ||
| 1665 | |||
| 1666 | # Get the object state. | ||
| 1667 | my $self = shift; | ||
| 1668 | |||
| 1669 | # Sanity check: Make sure we've been fed an object. | ||
| 1670 | unless (ref($self)) { | ||
| 1671 | $LOG->error("Error: Function must be called in reference to a " . | ||
| 1672 | __PACKAGE__ . "->new() object!"); | ||
| 1673 | Carp::croak "Error: Function must be called in reference to a " . | ||
| 1674 | __PACKAGE__ . "->new() object!\n"; | ||
| 1675 | } | ||
| 1676 | |||
| 1677 | # Construct a new status hashtable. | ||
| 1678 | my $status = { }; | ||
| 1679 | |||
| 1680 | # Set the total number of links processed. | ||
| 1681 | $status->{links_processed} = scalar(keys(%{$self->links_visited})) + | ||
| 1682 | scalar(keys(%{$self->links_timed_out})) + | ||
| 1683 | scalar(keys(%{$self->links_ignored})); | ||
| 1684 | |||
| 1685 | # Figure out if the next_link_to_visit is set. | ||
| 1686 | my $next_link_is_set = 0; | ||
| 1687 | if (defined($self->next_link_to_visit)) { | ||
| 1688 | $next_link_is_set = 1; | ||
| 1689 | } | ||
| 1690 | |||
| 1691 | # Set the number of relative links to process. | ||
| 1692 | $status->{relative_links_remaining} = scalar(keys(%{$self->relative_links_to_visit})) + | ||
| 1693 | $next_link_is_set; | ||
| 1694 | |||
| 1695 | # Figure out how many total links are left to process. | ||
| 1696 | $status->{links_remaining} = $status->{relative_links_remaining} + | ||
| 1697 | scalar(keys(%{$self->links_to_visit})); | ||
| 1698 | |||
| 1699 | # Set the total number of links in the object's state. | ||
| 1700 | $status->{links_total} = $status->{links_processed} + | ||
| 1701 | $status->{links_remaining}; | ||
| 1702 | |||
| 1703 | # Get the percentage of links complete. | ||
| 1704 | # Sanity check: Avoid divide by zero. | ||
| 1705 | if ($status->{links_total} <= 0) { | ||
| 1706 | $status->{links_total} = 1; | ||
| 1707 | } | ||
| 1708 | $status->{percent_complete} = sprintf("%.2f%%", (($status->{links_processed} + 0.00) / | ||
| 1709 | ($status->{links_total} + 0.00)) * 100.00); | ||
| 1710 | |||
| 1711 | # Return status. | ||
| 1712 | return $status; | ||
| 1713 | } | ||
| 1714 | |||
| 1715 | ####################################################################### | ||
| 1716 | |||
| 1717 | 1; | ||
| 1718 | |||
| 1719 | ####################################################################### | ||
| 1720 | # Additional Module Documentation # | ||
| 1721 | ####################################################################### | ||
| 1722 | |||
| 1723 | __END__ |