Agent/Driver.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 128 CC 4 SCAR 17.9
CC
4
Cov
0%
AUTOLOAD
20.0
BEGIN
2.0
DESTROY
2.0
CRAP 6.0
1 #######################################################################
2 # Created on: May 11, 2006
3 # Package: HoneyClient::Agent::Driver
4 # File: Driver.pm
5 # Description: Generic driver model for all drivers running inside a
6 # HoneyClient VM.
7 #
8 # CVS: $Id: Driver.pm 773 2007-07-26 19:04:55Z kindlund $
9 #
10 # @author knwang, ttruong, kindlund
11 #
12 # Copyright (C) 2007 The MITRE Corporation. All rights reserved.
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation, using version 2
17 # of the License.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301, USA.
28 #
29 #######################################################################
30  
31 =pod
32  
33 =head1 NAME
34  
35 HoneyClient::Agent::Driver - Perl extension to provide a generic driver
36 interface for all drivers resident within any HoneyClient VM.
37  
38 =head1 VERSION
39  
40 This documentation refers to HoneyClient::Agent::Driver version 0.98.
41  
42 =head1 SYNOPSIS
43  
44 # NOTE: This package is an INTERFACE specification only! It is
45 # NOT intended to be used directly. Rather, it is expected that
46 # other Driver specific sub-packages will INHERIT and IMPLEMENT
47 # these methods.
48  
49 # Eventually, change each reference of 'HoneyClient::Agent::Driver'
50 # to an implementation-specific 'HoneyClient::Agent::Driver::*'
51 # package name.
52 use HoneyClient::Agent::Driver;
53  
54 # Library used exclusively for debugging complex objects.
55 use Data::Dumper;
56  
57 # Eventually, call the new() function on an implementation-specific
58 # Driver package name.
59 my $driver = HoneyClient::Agent::Driver->new();
60  
61 # If you want to see what type of "state information" is physically
62 # inside $driver, try this command at any time.
63 print Dumper($driver);
64  
65 # Continue to "drive" the driver, until it is finished.
66 while (!$driver->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($driver->next());
72  
73 # Now, drive the application.
74 $driver->drive();
75  
76 # Get status of current iteration of work.
77 print "Status:\n";
78 print Dumper($driver->status());
79
80 }
81  
82 =head1 DESCRIPTION
83  
84 This library allows the Agent module to access any drivers running on the
85 HoneyClient VM in a consistent fashion. This module is object-oriented in
86 design, allowing specific types of drivers to inherit these abstractly
87 defined interface methods.
88  
89 Fundamentally, a "Driver" is a programmatic construct, designed to
90 automate a back-end application that is intended to be exploited by
91 different types of malware. As such, the "Agent" interacts with each
92 B<application-specific> Driver running inside the HoneyClient VM, in
93 order to programmatically automate the corresponding applications.
94  
95 When a "Driver" is "driven", this implies that the back-end application
96 is accessing a B<new> Internet resource, in order to intentionally be exposed
97 to new malware and thus become exploited.
98  
99 Example implementation Drivers involve automating certain types and
100 B<versions> of web browsers (e.g., Microsoft Internet Explorer, Mozilla
101 Firefox) or even email applications (e.g., Microsoft Outlook, Mozilla
102 Thunderbird).
103  
104 =cut
105  
106 package HoneyClient::Agent::Driver;
107  
108 use strict;
109 use warnings;
110 use Carp ();
111  
112 #######################################################################
113 # Module Initialization #
114 #######################################################################
115  
116 BEGIN {
117 # Defines which functions can be called externally.
118 require Exporter;
119 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION);
120  
121 # Set our package version.
122 $VERSION = 0.98;
123  
124 @ISA = qw(Exporter);
125  
126 # Symbols to export automatically
127 # Note: Since this module is object-oriented, we do *NOT* export
128 # any functions other than "new" to call statically. Each function
129 # for this module *must* be called as a method from a unique
130 # object instance.
131 @EXPORT = qw();
132  
133 # Items to export into callers namespace by default. Note: do not export
134 # names by default without a very good reason. Use EXPORT_OK instead.
135 # Do not simply export all your public functions/methods/constants.
136  
137 # This allows declaration use HoneyClient::Agent::Driver ':all';
138 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
139 # will save memory.
140  
141 # Note: Since this module is object-oriented, we do *NOT* export
142 # any functions other than "new" to call statically. Each function
143 # for this module *must* be called as a method from a unique
144 # object instance.
145 %EXPORT_TAGS = (
146 'all' => [ qw() ],
147 );
148  
149 # Symbols to autoexport (when qw(:all) tag is used)
150 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
151  
152 $SIG{PIPE} = 'IGNORE'; # Do not exit on broken pipes.
153 }
154 our (@EXPORT_OK, $VERSION);
155  
156 =pod
157  
158 =begin testing
159  
160 # Make sure Log::Log4perl loads
161 BEGIN { use_ok('Log::Log4perl', qw(:nowarn))
162 or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path.");
163
164 # Suppress all logging messages, since we need clean output for unit testing.
165 Log::Log4perl->init({
166 "log4perl.rootLogger" => "DEBUG, Buffer",
167 "log4perl.appender.Buffer" => "Log::Log4perl::Appender::TestBuffer",
168 "log4perl.appender.Buffer.min_level" => "fatal",
169 "log4perl.appender.Buffer.layout" => "Log::Log4perl::Layout::PatternLayout",
170 "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
171 });
172 }
173 require_ok('Log::Log4perl');
174 use Log::Log4perl qw(:easy);
175  
176 # Make sure HoneyClient::Util::Config loads.
177 BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar))
178 or diag("Can't load HoneyClient::Util::Config package. Check to make sure the package library is correctly listed within the path.");
179  
180 # Suppress all logging messages, since we need clean output for unit testing.
181 Log::Log4perl->init({
182 "log4perl.rootLogger" => "DEBUG, Buffer",
183 "log4perl.appender.Buffer" => "Log::Log4perl::Appender::TestBuffer",
184 "log4perl.appender.Buffer.min_level" => "fatal",
185 "log4perl.appender.Buffer.layout" => "Log::Log4perl::Layout::PatternLayout",
186 "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
187 });
188
189 }
190 require_ok('HoneyClient::Util::Config');
191 can_ok('HoneyClient::Util::Config', 'getVar');
192 use HoneyClient::Util::Config qw(getVar);
193  
194 # Make sure the module loads properly, with the exportable
195 # functions shared.
196 BEGIN { use_ok('HoneyClient::Agent::Driver') or diag("Can't load HoneyClient::Agent::Driver package. Check to make sure the package library is correctly listed within the path."); }
197 require_ok('HoneyClient::Agent::Driver');
198 can_ok('HoneyClient::Agent::Driver', 'new');
199 can_ok('HoneyClient::Agent::Driver', 'drive');
200 can_ok('HoneyClient::Agent::Driver', 'isFinished');
201 can_ok('HoneyClient::Agent::Driver', 'next');
202 can_ok('HoneyClient::Agent::Driver', 'status');
203 use HoneyClient::Agent::Driver;
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 # Make sure we use the exception testing library.
215 require_ok('Test::Exception');
216 can_ok('Test::Exception', 'dies_ok');
217 use Test::Exception;
218  
219 # Make sure Storable loads.
220 BEGIN { use_ok('Storable', qw(dclone)) or diag("Can't load Storable package. Check to make sure the package library is correctly listed within the path."); }
221 require_ok('Storable');
222 can_ok('Storable', 'dclone');
223 use Storable qw(dclone);
224  
225 =end testing
226  
227 =cut
228  
229 #######################################################################
230  
231 # Include Global Configuration Processing Library
232 use HoneyClient::Util::Config qw(getVar);
233  
234 # Use Storable Library
235 use Storable qw(dclone);
236  
237 # Package Global Variable
238 our $AUTOLOAD;
239  
240 # Include Logging Library
241 use Log::Log4perl qw(:easy);
242  
243 # The global logging object.
244 our $LOG = get_logger();
245  
246 =pod
247  
248 =head1 DEFAULT PARAMETER LIST
249  
250 When a Driver B<$object> is instantiated using the B<new()> function,
251 the following parameters are supplied default values. Each value
252 can be overridden by specifying the new (key => value) pair into the
253 B<new()> function, as arguments.
254  
255 Furthermore, as each parameter is initialized, each can be individually
256 retrieved and set at any time, using the following syntax:
257  
258 my $value = $object->{key}; # Gets key's value.
259 $object->{key} = $value; # Sets key's value.
260  
261 =head2 timeout
262  
263 =over 4
264  
265 This parameter indicates how long (in seconds) the Driver should wait
266 for an application response, once driven for one iteration.
267 The default value is any valid "timeout" setting located within the
268 global configuration file that matches any portion of this package's
269 namespace. See L<HoneyClient::Util::Config> for more information.
270  
271 =back
272  
273 =cut
274  
275 #######################################################################
276 # Private Methods Implemented #
277 #######################################################################
278  
279 # Helper function designed to programmatically get or set parameters
280 # within this object, through indirect use of the AUTOLOAD function.
281 #
282 # It's best to explain by example:
283 # Assume we have defined a driver object, like the following.
284 #
285 # use HoneyClient::Agent::Driver;
286 # my $driver = HoneyClient::Agent::Driver->new(someVar => 'someValue');
287 #
288 # What this function allows us to do, is programmatically, get or set
289 # the 'someVar' parameter, like:
290 #
291 # my $value = $driver->someVar(); # Gets the value of 'someVar'.
292 # my $value = $driver->someVar('2'); # Sets the value of 'someVar' to '2'
293 # # and returns '2'.
294 #
295 # Rather than creating getter/setter functions for every possible parameter,
296 # the AUTOLOAD function allows us to create these operations in a generic,
297 # reusable fashion.
298 #
299 # See "Autoloaded Data Methods" in perltoot for more details.
300 #
301 # Inputs: set a new value (optional)
302 # Outputs: the currently set value
303 sub AUTOLOAD {
304 # Get the object.
305 my $self = shift;
306  
307 # Sanity check: Make sure the supplied value is an object.
308 my $type = ref($self);
309 unless(defined($type)) {
310 $LOG->error("Error: $self is not an object!");
311 Carp::croak "Error: $self is not an object!\n";
312 }
313  
314 # Now, get the name of the function.
315 my $name = $AUTOLOAD;
316  
317 # Strip the fully-qualified portion of the function name.
318 $name =~ s/.*://;
319  
320 # Make sure the parameter exists in the object, before we try
321 # to get or set it.
322 unless (exists $self->{$name}) {
323 $LOG->error("Can't access '$name' parameter in class $type!");
324 Carp::croak "Error: Can't access '$name' parameter in class $type!\n";
325 }
326  
327 if (@_) {
328 # If we were given an argument, then set the parameter's value.
329 return $self->{$name} = shift;
330 } else {
331 # Else, just return the existing value.
332 return $self->{$name};
333 }
334 }
335  
336 # Base destructor function.
337 # Since none of our state data ever contains circular references,
338 # we can simply leave the garbage collection up to Perl's internal
339 # mechanism.
340 sub DESTROY {
341 }
342  
343 #######################################################################
344 # Public Methods Implemented #
345 #######################################################################
346  
347 =pod
348  
349 =head1 METHOD INTERFACES
350  
351 The following functions B<must> be implemented be each driver
352 implementation, upon inheriting this package interface.
353  
354 =head2 HoneyClient::Agent::Driver->new($param => $value, ...)
355  
356 =over 4
357  
358 Creates a new Driver object, which contains a hashtable
359 containing any of the supplied "param => value" arguments.
360  
361 I<Inputs>:
362 B<$param> is an optional parameter variable.
363 B<$value> is $param's corresponding value.
364
365 Note: If any $param(s) are supplied, then an equal number of
366 corresponding $value(s) B<must> also be specified.
367  
368 I<Output>: The instantiated Driver B<$object>, fully initialized.
369  
370 =back
371  
372 =begin testing
373  
374 # Create a generic driver, with test state data.
375 my $driver = HoneyClient::Agent::Driver->new(test => 1);
376 is($driver->{test}, 1, "new(test => 1)") or diag("The new() call failed.");
377 isa_ok($driver, 'HoneyClient::Agent::Driver', "new(test => 1)") or diag("The new() call failed.");
378  
379 =end testing
380  
381 =cut
382  
383 sub new {
384 # - This function takes in an optional hashtable,
385 # that contains various key => 'value' configuration
386 # parameters.
387 #
388 # - For each parameter given, it overwrites any corresponding
389 # parameters specified within the default hashtable, %params,
390 # with custom entries that were given as parameters.
391 #
392 # - Finally, it returns a blessed instance of the
393 # merged hashtable, as an 'object'.
394  
395 # Get the class name.
396 my $self = shift;
397  
398 # Get the rest of the arguments, as a hashtable.
399 # Hash-based arguments are used, since HoneyClient::Util::SOAP is unable to handle
400 # hash references directly. Thus, flat hashtables are used throughout the code
401 # for consistency.
402 my %args = @_;
403  
404 # Check to see if the class name is inherited or defined.
405 my $class = ref($self) || $self;
406  
407 # Initialize default parameters.
408 $self = { };
409 my %params = (
410 timeout => getVar(name => "timeout"), # Timeout (in seconds).
411 );
412  
413 @{$self}{keys %params} = values %params;
414  
415 # Now, overwrite any default parameters that were redefined
416 # in the supplied arguments.
417 @{$self}{keys %args} = values %args;
418  
419 # Now, assign our object the appropriate namespace.
420 bless $self, $class;
421  
422 # Finally, return the blessed object.
423 return $self;
424 }
425  
426 =pod
427  
428 =head2 $object->drive()
429  
430 =over 4
431  
432 Drives the back-end application for one iteration, updating the
433 corresponding internal object state with information obtained
434 from driving this application for one iteration.
435  
436 I<Output>: The updated Driver B<$object>, containing state information
437 from driving the application for one iteration. Will croak if
438 operation fails.
439  
440 =back
441  
442 =begin testing
443  
444 # Create a generic driver, with test state data.
445 my $driver = HoneyClient::Agent::Driver->new(test => 1);
446 dies_ok {$driver->drive()} 'drive()' or diag("The drive() call failed. Expected drive() to throw an exception.");
447  
448 =end testing
449  
450 =cut
451  
452 sub drive {
453 # Get the class name.
454 my $self = shift;
455
456 # Check to see if the class name is inherited or defined.
457 my $class = ref($self) || $self;
458  
459 # Emit generic "not implemented" error message.
460 $LOG->error($class . "->drive() is not implemented!");
461 Carp::croak "Error: " . $class . "->drive() is not implemented!\n";
462 }
463  
464 =pod
465  
466 =head2 $object->isFinished()
467  
468 =over 4
469  
470 Indicates if the Driver B<$object> has driven the back-end application
471 through the Driver's entire state and is unable to drive the application
472 further without additional input.
473  
474 I<Output>: True if the Driver B<$object> is finished, false otherwise.
475  
476 =back
477  
478 =begin testing
479  
480 # Create a generic driver, with test state data.
481 my $driver = HoneyClient::Agent::Driver->new(test => 1);
482 dies_ok {$driver->isFinished()} 'isFinished()' or diag("The isFinished() call failed. Expected isFinished() to throw an exception.");
483  
484 =end testing
485  
486 =cut
487  
488 sub isFinished {
489 # Get the class name.
490 my $self = shift;
491
492 # Check to see if the class name is inherited or defined.
493 my $class = ref($self) || $self;
494  
495 # Emit generic "not implemented" error message.
496 $LOG->error($class . "->isFinished() is not implemented!");
497 Carp::croak "Error: " . $class . "->isFinished() is not implemented!\n";
498 }
499  
500 =pod
501  
502 =head2 $object->next()
503  
504 =over 4
505  
506 Returns the next set of server hostnames and/or IP addresses that the
507 back-end application will contact, upon the next subsequent call to
508 the B<$object>'s drive() method.
509  
510 Specifically, the returned data is a reference to a hashtable, containing
511 detailed information about which resources, hostnames, IPs, protocols, and
512 ports that the application will contact upon the next iteration.
513  
514 Here is an example of such returned data:
515  
516 $hashref = {
517
518 # The set of servers that the driver will contact upon
519 # the next drive() operation.
520 targets => {
521 # The application will contact 'site.com' using
522 # TCP ports 80 and 81.
523 'site.com' => {
524 'tcp' => [ 80, 81 ],
525 },
526  
527 # The application will contact '192.168.1.1' using
528 # UDP ports 53 and 123.
529 '192.168.1.1' => {
530 'udp' => [ 53, 123 ],
531 },
532
533 # Or, more generically:
534 'hostname_or_IP' => {
535 'protocol_type' => [ portnumbers_as_list ],
536 },
537 },
538  
539 # The set of resources that the driver will operate upon
540 # the next drive() operation.
541 resources => {
542 'http://www.mitre.org/' => 1,
543 },
544 };
545  
546 B<Note>: For each hostname or IP address specified, if B<no>
547 corresponding protocol/port sub-hastables are given, then it
548 must be B<assumed> that the back-end application may contact
549 the hostname or IP address using B<ANY> protocol/port.
550  
551 I<Output>: The aforementioned B<$hashref> containing the next set of
552 resources that the back-end application will attempt to contact upon
553 the next drive() iteration.
554  
555 # XXX: Resolve this.
556  
557 B<Note>: Eventually this B<$hashref> will become a structured object,
558 created via a HoneyClient::Util::* package. However, the underlying
559 structure of this hashtable is not expected to change.
560  
561 =back
562  
563 =begin testing
564  
565 # Create a generic driver, with test state data.
566 my $driver = HoneyClient::Agent::Driver->new(test => 1);
567 dies_ok {$driver->next()} 'next()' or diag("The next() call failed. Expected next() to throw an exception.");
568  
569 =end testing
570  
571 =cut
572  
573 sub next {
574 # Get the class name.
575 my $self = shift;
576
577 # Check to see if the class name is inherited or defined.
578 my $class = ref($self) || $self;
579  
580 # Emit generic "not implemented" error message.
581 $LOG->error($class . "->next() is not implemented!");
582 Carp::croak "Error: " . $class . "->next() is not implemented!\n";
583 }
584  
585 =pod
586  
587 =head2 $object->status()
588  
589 =over 4
590  
591 Returns the current status of the Driver B<$object>, as it's state
592 exists, between subsequent calls to $object->driver().
593  
594 Specifically, the data returned is a reference to a hashtable,
595 containing specific statistical information about the status
596 of the Driver's progress during back-end application automation.
597  
598 As such, the exact structure of this returned hashtable is not strictly
599 defined. Instead, it is left up to each specific Driver implementation
600 to return useful, statistical information back to the Agent that
601 makes sense for the driven application.
602  
603 For example, if an Internet Explorer specific Driver were implemented,
604 then the corresponding status hashtable reference returned may look
605 something like:
606  
607 $hashref = {
608 'links_remaining' => 56, # Number of URLs left to process.
609 'links_processed' => 44, # Number of URLs processed.
610 'links_total' => 100, # Total number of URLs given.
611 'percent_complete' => '44.00%', # Percent complete.
612 };
613  
614 For another example, if an Outlook specific Driver were implemented,
615 then the corresponding status hashtable reference returned may look
616 something like:
617  
618 $hashref = {
619 'mail_remaining' => 56, # Number of messages left to process.
620 'mail_processed' => 44, # Number of messages processed.
621 'mail_total' => 100, # Total number of messages given.
622 'percent_complete' => '44.00%', # Percent complete.
623 };
624  
625 I<Output>: A corresponding B<$hashref>, containing statistical information
626 about the Driver's progress, as previously mentioned.
627  
628 # XXX: Resolve this.
629  
630 B<Note>: The exact structure of this status hashtable may become more
631 concrete, as we define a generic concept of a "unit of work" per every
632 iteration of the $object->drive() method. For example, it may be
633 likely that each Driver will attempt to contact a series of resources
634 per every "unit of work" iteration. As such, we may generically
635 record how many "work units" are remaining, processed, and total --
636 rather than specifically state "links" or "mail" within the hashtable
637 key names, accordingly.
638  
639 At the least, it can be assumed that even if a generic structure were
640 defined, we would leave room available in the status hashtable to
641 capture additional, implementation-specific statistics that are not
642 generic among every Driver implementation.
643  
644 =back
645  
646 =begin testing
647  
648 # Create a generic driver, with test state data.
649 my $driver = HoneyClient::Agent::Driver->new(test => 1);
650 dies_ok {$driver->status()} 'status()' or diag("The status() call failed. Expected status() to throw an exception.");
651  
652 =end testing
653  
654 =cut
655  
656 sub status {
657 # Get the class name.
658 my $self = shift;
659
660 # Check to see if the class name is inherited or defined.
661 my $class = ref($self) || $self;
662  
663 # Emit generic "not implemented" error message.
664 $LOG->error($class . "->next() is not implemented!");
665 Carp::croak "Error: " . $class . "->next() is not implemented!\n";
666 }
667  
668  
669 #######################################################################
670  
671 1;
672  
673 #######################################################################
674 # Additional Module Documentation #
675 #######################################################################
676  
677 __END__