forked from guardicore/monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from guardicore/develop
Updating my Local Fork
- Loading branch information
Showing
37 changed files
with
479 additions
and
218 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
[submodule "monkey/monkey_island/cc/services/attack/attack_data"] | ||
path = monkey/monkey_island/cc/services/attack/attack_data | ||
url = https://github.com/mitre/cti | ||
url = https://github.com/guardicore/cti |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 14 additions & 34 deletions
48
envs/monkey_zoo/blackbox/analyzers/performance_analyzer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,8 @@ | ||
from time import sleep | ||
import abc | ||
|
||
import logging | ||
|
||
from envs.monkey_zoo.blackbox.utils.test_timer import TestTimer | ||
|
||
MAX_TIME_FOR_MONKEYS_TO_DIE = 5 * 60 | ||
WAIT_TIME_BETWEEN_REQUESTS = 10 | ||
TIME_FOR_MONKEY_PROCESS_TO_FINISH = 40 | ||
DELAY_BETWEEN_ANALYSIS = 3 | ||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class BasicTest(object): | ||
|
||
def __init__(self, name, island_client, config_parser, analyzers, timeout, post_exec_analyzers, log_handler): | ||
self.name = name | ||
self.island_client = island_client | ||
self.config_parser = config_parser | ||
self.analyzers = analyzers | ||
self.post_exec_analyzers = post_exec_analyzers | ||
self.timeout = timeout | ||
self.log_handler = log_handler | ||
class BasicTest(abc.ABC): | ||
|
||
@abc.abstractmethod | ||
def run(self): | ||
self.island_client.import_config(self.config_parser.config_raw) | ||
self.print_test_starting_info() | ||
try: | ||
self.island_client.run_monkey_local() | ||
self.test_until_timeout() | ||
finally: | ||
self.island_client.kill_all_monkeys() | ||
self.wait_until_monkeys_die() | ||
self.wait_for_monkey_process_to_finish() | ||
self.test_post_exec_analyzers() | ||
self.parse_logs() | ||
self.island_client.reset_env() | ||
|
||
def print_test_starting_info(self): | ||
LOGGER.info("Started {} test".format(self.name)) | ||
LOGGER.info("Machines participating in test: " + ", ".join(self.config_parser.get_ips_of_targets())) | ||
print("") | ||
|
||
def test_until_timeout(self): | ||
timer = TestTimer(self.timeout) | ||
while not timer.is_timed_out(): | ||
if self.all_analyzers_pass(): | ||
self.log_success(timer) | ||
return | ||
sleep(DELAY_BETWEEN_ANALYSIS) | ||
LOGGER.debug("Waiting until all analyzers passed. Time passed: {}".format(timer.get_time_taken())) | ||
self.log_failure(timer) | ||
assert False | ||
|
||
def log_success(self, timer): | ||
LOGGER.info(self.get_analyzer_logs()) | ||
LOGGER.info("{} test passed, time taken: {:.1f} seconds.".format(self.name, timer.get_time_taken())) | ||
|
||
def log_failure(self, timer): | ||
LOGGER.info(self.get_analyzer_logs()) | ||
LOGGER.error("{} test failed because of timeout. Time taken: {:.1f} seconds.".format(self.name, | ||
timer.get_time_taken())) | ||
|
||
def all_analyzers_pass(self): | ||
analyzers_results = [analyzer.analyze_test_results() for analyzer in self.analyzers] | ||
return all(analyzers_results) | ||
|
||
def get_analyzer_logs(self): | ||
log = "" | ||
for analyzer in self.analyzers: | ||
log += "\n" + analyzer.log.get_contents() | ||
return log | ||
|
||
def wait_until_monkeys_die(self): | ||
time_passed = 0 | ||
while not self.island_client.is_all_monkeys_dead() and time_passed < MAX_TIME_FOR_MONKEYS_TO_DIE: | ||
sleep(WAIT_TIME_BETWEEN_REQUESTS) | ||
time_passed += WAIT_TIME_BETWEEN_REQUESTS | ||
LOGGER.debug("Waiting for all monkeys to die. Time passed: {}".format(time_passed)) | ||
if time_passed > MAX_TIME_FOR_MONKEYS_TO_DIE: | ||
LOGGER.error("Some monkeys didn't die after the test, failing") | ||
assert False | ||
|
||
def parse_logs(self): | ||
LOGGER.info("Parsing test logs:") | ||
self.log_handler.parse_test_logs() | ||
|
||
@staticmethod | ||
def wait_for_monkey_process_to_finish(): | ||
""" | ||
There is a time period when monkey is set to dead, but the process is still closing. | ||
If we try to launch monkey during that time window monkey will fail to start, that's | ||
why test needs to wait a bit even after all monkeys are dead. | ||
""" | ||
LOGGER.debug("Waiting for Monkey process to close...") | ||
sleep(TIME_FOR_MONKEY_PROCESS_TO_FINISH) | ||
|
||
def test_post_exec_analyzers(self): | ||
post_exec_analyzers_results = [analyzer.analyze_test_results() for analyzer in self.post_exec_analyzers] | ||
assert all(post_exec_analyzers_results) | ||
pass |
Oops, something went wrong.