| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 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 1.0. |
|---|
| 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 |
=head1 DESCRIPTION |
|---|
| 90 |
|
|---|
| 91 |
This library allows the Agent module to drive an instance of any broswer, |
|---|
| 92 |
running inside the HoneyClient VM. The purpose of this module is to |
|---|
| 93 |
programmatically navigate the browser to different websites, in order to |
|---|
| 94 |
become purposefully infected with new malware. |
|---|
| 95 |
|
|---|
| 96 |
This module is object-oriented in design, retaining all state information |
|---|
| 97 |
within itself for easy access. A specific browser class must inherit from |
|---|
| 98 |
Browser. |
|---|
| 99 |
|
|---|
| 100 |
Fundamentally, the browser driver is initialized with a set of absolute URLs |
|---|
| 101 |
for the browser to drive to. Upon visiting each URL, the driver collects |
|---|
| 102 |
any B<new> links found and will attempt to drive the browser to each |
|---|
| 103 |
valid URL upon subsequent iterations of work. |
|---|
| 104 |
|
|---|
| 105 |
For each top-level URL given, the driver will attempt to process all |
|---|
| 106 |
corresponding links that are hosted on the same server, in order to |
|---|
| 107 |
simulate a complete 'spider' of each server. B<However>, because |
|---|
| 108 |
URLs are added and removed from hashtables, the order of which URLs |
|---|
| 109 |
are processed B<cannot be guaranteed nor maintained across subsequent |
|---|
| 110 |
iterations of work>. |
|---|
| 111 |
|
|---|
| 112 |
This means that the browser driver will try to visit all links shared by a |
|---|
| 113 |
common server in random order before moving on to drive to other, |
|---|
| 114 |
external links in a random fashion. B<However>, this cannot be |
|---|
| 115 |
guaranteed, as additional links from the same server may be found |
|---|
| 116 |
later, after processing the contents of an external link. |
|---|
| 117 |
|
|---|
| 118 |
As the browser driver navigates the browser to each link, it |
|---|
| 119 |
maintains a set of hashtables that record when valid links were |
|---|
| 120 |
visited (see L<links_visited>); when invalid links were found |
|---|
| 121 |
(see L<links_ignored>); and when the browser attempted to visit |
|---|
| 122 |
a link but the operation timed out (see L<links_timed_out>). |
|---|
| 123 |
By maintaining this internal history, the driver will B<never> |
|---|
| 124 |
navigate the browser to the same link twice. |
|---|
| 125 |
|
|---|
| 126 |
Lastly, it is highly recommended that for each driver B<$object>, |
|---|
| 127 |
one should call $object->isFinished() prior to making a subsequent |
|---|
| 128 |
call to $object->drive(), in order to verify that the driver has |
|---|
| 129 |
not exhausted its set of links to visit. Otherwise, if |
|---|
| 130 |
$object->drive() is called with an empty set of links to visit, |
|---|
| 131 |
the corresponding operation will B<croak>. |
|---|
| 132 |
|
|---|
| 133 |
=cut |
|---|
| 134 |
|
|---|
| 135 |
package HoneyClient::Agent::Driver::Browser; |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
use strict; |
|---|
| 140 |
use warnings; |
|---|
| 141 |
use Config; |
|---|
| 142 |
use Carp (); |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
use sigtrap qw(die untrapped normal-signals error-signals); |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
BEGIN { |
|---|
| 152 |
|
|---|
| 153 |
require Exporter; |
|---|
| 154 |
our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION); |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
$VERSION = 0.9; |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
use HoneyClient::Agent::Driver; |
|---|
| 161 |
|
|---|
| 162 |
@ISA = qw(Exporter HoneyClient::Agent::Driver); |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
@EXPORT = qw(); |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
%EXPORT_TAGS = ( |
|---|
| 184 |
'all' => [ qw() ], |
|---|
| 185 |
); |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
$SIG{PIPE} = 'IGNORE'; |
|---|
| 197 |
} |
|---|
| 198 |
our (@EXPORT_OK, $VERSION); |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
use HoneyClient::Util::Config qw(getVar); |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
use DateTime::HiRes; |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
use Time::HiRes qw(sleep); |
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
use Storable qw(dclone); |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
use threads; |
|---|
| 219 |
|
|---|
| 220 |
use threads::shared; |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
use HoneyClient::Util::SOAP qw(getClientHandle); |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
use LWP::UserAgent; |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
use HTTP::Request::Common; |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
use HTML::LinkExtor; |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
use URI::URL; |
|---|
| 236 |
|
|---|
| 237 |
=pod |
|---|
| 238 |
|
|---|
| 239 |
=head1 DEFAULT PARAMETER LIST |
|---|
| 240 |
|
|---|
| 241 |
When a Browser B<$object> is instantiated using the B<new()> function, |
|---|
| 242 |
the following parameters are supplied default values. Each value |
|---|
| 243 |
can be overridden by specifying the new (key => value) pair into the |
|---|
| 244 |
B<new()> function, as arguments. |
|---|
| 245 |
|
|---|
| 246 |
Furthermore, as each parameter is initialized, each can be individually |
|---|
| 247 |
retrieved and set at any time, using the following syntax: |
|---|
| 248 |
|
|---|
| 249 |
my $value = $object->{key}; # Gets key's value. |
|---|
| 250 |
$object->{key} = $value; # Sets key's value. |
|---|
| 251 |
|
|---|
| 252 |
=head2 links_to_visit |
|---|
| 253 |
|
|---|
| 254 |
=over 4 |
|---|
| 255 |
|
|---|
| 256 |
This parameter is a hashtable of fully qualified URLs for the browser |
|---|
| 257 |
to visit. Specifically, each 'key' corresponds to an absolute URL |
|---|
| 258 |
and the 'value' is always 1. |
|---|
| 259 |
|
|---|
| 260 |
=back |
|---|
| 261 |
|
|---|
| 262 |
=head2 links_visited |
|---|
| 263 |
|
|---|
| 264 |
=over 4 |
|---|
| 265 |
|
|---|
| 266 |
This parameter is a hashtable of fully qualified URLs that the |
|---|
| 267 |
browser has already visited. Specifically, each 'key' corresponds |
|---|
| 268 |
to an absolute URL and the 'value' is a string representing the |
|---|
| 269 |
date and time of when the link was visited. |
|---|
| 270 |
|
|---|
| 271 |
B<Note>: See internal documentation of _getTimestamp() for the |
|---|
| 272 |
corresponding date/time format of each value. |
|---|
| 273 |
|
|---|
| 274 |
=back |
|---|
| 275 |
|
|---|
| 276 |
=head2 links_ignored |
|---|
| 277 |
|
|---|
| 278 |
=over 4 |
|---|
| 279 |
|
|---|
| 280 |
This parameter is a hashtable of fully qualified URLs that the browser |
|---|
| 281 |
has found during its link traversal process, but the browser could not |
|---|
| 282 |
access the link. |
|---|
| 283 |
|
|---|
| 284 |
Links could be added to this list if access requires any type of |
|---|
| 285 |
authentication, or if the link points to a non-HTTP or HTTPS |
|---|
| 286 |
resource (i.e., "javascript:doNetDetect()"). |
|---|
| 287 |
|
|---|
| 288 |
Specifically, each 'key' corresponds to an absolute URL and the |
|---|
| 289 |
'value' is a string representing the date and time of when the link |
|---|
| 290 |
was visited. |
|---|
| 291 |
|
|---|
| 292 |
B<Note>: See internal documentation of _getTimestamp() for the |
|---|
| 293 |
corresponding date/time format of each value. |
|---|
| 294 |
|
|---|
| 295 |
=back |
|---|
| 296 |
|
|---|
| 297 |
=head2 relative_links_to_visit |
|---|
| 298 |
|
|---|
| 299 |
=over 4 |
|---|
| 300 |
|
|---|
| 301 |
This parameter is a hashtable of fully qualified URLs, such that each |
|---|
| 302 |
URL shares a common B<hostname>. This is an internal hashtable used |
|---|
| 303 |
by the Browser driver that should be initially empty. As the Browser |
|---|
| 304 |
driver extracts and removes new URLs off the B<links_to_visit> hashtable, |
|---|
| 305 |
driving the browser to each URL, any B<relative> links found are |
|---|
| 306 |
added into this hashtable; any B<external> links found are added |
|---|
| 307 |
back into the B<links_to_visit> hashtable. |
|---|
| 308 |
|
|---|
| 309 |
When driving to the next link, this hashtable is exhausted prior |
|---|
| 310 |
to the main B<links_to_visit> hashtable. This allows a |
|---|
| 311 |
browser to navigate to all links hosted on the same server, prior |
|---|
| 312 |
to contacting a different server. |
|---|
| 313 |
|
|---|
| 314 |
Specifically, each 'key' corresponds to an absolute URL and the |
|---|
| 315 |
'value' is always 1. |
|---|
| 316 |
|
|---|
| 317 |
=back |
|---|
| 318 |
|
|---|
| 319 |
=head2 next_link_to_visit |
|---|
| 320 |
|
|---|
| 321 |
=over 4 |
|---|
| 322 |
|
|---|
| 323 |
This parameter is a scalar that contains the next URL to visit. |
|---|
| 324 |
It is updated dynamically, any time $object->getNextLink() is called. |
|---|
| 325 |
|
|---|
| 326 |
When the browser is ready to drive to the next link, B<next_link_to_visit> |
|---|
| 327 |
is checked first. If that value is B<undef>, then the B<relative_links_to_visit> |
|---|
| 328 |
hashtable is checked next. If that hashtable is empty, then finally the |
|---|
| 329 |
B<links_to_visit> hashtable is checked last. |
|---|
| 330 |
|
|---|
| 331 |
=back |
|---|
| 332 |
|
|---|
| 333 |
=head2 links_timed_out |
|---|
| 334 |
|
|---|
| 335 |
=over 4 |
|---|
| 336 |
|
|---|
| 337 |
This parameter is a hashtable of fully qualified URLs that the browser |
|---|
| 338 |
has found during its link traversal process, but the browser |
|---|
| 339 |
could not access the corresponding resource due to the operation |
|---|
| 340 |
timing out. |
|---|
| 341 |
|
|---|
| 342 |
Specifically, each 'key' corresponds to an absolute URL and the |
|---|
| 343 |
'value' is a string representing the date and time of when access to |
|---|
| 344 |
the resource was attempted. |
|---|
| 345 |
|
|---|
| 346 |
B<Note>: See internal documentation of _getTimestamp() for the |
|---|
| 347 |
corresponding date/time format of each value. |
|---|
| 348 |
|
|---|
| 349 |
=back |
|---|
| 350 |
|
|---|
| 351 |
=head2 ignore_timed_out_links |
|---|
| 352 |
|
|---|
| 353 |
=over 4 |
|---|
| 354 |
|
|---|
| 355 |
If this parameter is set to 1, then the browser will also |
|---|
| 356 |
never attempt to revisit any links that caused the browser to |
|---|
| 357 |
time out. |
|---|
| 358 |
|
|---|
| 359 |
=back |
|---|
| 360 |
|
|---|
| 361 |
=head2 process_name |
|---|
| 362 |
|
|---|
| 363 |
=over 4 |
|---|
| 364 |
|
|---|
| 365 |
A string containing the process name of the browser application, |
|---|
| 366 |
as it appears in the Task Manager. |
|---|
| 367 |
|
|---|
| 368 |
=back |
|---|
| 369 |
|
|---|
| 370 |
=head2 max_relative_links_to_visit |
|---|
| 371 |
|
|---|
| 372 |
=over 4 |
|---|
| 373 |
|
|---|
| 374 |
An integer, representing the maximum number of relative links that |
|---|
| 375 |
the browser should visit, before moving onto another website. If |
|---|
| 376 |
negative, then the browser will exhaust all possible relative links |
|---|
| 377 |
found, before moving on. This functionality is best effort; it's |
|---|
| 378 |
possible for the browser to visit new links on previously visited |
|---|
| 379 |
websites. |
|---|
| 380 |
|
|---|
| 381 |
=back |
|---|
| 382 |
|
|---|
| 383 |
=cut |
|---|
| 384 |
|
|---|
| 385 |
my %PARAMS = ( |
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
links_to_visit => { }, |
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
links_visited => { }, |
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
links_ignored => { }, |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
relative_links_to_visit => { }, |
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
next_link_to_visit => undef, |
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
links_timed_out => { }, |
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
ignore_links_timed_out => getVar(name => "ignore_links_timed_out"), |
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
process_name => getVar(name => "process_name"), |
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
_remaining_number_of_relative_links_to_visit => getVar(name => "max_relative_links_to_visit"), |
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
|
|---|
| 475 |
max_relative_links_to_visit => getVar(name => "max_relative_links_to_visit"), |
|---|
| 476 |
|
|---|
| 477 |
); |
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 |
sub _getNextLink { |
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
my $self = shift; |
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
my $link = undef; |
|---|
| 507 |
|
|---|
| 508 |
while (!defined($link) or ($link eq "")) { |
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
$link = $self->next_link_to_visit; |
|---|
| 512 |
$self->{next_link_to_visit} = undef; |
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
unless (defined($link)) { |
|---|
| 517 |
$link = _pop($self->relative_links_to_visit); |
|---|
| 518 |
} |
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
unless (defined($link)) { |
|---|
| 523 |
$link = _pop($self->links_to_visit); |
|---|
| 524 |
} |
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
|
|---|
| 528 |
unless (defined($link)) { |
|---|
| 529 |
return $link; |
|---|
| 530 |
} |
|---|
| 531 |
|
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
$link = $self->_validateLink($link); |
|---|
| 537 |
} |
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
return $link; |
|---|
| 541 |
} |
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
sub _getTimestamp { |
|---|
| 551 |
my $dt = DateTime::HiRes->now(); |
|---|
| 552 |
return $dt->ymd('-') . " " . |
|---|
| 553 |
$dt->hms(':') . "." . |
|---|
| 554 |
$dt->nanosecond(); |
|---|
| 555 |
} |
|---|
| 556 |
|
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
sub _pop { |
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
my $hash = shift; |
|---|
| 571 |
|
|---|
| 572 |
|
|---|
| 573 |
my @keys = keys(%{$hash}); |
|---|
| 574 |
my $key = pop(@keys); |
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
if (defined($key)) { |
|---|
| 578 |
delete $hash->{$key}; |
|---|
| 579 |
} |
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
return $key; |
|---|
| 583 |
} |
|---|
| 584 |
|
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 |
sub getContent { |
|---|
| 589 |
|
|---|
| 590 |
} |
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
sub _getAllLinks { |
|---|
| 599 |
|
|---|
| 600 |
my $response = shift; |
|---|
| 601 |
my $hostname = shift; |
|---|
| 602 |
my @links = (); |
|---|
| 603 |
my $thislink; |
|---|
| 604 |
|
|---|
| 605 |
my $html = $response->content; |
|---|
| 606 |
|
|---|
| 607 |
while( $html =~ m/<A HREF=\"(.*?)\"/gi ) { |
|---|
| 608 |
$thislink = $1; |
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
if ($thislink =~ /^\//) { |
|---|
| 613 |
$thislink = "http://" . $hostname . $thislink; |
|---|
| 614 |
} |
|---|
| 615 |
|
|---|
| 616 |
push @links, $thislink; |
|---|
| 617 |
} |
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
return @links; |
|---|
| 621 |
} |
|---|
| 622 |
|
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 |
sub _extractHostname { |
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 |
my $arg = shift(); |
|---|
| 636 |
|
|---|
| 637 |
if (!defined($arg)) { |
|---|
| 638 |
return ""; |
|---|
| 639 |
} |
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
my $url = $arg . "/"; |
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
$url =~ s/^.*?\/\/(.*?)\/.*$/$1/; |
|---|
| 647 |
|
|---|
| 648 |
|
|---|
| 649 |
return $url; |
|---|
| 650 |
} |
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
|
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
sub _processLinks { |
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
my $self = shift; |
|---|
| 689 |
|
|---|
| 690 |
|
|---|
| 691 |
my ($referrer, @links) = @_; |
|---|
| 692 |
|
|---|
| 693 |
foreach my $url (@links) { |
|---|
| 694 |
|
|---|
| 695 |
|
|---|
| 696 |
unless (defined($url)) { |
|---|
| 697 |
next; |
|---|
| 698 |
} |
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
$url = $self->_validateLink($url); |
|---|
| 702 |
|
|---|
| 703 |
if (!defined($url) or ($url eq "")) { |
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
next; |
|---|
| 708 |
} |
|---|
| 709 |
|
|---|
| 710 |
|
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
my $hostname = _extractHostname($url); |
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
if ($hostname eq $referrer) { |
|---|
| 719 |
|
|---|
| 720 |
|
|---|
| 721 |
$self->relative_links_to_visit->{$url} = 1; |
|---|
| 722 |
} else { |
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
$self->links_to_visit->{$url} = 1; |
|---|
| 726 |
} |
|---|
| 727 |
} |
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
return $self; |
|---|
| 731 |
} |
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
sub _validateLink { |
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
my $self = shift; |
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
my ($link) = @_; |
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
$link =~ s/\ |
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
|
|---|
| 771 |
unless ($link =~ /^http[s]?:\/\/.*/i) { |
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 |
unless (scalar(%{$self->links_ignored}) and |
|---|
| 778 |
exists($self->links_ignored->{$link})) { |
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
$self->links_ignored->{$link} = _getTimestamp(); |
|---|
| 782 |
} |
|---|
| 783 |
|
|---|
| 784 |
|
|---|
| 785 |
return ""; |
|---|
| 786 |
} |
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
if ((scalar(%{$self->links_visited}) and |
|---|
| 792 |
exists($self->links_visited->{$link})) or |
|---|
| 793 |
(scalar(%{$self->links_ignored}) and |
|---|
| 794 |
exists($self->links_ignored->{$link}))) { |
|---|
| 795 |
|
|---|
| 796 |
|
|---|
| 797 |
return; |
|---|
| 798 |
} |
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
return $link; |
|---|
| 803 |
} |
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
sub _killProcess { |
|---|
| 811 |
|
|---|
| 812 |
|
|---|
| 813 |
my $self = shift; |
|---|
| 814 |
|
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 |
|
|---|
| 818 |
|
|---|
| 819 |
my $stub = getClientHandle(address => 'localhost', |
|---|
| 820 |
namespace => 'HoneyClient::Agent'); |
|---|
| 821 |
|
|---|
| 822 |
my $som = $stub->killProcess($self->process_name); |
|---|
| 823 |
|
|---|
| 824 |
if (!$som->result) { |
|---|
| 825 |
Carp::carp "Failed to kill process: '" . $self->process_name . "'!\n"; |
|---|
| 826 |
} |
|---|
| 827 |
} |
|---|
| 828 |
|
|---|
| 829 |
|
|---|
| 830 |
|
|---|
| 831 |
|
|---|
| 832 |
|
|---|
| 833 |
=pod |
|---|
| 834 |
|
|---|
| 835 |
=head1 METHODS IMPLEMENTED |
|---|
| 836 |
|
|---|
| 837 |
The following functions have been implemented by the Browser driver. Many |
|---|
| 838 |
of these methods were implementations of the parent Driver interface. |
|---|
| 839 |
|
|---|
| 840 |
As such, the following code descriptions pertain to this particular |
|---|
| 841 |
Driver implementation. For further information about the generic |
|---|
| 842 |
Driver interface, see the L<HoneyClient::Agent::Driver> documentation. |
|---|
| 843 |
|
|---|
| 844 |
=head2 HoneyClient::Agent::Driver::Browser->new($param => $value, ...) |
|---|
| 845 |
|
|---|
| 846 |
=over 4 |
|---|
| 847 |
|
|---|
| 848 |
Creates a new Browser driver object, which contains a hashtable |
|---|
| 849 |
containing any of the supplied "param => value" arguments. |
|---|
| 850 |
|
|---|
| 851 |
I<Inputs>: |
|---|
| 852 |
B<$param> is an optional parameter variable. |
|---|
| 853 |
B<$value> is $param's corresponding value. |
|---|
| 854 |
|
|---|
| 855 |
Note: If any $param(s) are supplied, then an equal number of |
|---|
| 856 |
corresponding $value(s) B<must> also be specified. |
|---|
| 857 |
|
|---|
| 858 |
I<Output>: The instantiated Browser driver B<$object>, fully initialized. |
|---|
| 859 |
|
|---|
| 860 |
=back |
|---|
| 861 |
|
|---|
| 862 |
=begin testing |
|---|
| 863 |
|
|---|
| 864 |
# XXX: Add this. |
|---|
| 865 |
1; |
|---|
| 866 |
|
|---|
| 867 |
=end testing |
|---|
| 868 |
|
|---|
| 869 |
=cut |
|---|
| 870 |
|
|---|
| 871 |
sub new { |
|---|
| 872 |
|
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
|
|---|
| 876 |
|
|---|
| 877 |
|
|---|
| 878 |
|
|---|
| 879 |
|
|---|
| 880 |
|
|---|
| 881 |
|
|---|