Malware/Parser/NormanSandBoxLive.pm untested

filter: Statementstmt 0.0% 0 / 217 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 352 CC 42 SCAR 48.5
CC
42
Cov
0%
parser
342.0
parse_txtFile
132.0
init
30.0
CRAP 128.3
1 package Malware::Parser::NormanSandBoxLive;
2  
3 =head1 NAME
4  
5 Malware::Parser::NormanSandBoxLive - Perl extension for parsing norman sandbox email digests
6  
7 =head1 SYNOPSIS
8 use Data::Dumper;
9 use Malware::Parser::NormanSandBoxLive;
10 my $f = 'digest.zip';
11  
12 my $n = Malware::Parser::NormanSandBoxLive->new( -zip => $f, -cleanup => 1);
13  
14 my (@http,@bn);
15 foreach my $m (@{$n->malware()}){
16 if(my @a = $m->returnConnectionsByLayer_array(-type => 'url', -layer => 7, -protocol => 'http')){
17 push(@http,@a) if(@a);
18 }
19 if(my @a = $m->returnConnectionsByLayer_array(-type => 'dns', -layer => 7, -protocol => 'irc')){
20 push(@bn,@a) if(@a);
21 }
22 }
23  
24 warn Dumper(@http);
25 warn Dumper(@bn);
26  
27 =head1 DESCRIPTION
28  
29 This module is a parser for NormanSandBoxLive email digests (does the web too...but email is better if you can get it). From here you can take the malware behavior and use it to create detection rules etc...
30  
31 =cut
32  
33 use 5.008007;
34 use strict;
35 use warnings;
36 use Malware;
37 use Archive::Extract;
38 use Net::Protocol::Simple;
39 use Net::Connection::Simple;
40 use Class::ParmList qw(parse_parms);
41  
42 use constant AUTO => 1; # auto extract and parse?
43 use constant CLEAN => 1; # auto unlink() files?
44  
45 our $VERSION = '1.02';
46  
47 =head1 OBJECT METHODS
48  
49 =head2 new()
50  
51 my $n = Malware::Parser::NormanSandBoxLive->new(
52 -auto => 1, # default, do we want to auto extract the zip and parse? (yes, rgc, i am that lazy ;-)
53 -zip => $PATH_TO_ZIPFILE,
54 -path => $PATH_TO_SANDBOX_FILE # use this if you dont have the zip, filename must start with 'sandbox_'
55 -cleanup => 1, # clean up the zip file and its extractions after?
56 }
57  
58 =cut
59  
60 sub new {
61 my ($class,%parms) = @_;
62 my $self = {};
63 bless($self,$class);
64 $self->init(%parms);
65 return $self;
66 }
67  
68 sub init {
69 my ($self,%parms) = @_;
70 $parms{-auto} = AUTO() if(!$parms{-auto});
71 $self->zip( $parms{-zip});
72 $self->path( $parms{-path});
73 $self->cleanup( $parms{-cleanup});
74 $self->auto( $parms{-auto});
75  
76 if($self->auto()){
77 $self->extractFromZip() if($self->zip());
78 if($self->path()){
79 return $self->parse_txtFile();
80 }
81 else { die 'No Text file found...'."\n";}
82 }
83 }
84  
85 =head2 extractFromZip()
86  
87 Uses Archive::Extract to open up the zip file (used internally if you -auto on new()) and sets the path of the sandbox file.
88  
89 $n->zip($zipFilePath);
90 $n->extractFromZip();
91  
92 Returns 1 on sucess, dies() on error
93  
94 =cut
95  
96 sub extractFromZip {
97 my $self = shift;
98 my $fz = Archive::Extract->new( archive => $self->zip() );
99 my $ok = $fz->extract() || die $fz->error();
100 foreach my $x (@{$fz->files()}){
101 if((uc($x)) =~ /SANDBOX_/){
102 $self->path($x);
103 next;
104 }
105 unlink($x) if(!defined($self->cleanup()) || $self->cleanup() == 1);
106 }
107 unlink($self->zip()) if(!defined($self->cleanup()) || $self->cleanup() == 1);
108 return 1;
109 }
110  
111 =head2 parse_txtFile()
112  
113 Parses the main text file and pushes each malware object it creates into $self->malware()
114  
115 Returns 1 on success, dies() if the file can't be opened
116  
117 =cut
118  
119 sub parse_txtFile {
120 my $self = shift;
121 my ($ct,$s) = (0,0); # count,start
122 my $hr = {};
123 my (@a,$m,$tmpSec); # return array, tmp_maleware_placeholder, tmp_section
124  
125 open(F, $self->path()) || die 'can not open file: '.$self->path();
126 while(<F>){
127 chomp();
128 next unless length();
129 next if(/ \* \*\*IMPORTANT: PLEASE SEND THE SCANNED FILE TO/);
130 if($s == 0){
131 if(/^Detection:\s.*$/){ $s=1; }
132 else { next; }
133 }
134 if(/^Detection:\s(.*)$/){
135 if($ct > 0){
136 $m->rawReport($hr->{$ct});
137 $self->parser($m);
138 $self->malware($m);
139 }
140 $ct++;
141 my $platform = undef;
142 for(uc($1)){
143 if(/^W32/){
144 $platform = 'Win32';
145 last;
146 }
147 $self->unknown($1);
148 }
149 $m = Malware->new(
150 -source => 'NormanSandBox',
151 -sourceLoc => 'NormanEmailDigest',
152 -classification => $platform,
153 -platform => $platform,
154 );
155 next;
156 }
157 if(/\[\s(.*)\s\]/){
158 $tmpSec = $1;
159 next;
160 }
161 if(/\*\s(.*)\./){
162 push(@{$hr->{$ct}->{$tmpSec}},$1);
163 next;
164 }
165 }
166 close(F);
167 unlink($self->path()) if(!defined($self->cleanup()) || $self->cleanup() == 1);
168 return 1;
169 }
170  
171 # ACCESSORS / MODIFIERS
172  
173 =head1 ACCESSORS / MODIFIERS
174  
175 =head2 zip()
176  
177 Sets and Returns the zipFile path
178  
179 =cut
180  
181 sub zip {
182 my ($self,$v) = @_;
183 $self->{_zip} = $v if(defined($v));
184 return $self->{_zip};
185 }
186  
187 =head2 path()
188  
189 Sets and Returns the sandbox_ file path
190  
191 =cut
192  
193 sub path {
194 my ($self,$v) = @_;
195 $self->{_path} = $v if(defined($v));
196 return $self->{_path};
197 }
198  
199 =head2 auto()
200  
201 Sets and Returns auto property (auto parse)
202  
203 =cut
204  
205 sub auto {
206 my ($self,$v) = @_;
207 $self->{_auto} = $v if(defined($v));
208 return $self->{_auto};
209 }
210  
211 =head2 cleanup()
212  
213 Sets and Returns the cleanup property (do we unlink() files on parse?)
214  
215 =cut
216  
217 sub cleanup {
218 my ($self,$v) = @_;
219 $self->{_cleanup} = $v if(defined($v));
220 return $self->{_cleanup};
221 }
222  
223 =head2 malware()
224  
225 Stores an arrayref of malware objects.
226  
227 Accepts Malware Objects or return ($errstr,undef)
228  
229 Returns arrayref of malware objects
230  
231 =cut
232  
233 sub malware {
234 my ($self,$v) = @_;
235 if(defined($v)){
236 return ('expecting malware object', undef) if(!(ref($v) =~ /^Malware/));
237 push(@{$self->{_malware}},$v);
238 }
239 return $self->{_malware};
240 }
241  
242 =head2 unknown()
243  
244 Stores an array of strings that hold unknown classifications
245  
246 Accepts strings
247  
248 Returns arrayref of strings
249  
250 =cut
251  
252 sub unknown {
253 my ($self,$v) = @_;
254 push(@{$self->{_unknown}},$v) if(defined($v));
255 return $self->{_unknown};
256 }
257  
258 # Internal functions
259  
260 sub parser {
261 my ($self,$m) = @_;
262 my $hr = $m->rawReport();
263 foreach my $x (keys %$hr) {
264 for($x){
265 if(/^classification$/){
266 $m->classification($hr->{$x});
267 next;
268 }
269 if(/^Network services$/){
270 foreach my $y (@{$hr->{$x}}) {
271 for($y){
272 if(/^Connects to "(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)" on port (\d+) \((\S+)\)/){
273 my $c = Net::Connection::Simple->new();
274  
275 my $p = Net::Protocol::Simple->new(protocol => 'ip', layer => 3);
276 $p->{dip} = $1;
277 $c->protocols($p);
278  
279 $p = Net::Protocol::Simple->new(protocol => $3, layer => 4);
280 $p->{dport} = $2;
281 $c->protocols($p);
282  
283 $p = Net::Protocol::Simple->new(protocol => 'irc', layer => 7);
284 $c->protocols($p);
285  
286 $m->connections($c);
287 last;
288 }
289 if(/^Connects to "(\S+\.\S+\.\S+)" on port (\d+) \((\S+)\)/){
290 my $c = Net::Connection::Simple->new();
291 my $p;
292  
293 $p = Net::Protocol::Simple->new(protocol => 'ip', layer => 3);
294 $c->protocols($p);
295  
296 $p = Net::Protocol::Simple->new(protocol => $3, layer => 4);
297 $p->{_dport} = $2;
298 $c->protocols($p);
299  
300 $p = Net::Protocol::Simple->new(protocol => 'irc', layer => 7);
301 $p->{_dns} = $1;
302 $c->protocols($p);
303  
304 $m->connections($c);
305 last;
306 }
307 if(/^IRC: Joins channel (\S+) with password (\S+)/){
308 last;
309 }
310 if(/^Downloads file from (.*) as.*$/){
311 my $c = Net::Connection::Simple->new();
312 my $p;
313  
314 $p = Net::Protocol::Simple->new(protocol => 'ip', layer => 3);
315 $c->protocols($p);
316  
317 $p = Net::Protocol::Simple->new(protocol => 'tcp', layer => 4);
318 $p->{_dport} = 80;
319 $c->protocols($p);
320  
321 $p = Net::Protocol::Simple->new(protocol => 'HTTP', layer => 7);
322 $p->{_url} = $1;
323 $c->protocols($p);
324  
325 $m->connections($c);
326 last;
327 }
328 }
329 }
330 last;
331 }
332 if(/^General information$/){
333 foreach my $y (@{$hr->{$x}}) {
334 if($y =~ /^MD5 hash:\s(.*)$/){
335 $m->md5sum($1);
336 next;
337 }
338 if($y =~ /^Anti debug\/emulation code present$/){
339 $m->antiEmulation(1);
340 next;
341 }
342 if($y =~ /^File length:\s+(\d+)\s(\S+)$/){
343 $m->filesize($1);
344 $m->filesizeUnits($2);
345 }
346 }
347 last;
348 }
349 if(/^Changes to registry$/){
350 foreach my $y (@{$hr->{$x}}) {
351 $m->registry($y);
352 }
353 last;
354 }
355 if(/^Changes to filesystem$/){
356 foreach my $y (@{$hr->{$x}}) {
357 $m->filesystem($y);
358 }
359 last;
360 }
361 if(/^Security issues$/){
362 foreach my $y (@{$hr->{$x}}) {
363 if($y =~ /^Possible backdoor functionality \S+ port (\d+)$/){
364 my $p = Net::Protocol::Simple->new(protocol => 'tcp', layer => 4);
365 $p->{_sport} = $1;
366 $m->backdoors($p);
367 next;
368 }
369 $m->securityIssues($y);
370 }
371 last;
372 }
373 if(/^Process\/window information$/){
374 foreach my $y (@{$hr->{$x}}) {
375 $m->processInfo($y);
376 }
377 last;
378 }
379 if(/^Signature Scanning$/){
380 my $hr2;
381 foreach my $y (@{$hr->{$x}}) {
382 if($y =~ /^(.*)\s\(.*\)\s:\s(.*)$/){
383 $hr2->{$1} = $2;
384 }
385 }
386 $m->malwareFiles($hr2);
387 last;
388 }
389 }
390 }
391 }
392 1;
393 __END__