forked from Jack28/PeekabooAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·1198 lines (1027 loc) · 49.8 KB
/
test.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
###############################################################################
# #
# Peekaboo Extended Email Attachment Behavior Observation Owl #
# #
# test.py #
###############################################################################
# #
# Copyright (C) 2016-2019 science + computing ag #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or (at #
# your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
###############################################################################
""" The testsuite. """
from future.builtins import super # pylint: disable=wrong-import-order
import gettext
import sys
import os
import tempfile
import logging
import shutil
import unittest
from datetime import datetime, timedelta
# Add Peekaboo to PYTHONPATH
# pylint: disable=wrong-import-position
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from peekaboo.exceptions import PeekabooConfigException, \
PeekabooRulesetConfigError
from peekaboo.config import PeekabooConfig, PeekabooConfigParser
from peekaboo.sample import SampleFactory
from peekaboo.ruleset import RuleResult, Result
from peekaboo.ruleset.engine import RulesetEngine
from peekaboo.ruleset.rules import FileTypeOnWhitelistRule, \
FileTypeOnGreylistRule, CuckooAnalysisFailedRule, \
KnownRule, FileLargerThanRule, CuckooEvilSigRule, \
CuckooScoreRule, RequestsEvilDomainRule, FinalRule, \
OfficeMacroRule, OfficeMacroWithSuspiciousKeyword, \
ExpressionRule
from peekaboo.toolbox.cuckoo import CuckooReport
from peekaboo.db import PeekabooDatabase, PeekabooDatabaseError
# pylint: enable=wrong-import-position
""" Since Python 3.2 assertRegexpMatches and assertRaisesRegexp
have been renamed to assertRegex() and assertRaisesRegex(). """
if sys.version_info[0] < 3:
class CompatibleTestCase(unittest.TestCase):
def assertRaisesRegex(self, exc, r, callable=None, *args, **kwds):
return self.assertRaisesRegexp(exc, r, callable, *args, **kwds)
def assertRegex(self, text, regex, msg=None):
return self.assertRegexpMatches(text, regex, msg)
else:
class CompatibleTestCase(unittest.TestCase):
pass
class CreatingConfigMixIn(object):
""" A class for adding config file creation logic to any other class. """
def create_config(self, content):
""" Create a configuration file with defined content and pass it to the
parent constructor for parsing. """
_, self.created_config_file = tempfile.mkstemp()
with open(self.created_config_file, 'w') as file_desc:
file_desc.write(content)
def remove_config(self):
""" Remove the configuration file we've created. """
os.unlink(self.created_config_file)
class CreatingConfigParser(PeekabooConfigParser, CreatingConfigMixIn):
""" A special kind of config parser that creates the configuration file
with defined content. """
def __init__(self, content=''):
self.created_config_file = None
self.create_config(content)
PeekabooConfigParser.__init__(self, self.created_config_file)
def __del__(self):
self.remove_config()
class CreatingPeekabooConfig(PeekabooConfig, CreatingConfigMixIn):
""" A special kind of Peekaboo config that creates the configuration file
with defined content. """
def __init__(self, content=''):
self.created_config_file = None
self.create_config(content)
PeekabooConfig.__init__(self, self.created_config_file)
def __del__(self):
self.remove_config()
class TestConfigParser(CompatibleTestCase):
""" Test a configuration with all values different from the defaults. """
@classmethod
def setUpClass(cls):
""" Set up common test case resources. """
cls.config = CreatingConfigParser('''#[rule0]
[rule1]
option1: foo
option2.1: bar
option2.2: baz
[rules]
rule.1 : rule1
#rule.2 : rule2
rule.3 : rule3
''')
def test_2_values(self):
""" Test rule configuration values """
with self.assertRaises(KeyError):
self.config['rule0']
self.assertEqual(self.config['rule1']['option1'], 'foo')
self.assertEqual(self.config['rule1'].getlist('option2'),
['bar', 'baz'])
def test_3_type_mismatch(self):
""" Test correct error is thrown if the option type is mismatched """
config = '''[rule1]
option1: foo
option1.1: bar'''
with self.assertRaisesRegex(
PeekabooConfigException,
'Option option1 in section rule1 is supposed to be a list but '
'given as individual setting'):
CreatingConfigParser(config).getlist('rule1', 'option1')
class TestDefaultConfig(CompatibleTestCase):
""" Test a configuration of all defaults. """
@classmethod
def setUpClass(cls):
""" Set up common test case resources. """
cls.config = CreatingPeekabooConfig()
def test_1_default_settings(self):
""" Test a configuration with just defaults """
self.assertEqual(
self.config.config_file, self.config.created_config_file)
self.assertEqual(self.config.user, 'peekaboo')
self.assertEqual(self.config.group, 'peekaboo')
self.assertEqual(
self.config.sock_file, '/var/run/peekaboo/peekaboo.sock')
self.assertEqual(
self.config.pid_file, '/var/run/peekaboo/peekaboo.pid')
self.assertEqual(self.config.interpreter, '/usr/bin/python2 -u')
self.assertEqual(self.config.worker_count, 3)
self.assertEqual(self.config.sample_base_dir, '/tmp')
self.assertEqual(
self.config.job_hash_regex, '/amavis/tmp/([^/]+)/parts/')
self.assertEqual(self.config.use_debug_module, False)
self.assertEqual(self.config.keep_mail_data, False)
self.assertEqual(
self.config.processing_info_dir,
'/var/lib/peekaboo/malware_reports')
self.assertEqual(
self.config.ruleset_config, '/opt/peekaboo/etc/ruleset.conf')
self.assertEqual(self.config.log_level, logging.INFO)
self.assertEqual(
self.config.log_format, '%(asctime)s - %(name)s - '
'(%(threadName)s) - %(levelname)s - %(message)s')
self.assertEqual(self.config.db_url, 'sqlite:////var/lib/peekaboo/peekaboo.db')
self.assertEqual(self.config.cuckoo_mode, 'api')
self.assertEqual(self.config.cuckoo_exec, '/opt/cuckoo/bin/cuckoo')
self.assertEqual(self.config.cuckoo_submit, '/opt/cuckoo/bin/cuckoo submit')
self.assertEqual(self.config.cuckoo_storage, '/var/lib/peekaboo/.cuckoo/storage')
self.assertEqual(self.config.cuckoo_url, 'http://127.0.0.1:8090')
self.assertEqual(self.config.cuckoo_poll_interval, 5)
self.assertEqual(self.config.cluster_instance_id, 0)
self.assertEqual(self.config.cluster_stale_in_flight_threshold, 15*60)
self.assertEqual(self.config.cluster_duplicate_check_interval, 60)
class TestValidConfig(CompatibleTestCase):
""" Test a configuration with all values different from the defaults. """
@classmethod
def setUpClass(cls):
""" Set up common test case resources. """
cls.config = CreatingPeekabooConfig('''[global]
user : user1
group : group1
socket_file : /socket/1
pid_file : /pid/1
interpreter : /inter/1
worker_count : 18
sample_base_dir : /tmp/1
job_hash_regex : /var/2
use_debug_module : yes
keep_mail_data : yes
processing_info_dir : /var/3
[ruleset]
config : /rules/1
[logging]
log_level : DEBUG
log_format : format%%foo1
[db]
url : sqlite:////peekaboo.db1
[cuckoo]
mode : api1
exec : /cuckoo/1
submit : /submit/1
storage_path : /storage/1
url : http://api:1111
poll_interval : 51
[cluster]
instance_id: 12
stale_in_flight_threshold: 31
duplicate_check_interval: 61
''')
def test_1_read_settings(self):
""" Test reading of configuration settings from file """
self.assertEqual(
self.config.config_file, self.config.created_config_file)
self.assertEqual(self.config.user, 'user1')
self.assertEqual(self.config.group, 'group1')
self.assertEqual(self.config.sock_file, '/socket/1')
self.assertEqual(self.config.pid_file, '/pid/1')
self.assertEqual(self.config.interpreter, '/inter/1')
self.assertEqual(self.config.worker_count, 18)
self.assertEqual(self.config.sample_base_dir, '/tmp/1')
self.assertEqual(self.config.job_hash_regex, '/var/2')
self.assertEqual(self.config.use_debug_module, True)
self.assertEqual(self.config.keep_mail_data, True)
self.assertEqual(self.config.processing_info_dir, '/var/3')
self.assertEqual(self.config.ruleset_config, '/rules/1')
self.assertEqual(self.config.log_level, logging.DEBUG)
self.assertEqual(self.config.log_format, 'format%foo1')
self.assertEqual(self.config.db_url, 'sqlite:////peekaboo.db1')
self.assertEqual(self.config.cuckoo_mode, 'api1')
self.assertEqual(self.config.cuckoo_exec, '/cuckoo/1')
self.assertEqual(self.config.cuckoo_submit, '/submit/1')
self.assertEqual(self.config.cuckoo_storage, '/storage/1')
self.assertEqual(self.config.cuckoo_url, 'http://api:1111')
self.assertEqual(self.config.cuckoo_poll_interval, 51)
self.assertEqual(self.config.cluster_instance_id, 12)
self.assertEqual(self.config.cluster_stale_in_flight_threshold, 31)
self.assertEqual(self.config.cluster_duplicate_check_interval, 61)
class TestInvalidConfig(CompatibleTestCase):
""" Various tests of invalid config files. """
def test_1_section_header(self):
""" Test correct error is thrown if section header syntax is wrong """
with self.assertRaisesRegex(
PeekabooConfigException,
'Configuration file ".*" can not be parsed: File contains no '
'section headers'):
CreatingPeekabooConfig('''[global[
user: peekaboo''')
def test_2_value_separator(self):
""" Test correct error is thrown if the value separator is wrong """
with self.assertRaisesRegex(
PeekabooConfigException,
'Configuration file ".*" can not be parsed: (File|Source) '
'contains parsing errors:'):
CreatingPeekabooConfig('''[global]
user; peekaboo''')
def test_3_section_header(self):
""" Test correct error is thrown if the config file is missing """
_, config_file = tempfile.mkstemp()
os.unlink(config_file)
with self.assertRaisesRegex(
PeekabooConfigException,
'Configuration file "%s" can not be opened for reading: '
r'\[Errno 2\] No such file or directory' % config_file):
PeekabooConfig(config_file)
def test_4_unknown_section(self):
""" Test correct error is thrown if an unknown section name is given.
"""
with self.assertRaisesRegex(
PeekabooConfigException,
r'Unknown section\(s\) found in config: globl'):
CreatingPeekabooConfig('''[globl]''')
def test_5_unknown_option(self):
""" Test correct error is thrown if an unknown option name is given.
"""
with self.assertRaisesRegex(
PeekabooConfigException,
r'Unknown config option\(s\) found in section global: foo'):
CreatingPeekabooConfig('''[global]
foo: bar''')
def test_6_unknown_loglevel(self):
""" Test with an unknown log level """
with self.assertRaisesRegex(
PeekabooConfigException,
'Unknown log level FOO'):
CreatingPeekabooConfig('''[logging]
log_level: FOO''')
class CreatingSampleFactory(SampleFactory):
""" A special kind of sample factory that creates the sample files with
defined content in a temporary directory and cleans up after itself. """
def __init__(self, *args, **kwargs):
self.directory = tempfile.mkdtemp()
super().__init__(*args, **kwargs)
def create_sample(self, relpath, content, *args, **kwargs):
""" Make a new sample with defined base name and content in the
previously created temporary directory. The given basename can
optionally be a path relative to the temporary directory and the
subdirectory will be created automatically. """
file_path = os.path.join(self.directory, relpath)
subdir = os.path.dirname(file_path)
if subdir != self.directory:
os.makedirs(subdir)
with open(file_path, 'w') as file_desc:
file_desc.write(content)
return super().make_sample(file_path, *args, **kwargs)
def __del__(self):
""" Remove the sample files we've created and the temporary directory
itself. """
shutil.rmtree(self.directory)
class TestDatabase(CompatibleTestCase):
""" Unittests for Peekaboo's database module. """
@classmethod
def setUpClass(cls):
""" Set up common test case resources. """
cls.test_db = os.path.abspath('./test.db')
cls.conf = CreatingPeekabooConfig()
cls.db_con = PeekabooDatabase('sqlite:///' + cls.test_db,
instance_id=1,
stale_in_flight_threshold=10)
cls.no_cluster_db = PeekabooDatabase('sqlite:///' + cls.test_db,
instance_id=0)
cls.factory = CreatingSampleFactory(
cuckoo=None, base_dir=cls.conf.sample_base_dir,
job_hash_regex=cls.conf.job_hash_regex, keep_mail_data=False,
processing_info_dir=None)
cls.sample = cls.factory.create_sample('test.py', 'test')
result = RuleResult('Unittest',
Result.failed,
'This is just a test case.',
further_analysis=False)
cls.sample.add_rule_result(result)
def test_1_analysis_save(self):
""" Test saving of analysis results. """
self.db_con.analysis_save(self.sample)
def test_2_sample_info_fetch(self):
""" Test retrieval of analysis results. """
sample_info = self.db_con.sample_info_fetch(self.sample)
self.assertEqual(sample_info.sha256sum, self.sample.sha256sum)
self.assertEqual(sample_info.result, Result.failed)
self.assertEqual(sample_info.reason, 'This is just a test case.')
def test_5_in_flight_no_cluster(self):
""" Test that marking of samples as in-flight on a non-cluster-enabled
database are no-ops. """
self.assertTrue(self.no_cluster_db.mark_sample_in_flight(self.sample))
self.assertTrue(self.no_cluster_db.mark_sample_in_flight(self.sample))
self.assertIsNone(self.no_cluster_db.clear_sample_in_flight(self.sample))
self.assertIsNone(self.no_cluster_db.clear_sample_in_flight(self.sample))
self.assertIsNone(self.no_cluster_db.clear_in_flight_samples())
def test_6_in_flight_cluster(self):
""" Test marking of samples as in-flight. """
self.assertTrue(self.db_con.mark_sample_in_flight(self.sample, 1))
# re-locking the same sample should fail
self.assertFalse(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertIsNone(self.db_con.clear_sample_in_flight(self.sample, 1))
# unlocking twice should fail
self.assertRaisesRegex(
PeekabooDatabaseError, "Unexpected inconsistency: Sample .* not "
"recoreded as in-flight upon clearing flag",
self.db_con.clear_sample_in_flight, self.sample, 1)
def test_7_in_flight_clear(self):
""" Test clearing of in-flight markers. """
sample2 = self.factory.create_sample('foo.pyc', 'foo')
sample3 = self.factory.create_sample('bar.pyc', 'bar')
self.assertTrue(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertTrue(self.db_con.mark_sample_in_flight(sample2, 1))
self.assertTrue(self.db_con.mark_sample_in_flight(sample3, 2))
# should only clear samples of instance 1
self.assertIsNone(self.db_con.clear_in_flight_samples(1))
self.assertTrue(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertTrue(self.db_con.mark_sample_in_flight(sample2, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample3, 2))
# should only clear samples of instance 2
self.assertIsNone(self.db_con.clear_in_flight_samples(2))
self.assertFalse(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample2, 1))
self.assertTrue(self.db_con.mark_sample_in_flight(sample3, 2))
# should clear all samples
self.assertIsNone(self.db_con.clear_in_flight_samples(-1))
self.assertTrue(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertTrue(self.db_con.mark_sample_in_flight(sample2, 1))
self.assertTrue(self.db_con.mark_sample_in_flight(sample3, 2))
# should be a no-op because there will never be any entries of instance
# 0
self.assertIsNone(self.db_con.clear_in_flight_samples(0))
self.assertFalse(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample2, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample3, 2))
# should be a no-op because this database is not cluster-enabled
self.assertIsNone(self.no_cluster_db.clear_in_flight_samples())
self.assertFalse(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample2, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample3, 2))
# leave as found
self.assertIsNone(self.db_con.clear_in_flight_samples(-1))
def test_8_stale_in_flight(self):
""" Test the cleaning of stale in-flight markers. """
stale = datetime.utcnow() - timedelta(seconds=20)
self.assertTrue(self.db_con.mark_sample_in_flight(
self.sample, 1, stale))
sample2 = self.factory.create_sample('baz.pyc', 'baz')
self.assertTrue(self.db_con.mark_sample_in_flight(sample2, 1))
# should not clear anything because the database is not cluster-enabled
self.assertTrue(self.no_cluster_db.clear_stale_in_flight_samples())
self.assertFalse(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample2, 1))
# should clear sample marker because it is stale but not sample2
self.assertTrue(self.db_con.clear_stale_in_flight_samples())
self.assertTrue(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample2, 1))
# should not clear anything because all markers are fresh
self.assertFalse(self.db_con.clear_stale_in_flight_samples())
self.assertFalse(self.db_con.mark_sample_in_flight(self.sample, 1))
self.assertFalse(self.db_con.mark_sample_in_flight(sample2, 1))
# set up new constellation
self.assertIsNone(self.db_con.clear_in_flight_samples(-1))
self.assertTrue(self.db_con.mark_sample_in_flight(
self.sample, 1, stale))
self.assertTrue(self.db_con.mark_sample_in_flight(sample2, 1, stale))
# should clear all markers because all are stale
self.assertTrue(self.db_con.clear_stale_in_flight_samples())
self.assertTrue(self.db_con.mark_sample_in_flight(
self.sample, 1, stale))
self.assertTrue(self.db_con.mark_sample_in_flight(sample2, 1, stale))
# leave as found
self.assertTrue(self.db_con.clear_stale_in_flight_samples())
@classmethod
def tearDownClass(cls):
""" Clean up after the tests. """
os.unlink(cls.test_db)
# test framework doesn't seem to give up reference so that __del__ is
# never run
del cls.factory
class TestSample(CompatibleTestCase):
""" Unittests for Samples. """
@classmethod
def setUpClass(cls):
""" Set up common test case resources. """
cls.test_db = os.path.abspath('./test.db')
cls.conf = CreatingPeekabooConfig()
cls.db_con = PeekabooDatabase('sqlite:///' + cls.test_db)
cls.factory = CreatingSampleFactory(
cuckoo=None, base_dir=cls.conf.sample_base_dir,
job_hash_regex=cls.conf.job_hash_regex, keep_mail_data=False,
processing_info_dir=None)
cls.sample = cls.factory.create_sample('test.py', 'test')
def test_job_hash_regex(self):
""" Test extraction of the job hash from the working directory path.
"""
# class sample has no job hash in path and therefore generates one
# itself
self.assertIn('peekaboo-run_analysis', self.sample.job_hash)
# a new sample with a job hash in it's path should return it
job_hash = 'amavis-20170831T132736-07759-iSI0rJ4b'
path_with_job_hash = 'd/var/lib/amavis/tmp/%s/parts/file' % job_hash
sample = self.factory.make_sample(path_with_job_hash, 'file')
self.assertEqual(job_hash, sample.job_hash,
'Job hash regex is not working')
legacy_factory = CreatingSampleFactory(
cuckoo=None, base_dir=self.conf.sample_base_dir,
job_hash_regex=r'/var/lib/amavis/tmp/([^/]+)/parts.*',
keep_mail_data=False, processing_info_dir=None)
sample = legacy_factory.make_sample(path_with_job_hash, 'file')
self.assertEqual(job_hash, sample.job_hash,
'Job hash regex is not working')
def test_3_sample_attributes(self):
""" Test the various sample attribute getters. """
self.assertEqual(self.sample.file_path,
os.path.join(self.factory.directory, 'test.py'))
self.assertEqual(self.sample.filename, 'test.py')
self.assertEqual(self.sample.file_extension, 'py')
self.assertTrue(set(['text/x-python']).issubset(self.sample.mimetypes))
self.assertEqual(
self.sample.sha256sum,
'9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')
self.assertEqual(self.sample.job_id, -1)
self.assertEqual(self.sample.result, Result.unchecked)
self.assertEqual(self.sample.reason, None)
self.assertRegex(
self.sample.peekaboo_report[0],
'File "%s" is considered "unchecked"'
% self.sample.filename)
self.assertEqual(self.sample.cuckoo_report, None)
self.assertEqual(self.sample.done, False)
self.assertEqual(self.sample.submit_path, None)
self.assertEqual(self.sample.file_size, 4)
def test_4_initialised_sample_attributes(self):
""" Test the various sample attributes of an initialised sample. """
self.sample.init()
self.assertEqual(self.sample.file_path,
os.path.join(self.factory.directory, 'test.py'))
self.assertEqual(self.sample.filename, 'test.py')
self.assertEqual(self.sample.file_extension, 'py')
self.assertTrue(set(['text/x-python']).issubset(self.sample.mimetypes))
self.assertEqual(
self.sample.sha256sum,
'9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')
self.assertEqual(self.sample.job_id, -1)
self.assertEqual(self.sample.result, Result.unchecked)
self.assertEqual(self.sample.reason, None)
self.assertRegex(
self.sample.peekaboo_report[0], 'File "%s" %s is being analyzed'
% (self.sample.filename, self.sample.sha256sum))
self.assertRegex(
self.sample.peekaboo_report[1],
'File "%s" is considered "unchecked"'
% self.sample.filename)
self.assertEqual(self.sample.cuckoo_report, None)
self.assertEqual(self.sample.done, False)
self.assertRegex(
self.sample.submit_path, '/%s.py$' % self.sample.sha256sum)
self.assertEqual(self.sample.file_size, 4)
def test_5_mark_done(self):
""" Test the marking of a sample as done. """
self.sample.mark_done()
self.assertEqual(self.sample.done, True)
def test_6_add_rule_result(self):
""" Test the adding of a rule result. """
reason = 'This is just a test case.'
result = RuleResult('Unittest', Result.failed,
reason,
further_analysis=False)
self.sample.add_rule_result(result)
self.assertEqual(self.sample.result, Result.failed)
self.assertEqual(self.sample.reason, reason)
def test_sample_attributes_with_meta_info(self):
""" Test use of optional meta data. """
sample = self.factory.make_sample(
'test.pyc', metainfo={
'full_name': '/tmp/test.pyc',
'name_declared': 'test.pyc',
'type_declared': 'application/x-bytecode.python',
'type_long': 'application/x-python-bytecode',
'type_short': 'pyc',
'size': '200'})
self.assertEqual(sample.file_extension, 'pyc')
def test_sample_without_suffix(self):
""" Test extraction of file extension from declared name. """
sample = self.factory.make_sample(
'junk', metainfo={
'full_name': '/tmp/junk',
'name_declared': 'Report.docx',
'type_declared': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type_long': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type_short': 'docx',
'size': '212'})
self.assertEqual(sample.file_extension, 'docx')
@classmethod
def tearDownClass(cls):
""" Clean up after the tests. """
os.unlink(cls.test_db)
del cls.factory
class TestRulesetEngine(CompatibleTestCase):
""" Unittests for the Ruleset Engine. """
def test_no_rules_configured(self):
""" Test that correct error is shown if no rules are configured. """
config = CreatingConfigParser()
with self.assertRaisesRegex(
PeekabooRulesetConfigError,
r'No enabled rules found, check ruleset config.'):
RulesetEngine(ruleset_config=config, db_con=None)
def test_unknown_rule_enabled(self):
""" Test that correct error is shown if an unknown rule is enabled. """
config = CreatingConfigParser('''[rules]
rule.1: foo''')
with self.assertRaisesRegex(
PeekabooRulesetConfigError,
r'Unknown rule\(s\) enabled: foo'):
RulesetEngine(ruleset_config=config, db_con=None)
def test_invalid_type(self):
""" Test that correct error is shown if rule config option has wrong
type. """
config = CreatingConfigParser('''[rules]
rule.1: cuckoo_score
[cuckoo_score]
higher_than: foo''')
with self.assertRaisesRegex(
ValueError,
r"could not convert string to float: '?foo'?"):
RulesetEngine(ruleset_config=config, db_con=None)
def test_disabled_config(self):
""" Test that no error is shown if disabled rule has config. """
config = CreatingConfigParser('''[rules]
rule.1: known
#rule.2: cuckoo_score
[cuckoo_score]
higher_than: 4.0''')
RulesetEngine(ruleset_config=config, db_con=None)
class MimetypeSample(object): # pylint: disable=too-few-public-methods
""" A dummy sample class that only contains a set of MIME types for testing
whitelist and greylist rules with it. """
def __init__(self, types):
# don't even need to make it a property
self.mimetypes = set(types)
class CuckooReportSample(object): # pylint: disable=too-few-public-methods
""" A dummy sample that only contains a configurable cuckoo report. """
def __init__(self, report):
self.cuckoo_report = CuckooReport(report)
class TestRules(CompatibleTestCase):
""" Unittests for Rules. """
@classmethod
def setUpClass(cls):
""" Set up common test case resources. """
cls.config = CreatingConfigParser('''[file_type_on_whitelist]
whitelist.1 : text/plain
[file_type_on_greylist]
greylist.1 : application/x-dosexec
greylist.2 : application/zip
greylist.3 : application/msword
[cuckoo_analysis_failed]
failure.1: end of analysis reached!
success.1: analysis completed successfully''')
def test_config_known(self): # pylint: disable=no-self-use
""" Test the known rule configuration. """
config = '''[known]
unknown : baz'''
# there is no exception here since empty config is acceptable
KnownRule(CreatingConfigParser())
# there is no exception here since the known rule simply does
# not look at the configuration at all - maybe we should have a
# 'unknown section' error here
KnownRule(CreatingConfigParser(config))
def test_config_file_larger_than(self):
""" Test the file larger than rule configuration. """
config = '''[file_larger_than]
bytes : 10
unknown : baz'''
# there is no exception here since empty config is acceptable
FileLargerThanRule(CreatingConfigParser())
with self.assertRaisesRegex(
PeekabooConfigException,
r'Unknown config option\(s\) found in section '
r'file_larger_than: unknown'):
FileLargerThanRule(CreatingConfigParser(config))
def test_rule_file_type_on_whitelist(self):
""" Test whitelist rule. """
combinations = [
[False, ['text/plain']],
[True, ['application/vnd.ms-excel']],
[True, ['text/plain', 'application/vnd.ms-excel']],
[True, ['image/png', 'application/zip', 'application/vnd.ms-excel']],
[True, ['', 'asdfjkl', '93219843298']],
[True, []],
]
rule = FileTypeOnWhitelistRule(self.config)
for expected, types in combinations:
result = rule.evaluate(MimetypeSample(types))
self.assertEqual(result.further_analysis, expected)
def test_rule_office_ole(self):
""" Test rule office_ole. """
config = '''[office_macro_with_suspicious_keyword]
keyword.1 : AutoOpen
keyword.2 : AutoClose
keyword.3 : suSPi.ious'''
rule = OfficeMacroWithSuspiciousKeyword(CreatingConfigParser(config))
# sampe factory to create samples
factory = CreatingSampleFactory(
cuckoo=None, base_dir=None, job_hash_regex=None,
keep_mail_data=False, processing_info_dir=None)
tests_data_dir = os.path.dirname(os.path.abspath(__file__))+"/test-data"
combinations = [
# no office document file extension
[Result.unknown, factory.create_sample('test.nodoc', 'test')],
# test with empty file
[Result.unknown, factory.make_sample(tests_data_dir+'/office/empty.doc')],
# office document with 'suspicious' in macro code
[Result.bad, factory.make_sample(tests_data_dir+'/office/suspiciousMacro.doc')],
# test with blank word doc
[Result.unknown, factory.make_sample(tests_data_dir+'/office/blank.doc')],
# test with legitimate macro
[Result.unknown, factory.make_sample(tests_data_dir+'/office/legitmacro.xls')]
]
for expected, sample in combinations:
result = rule.evaluate(sample)
self.assertEqual(result.result, expected)
# test if macro present
rule = OfficeMacroRule(CreatingConfigParser(config))
combinations = [
# no office document file extension
[Result.unknown, factory.create_sample('test.nodoc', 'test')],
# test with empty file
[Result.unknown, factory.make_sample(tests_data_dir+'/office/empty.doc')],
# office document with 'suspicious' in macro code
[Result.bad, factory.make_sample(tests_data_dir+'/office/suspiciousMacro.doc')],
# test with blank word doc
[Result.unknown, factory.make_sample(tests_data_dir+'/office/blank.doc')],
# test with legitimate macro
[Result.bad, factory.make_sample(tests_data_dir+'/office/legitmacro.xls')]
]
for expected, sample in combinations:
result = rule.evaluate(sample)
self.assertEqual(result.result, expected)
def test_rule_ignore_generic_whitelist(self):
""" Test rule to ignore file types on whitelist. """
config = '''[expressions]
expression.4 : sample.mimetypes <= {'text/plain', 'inode/x-empty', 'image/jpeg'} -> ignore
'''
factory = CreatingSampleFactory(
cuckoo=None, base_dir="",
job_hash_regex="", keep_mail_data=False,
processing_info_dir=None)
sample = factory.create_sample('file.txt', 'abc')
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.ignored)
sample = factory.create_sample('file.html', '<html')
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.unknown)
# bzip2 compressed data
sample = factory.create_sample('file.txt', 'BZh91AY=')
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.unknown)
def test_rule_ignore_no_name_declared(self):
""" Test rule to ignore file with no name_declared. """
config = '''[expressions]
expression.3 : not sample.name_declared -> ignore
'''
factory = CreatingSampleFactory(
cuckoo=None, base_dir="",
job_hash_regex="", keep_mail_data=False,
processing_info_dir=None)
part = {"full_name": "file1.gif",
"name_declared": "file1.gif",
"type_declared": "image/gif"
}
sample = factory.create_sample('file1.gif', 'GIF87...', metainfo=part)
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.unknown)
sample = factory.create_sample('file2.gif', 'GIF87...')
sample.meta_info_name_declared = None
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.ignored)
config = '''[expressions]
expression.3 : sample.name_declared is None -> ignore
'''
sample = factory.create_sample('file2.gif', 'GIF87...')
sample.meta_info_name_declared = None
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.ignored)
def test_rule_ignore_mail_signatures(self):
""" Test rule to ignore cryptographic mail signatures. """
config = '''[expressions]
expression.1 : sample.meta_info_name_declared == 'smime.p7s'
and sample.meta_info_type_declared in {
'application/pkcs7-signature',
'application/x-pkcs7-signature',
'application/pkcs7-mime',
'application/x-pkcs7-mime'
} -> ignore
expression.2 : sample.meta_info_name_declared == 'signature.asc'
and sample.meta_info_type_declared in {
'application/pgp-signature'
} -> ignore'''
part = {"full_name": "p001",
"name_declared": "smime.p7s",
"type_declared": "application/pkcs7-signature"
}
factory = SampleFactory(
cuckoo=None, base_dir=None, job_hash_regex=None,
keep_mail_data=False, processing_info_dir=None)
# test smime signatures
sample = factory.make_sample('', metainfo=part)
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.ignored)
sample.meta_info_name_declared = "file"
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.unknown)
# test gpg signatures
sample.meta_info_name_declared = "signature.asc"
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.unknown)
sample.meta_info_type_declared = "application/pgp-signature"
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.ignored)
def test_rule_expressions(self):
""" Test generic rule on cuckoo report. """
config = '''[expressions]
expression.1 : /DDE/ in cuckooreport.signature_descriptions -> bad
'''
report = {
"signatures": [
{ "description": "Malicious document featuring Office DDE has been identified" }
]
}
cuckooreport = CuckooReport(report)
factory = SampleFactory(
cuckoo=None, base_dir=None, job_hash_regex=None,
keep_mail_data=False, processing_info_dir=None)
sample = factory.make_sample('')
sample.register_cuckoo_report(cuckooreport)
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.bad)
def test_rule_expressions_cuckooreport_context(self):
""" Test generic rule cuckooreport context """
config = '''[expressions]
expression.3 : "EVIL" in cuckooreport.signature_descriptions
and cuckooreport.score > 4 -> bad
'''
factory = CreatingSampleFactory(
cuckoo=None, base_dir=None, job_hash_regex=None,
keep_mail_data=False, processing_info_dir=None)
tests_data_dir = os.path.dirname(os.path.abspath(__file__))+"/test-data"
report = {
"signatures": [
{"description": "EVIL"}
],
"info": {"score": 4.2}
}
cuckooreport = CuckooReport(report)
sample = factory.make_sample(tests_data_dir+'/office/blank.doc')
sample.register_cuckoo_report(cuckooreport)
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.bad)
def test_rule_expressions_olereport_context(self):
""" Test generic rule olereport context """
config = '''[expressions]
expression.3 : sample.file_extension in {'doc', 'rtf'} and olereport.has_office_macros == True -> bad
'''
factory = CreatingSampleFactory(
cuckoo=None, base_dir=None, job_hash_regex=None,
keep_mail_data=False, processing_info_dir=None)
tests_data_dir = os.path.dirname(os.path.abspath(__file__))+"/test-data"
sample = factory.make_sample(tests_data_dir+'/office/blank.doc')
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.unknown)
sample = factory.make_sample(tests_data_dir+'/office/example.rtf')
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.unknown)
sample = factory.make_sample(tests_data_dir+'/office/suspiciousMacro.rtf')
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.bad)
sample = factory.make_sample(tests_data_dir+'/office/suspiciousMacro.doc')
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.bad)
config = '''[expressions]
expression.3 : /suspicious/ in olereport.vba_code -> bad
'''
rule = ExpressionRule(CreatingConfigParser(config))
result = rule.evaluate(sample)
self.assertEqual(result.result, Result.bad)
def test_config_file_type_on_whitelist(self):
""" Test whitelist rule configuration. """
config = '''[file_type_on_whitelist]
whitelist.1 : foo/bar
unknown : baz'''
with self.assertRaisesRegex(
PeekabooRulesetConfigError,
r'Empty whitelist, check file_type_on_whitelist rule config.'):
FileTypeOnWhitelistRule(CreatingConfigParser())