From 92c5c3b6826631a60a5959abc3a11a90dae877ca Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 8 Jul 2021 12:04:50 -0400 Subject: [PATCH] Agent: Extract method _copy_file() from _leave_readme() Reworks the logic in _leave_readme() to reduce indenting and improve clarity and extracts the logic to copy the file into _copy_readme_file() --- .../ransomware/ransomware_payload.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/monkey/infection_monkey/ransomware/ransomware_payload.py b/monkey/infection_monkey/ransomware/ransomware_payload.py index 5b4a0cb8a..0e5cf813e 100644 --- a/monkey/infection_monkey/ransomware/ransomware_payload.py +++ b/monkey/infection_monkey/ransomware/ransomware_payload.py @@ -97,15 +97,21 @@ class RansomwarePayload: self._telemetry_messenger.send_telemetry(encryption_attempt) 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 + if not self._readme_enabled: + return - LOG.info(f"Leaving a ransomware README file at {readme_dest_path}") + readme_dest_path = self._target_dir / README_DEST - try: - self._copy_file(README_SRC, readme_dest_path) - except Exception as ex: - LOG.warning(f"An error occurred while attempting to leave a README.txt file: {ex}") + if readme_dest_path.exists(): + LOG.warning(f"{readme_dest_path} already exists, not leaving a new README.txt") + return + + self._copy_readme_file(readme_dest_path) + + def _copy_readme_file(self, dest: Path): + LOG.info(f"Leaving a ransomware README file at {dest}") + + try: + self._copy_file(README_SRC, dest) + except Exception as ex: + LOG.warning(f"An error occurred while attempting to leave a README.txt file: {ex}")