diff --git a/monkey/infection_monkey/ransomware/ransomware_payload.py b/monkey/infection_monkey/ransomware/ransomware_payload.py index b973716a8..327f3b581 100644 --- a/monkey/infection_monkey/ransomware/ransomware_payload.py +++ b/monkey/infection_monkey/ransomware/ransomware_payload.py @@ -56,12 +56,16 @@ class RansomwarePayload: return None def run_payload(self): + if not self._target_dir: + return + + LOG.info("Running ransomware payload") + if self._encryption_enabled and self._target_dir: - LOG.info("Running ransomware payload") file_list = self._find_files() self._encrypt_files(file_list) - if self._target_dir: + if self._readme_enabled: self._leave_readme() def _find_files(self) -> List[Path]: @@ -93,8 +97,6 @@ class RansomwarePayload: self._telemetry_messenger.send_telemetry(encryption_attempt) def _leave_readme(self): - if not self._readme_enabled: - return readme_dest_path = self._target_dir / README_DEST 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 37af9509e..3c911927c 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 @@ -247,13 +247,18 @@ def test_readme_already_exists( def test_no_readme_if_no_directory( - build_ransomware_payload, ransomware_payload_config, ransomware_target + 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["encryption"]["enabled"] = True ransomware_payload_config["encryption"]["directories"]["linux_target_dir"] = "" ransomware_payload_config["encryption"]["directories"]["windows_target_dir"] = "" ransomware_payload_config["other_behaviors"]["readme"] = True - ransomware_payload = build_ransomware_payload(ransomware_payload_config) - ransomware_payload.run_payload() - assert not Path(ransomware_target / README_DEST).exists() + RansomwarePayload( + ransomware_payload_config, telemetry_messenger_spy, mock_copy_file + ).run_payload() + + mock_copy_file.assert_not_called()