From 9aff40d974524e0502da1528a8906591729f9514 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 7 May 2020 11:20:08 +0300 Subject: [PATCH 1/7] Fake IP's don't get out of range, fixed typo in requirements and added logging to telem parsing --- .../performance/utils/fake_ip_generator.py | 20 ++++++++++++++++--- .../utils/test_fake_ip_generator.py | 18 +++++++++++++++++ .../cc/resources/test/utils/telem_store.py | 8 ++++++++ monkey/monkey_island/requirements.txt | 2 +- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 envs/monkey_zoo/blackbox/tests/performance/utils/test_fake_ip_generator.py diff --git a/envs/monkey_zoo/blackbox/tests/performance/utils/fake_ip_generator.py b/envs/monkey_zoo/blackbox/tests/performance/utils/fake_ip_generator.py index 8abe53453..90422f9a0 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/utils/fake_ip_generator.py +++ b/envs/monkey_zoo/blackbox/tests/performance/utils/fake_ip_generator.py @@ -1,11 +1,25 @@ +from typing import List + + class FakeIpGenerator: def __init__(self): self.fake_ip_parts = [1, 1, 1, 1] - def generate_fake_ips_for_real_ips(self, real_ips): - self.fake_ip_parts[2] += 1 + def generate_fake_ips_for_real_ips(self, real_ips: List[str]) -> List[str]: fake_ips = [] for i in range(len(real_ips)): fake_ips.append('.'.join(str(part) for part in self.fake_ip_parts)) - self.fake_ip_parts[3] += 1 + self.increment_ip() return fake_ips + + def increment_ip(self): + self.fake_ip_parts[3] += 1 + self.try_fix_ip_range() + + def try_fix_ip_range(self): + for i in range(len(self.fake_ip_parts)): + if self.fake_ip_parts[i] > 256: + if i-1 < 0: + raise Exception("Fake IP's out of range.") + self.fake_ip_parts[i-1] += 1 + self.fake_ip_parts[i] = 1 diff --git a/envs/monkey_zoo/blackbox/tests/performance/utils/test_fake_ip_generator.py b/envs/monkey_zoo/blackbox/tests/performance/utils/test_fake_ip_generator.py new file mode 100644 index 000000000..96609a8a9 --- /dev/null +++ b/envs/monkey_zoo/blackbox/tests/performance/utils/test_fake_ip_generator.py @@ -0,0 +1,18 @@ +from unittest import TestCase + +from envs.monkey_zoo.blackbox.tests.performance.utils.fake_ip_generator import FakeIpGenerator + + +class TestFakeIpGenerator(TestCase): + + def test_fake_ip_generation(self): + fake_ip_gen = FakeIpGenerator() + self.assertListEqual([1, 1, 1, 1], fake_ip_gen.fake_ip_parts) + for i in range(256): + fake_ip_gen.generate_fake_ips_for_real_ips(['1.1.1.1']) + self.assertListEqual(['1.1.2.1'], fake_ip_gen.generate_fake_ips_for_real_ips(['1.1.1.1'])) + fake_ip_gen.fake_ip_parts = [256, 256, 255, 256] + self.assertListEqual(['256.256.255.256', '256.256.256.1'], + fake_ip_gen.generate_fake_ips_for_real_ips(['1.1.1.1', '1.1.1.2'])) + fake_ip_gen.fake_ip_parts = [256, 256, 256, 256] + self.assertRaises(Exception, fake_ip_gen.generate_fake_ips_for_real_ips(['1.1.1.1'])) diff --git a/monkey/monkey_island/cc/resources/test/utils/telem_store.py b/monkey/monkey_island/cc/resources/test/utils/telem_store.py index 49d7ea27b..031a0d02a 100644 --- a/monkey/monkey_island/cc/resources/test/utils/telem_store.py +++ b/monkey/monkey_island/cc/resources/test/utils/telem_store.py @@ -1,3 +1,4 @@ +import logging from functools import wraps from os import mkdir, path import shutil @@ -12,6 +13,10 @@ TEST_TELEM_DIR = "./test_telems" MAX_SAME_CATEGORY_TELEMS = 10000 +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + class TestTelemStore: @staticmethod @@ -31,14 +36,17 @@ class TestTelemStore: @staticmethod def export_test_telems(): + logger.info(f"Exporting all telemetries to {TEST_TELEM_DIR}") try: mkdir(TEST_TELEM_DIR) except FileExistsError: + logger.info("Deleting all previous telemetries.") shutil.rmtree(TEST_TELEM_DIR) mkdir(TEST_TELEM_DIR) for test_telem in TestTelem.objects(): with open(TestTelemStore.get_unique_file_path_for_test_telem(TEST_TELEM_DIR, test_telem), 'w') as file: file.write(test_telem.to_json()) + logger.info("Telemetries exported!") @staticmethod def get_unique_file_path_for_test_telem(target_dir: str, test_telem: TestTelem): diff --git a/monkey/monkey_island/requirements.txt b/monkey/monkey_island/requirements.txt index 39364cbb5..ce376ab4c 100644 --- a/monkey/monkey_island/requirements.txt +++ b/monkey/monkey_island/requirements.txt @@ -24,4 +24,4 @@ requests dpath ring stix2 -tcdm +tqdm From e189e9625962f88526b47e16da36471d0c790f6e Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 11 May 2020 16:32:18 +0300 Subject: [PATCH 2/7] Improved the dir structure of telem parsing, other minor CR comments fixed --- .gitignore | 2 +- envs/monkey_zoo/.gitignore | 2 +- envs/monkey_zoo/blackbox/README.md | 7 ++- .../telem_sample_parsing/__init__.py | 0 .../sample_file_parser.py | 43 ++++++++++++++ .../sample_multiplier/__init__.py | 0 .../sample_multiplier}/fake_ip_generator.py | 0 .../sample_multiplier}/fake_monkey.py | 3 +- .../sample_multiplier/sample_multiplier.py} | 59 ++++++------------- .../test_fake_ip_generator.py | 3 +- .../performance/telemetry_performance_test.py | 4 +- .../cc/resources/test/utils/telem_store.py | 14 ++--- 12 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/__init__.py create mode 100644 envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_file_parser.py create mode 100644 envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/__init__.py rename envs/monkey_zoo/blackbox/tests/performance/{utils => telem_sample_parsing/sample_multiplier}/fake_ip_generator.py (100%) rename envs/monkey_zoo/blackbox/tests/performance/{utils => telem_sample_parsing/sample_multiplier}/fake_monkey.py (82%) rename envs/monkey_zoo/blackbox/tests/performance/{utils/telem_parser.py => telem_sample_parsing/sample_multiplier/sample_multiplier.py} (57%) rename envs/monkey_zoo/blackbox/tests/performance/{utils => telem_sample_parsing/sample_multiplier}/test_fake_ip_generator.py (85%) diff --git a/.gitignore b/.gitignore index 97e618d43..a10127767 100644 --- a/.gitignore +++ b/.gitignore @@ -83,7 +83,7 @@ MonkeyZoo/* !MonkeyZoo/MonkeyZooDocs.pdf # Exported monkey telemetries -/monkey/test_telems/ +/monkey/telem_sample/ # vim swap files *.swp diff --git a/envs/monkey_zoo/.gitignore b/envs/monkey_zoo/.gitignore index 04310b6fd..be22d3037 100644 --- a/envs/monkey_zoo/.gitignore +++ b/envs/monkey_zoo/.gitignore @@ -1,2 +1,2 @@ logs/ -/blackbox/tests/performance/test_telems/* +/blackbox/tests/performance/telem_sample diff --git a/envs/monkey_zoo/blackbox/README.md b/envs/monkey_zoo/blackbox/README.md index e800537de..b31fbdcab 100644 --- a/envs/monkey_zoo/blackbox/README.md +++ b/envs/monkey_zoo/blackbox/README.md @@ -24,13 +24,14 @@ To run telemetry performance test follow these steps: 1. Enable "Export monkey telemetries" in Configuration -> Internal -> Tests if you don't have exported telemetries already. 2. Run monkey and wait until infection is done. - 3. All telemetries are gathered in `monkey/test_telems` + 3. All telemetries are gathered in `monkey/telem_sample` 2. Run telemetry performance test. 1. Move directory `monkey/test_telems` to `envs/monkey_zoo/blackbox/tests/performance/test_telems` 2. (Optional) Use `envs/monkey_zoo/blackbox/tests/performance/utils/telem_parser.py` to multiply telemetries gathered. - 1. Run `telem_parser.py` scrip with working directory set to `monkey\envs\monkey_zoo\blackbox` + 1. Run `telem_parser.py` script with working directory set to `monkey\envs\monkey_zoo\blackbox` 2. Pass integer to indicate the multiplier. For example running `telem_parser.py 4` will replicate telemetries 4 times. 3. If you're using pycharm check "Emulate terminal in output console" on debug/run configuraion. - 3. Run blackbox tests, telemetry performance test will run as part of it. + 3. Performance test will run as part of BlackBox tests or you can run it separately by adding + `-k 'test_telem_performance'` option. diff --git a/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/__init__.py b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_file_parser.py b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_file_parser.py new file mode 100644 index 000000000..f12704d2d --- /dev/null +++ b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_file_parser.py @@ -0,0 +1,43 @@ +import json +import logging +from os import listdir, path +from typing import List, Dict + +from tqdm import tqdm + +TELEM_DIR_PATH = './tests/performance/telem_sample' +MAX_SAME_TYPE_TELEM_FILES = 10000 +LOGGER = logging.getLogger(__name__) + + +class SampleFileParser: + + @staticmethod + def save_teletries_to_files(telems: List[Dict]): + for telem in (tqdm(telems, desc="Telemetries saved to files", position=3)): + SampleFileParser.save_telemetry_to_file(telem) + + @staticmethod + def save_telemetry_to_file(telem: Dict): + telem_filename = telem['name'] + telem['method'] + for i in range(MAX_SAME_TYPE_TELEM_FILES): + if not path.exists(path.join(TELEM_DIR_PATH, (str(i) + telem_filename))): + telem_filename = str(i) + telem_filename + break + with open(path.join(TELEM_DIR_PATH, telem_filename), 'w') as file: + file.write(json.dumps(telem)) + + @staticmethod + def read_telem_files() -> List[str]: + telems = [] + file_paths = [path.join(TELEM_DIR_PATH, f) for f in listdir(TELEM_DIR_PATH) + if path.isfile(path.join(TELEM_DIR_PATH, f))] + for file_path in file_paths: + with open(file_path, 'r') as telem_file: + telem_string = "".join(telem_file.readlines()).replace("\n", "") + telems.append(telem_string) + return telems + + @staticmethod + def get_all_telemetries() -> List[Dict]: + return [json.loads(t) for t in SampleFileParser.read_telem_files()] diff --git a/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/__init__.py b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/envs/monkey_zoo/blackbox/tests/performance/utils/fake_ip_generator.py b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/fake_ip_generator.py similarity index 100% rename from envs/monkey_zoo/blackbox/tests/performance/utils/fake_ip_generator.py rename to envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/fake_ip_generator.py diff --git a/envs/monkey_zoo/blackbox/tests/performance/utils/fake_monkey.py b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/fake_monkey.py similarity index 82% rename from envs/monkey_zoo/blackbox/tests/performance/utils/fake_monkey.py rename to envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/fake_monkey.py index dab84a1e6..89cdf5cad 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/utils/fake_monkey.py +++ b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/fake_monkey.py @@ -1,6 +1,7 @@ import random -from envs.monkey_zoo.blackbox.tests.performance.utils.fake_ip_generator import FakeIpGenerator +from envs.monkey_zoo.blackbox.tests.performance.\ + telem_sample_parsing.sample_multiplier.fake_ip_generator import FakeIpGenerator class FakeMonkey: diff --git a/envs/monkey_zoo/blackbox/tests/performance/utils/telem_parser.py b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/sample_multiplier.py similarity index 57% rename from envs/monkey_zoo/blackbox/tests/performance/utils/telem_parser.py rename to envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/sample_multiplier.py index df7e9f5be..da3c22b05 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/utils/telem_parser.py +++ b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/sample_multiplier.py @@ -2,35 +2,37 @@ import copy import json import logging import sys -from os import listdir, path from typing import List, Dict from tqdm import tqdm -from envs.monkey_zoo.blackbox.tests.performance.utils.fake_ip_generator import FakeIpGenerator -from envs.monkey_zoo.blackbox.tests.performance.utils.fake_monkey import FakeMonkey +from envs.monkey_zoo.blackbox.tests.performance.telem_sample_parsing.sample_file_parser import SampleFileParser +from envs.monkey_zoo.blackbox.tests.performance.\ + telem_sample_parsing.sample_multiplier.fake_ip_generator import FakeIpGenerator +from envs.monkey_zoo.blackbox.tests.performance.telem_sample_parsing.sample_multiplier.fake_monkey import FakeMonkey -TELEM_DIR_PATH = './tests/performance/test_telems' +TELEM_DIR_PATH = './tests/performance/telemetry_sample' LOGGER = logging.getLogger(__name__) -class TelemParser: +class SampleMultiplier: def __init__(self, multiplier: int): self.multiplier = multiplier self.fake_ip_generator = FakeIpGenerator() def multiply_telems(self): - telems = TelemParser.get_all_telemetries() + telems = SampleFileParser.get_all_telemetries() telem_contents = [json.loads(telem['content']) for telem in telems] monkeys = self.get_monkeys_from_telems(telem_contents) for i in tqdm(range(self.multiplier), desc="Batch of fabricated telemetries", position=1): for monkey in monkeys: monkey.change_fake_data() fake_telem_batch = copy.deepcopy(telems) - TelemParser.fabricate_monkeys_in_telems(fake_telem_batch, monkeys) - TelemParser.offset_telem_times(iteration=i, telems=fake_telem_batch) - TelemParser.save_teletries_to_files(fake_telem_batch) + SampleMultiplier.fabricate_monkeys_in_telems(fake_telem_batch, monkeys) + SampleMultiplier.offset_telem_times(iteration=i, telems=fake_telem_batch) + SampleFileParser.save_teletries_to_files(fake_telem_batch) + LOGGER.info("") @staticmethod def fabricate_monkeys_in_telems(telems: List[Dict], monkeys: List[FakeMonkey]): @@ -38,7 +40,8 @@ class TelemParser: for monkey in monkeys: if monkey.on_island: continue - if (monkey.original_guid in telem['content'] or monkey.original_guid in telem['endpoint']) and not monkey.on_island: + if (monkey.original_guid in telem['content'] or monkey.original_guid in telem['endpoint']) \ + and not monkey.on_island: telem['content'] = telem['content'].replace(monkey.original_guid, monkey.fake_guid) telem['endpoint'] = telem['endpoint'].replace(monkey.original_guid, monkey.fake_guid) for i in range(len(monkey.original_ips)): @@ -49,39 +52,11 @@ class TelemParser: for telem in telems: telem['time']['$date'] += iteration * 1000 - @staticmethod - def save_teletries_to_files(telems: List[Dict]): - for telem in (tqdm(telems, desc="Telemetries saved to files", position=3)): - TelemParser.save_telemetry_to_file(telem) - - @staticmethod - def save_telemetry_to_file(telem: Dict): - telem_filename = telem['name'] + telem['method'] - for i in range(10000): - if not path.exists(path.join(TELEM_DIR_PATH, (str(i) + telem_filename))): - telem_filename = str(i) + telem_filename - break - with open(path.join(TELEM_DIR_PATH, telem_filename), 'w') as file: - file.write(json.dumps(telem)) - - @staticmethod - def read_telem_files() -> List[str]: - telems = [] - file_paths = [path.join(TELEM_DIR_PATH, f) for f in listdir(TELEM_DIR_PATH) - if path.isfile(path.join(TELEM_DIR_PATH, f))] - for file_path in file_paths: - with open(file_path, 'r') as telem_file: - telems.append(telem_file.readline()) - return telems - - @staticmethod - def get_all_telemetries() -> List[Dict]: - return [json.loads(t) for t in TelemParser.read_telem_files()] - def get_monkeys_from_telems(self, telems: List[Dict]): - island_ips = TelemParser.get_island_ips_from_telems(telems) + island_ips = SampleMultiplier.get_island_ips_from_telems(telems) monkeys = [] - for telem in [telem for telem in telems if 'telem_category' in telem and telem['telem_category'] == 'system_info']: + for telem in [telem for telem in telems + if 'telem_category' in telem and telem['telem_category'] == 'system_info']: if 'network_info' not in telem['data']: continue guid = telem['monkey_guid'] @@ -111,4 +86,4 @@ class TelemParser: if __name__ == "__main__": - TelemParser(multiplier=int(sys.argv[1])).multiply_telems() + SampleMultiplier(multiplier=int(sys.argv[1])).multiply_telems() diff --git a/envs/monkey_zoo/blackbox/tests/performance/utils/test_fake_ip_generator.py b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/test_fake_ip_generator.py similarity index 85% rename from envs/monkey_zoo/blackbox/tests/performance/utils/test_fake_ip_generator.py rename to envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/test_fake_ip_generator.py index 96609a8a9..d8adef827 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/utils/test_fake_ip_generator.py +++ b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_multiplier/test_fake_ip_generator.py @@ -1,6 +1,7 @@ from unittest import TestCase -from envs.monkey_zoo.blackbox.tests.performance.utils.fake_ip_generator import FakeIpGenerator +from envs.monkey_zoo.blackbox.tests.performance.\ + telem_sample_parsing.sample_multiplier.fake_ip_generator import FakeIpGenerator class TestFakeIpGenerator(TestCase): diff --git a/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test.py b/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test.py index 4086a234e..5fc97d222 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test.py +++ b/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test.py @@ -8,7 +8,7 @@ from envs.monkey_zoo.blackbox.analyzers.performance_analyzer import PerformanceA from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient from envs.monkey_zoo.blackbox.island_client.supported_request_method import SupportedRequestMethod from envs.monkey_zoo.blackbox.tests.performance.performance_test_config import PerformanceTestConfig -from envs.monkey_zoo.blackbox.tests.performance.utils.telem_parser import TelemParser +from envs.monkey_zoo.blackbox.tests.performance.telem_sample_parsing.sample_file_parser import SampleFileParser LOGGER = logging.getLogger(__name__) @@ -24,7 +24,7 @@ class TelemetryPerformanceTest: def test_telemetry_performance(self): LOGGER.info("Starting telemetry performance test.") try: - all_telemetries = TelemParser.get_all_telemetries() + all_telemetries = SampleFileParser.get_all_telemetries() except FileNotFoundError: LOGGER.error("Telemetries to send not found. Refer to readme to figure out how to generate telemetries " "and where to put them.") diff --git a/monkey/monkey_island/cc/resources/test/utils/telem_store.py b/monkey/monkey_island/cc/resources/test/utils/telem_store.py index 031a0d02a..18ebfd244 100644 --- a/monkey/monkey_island/cc/resources/test/utils/telem_store.py +++ b/monkey/monkey_island/cc/resources/test/utils/telem_store.py @@ -9,7 +9,7 @@ from flask import request from monkey_island.cc.models.test_telem import TestTelem from monkey_island.cc.services.config import ConfigService -TEST_TELEM_DIR = "./test_telems" +TELEM_SAMPLE_DIR = "./telem_sample" MAX_SAME_CATEGORY_TELEMS = 10000 @@ -36,16 +36,16 @@ class TestTelemStore: @staticmethod def export_test_telems(): - logger.info(f"Exporting all telemetries to {TEST_TELEM_DIR}") + logger.info(f"Exporting all telemetries to {TELEM_SAMPLE_DIR}") try: - mkdir(TEST_TELEM_DIR) + mkdir(TELEM_SAMPLE_DIR) except FileExistsError: logger.info("Deleting all previous telemetries.") - shutil.rmtree(TEST_TELEM_DIR) - mkdir(TEST_TELEM_DIR) + shutil.rmtree(TELEM_SAMPLE_DIR) + mkdir(TELEM_SAMPLE_DIR) for test_telem in TestTelem.objects(): - with open(TestTelemStore.get_unique_file_path_for_test_telem(TEST_TELEM_DIR, test_telem), 'w') as file: - file.write(test_telem.to_json()) + with open(TestTelemStore.get_unique_file_path_for_test_telem(TELEM_SAMPLE_DIR, test_telem), 'w') as file: + file.write(test_telem.to_json(indent=2)) logger.info("Telemetries exported!") @staticmethod From 67b7d0b769902232fc25382f5fac238ded888f52 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 11 May 2020 16:56:05 +0300 Subject: [PATCH 3/7] Improved telemetry fabrication logging and failing if telemetry folders don't exist --- .../telem_sample_parsing/sample_file_parser.py | 8 ++++++-- .../tests/performance/telemetry_performance_test.py | 5 ++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_file_parser.py b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_file_parser.py index f12704d2d..70e25d8e7 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_file_parser.py +++ b/envs/monkey_zoo/blackbox/tests/performance/telem_sample_parsing/sample_file_parser.py @@ -30,8 +30,12 @@ class SampleFileParser: @staticmethod def read_telem_files() -> List[str]: telems = [] - file_paths = [path.join(TELEM_DIR_PATH, f) for f in listdir(TELEM_DIR_PATH) - if path.isfile(path.join(TELEM_DIR_PATH, f))] + try: + file_paths = [path.join(TELEM_DIR_PATH, f) for f in listdir(TELEM_DIR_PATH) + if path.isfile(path.join(TELEM_DIR_PATH, f))] + except FileNotFoundError: + raise FileNotFoundError("Telemetries to send not found. " + "Refer to readme to figure out how to generate telemetries and where to put them.") for file_path in file_paths: with open(file_path, 'r') as telem_file: telem_string = "".join(telem_file.readlines()).replace("\n", "") diff --git a/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test.py b/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test.py index 5fc97d222..4de77e41a 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test.py +++ b/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test.py @@ -26,9 +26,8 @@ class TelemetryPerformanceTest: try: all_telemetries = SampleFileParser.get_all_telemetries() except FileNotFoundError: - LOGGER.error("Telemetries to send not found. Refer to readme to figure out how to generate telemetries " - "and where to put them.") - return False + raise FileNotFoundError("Telemetries to send not found. " + "Refer to readme to figure out how to generate telemetries and where to put them.") LOGGER.info("Telemetries imported successfully.") all_telemetries.sort(key=lambda telem: telem['time']['$date']) telemetry_parse_times = {} From 9b350b8bf58884b27ff17752056c4a30afd328fa Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 11 May 2020 17:58:58 +0300 Subject: [PATCH 4/7] Minor fixes and improvements --- .../tests/performance/telemetry_performance_test_workflow.py | 5 ++--- monkey/monkey_island/cc/resources/reporting/report.py | 2 -- .../monkey_island/cc/resources/zero_trust/finding_event.py | 2 -- .../cc/services/reporting/zero_trust_service.py | 5 ++--- .../components/report-components/zerotrust/EventsModal.js | 4 ++-- 5 files changed, 6 insertions(+), 12 deletions(-) diff --git a/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test_workflow.py b/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test_workflow.py index 320973d97..b5acf4a9e 100644 --- a/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test_workflow.py +++ b/envs/monkey_zoo/blackbox/tests/performance/telemetry_performance_test_workflow.py @@ -13,9 +13,8 @@ class TelemetryPerformanceTestWorkflow(BasicTest): def run(self): try: - # TelemetryPerformanceTest(island_client=self.island_client).test_telemetry_performance() + TelemetryPerformanceTest(island_client=self.island_client).test_telemetry_performance() performance_test = EndpointPerformanceTest(self.name, self.performance_config, self.island_client) assert performance_test.run() finally: - pass - # self.island_client.reset_env() + self.island_client.reset_env() diff --git a/monkey/monkey_island/cc/resources/reporting/report.py b/monkey/monkey_island/cc/resources/reporting/report.py index 6770512e6..961e745a8 100644 --- a/monkey/monkey_island/cc/resources/reporting/report.py +++ b/monkey/monkey_island/cc/resources/reporting/report.py @@ -6,7 +6,6 @@ from flask import jsonify from monkey_island.cc.auth import jwt_required from monkey_island.cc.services.reporting.report import ReportService from monkey_island.cc.services.reporting.zero_trust_service import ZeroTrustService -from monkey_island.cc.testing.profiler_decorator import profile ZERO_TRUST_REPORT_TYPE = "zero_trust" SECURITY_REPORT_TYPE = "security" @@ -22,7 +21,6 @@ __author__ = ["itay.mizeretz", "shay.nehmad"] class Report(flask_restful.Resource): @jwt_required() - @profile() def get(self, report_type=SECURITY_REPORT_TYPE, report_data=None): if report_type == SECURITY_REPORT_TYPE: return ReportService.get_report() diff --git a/monkey/monkey_island/cc/resources/zero_trust/finding_event.py b/monkey/monkey_island/cc/resources/zero_trust/finding_event.py index 73cfa7f4c..16c545241 100644 --- a/monkey/monkey_island/cc/resources/zero_trust/finding_event.py +++ b/monkey/monkey_island/cc/resources/zero_trust/finding_event.py @@ -3,12 +3,10 @@ import json from monkey_island.cc.auth import jwt_required from monkey_island.cc.services.reporting.zero_trust_service import ZeroTrustService -from monkey_island.cc.testing.profiler_decorator import profile class ZeroTrustFindingEvent(flask_restful.Resource): @jwt_required() - @profile() def get(self, finding_id: str): return {'events_json': json.dumps(ZeroTrustService.get_events_by_finding(finding_id), default=str)} diff --git a/monkey/monkey_island/cc/services/reporting/zero_trust_service.py b/monkey/monkey_island/cc/services/reporting/zero_trust_service.py index 7ecac4e7f..821d2104a 100644 --- a/monkey/monkey_island/cc/services/reporting/zero_trust_service.py +++ b/monkey/monkey_island/cc/services/reporting/zero_trust_service.py @@ -109,8 +109,7 @@ class ZeroTrustService(object): @staticmethod def get_all_findings(): - pipeline = [{'$match': {}}, - {'$addFields': {'oldest_events': {'$slice': ['$events', EVENT_FETCH_CNT]}, + pipeline = [{'$addFields': {'oldest_events': {'$slice': ['$events', EVENT_FETCH_CNT]}, 'latest_events': {'$slice': ['$events', -1*EVENT_FETCH_CNT]}, 'event_count': {'$size': '$events'}}}, {'$unset': ['events']}] @@ -130,7 +129,7 @@ class ZeroTrustService(object): elif overlap_count <= 0: return [] else: - return events[ -overlap_count :] + return events[-1 * overlap_count:] @staticmethod def __get_enriched_finding(finding): diff --git a/monkey/monkey_island/cc/ui/src/components/report-components/zerotrust/EventsModal.js b/monkey/monkey_island/cc/ui/src/components/report-components/zerotrust/EventsModal.js index 4d31816b0..4ddfdf93c 100644 --- a/monkey/monkey_island/cc/ui/src/components/report-components/zerotrust/EventsModal.js +++ b/monkey/monkey_island/cc/ui/src/components/report-components/zerotrust/EventsModal.js @@ -1,4 +1,4 @@ -import React, {Component} from 'react'; +import React from 'react'; import {Modal} from 'react-bootstrap'; import EventsTimeline from './EventsTimeline'; import * as PropTypes from 'prop-types'; @@ -6,7 +6,7 @@ import saveJsonToFile from '../../utils/SaveJsonToFile'; import EventsModalButtons from './EventsModalButtons'; import AuthComponent from '../../AuthComponent'; import Pluralize from 'pluralize'; -import SkippedEventsTimeline from "./SkippedEventsTimeline"; +import SkippedEventsTimeline from './SkippedEventsTimeline'; const FINDING_EVENTS_URL = '/api/zero-trust/finding-event/'; From 2debe9805244b5078c0b1e7a48fc2a98b04fe367 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 12 May 2020 12:43:41 +0300 Subject: [PATCH 5/7] Profiling decorator: added readme and profiler logs added to gitignore --- .gitignore | 3 +++ monkey/monkey_island/cc/testing/README.md | 9 +++++++++ 2 files changed, 12 insertions(+) create mode 100644 monkey/monkey_island/cc/testing/README.md diff --git a/.gitignore b/.gitignore index a10127767..3b602d4aa 100644 --- a/.gitignore +++ b/.gitignore @@ -85,5 +85,8 @@ MonkeyZoo/* # Exported monkey telemetries /monkey/telem_sample/ +# Profiling logs +/monkey/profiler_logs/ + # vim swap files *.swp diff --git a/monkey/monkey_island/cc/testing/README.md b/monkey/monkey_island/cc/testing/README.md new file mode 100644 index 000000000..1c1446b2f --- /dev/null +++ b/monkey/monkey_island/cc/testing/README.md @@ -0,0 +1,9 @@ +# Profiling island + +To profile specific methods on island a `@profile(sort_args=['cumulative'], print_args=[100])` +decorator can be used. +Use it as any other decorator. After decorated method is used, a file will appear in a +directory provided in `profiler_decorator.py`. Filename describes the path of +the method that was profiled. For example if method `monkey_island/cc/resources/netmap.get` +was profiled, then the results of this profiling will appear in +`monkey_island_cc_resources_netmap_get`. From ee6b122f0181fa4986031c56b2d86a7833044037 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 12 May 2020 15:45:51 +0300 Subject: [PATCH 6/7] Minor improvements --- .../monkey_island/cc/models/zero_trust/aggregate_finding.py | 3 +-- monkey/monkey_island/cc/models/zero_trust/finding.py | 4 ++++ .../cc/services/reporting/test_zero_trust_service.py | 6 +++--- .../cc/services/reporting/zero_trust_service.py | 6 +++--- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/monkey/monkey_island/cc/models/zero_trust/aggregate_finding.py b/monkey/monkey_island/cc/models/zero_trust/aggregate_finding.py index ff3c4e4d9..c3817313f 100644 --- a/monkey/monkey_island/cc/models/zero_trust/aggregate_finding.py +++ b/monkey/monkey_island/cc/models/zero_trust/aggregate_finding.py @@ -20,8 +20,7 @@ class AggregateFinding(Finding): else: # Now we know for sure this is the only one orig_finding = existing_findings[0] - orig_finding.update(push_all__events=events) - orig_finding.save() + orig_finding.add_events(events) def add_malicious_activity_to_timeline(events): diff --git a/monkey/monkey_island/cc/models/zero_trust/finding.py b/monkey/monkey_island/cc/models/zero_trust/finding.py index ae1114655..2f3261ec4 100644 --- a/monkey/monkey_island/cc/models/zero_trust/finding.py +++ b/monkey/monkey_island/cc/models/zero_trust/finding.py @@ -2,6 +2,7 @@ """ Define a Document Schema for Zero Trust findings. """ +from typing import List from mongoengine import Document, StringField, EmbeddedDocumentListField @@ -54,3 +55,6 @@ class Finding(Document): finding.save() return finding + + def add_events(self, events: List) -> None: + self.update(push_all__events=events) diff --git a/monkey/monkey_island/cc/services/reporting/test_zero_trust_service.py b/monkey/monkey_island/cc/services/reporting/test_zero_trust_service.py index 403967d8f..e40af29f4 100644 --- a/monkey/monkey_island/cc/services/reporting/test_zero_trust_service.py +++ b/monkey/monkey_island/cc/services/reporting/test_zero_trust_service.py @@ -319,9 +319,9 @@ class TestZeroTrustService(IslandTestCase): def test_get_events_without_overlap(self): monkey_island.cc.services.reporting.zero_trust_service.EVENT_FETCH_CNT = 5 - self.assertListEqual([], ZeroTrustService._ZeroTrustService__get_events_without_overlap(5, [1, 2, 3])) - self.assertListEqual([3], ZeroTrustService._ZeroTrustService__get_events_without_overlap(6, [1, 2, 3])) - self.assertListEqual([1, 2, 3, 4, 5], ZeroTrustService._ZeroTrustService__get_events_without_overlap(10, [1, 2, 3, 4, 5])) + self.assertListEqual([], ZeroTrustService._get_events_without_overlap(5, [1, 2, 3])) + self.assertListEqual([3], ZeroTrustService._get_events_without_overlap(6, [1, 2, 3])) + self.assertListEqual([1, 2, 3, 4, 5], ZeroTrustService._get_events_without_overlap(10, [1, 2, 3, 4, 5])) def compare_lists_no_order(s, t): diff --git a/monkey/monkey_island/cc/services/reporting/zero_trust_service.py b/monkey/monkey_island/cc/services/reporting/zero_trust_service.py index 821d2104a..ee8fdd8bb 100644 --- a/monkey/monkey_island/cc/services/reporting/zero_trust_service.py +++ b/monkey/monkey_island/cc/services/reporting/zero_trust_service.py @@ -115,14 +115,14 @@ class ZeroTrustService(object): {'$unset': ['events']}] all_findings = list(Finding.objects.aggregate(*pipeline)) for finding in all_findings: - finding['latest_events'] = ZeroTrustService.__get_events_without_overlap(finding['event_count'], - finding['latest_events']) + finding['latest_events'] = ZeroTrustService._get_events_without_overlap(finding['event_count'], + finding['latest_events']) enriched_findings = [ZeroTrustService.__get_enriched_finding(f) for f in all_findings] return enriched_findings @staticmethod - def __get_events_without_overlap(event_count: int, events: List[object]) -> List[object]: + def _get_events_without_overlap(event_count: int, events: List[object]) -> List[object]: overlap_count = event_count - EVENT_FETCH_CNT if overlap_count >= EVENT_FETCH_CNT: return events From 0e4242b15a45b8a30b2e1e9355417f93ef5ea3ef Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 12 May 2020 17:08:26 +0300 Subject: [PATCH 7/7] Gitignore changed to ignore all "profiler_logs" dirs --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3b602d4aa..960e8c67c 100644 --- a/.gitignore +++ b/.gitignore @@ -86,7 +86,7 @@ MonkeyZoo/* /monkey/telem_sample/ # Profiling logs -/monkey/profiler_logs/ +profiler_logs/ # vim swap files *.swp