From 064525e6b9749f7da5c51497579eeadb6d3188b9 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 8 Jul 2021 11:59:50 -0400 Subject: [PATCH] Agent: Don't try to create README.txt if one already exists --- .../ransomware/ransomware_payload.py | 4 ++++ .../ransomware/test_ransomware_payload.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/monkey/infection_monkey/ransomware/ransomware_payload.py b/monkey/infection_monkey/ransomware/ransomware_payload.py index 05c2159d2..a6637b673 100644 --- a/monkey/infection_monkey/ransomware/ransomware_payload.py +++ b/monkey/infection_monkey/ransomware/ransomware_payload.py @@ -99,6 +99,10 @@ class RansomwarePayload: def _leave_readme(self): if self._readme_enabled: readme_dest_path = self._target_dir / README_DEST + if readme_dest_path.exists(): + LOG.warning(f"{readme_dest_path} already exists, not leaving a new README.txt") + return + LOG.info(f"Leaving a ransomware README file at {readme_dest_path}") try: diff --git a/monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py b/monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py index 89c9f5ff5..8a08a4595 100644 --- a/monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py +++ b/monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py @@ -1,6 +1,7 @@ import os import shutil from pathlib import Path, PurePosixPath +from unittest.mock import MagicMock import pytest from tests.unit_tests.infection_monkey.ransomware.ransomware_target_files import ( @@ -228,3 +229,18 @@ def test_readme_true(build_ransomware_payload, ransomware_payload_config, ransom ransomware_payload.run_payload() assert Path(ransomware_target / README_DEST).exists() + + +def test_readme_already_exists( + monkeypatch, ransomware_payload_config, telemetry_messenger_spy, ransomware_target +): + monkeypatch.setattr(ransomware_payload_module, "TARGETED_FILE_EXTENSIONS", set()), + mock_copy_file = MagicMock() + + ransomware_payload_config["other_behaviors"]["readme"] = True + Path(ransomware_target / README_DEST).touch() + RansomwarePayload( + ransomware_payload_config, telemetry_messenger_spy, mock_copy_file + ).run_payload() + + mock_copy_file.assert_not_called()