Agent/Driver/Browser/IE.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 128 CC 9 SCAR 37.8
CC
9
Cov
0%
drive
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 43.7
1 #######################################################################
2 # Created on: May 11, 2006
3 # Package: HoneyClient::Agent::Driver::Browser::IE
4 # File: IE.pm
5 # Description: A specific driver for automating an instance of
6 # the Internet Explorer browser, running inside a
7 # HoneyClient VM.
8 #
9 # CVS: $Id: IE.pm 773 2007-07-26 19:04:55Z kindlund $
10 #
11 # @author knwang, ttruong, 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::IE - Perl extension to drive Microsoft
38 Internet Explorer to a given web page. This package extends the
39 HoneyClient::Agent::Driver::Browser package, by overridding the drive() method.
40  
41 =head1 VERSION
42  
43 This documentation refers to HoneyClient::Agent::Driver::Browser::IE version 0.98.
44  
45 =head1 SYNOPSIS
46  
47 use HoneyClient::Agent::Driver::Browser::IE;
48  
49 # Library used exclusively for debugging complex objects.
50 use Data::Dumper;
51  
52 # Create a new IE object, initialized with a collection
53 # of URLs to visit.
54 my $browser = HoneyClient::Agent::Driver::Browser::IE->new(
55 links_to_visit => {
56 'http://www.google.com' => 1,
57 'http://www.cnn.com' => 1,
58 },
59 );
60  
61 # If you want to see what type of "state information" is physically
62 # inside $browser, try this command at any time.
63 print Dumper($browser);
64  
65 # Continue to "drive" the driver, until it is finished.
66 while (!$browser->isFinished()) {
67  
68 # Before we drive the application to a new set of resources,
69 # find out where we will be going within the application, first.
70 print "About to contact the following resources:\n";
71 print Dumper($browser->next());
72  
73 # Now, drive browser for one iteration.
74 $browser->drive();
75  
76 # Get the driver's progress.
77 print "Status:\n";
78 print Dumper($browser->status());
79  
80 }
81  
82 # At this stage, the driver has exhausted its collection of links
83 # to visit. Let's say we want to add the URL "http://www.mitre.org"
84 # to the driver's list.
85 $browser->{links_to_visit}->{'http://www.mitre.org'} = 1;
86  
87 # Now, drive the browser for one iteration.
88 $browser->drive();
89
90 # Or, we can specify the URL as an argument.
91 $browser->drive(url => "http://www.mitre.org");
92  
93 =head1 DESCRIPTION
94  
95 This library allows the Agent module to drive an instance of Microsoft
96 Internet Explorer inside the HoneyClient VM. The purpose of this module is to
97 programmatically navigate this browser to different websites, in order to
98 become purposefully infected with new malware.
99  
100 This module is object-oriented in design, retaining all state information
101 within itself for easy access. This specific browser implementation inherits
102 all code from the HoneyClient::Agent::Driver::Browser package.
103  
104 Fundamentally, the IE driver is initialized with a set of absolute URLs
105 for the browser to drive to. Upon visiting each URL, the driver collects
106 any B<new> links found and will attempt to drive the browser to each
107 valid URL upon subsequent iterations of work.
108  
109 For each top-level URL given, the driver will attempt to process all
110 corresponding links that are hosted on the same server, in order to
111 simulate a complete 'spider' of each server.
112  
113 URLs are added and removed from hashtables, as keys. For each URL, a
114 calculated "priority" (a positive integer) of the URL is assigned the
115 value. When the IE driver is ready to go to a new link, it will always go
116 to the next link that has the highest priority. If two URLs have the same
117 priority, then the IE driver will chose among those two at random.
118  
119 Furthermore, the IE driver will try to visit all links shared by a
120 common server in order before moving on to drive to other,
121 external links in an ordered fashion. B<However>, the IE driver may end
122 up re-visiting old sites, if new links were found that the
123 IE driver has not visited yet.
124  
125 As the IE driver navigates the browser to each link, it
126 maintains a set of hashtables that record when valid links were
127 visited (see L<links_visited>); when invalid links were found
128 (see L<links_ignored>); and when the browser attempted to visit
129 a link but the operation timed out (see L<links_timed_out>).
130 By maintaining this internal history, the driver will B<never>
131 navigate the browser to the same link twice.
132  
133 Lastly, it is highly recommended that for each driver B<$object>,
134 one should call $object->isFinished() prior to making a subsequent
135 call to $object->drive(), in order to verify that the driver has
136 not exhausted its set of links to visit. Otherwise, if
137 $object->drive() is called with an empty set of links to visit,
138 the corresponding operation will B<croak>.
139  
140 =cut
141  
142 package HoneyClient::Agent::Driver::Browser::IE;
143  
144 use strict;
145 use warnings;
146 use Carp ();
147  
148 # Traps signals, allowing END: blocks to perform cleanup.
149 use sigtrap qw(die untrapped normal-signals error-signals);
150  
151 #######################################################################
152 # Module Initialization #
153 #######################################################################
154  
155 BEGIN {
156  
157 # Defines which functions can be called externally.
158 require Exporter;
159 our ( @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION );
160  
161 # Set our package version.
162 $VERSION = 0.98;
163  
164 # Define inherited modules.
165 use HoneyClient::Agent::Driver::Browser;
166  
167 @ISA = qw(Exporter HoneyClient::Agent::Driver::Browser);
168  
169 # Symbols to export automatically
170 # Note: Since this module is object-oriented, we do *NOT* export
171 # any functions other than "new" to call statically. Each function
172 # for this module *must* be called as a method from a unique
173 # object instance.
174 @EXPORT = qw();
175  
176 # Items to export into callers namespace by default. Note: do not export
177 # names by default without a very good reason. Use EXPORT_OK instead.
178 # Do not simply export all your public functions/methods/constants.
179  
180 # This allows declaration use HoneyClient::Agent::Driver::Browser::IE ':all';
181 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
182 # will save memory.
183  
184 # Note: Since this module is object-oriented, we do *NOT* export
185 # any functions other than "new" to call statically. Each function
186 # for this module *must* be called as a method from a unique
187 # object instance.
188 %EXPORT_TAGS = ( 'all' => [qw()], );
189  
190 # Symbols to autoexport (when qw(:all) tag is used)
191 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
192  
193 $SIG{PIPE} = 'IGNORE'; # Do not exit on broken pipes.
194 }
195 our ( @EXPORT_OK, $VERSION );
196  
197 =pod
198  
199 =begin testing
200  
201 # Make sure Log::Log4perl loads
202 BEGIN { use_ok('Log::Log4perl', qw(:nowarn))
203 or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path.");
204
205 # Suppress all logging messages, since we need clean output for unit testing.
206 Log::Log4perl->init({
207 "log4perl.rootLogger" => "DEBUG, Buffer",
208 "log4perl.appender.Buffer" => "Log::Log4perl::Appender::TestBuffer",
209 "log4perl.appender.Buffer.min_level" => "fatal",
210 "log4perl.appender.Buffer.layout" => "Log::Log4perl::Layout::PatternLayout",
211 "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
212 });
213 }
214 require_ok('Log::Log4perl');
215 use Log::Log4perl qw(:easy);
216  
217 # Make sure HoneyClient::Util::Config loads.
218 BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar))
219 or diag("Can't load HoneyClient::Util::Config package. Check to make sure the package library is correctly listed within the path.");
220  
221 # Suppress all logging messages, since we need clean output for unit testing.
222 Log::Log4perl->init({
223 "log4perl.rootLogger" => "DEBUG, Buffer",
224 "log4perl.appender.Buffer" => "Log::Log4perl::Appender::TestBuffer",
225 "log4perl.appender.Buffer.min_level" => "fatal",
226 "log4perl.appender.Buffer.layout" => "Log::Log4perl::Layout::PatternLayout",
227 "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
228 });
229
230 }
231 require_ok('HoneyClient::Util::Config');
232 can_ok('HoneyClient::Util::Config', 'getVar');
233 use HoneyClient::Util::Config qw(getVar);
234  
235 # Make sure the module loads properly, with the exportable
236 # functions shared.
237 BEGIN { use_ok('HoneyClient::Agent::Driver::Browser::IE') or diag("Can't load HoneyClient::Agent::Driver::Browser::IE package. Check to make sure the package library is correctly listed within the path."); }
238 require_ok('HoneyClient::Agent::Driver::Browser::IE');
239 can_ok('HoneyClient::Agent::Driver::Browser::IE', 'new');
240 can_ok('HoneyClient::Agent::Driver::Browser::IE', 'drive');
241 can_ok('HoneyClient::Agent::Driver::Browser::IE', 'isFinished');
242 can_ok('HoneyClient::Agent::Driver::Browser::IE', 'next');
243 can_ok('HoneyClient::Agent::Driver::Browser::IE', 'status');
244 can_ok('HoneyClient::Agent::Driver::Browser::IE', 'getNextLink');
245 use HoneyClient::Agent::Driver::Browser::IE;
246  
247 # Suppress all logging messages, since we need clean output for unit testing.
248 Log::Log4perl->init({
249 "log4perl.rootLogger" => "DEBUG, Buffer",
250 "log4perl.appender.Buffer" => "Log::Log4perl::Appender::TestBuffer",
251 "log4perl.appender.Buffer.min_level" => "fatal",
252 "log4perl.appender.Buffer.layout" => "Log::Log4perl::Layout::PatternLayout",
253 "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
254 });
255  
256 # Make sure Win32::Job loads.
257 BEGIN { use_ok('Win32::Job') or diag("Can't load Win32::Job package. Check to make sure the package library is correctly listed within the path."); }
258 require_ok('Win32::Job');
259 use Win32::Job;
260  
261 # Make sure ExtUtils::MakeMaker loads.
262 BEGIN { use_ok('ExtUtils::MakeMaker', qw(prompt)) or diag("Can't load ExtUtils::MakeMaker package. Check to make sure the package library is correctly listed within the path."); }
263 require_ok('ExtUtils::MakeMaker');
264 can_ok('ExtUtils::MakeMaker', 'prompt');
265 use ExtUtils::MakeMaker qw(prompt);
266  
267 =end testing
268  
269 =cut
270  
271 #######################################################################
272  
273 # Include the Global Configuration Processing Library
274 use HoneyClient::Util::Config qw(getVar);
275  
276 # Include Win32 Job Library
277 use Win32::Job;
278  
279 # Include Logging Library
280 use Log::Log4perl qw(:easy);
281  
282 # The global logging object.
283 our $LOG = get_logger();
284  
285 #######################################################################
286 # Public Methods Implemented #
287 #######################################################################
288  
289 =pod
290  
291 =head1 METHODS OVERRIDDEN
292  
293 The following functions have been overridden by the IE driver. All other
294 methods were implemented by the generic Browser driver. For further
295 information about the Browser driver, see the L<HoneyClient::Agent::Driver::Browser>
296 documentation.
297  
298 =head2 $object->drive(url => $url)
299  
300 =over 4
301  
302 Drives an instance of Microsoft Internet Explorer for one iteration,
303 navigating either to the specified URL or to the next URL computed within
304 the Browser driver's internal hashtables.
305  
306 For a description of which hashtable is consulted upon each iteration of
307 drive(), see the B<next_link_to_visit> description of the
308 L<HoneyClient::Agent::Driver::Browser> documentation, in the
309 "DEFAULT PARAMETER LIST" section.
310  
311 Once a drive() iternation has completed, the corresponding browser process
312 is terminated. Thus, each call to drive() invokes a new instance of the
313 browser.
314  
315 I<Inputs>:
316 B<$url> is an optional argument, specifying the next immediate URL the browser
317 must drive to.
318  
319 I<Output>: The updated IE driver B<$object>, containing state information from driving the
320 browser for one iteration.
321  
322 B<Warning>: This method will B<croak>, if the IE driver object is B<unable>
323 to navigate to a new link, because its list of links to vist is empty and no new
324 URL was supplied.
325  
326 =back
327  
328 =begin testing
329  
330 # Generate a notice, to clarify our assumptions.
331 diag("");
332 diag("About to run basic IE-specific browser tests.");
333 diag("Note: These tests *require* network connectivity and");
334 diag("*expect* IE to be installed at the following location.");
335 diag("");
336  
337 my $processExec = getVar(name => "process_exec",
338 namespace => "HoneyClient::Agent::Driver::Browser::IE");
339 my $processName = getVar(name => "process_name",
340 namespace => "HoneyClient::Agent::Driver::Browser::IE");
341  
342 diag("Process Name:\t\t'" . $processName . "'");
343 diag("Process Location:\t'" . $processExec . "'");
344 diag("");
345 diag("If IE is installed in a different location or has a different executable name,");
346 diag("then please answer *NO* to the next question and update your etc/honeyclient.xml");
347 diag("file, changing the 'process_name' and 'process_exec' elements in the");
348 diag("<HoneyClient/><Agent/><Driver/><Browser/><IE/> section.");
349 diag("");
350 diag("Then, once updated, re-run these tests.");
351 diag("");
352  
353 my $question;
354 $question = prompt("# Do you want to run these tests?", "yes");
355 if ($question !~ /^y.*/i) {
356 exit;
357 }
358  
359 my $ie = HoneyClient::Agent::Driver::Browser::IE->new(test => 1);
360 is($ie->{test}, 1, "new(test => 1)") or diag("The new() call failed.");
361 isa_ok($ie, 'HoneyClient::Agent::Driver::Browser::IE', "new(test => 1)") or diag("The new() call failed.");
362  
363 diag("");
364 diag("About to drive IE to a specific website for *exactly* " . $ie->{timeout} . " seconds.");
365 diag("Note: Please do *NOT* close the browser manually; the test code should close it automatically.");
366 diag("");
367  
368 $question = prompt("# Which website should IE browse to?", "http://www.google.com");
369 $ie->drive(url => $question);
370  
371 diag("");
372 $question = prompt("# Did IE properly render the page and automatically exit?", "yes");
373 if ($question !~ /^y.*/i) {
374 diag("");
375 diag("Check your network connectivity and verify that you can manually browse this page in IE.");
376 diag("Then, re-run these tests.");
377 diag("");
378 diag("If the tests still do not work, please submit a ticket to:");
379 diag("http://www.honeyclient.org/trac/newticket");
380 diag("");
381 fail("The drive() call failed.");
382 }
383  
384 =end testing
385  
386 =cut
387  
388 sub drive {
389  
390 # Extract arguments.
391 my ($self, %args) = @_;
392  
393 # Sanity check: Make sure we've been fed an object.
394 unless (ref($self)) {
395 $LOG->error("Error: Function must be called in reference to a " .
396 __PACKAGE__ . "->new() object!");
397 Carp::croak "Error: Function must be called in reference to a " .
398 __PACKAGE__ . "->new() object!\n";
399 }
400  
401 # Sanity check, don't get the next link, if
402 # we've been fed a url.
403 my $argsExist = scalar(%args);
404 if (!$argsExist ||
405 !exists($args{'url'}) ||
406 !defined($args{'url'})) {
407 # Get the next URL from our hashtables.
408 $args{'url'} = $self->_getNextLink();
409 }
410  
411 # Drive the generic browser before opening with IE
412 $self = $self->SUPER::drive(%args);
413
414 # Sanity check: Make sure our next URL is defined.
415 unless (defined($args{'url'})) {
416 $LOG->error("Error: Unable to drive browser - no links left to browse!");
417 Carp::croak "Error: Unable to drive browser - no links left to browse!\n";
418 }
419  
420 # Indicates how long we wait for each drive operation to complete,
421 # before registering attempt as a failure.
422 my $timeout : shared = $self->timeout();
423  
424 # Create a new Job.
425 my $job = Win32::Job->new();
426  
427 # Sanity check.
428 if (!defined($job)) {
429 $LOG->error("Error: Unable to spawn a new process - " . $^E . ".");
430 Carp::croak "Error: Unable to spawn a new process - " . $^E . ".\n";
431 }
432  
433 # Spawn the job.
434 my $processExec = getVar(name => "process_exec");
435 my $processName = getVar(name => "process_name");
436 my $status = $job->spawn($processExec, $processName . " " . $args{'url'});
437  
438 # Sanity check.
439 if (!defined($status)) {
440 $LOG->error("Error: Unable to spawn a new browser - " . $^E . ".");
441 Carp::croak "Error: Unable to spawn a new browser - " . $^E . ".\n";
442 }
443  
444 # Run the job.
445 $job->run($timeout);
446  
447 # Check to see if run fails.
448 $status = $job->status();
449  
450 # Sanity check.
451 if (!defined($status) ||
452 !scalar(%{$status})) {
453 $LOG->error("Error: Unable to retrieve job status from spawned process.");
454 Carp::croak "Error: Unable to retrieve job status from spawned process.\n";
455 }
456  
457 # Figure out the correct Process ID.
458 my @keys = keys(%{$status});
459 my $processID = pop(@keys);
460  
461 # Sanity checks.
462 if (!defined($processID) ||
463 !exists($status->{$processID}->{'exitcode'}) ||
464 !defined($status->{$processID}->{'exitcode'})) {
465 $LOG->error("Error: Unable to retrieve job status from spawned process.");
466 Carp::croak "Error: Unable to retrieve job status from spawned process.\n";
467 }
468  
469 # TODO: We may want to report this suspicious activity back to the Agent;
470 # perhaps force the Agent to do an early integrity check, to make sure nothing
471 # sketchy is going on.
472  
473 # Check to make sure the exitcode is '293', meaning, that the
474 # application didn't unexpectedly die early.
475 if ($status->{$processID}->{'exitcode'} != 293) {
476 $LOG->warn("Unexpected: '" . $processName . "' process (ID = " . $processID . ") terminated early!");
477 }
478  
479 # Return the modified object state.
480 return $self;
481 }
482  
483 #######################################################################
484  
485 1;
486  
487 #######################################################################
488 # Additional Module Documentation #
489 #######################################################################
490  
491 __END__