Agent: Don't try to create README.txt if one already exists

This commit is contained in:
Mike Salvatore 2021-07-08 11:59:50 -04:00
parent f0e9109f64
commit 064525e6b9
2 changed files with 20 additions and 0 deletions

View File

@ -99,6 +99,10 @@ class RansomwarePayload:
def _leave_readme(self): def _leave_readme(self):
if self._readme_enabled: if self._readme_enabled:
readme_dest_path = self._target_dir / README_DEST 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}") LOG.info(f"Leaving a ransomware README file at {readme_dest_path}")
try: try:

View File

@ -1,6 +1,7 @@
import os import os
import shutil import shutil
from pathlib import Path, PurePosixPath from pathlib import Path, PurePosixPath
from unittest.mock import MagicMock
import pytest import pytest
from tests.unit_tests.infection_monkey.ransomware.ransomware_target_files import ( 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() ransomware_payload.run_payload()
assert Path(ransomware_target / README_DEST).exists() 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()