|
| 1 | +# SPDX-FileCopyrightText: 2021 Birger Schacht |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + |
| 5 | +# -*- coding: utf-8 -*- |
| 6 | + |
| 7 | +import os |
| 8 | +import unittest |
| 9 | + |
| 10 | +import intelmq.lib.test as test |
| 11 | +import intelmq.lib.utils as utils |
| 12 | +from intelmq.bots.parsers.shadowserver.parser import ShadowserverParserBot |
| 13 | + |
| 14 | +with open(os.path.join(os.path.dirname(__file__), |
| 15 | + 'testdata/event4_microsoft_sinkhole_http.csv')) as handle: |
| 16 | + EXAMPLE_FILE = handle.read() |
| 17 | +EXAMPLE_LINES = EXAMPLE_FILE.splitlines() |
| 18 | + |
| 19 | +EXAMPLE_REPORT = {'feed.name': 'HTTP Microsoft Sinkhole IPv4', |
| 20 | + "raw": utils.base64_encode(EXAMPLE_FILE), |
| 21 | + "__type": "Report", |
| 22 | + "time.observation": "2015-01-01T00:00:00+00:00", |
| 23 | + "extra.file_name": "2019-01-01-event4_microsoft_sinkhole_http.csv", |
| 24 | + } |
| 25 | +EVENTS = [{'__type': 'Event', |
| 26 | + 'feed.name': 'HTTP Microsoft Sinkhole IPv4', |
| 27 | + 'classification.identifier': 'necurs', |
| 28 | + 'classification.taxonomy': 'malicious-code', |
| 29 | + 'classification.type': 'infected-system', |
| 30 | + 'destination.asn': 8075, |
| 31 | + 'destination.geolocation.cc': 'US', |
| 32 | + 'destination.geolocation.city': 'ASHBURN', |
| 33 | + 'destination.geolocation.region': 'VIRGINIA', |
| 34 | + 'destination.ip': '40.121.206.97', |
| 35 | + 'destination.port': 80, |
| 36 | + 'destination.url': 'http://40.121.206.97/locator.php', |
| 37 | + 'extra.destination.naics': 334111, |
| 38 | + 'extra.destination.sector': 'Information', |
| 39 | + 'extra.public_source': 'MSDCU', |
| 40 | + 'malware.name': 'necurs', |
| 41 | + 'protocol.application': 'http', |
| 42 | + 'protocol.transport': 'tcp', |
| 43 | + 'raw': utils.base64_encode('\n'.join([EXAMPLE_LINES[0], |
| 44 | + EXAMPLE_LINES[1]])), |
| 45 | + 'source.asn': 8386, |
| 46 | + 'source.geolocation.cc': 'TR', |
| 47 | + 'source.geolocation.city': 'KEPEZ', |
| 48 | + 'source.geolocation.region': 'ANTALYA', |
| 49 | + 'source.ip': '31.206.1.2', |
| 50 | + 'source.port': 49245, |
| 51 | + 'time.observation': '2015-01-01T00:00:00+00:00', |
| 52 | + 'time.source': '2021-06-07T00:00:00+00:00'}, |
| 53 | + {'__type': 'Event', |
| 54 | + 'feed.name': 'HTTP Microsoft Sinkhole IPv4', |
| 55 | + 'classification.identifier': 'caphaw', |
| 56 | + 'classification.taxonomy': 'malicious-code', |
| 57 | + 'classification.type': 'infected-system', |
| 58 | + 'destination.asn': 8075, |
| 59 | + 'destination.fqdn': '3fo8jrthz3y.rgk.cc', |
| 60 | + 'destination.geolocation.cc': 'US', |
| 61 | + 'destination.geolocation.city': 'REDMOND', |
| 62 | + 'destination.geolocation.region': 'WASHINGTON', |
| 63 | + 'destination.ip': '204.95.99.204', |
| 64 | + 'destination.port': 443, |
| 65 | + 'destination.url': 'http://3fo8jrthz3y.rgk.cc/index.php', |
| 66 | + 'extra.destination.naics': 334111, |
| 67 | + 'extra.destination.sector': 'Information', |
| 68 | + 'extra.http_agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.6103)', |
| 69 | + 'extra.http_referer': 'null', |
| 70 | + 'extra.public_source': 'MSDCU', |
| 71 | + 'extra.source.naics': 517312, |
| 72 | + 'malware.name': 'caphaw', |
| 73 | + 'protocol.application': 'http', |
| 74 | + 'protocol.transport': 'tcp', |
| 75 | + 'raw': utils.base64_encode('\n'.join([EXAMPLE_LINES[0], |
| 76 | + EXAMPLE_LINES[2]])), |
| 77 | + 'source.asn': 28573, |
| 78 | + 'source.geolocation.cc': 'BR', |
| 79 | + 'source.geolocation.city': 'SAO PAULO', |
| 80 | + 'source.geolocation.region': 'SAO PAULO', |
| 81 | + 'source.ip': '177.140.3.4', |
| 82 | + 'source.port': 35919, |
| 83 | + 'time.observation': '2015-01-01T00:00:00+00:00', |
| 84 | + 'time.source': '2021-06-07T00:00:00+00:00'}, |
| 85 | + {'__type': 'Event', |
| 86 | + 'feed.name': 'HTTP Microsoft Sinkhole IPv4', |
| 87 | + 'classification.identifier': 'necurs', |
| 88 | + 'classification.taxonomy': 'malicious-code', |
| 89 | + 'classification.type': 'infected-system', |
| 90 | + 'destination.asn': 8075, |
| 91 | + 'destination.geolocation.cc': 'US', |
| 92 | + 'destination.geolocation.city': 'ASHBURN', |
| 93 | + 'destination.geolocation.region': 'VIRGINIA', |
| 94 | + 'destination.ip': '40.121.206.97', |
| 95 | + 'destination.port': 80, |
| 96 | + 'destination.url': 'http://40.121.206.97/locator.php', |
| 97 | + 'extra.destination.naics': 334111, |
| 98 | + 'extra.destination.sector': 'Information', |
| 99 | + 'extra.public_source': 'MSDCU', |
| 100 | + 'extra.source.naics': 517311, |
| 101 | + 'malware.name': 'necurs', |
| 102 | + 'protocol.application': 'http', |
| 103 | + 'protocol.transport': 'tcp', |
| 104 | + 'raw': utils.base64_encode('\n'.join([EXAMPLE_LINES[0], |
| 105 | + EXAMPLE_LINES[3]])), |
| 106 | + 'source.asn': 132199, |
| 107 | + 'source.geolocation.cc': 'PH', |
| 108 | + 'source.geolocation.city': 'MANDAUE', |
| 109 | + 'source.geolocation.region': 'CEBU', |
| 110 | + 'source.ip': '180.190.5.6', |
| 111 | + 'source.port': 49264, |
| 112 | + 'time.observation': '2015-01-01T00:00:00+00:00', |
| 113 | + 'time.source': '2021-06-07T00:00:01+00:00'}, |
| 114 | + {'__type': 'Event', |
| 115 | + 'feed.name': 'HTTP Microsoft Sinkhole IPv4', |
| 116 | + 'classification.identifier': 'necurs', |
| 117 | + 'classification.taxonomy': 'malicious-code', |
| 118 | + 'classification.type': 'infected-system', |
| 119 | + 'destination.asn': 8075, |
| 120 | + 'destination.geolocation.cc': 'US', |
| 121 | + 'destination.ip': '40.121.206.97', |
| 122 | + 'destination.geolocation.city': 'ASHBURN', |
| 123 | + 'destination.geolocation.region': 'VIRGINIA', |
| 124 | + 'destination.port': 80, |
| 125 | + 'destination.url': 'http://40.121.206.97/news/stream.php', |
| 126 | + 'extra.destination.naics': 334111, |
| 127 | + 'extra.destination.sector': 'Information', |
| 128 | + 'extra.public_source': 'MSDCU', |
| 129 | + 'malware.name': 'necurs', |
| 130 | + 'protocol.application': 'http', |
| 131 | + 'protocol.transport': 'tcp', |
| 132 | + 'raw': utils.base64_encode('\n'.join([EXAMPLE_LINES[0], |
| 133 | + EXAMPLE_LINES[4]])), |
| 134 | + 'source.asn': 37129, |
| 135 | + 'source.geolocation.cc': 'KE', |
| 136 | + 'source.geolocation.city': 'NAIROBI', |
| 137 | + 'source.geolocation.region': 'NAIROBI CITY', |
| 138 | + 'source.ip': '197.157.7.8', |
| 139 | + 'source.port': 55307, |
| 140 | + 'time.observation': '2015-01-01T00:00:00+00:00', |
| 141 | + 'time.source': '2021-06-07T00:00:01+00:00'}, |
| 142 | + {'__type': 'Event', |
| 143 | + 'feed.name': 'HTTP Microsoft Sinkhole IPv4', |
| 144 | + 'classification.identifier': 'necurs', |
| 145 | + 'classification.taxonomy': 'malicious-code', |
| 146 | + 'classification.type': 'infected-system', |
| 147 | + 'destination.asn': 8075, |
| 148 | + 'destination.geolocation.cc': 'US', |
| 149 | + 'destination.geolocation.city': 'ASHBURN', |
| 150 | + 'destination.geolocation.region': 'VIRGINIA', |
| 151 | + 'destination.ip': '40.121.206.97', |
| 152 | + 'destination.port': 80, |
| 153 | + 'destination.url': 'http://40.121.206.97/locator.php', |
| 154 | + 'extra.destination.naics': 334111, |
| 155 | + 'extra.destination.sector': 'Information', |
| 156 | + 'extra.public_source': 'MSDCU', |
| 157 | + 'extra.source.naics': 517311, |
| 158 | + 'extra.source.sector': 'Communications, Service Provider, and Hosting Service', |
| 159 | + 'malware.name': 'necurs', |
| 160 | + 'protocol.application': 'http', |
| 161 | + 'protocol.transport': 'tcp', |
| 162 | + 'raw': utils.base64_encode('\n'.join([EXAMPLE_LINES[0], |
| 163 | + EXAMPLE_LINES[5]])), |
| 164 | + 'source.asn': 812, |
| 165 | + 'source.geolocation.cc': 'CA', |
| 166 | + 'source.geolocation.city': 'OTTAWA', |
| 167 | + 'source.geolocation.region': 'ONTARIO', |
| 168 | + 'source.ip': '174.114.9.10', |
| 169 | + 'source.port': 59000, |
| 170 | + 'time.observation': '2015-01-01T00:00:00+00:00', |
| 171 | + 'time.source': '2021-06-07T00:00:01+00:00'}] |
| 172 | + |
| 173 | + |
| 174 | +class TestShadowserverParserBot(test.BotTestCase, unittest.TestCase): |
| 175 | + """ |
| 176 | + A TestCase for a ShadowserverParserBot. |
| 177 | + """ |
| 178 | + |
| 179 | + @classmethod |
| 180 | + def set_bot(cls): |
| 181 | + cls.bot_reference = ShadowserverParserBot |
| 182 | + cls.default_input_message = EXAMPLE_REPORT |
| 183 | + |
| 184 | + def test_event(self): |
| 185 | + """ Test if correct Event has been produced. """ |
| 186 | + self.run_bot() |
| 187 | + for i, EVENT in enumerate(EVENTS): |
| 188 | + self.assertMessageEqual(i, EVENT) |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == '__main__': # pragma: no cover |
| 192 | + unittest.main() |
0 commit comments