|
| 1 | +# SPDX-FileCopyrightText: 2025 Ladislav Baco |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + |
| 5 | +# -*- coding: utf-8 -*- |
| 6 | +""" |
| 7 | +Test with example reports (STIX objects usually collected from TAXII server) |
| 8 | +""" |
| 9 | +import unittest |
| 10 | + |
| 11 | +import re |
| 12 | +import requests_mock |
| 13 | + |
| 14 | +import intelmq.lib.bot as bot |
| 15 | +import intelmq.lib.test as test |
| 16 | +from intelmq.bots.parsers.stix.parser_eset import ESETStixParserBot |
| 17 | + |
| 18 | + |
| 19 | +EXAMPLE_REPORT = {'__type': 'Report', |
| 20 | + 'feed.name': 'Botnet feed', |
| 21 | + 'feed.code': 'botnet stix 2.1', |
| 22 | + 'feed.provider': 'ESET', |
| 23 | + 'feed.documentation': 'https://help.eset.com/eti_portal/en-US/botnet-feed.', |
| 24 | + 'feed.accuracy': 100.0, |
| 25 | + 'feed.url': 'https://taxii.eset.com/taxii2/643f4eb5-f8b7-46a3-a606-6d61d5ce223a/collections/0abb06690b0b47e49cd7794396b76b20/', |
| 26 | + 'raw': 'eyJpZCI6ICJpbmRpY2F0b3ItLTAiLCAidHlwZSI6ICJpbmRpY2F0b3IiLCAic3BlY192ZXJzaW9uIjogIjIuMSIsICJjcmVhdGVkIjogIjE5NzAtMDEtMDFUMDA6MDA6MDAuMDAwWiIsICJtb2RpZmllZCI6ICIxOTcwLTAxLTAxVDAwOjAwOjAwLjAwMFoiLCAicGF0dGVybiI6ICJbdXJsOnZhbHVlID0gJ2h0dHA6Ly9leGFtcGxlLm9yZyddIiwgInBhdHRlcm5fdHlwZSI6ICJzdGl4IiwgInZhbGlkX2Zyb20iOiAiMTk3MC0wMS0wMVQwMDowMDowMFoiLCAiZGVzY3JpcHRpb24iOiAiQyZDIGluZGljYXRlcyB0aGF0IGEgYm90bmV0IFdpbjMyL1NweS5MdW1tYVN0ZWFsZXIuQiB0cm9qYW4gaXMgcHJlc2VudC4iLCAibGFiZWxzIjogWyJtYWxpY2lvdXMtYWN0aXZpdHkiXX0=' |
| 27 | + } |
| 28 | + |
| 29 | +EXAMPLE_EVENT = {'__type': 'Event', |
| 30 | + 'feed.name': 'Botnet feed', |
| 31 | + 'feed.code': 'botnet stix 2.1', |
| 32 | + 'feed.provider': 'ESET', |
| 33 | + 'feed.documentation': 'https://help.eset.com/eti_portal/en-US/botnet-feed.', |
| 34 | + 'feed.accuracy': 100.0, |
| 35 | + 'feed.url': 'https://taxii.eset.com/taxii2/643f4eb5-f8b7-46a3-a606-6d61d5ce223a/collections/0abb06690b0b47e49cd7794396b76b20/', |
| 36 | + 'source.url': 'http://example.org', |
| 37 | + 'time.source': '1970-01-01T00:00:00+00:00', |
| 38 | + 'classification.type': 'c2-server', |
| 39 | + 'malware.name': 'lummastealer', |
| 40 | + 'comment': 'C&C indicates that a botnet Win32/Spy.LummaStealer.B trojan is present.', |
| 41 | + 'extra.labels': ['malicious-activity'], |
| 42 | + 'raw': 'eyJpZCI6ICJpbmRpY2F0b3ItLTAiLCAidHlwZSI6ICJpbmRpY2F0b3IiLCAic3BlY192ZXJzaW9uIjogIjIuMSIsICJjcmVhdGVkIjogIjE5NzAtMDEtMDFUMDA6MDA6MDAuMDAwWiIsICJtb2RpZmllZCI6ICIxOTcwLTAxLTAxVDAwOjAwOjAwLjAwMFoiLCAicGF0dGVybiI6ICJbdXJsOnZhbHVlID0gJ2h0dHA6Ly9leGFtcGxlLm9yZyddIiwgInBhdHRlcm5fdHlwZSI6ICJzdGl4IiwgInZhbGlkX2Zyb20iOiAiMTk3MC0wMS0wMVQwMDowMDowMFoiLCAiZGVzY3JpcHRpb24iOiAiQyZDIGluZGljYXRlcyB0aGF0IGEgYm90bmV0IFdpbjMyL1NweS5MdW1tYVN0ZWFsZXIuQiB0cm9qYW4gaXMgcHJlc2VudC4iLCAibGFiZWxzIjogWyJtYWxpY2lvdXMtYWN0aXZpdHkiXX0=' |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +class TestESETStixParserBot(test.BotTestCase, unittest.TestCase): |
| 47 | + """ |
| 48 | + A TestCase for an ESETStixParserBot. |
| 49 | + """ |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def set_bot(cls): |
| 53 | + cls.bot_reference = ESETStixParserBot |
| 54 | + cls.sysconfig = {} |
| 55 | + |
| 56 | + def test_event(self): |
| 57 | + """ Test if correct Event has been produced. """ |
| 58 | + self.input_message = EXAMPLE_REPORT |
| 59 | + self.run_bot() |
| 60 | + self.assertMessageEqual(0, EXAMPLE_EVENT) |
| 61 | + |
| 62 | + def test_classification_by_string(self): |
| 63 | + """ Test if correct classification based on string is returned. """ |
| 64 | + classification_type, malware_name = self.bot_reference.classify('Host actively distributes high-severity malicious content in the form of executable code.') |
| 65 | + self.assertEqual(str(classification_type), 'malware-distribution') |
| 66 | + self.assertEqual(malware_name, None) |
| 67 | + |
| 68 | + classification_type, malware_name = self.bot_reference.classify('Host is known source of phishing or other fraudulent content.') |
| 69 | + self.assertEqual(str(classification_type), 'phishing') |
| 70 | + self.assertEqual(malware_name, None) |
| 71 | + |
| 72 | + classification_type, malware_name = self.bot_reference.classify('Host is used as command and control server.') |
| 73 | + self.assertEqual(str(classification_type), 'c2-server') |
| 74 | + self.assertEqual(malware_name, None) |
| 75 | + |
| 76 | + classification_type, malware_name = self.bot_reference.classify('Web services scanning and attacks') |
| 77 | + self.assertEqual(str(classification_type), 'scanner') |
| 78 | + self.assertEqual(malware_name, None) |
| 79 | + |
| 80 | + classification_type, malware_name = self.bot_reference.classify('RDP bruteforce IP') |
| 81 | + self.assertEqual(str(classification_type), 'brute-force') |
| 82 | + self.assertEqual(malware_name, None) |
| 83 | + |
| 84 | + def test_classification_by_regex(self): |
| 85 | + """ Test if correct classification based on regex is returned. """ |
| 86 | + classification_type, malware_name = self.bot_reference.classify('C&C indicates that a botnet Win32/Spy.LummaStealer.B trojan is present.') |
| 87 | + self.assertEqual(str(classification_type), 'c2-server') |
| 88 | + self.assertEqual(str(malware_name), 'lummastealer') |
| 89 | + |
| 90 | + classification_type, malware_name = self.bot_reference.classify('C&C of Win32/Spy.LummaStealer.B trojan') |
| 91 | + self.assertEqual(str(classification_type), 'c2-server') |
| 92 | + self.assertEqual(str(malware_name), 'lummastealer') |
| 93 | + |
| 94 | + classification_type, malware_name = self.bot_reference.classify('Host is used as command and control server of Win32/Emotet.BN trojan malware family.') |
| 95 | + self.assertEqual(str(classification_type), 'c2-server') |
| 96 | + self.assertEqual(str(malware_name), 'emotet') |
| 97 | + |
| 98 | + classification_type, malware_name = self.bot_reference.classify('WizardNet backdoor.') |
| 99 | + self.assertEqual(str(classification_type), 'malware') |
| 100 | + self.assertEqual(str(malware_name), 'wizardnet') |
| 101 | + |
| 102 | + classification_type, malware_name = self.bot_reference.classify('Loader for Emotet') |
| 103 | + self.assertEqual(str(classification_type), 'malware') |
| 104 | + self.assertEqual(str(malware_name), 'emotet') |
| 105 | + |
| 106 | + def test_unknown_classification(self): |
| 107 | + """ Test if undetermined classification is returned when comment contains something unexpected. """ |
| 108 | + classification_type, malware_name = self.bot_reference.classify('Example of unexpected comment.') |
| 109 | + self.assertEqual(str(classification_type), 'undetermined') |
| 110 | + self.assertEqual(malware_name, None) |
| 111 | + |
| 112 | + def test_malware_family_name_extraction(self): |
| 113 | + """ Test if correct malwae family name is extracted from the given malware string. """ |
| 114 | + malware_name = self.bot_reference.extract_malware_family('Win32/Spy.LummaStealer.B') |
| 115 | + self.assertEqual(str(malware_name), 'lummastealer') |
| 116 | + |
| 117 | + malware_name = self.bot_reference.extract_malware_family('Win32/Rescoms.B') |
| 118 | + self.assertEqual(str(malware_name), 'rescoms') |
| 119 | + |
| 120 | + malware_name = self.bot_reference.extract_malware_family('Emotet') |
| 121 | + self.assertEqual(str(malware_name), 'emotet') |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == '__main__': # pragma: no cover |
| 125 | + unittest.main() |
0 commit comments