From 31cdd29edb8d7661db9eadd549a4802042b0b7f2 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 7 Dec 2021 12:30:31 +0200 Subject: [PATCH] Island: add "was monkey killed by user" endpoint Using this endpoint monkey can check if kill command was issues and if it should die --- .../resources/agent_controls/stop_all_agents.py | 16 ++++++++++------ .../cc/services/infection_lifecycle.py | 12 ++---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/monkey/monkey_island/cc/resources/agent_controls/stop_all_agents.py b/monkey/monkey_island/cc/resources/agent_controls/stop_all_agents.py index 8d9c558ad..8c91ac739 100644 --- a/monkey/monkey_island/cc/resources/agent_controls/stop_all_agents.py +++ b/monkey/monkey_island/cc/resources/agent_controls/stop_all_agents.py @@ -10,9 +10,13 @@ from monkey_island.cc.services.infection_lifecycle import set_stop_all class StopAllAgents(flask_restful.Resource): @jwt_required def post(self): - data = json.loads(request.data) - if data["kill_time"]: - set_stop_all(data["kill_time"]) - return make_response({}, 200) - else: - return make_response({}, 400) + with AGENT_KILLING_SEMAPHORE: + data = json.loads(request.data) + if data["kill_time"]: + set_stop_all(data["kill_time"]) + return make_response({}, 200) + else: + return make_response({}, 400) + + def get(self, monkey_guid): + return {"stop_agent": was_monkey_killed(monkey_guid)} diff --git a/monkey/monkey_island/cc/services/infection_lifecycle.py b/monkey/monkey_island/cc/services/infection_lifecycle.py index 55c9f79b9..ca6e36924 100644 --- a/monkey/monkey_island/cc/services/infection_lifecycle.py +++ b/monkey/monkey_island/cc/services/infection_lifecycle.py @@ -23,16 +23,8 @@ def set_stop_all(time: float): agent_controls.save() -def should_agent_die(guid: int) -> bool: - monkey = Monkey.objects(guid=str(guid)).first() - return _is_monkey_marked_dead(monkey) or _is_monkey_killed_manually(monkey) - - -def _is_monkey_marked_dead(monkey: Monkey) -> bool: - return monkey.config.alive - - -def _is_monkey_killed_manually(monkey: Monkey) -> bool: +def was_monkey_killed(guid: int) -> bool: + monkey = Monkey.objects(guid=guid).first() if monkey.has_parent(): launch_timestamp = monkey.get_parent().launch_time else: