Agent/Driver/ActiveContent/Flash.pm untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 170 CC 9 SCAR 33.7
CC
9
Cov
0%
extract
72.0
_addURL
6.0
BEGIN
2.0
CRAP 29.0
1 #######################################################################
2 # Created on: December 14, 2006
3 # Package: HoneyClient::Agent::Driver::ActiveContent::Flash
4 # File: Flash.pm
5 # Description: An module used for extracting URLs from Adobe
6 # Flash (SWF) movies. If the URLs could not be
7 # extracted, but the movie appears "interesting,"
8 # then it is flagged for analyst review.
9 #
10 # CVS: $Id: Flash.pm 766 2007-07-26 02:46:00Z kindlund $
11 #
12 # @author jpuchalski
13 #
14 # Copyright (C) 2007 The MITRE Corporation. All rights reserved.
15 #
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation, using version 2
19 # of the License.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 # 02110-1301, USA.
30 #
31 #
32 #######################################################################
33  
34 =pod
35  
36 =head1 NAME
37  
38 HoneyClient::Agent::Driver::ActiveContent::Flash - Perl module that
39 extract URLs from Adobe Flash (SWF) movies. Returns a list of URLs.
40  
41 =head1 VERSION
42  
43 This documentation refers to
44 HoneyClient::Agent::Driver::ActiveContent::Flash version 0.1.
45  
46 =head1 SYNOPSIS
47  
48 use HoneyClient::Agent::Driver::ActiveContent::Flash;
49  
50  
51  
52 =head1 DESCRIPTION
53  
54 =cut
55  
56 package HoneyClient::Agent::Driver::ActiveContent::Flash;
57  
58 use strict;
59 use warnings;
60 use Carp ();
61  
62 #######################################################################
63 # Module Initialization #
64 #######################################################################
65  
66 BEGIN {
67 require Exporter;
68 our (@ISA, @EXPORT, @EXPORT_OK, $VERSION);
69  
70 # Set our package version.
71 $VERSION = 0.1;
72  
73 @ISA = qw(Exporter HoneyClient::Agent::Driver::ActiveContent);
74  
75 # Symbols to export by default
76 @EXPORT = qw();
77  
78 # Items to export into callers namespace by default. Note: do not export
79 # names by default without a very good reason. Use EXPORT_OK instead.
80 # Do not simply export all your public functions/methods/constants.
81
82 # Symbols to export on request
83 @EXPORT_OK = qw(extract);
84 }
85 our (@EXPORT_OK, $VERSION);
86  
87 #######################################################################
88  
89 # Include Global Configuration Processing Library
90 use HoneyClient::Util::Config qw(getVar);
91 use Log::Log4perl qw(:easy);
92  
93 # Include the Global Configuration Processing Library
94 use HoneyClient::Util::Config qw(getVar);
95  
96 # Include URL Parsing Library
97 use URI::URL;
98  
99 # Include Data Dumper API
100 use Data::Dumper;
101  
102 =pod
103  
104 =head1 GLOBAL VARIABLES
105  
106 =head2 flasm_exec
107  
108 =over 4
109  
110 Path to the flasm executable (default=./thirdparty/flasm/flasm.exe).
111  
112 =back
113  
114 =cut
115  
116 # Path to the flasm executable.
117 our $flasm_exec = getVar(name => "flasm_exec");
118  
119 # Our friendly local logger.
120 our $LOG = get_logger();
121  
122 # This variable holds the base URL reference for the SWF movie
123 # we are currently processing.
124 our $base_url;
125  
126  
127 #######################################################################
128 # Private Methods Implemented #
129 #######################################################################
130  
131 =pod
132  
133 =head1 PRIVATE METHODS
134  
135 =head2 HoneyClient::Agent::Driver::ActiveContent::Flash->_addURL($url, $urls)
136  
137 =over 4
138  
139 Adds the specified URL to the hash of URLs to be returned. Checks
140 to see if the URL is relative or absolute, and in the former case
141 appends the base URL appropriately.
142  
143 =back
144  
145 =cut
146  
147 sub _addURL {
148 # Extract arguments.
149 my ($url, $urls) = @_;
150  
151 # URL appears to be absolute, or a different protocol
152 if ($url =~ /^mailto/ or
153 $url =~ /^javascript/ or
154 $url =~ /^http/) {
155 $urls->{$url} = 1;
156 }
157 # URL appears to be relative, so add the base
158 else {
159 $url = url($url, $base_url)->abs;
160 $urls->{$url} = 1;
161 }
162 return $urls;
163 }
164  
165  
166 #######################################################################
167 # Public Methods Implemented #
168 #######################################################################
169  
170 =pod
171  
172 =head1 PUBLIC METHODS
173  
174 =head2 HoneyClient::Agent::Driver::ActiveContent::Flash->extract()
175  
176 =over 4
177  
178 Extracts URLs from an Adobe Flash SWF movie file. Takes in a file
179 name and a base URL, and uses the latter to construct relative URLs
180 to local links found in the movie. Returns a hash containing the
181 found URLs as keys, and values of 1 for each of them, where the
182 values represent the weights used by the link ranking code.
183  
184 =back
185  
186 =begin testing
187  
188 # Do testing here...
189 my $foo = HoneyClient::Something->blah(...);
190 dies_ok {$foo->dosomething()} 'dosomething()' or diag("The issomething() call failed.
191 Expected dosomething() to throw an exception.");
192  
193 =end testing
194  
195 =cut
196  
197 sub extract {
198 # Extract arguments.
199 my %args = @_;
200  
201 # Log resolved arguments.
202 $LOG->debug(sub {
203 # Make Dumper format more terse.
204 $Data::Dumper::Terse = 1;
205 $Data::Dumper::Indent = 0;
206 Dumper(\%args);
207 });
208  
209 # Put all the relative URLs that were retrieved into a hash as
210 # keys, but first turn them into full URLs. Set the value for
211 # each URL key to 1 (this is its score).
212 my $urls = {};
213  
214 my $filename = $args{'file'}->filename;
215  
216 # Extract the base URL.
217 $base_url = URI::URL->new($args{'url'});
218 $base_url = $base_url->canonical()->as_string();
219  
220 # Must encode all backslashes with double backslashes, since
221 # backtick commands don't like single backslashes.
222 if ($^O =~ m/win/i || $ENV{'OS'} =~ m/win/i) {
223 require Filesys::CygwinPaths;
224 $filename = Filesys::CygwinPaths::fullwin32path($filename);
225 $filename =~ s/\\/\\\\/g;
226 }
227  
228 # Call flasm and store the output bytecode string in an array
229 my $code = `$flasm_exec -d $filename`;
230  
231 # Check the return value on the flasm call that just happened.
232 # We care if the return code was anything other than 0.
233 if ($? >> 8) {
234 my $signal = ($? & 127);
235 $LOG->fatal("Call to flasm exited on signal $signal");
236 Carp::croak "Error: Call to flasm exited on signal $signal";
237 }
238  
239 # Strip out all control characters and place each line as
240 # separate entry in an array.
241 my @bytecode = split(/[[:cntrl:]]+/, $code);
242  
243 # Parse out lines that contain the getURL method (exclude any
244 # getURL2 calls from this, as they need to be handled differently)
245 my @geturl_calls = grep(/getURL /, @bytecode);
246  
247 # Each getURL line has getURL followed by the URL in single
248 # quotes. Extract the URL, remove the single quotes, and store
249 # the URL in a new array.
250 my @found_urls;
251  
252 foreach (@geturl_calls) {
253 $_ =~ s/^\s+//;
254 my ($fun, $url) = split(/\s+/);
255 $url =~ s/'//g;
256 push @found_urls, $url;
257 }
258  
259 foreach (@found_urls) {
260 $urls = _addURL($_, $urls);
261 }
262  
263 # We can exit here if there are no getURL2 calls
264 unless (grep(/getURL2/, @bytecode)) {
265 # TODO: Clean this up.
266 # Sanity check on the URLs.
267 foreach (sort(keys(%{$urls}))) {
268 $LOG->info($_);
269 }
270
271 return %{$urls};
272 }
273
274 # If we made it here, then at least one getURL2 call was
275 # detected. Proceed with additional processing.
276 $LOG->warn("Detected getURL2\n");
277  
278 # Before we forget, turn off unlink on destroy for our
279 # temporary file handle, so the file is kept around for
280 # analysts to look at later.
281 $args{'file'}->unlink_on_destroy(0);
282  
283 # What we do next is some parsing on the flasm decompilation.
284 # We are looking for getURL2 calls, and will then try to piece
285 # together the URLs that go into them.
286 my $i = 0;
287
288 foreach (@bytecode) {
289 # First, find a line containing a getURL2 call
290 if ($_ =~ /getURL2/) {
291 $LOG->info("Got a getURL2 on line " . ($i+1));
292  
293 my ($fun, $var, $val);
294 my $instr;
295 my $haveVar = 0;
296  
297 # Next, work backwards from the call. We start from $i - 2
298 # here because the $i - 1 line always contains the URL's target
299 # (e.g., '', '_parent', '_blank').
300 for (my $j = $i - 2; $bytecode[$j] !~ /^\s+constants/; $j--) {
301 # The first time through, look for a getVariable call, which
302 # tells us what variable has the value of the URL.
303 if (!$haveVar and $bytecode[$j] =~ /getVariable/) {
304 $LOG->info("Found getVariable on line " . ($j+1));
305 # The line before this one has the name of the
306 # variable that contains the URL
307 $instr = $bytecode[--$j];
308 $instr =~ s/^\s+//;
309 ($fun, $var) = split(/\s+/, $instr);
310 $LOG->info("Name of the URL variable is $var");
311 $haveVar = 1;
312 }
313 # Once we have the URL variable, we can look back further
314 # for the push call that sets its value.
315 elsif ($haveVar and $bytecode[$j] =~ /^\s+push $var/) {
316 $instr = $bytecode[$j];
317 $instr =~ s/^\s+//;
318 ($var, $val) = split(/, /, $instr);
319 # Sanity check.
320 if (defined($val)) {
321 $val =~ s/\s+$//;
322 $LOG->info("Value of the URL is $val");
323 $val =~ s/'//g;
324 $urls = _addURL($val, $urls);
325 }
326 last;
327 }
328 }
329 }
330  
331 $i++;
332 }
333  
334 # TODO: Clean this up.
335 # Sanity check on the URLs.
336 foreach (sort(keys(%{$urls}))) {
337 $LOG->info($_);
338 }
339  
340 return %{$urls};
341 }
342  
343 1;
344  
345 =pod
346  
347 =head1 AUTHORS
348  
349 Jeff Puchalski, E<lt>jpuchalski@mitre.orgE<gt>
350  
351 =head1 COPYRIGHT & LICENSE
352  
353 Copyright (C) 2007 The MITRE Corporation. All rights reserved.
354  
355 This program is free software; you can redistribute it and/or
356 modify it under the terms of the GNU General Public License
357 as published by the Free Software Foundation, using version 2
358 of the License.
359
360 This program is distributed in the hope that it will be useful,
361 but WITHOUT ANY WARRANTY; without even the implied warranty of
362 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
363 GNU General Public License for more details.
364
365 You should have received a copy of the GNU General Public License
366 along with this program; if not, write to the Free Software
367 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
368 02110-1301, USA.
369  
370 =cut