From 8ef0b44841b3d8d7b069a2586317209d1ddd2169 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 1 Jun 2022 12:40:00 +0200 Subject: [PATCH] Agent: Fix dropper date referencing * Make dropper date reference path from strings to Path objects * Fix try/except block logic and indentation --- monkey/infection_monkey/dropper.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/monkey/infection_monkey/dropper.py b/monkey/infection_monkey/dropper.py index 57259950f..d76a7bc66 100644 --- a/monkey/infection_monkey/dropper.py +++ b/monkey/infection_monkey/dropper.py @@ -7,7 +7,7 @@ import shutil import subprocess import sys import time -from pathlib import WindowsPath +from pathlib import PosixPath, WindowsPath from common.utils.attack_utils import UsageEnum from infection_monkey.utils.commands import ( @@ -34,8 +34,8 @@ except NameError: logger = logging.getLogger(__name__) MOVEFILE_DELAY_UNTIL_REBOOT = 4 -DATE_REFERENCE_PATH_WINDOWS = r"%windir%\system32\kernel32.dll" -DATE_REFERENCE_PATH_LINUX = "/bin/sh" +DATE_REFERENCE_PATH_WINDOWS = WindowsPath(r"%windir%\system32\kernel32.dll") +DATE_REFERENCE_PATH_LINUX = PosixPath("/bin/sh") class MonkeyDrops(object): @@ -115,14 +115,19 @@ class MonkeyDrops(object): dropper_date_reference_path = os.path.expandvars(DATE_REFERENCE_PATH_WINDOWS) else: dropper_date_reference_path = DATE_REFERENCE_PATH_LINUX + + try: + ref_stat = os.stat(dropper_date_reference_path) + except OSError: + logger.warning( + "Cannot set reference date using '%s', file not found", + dropper_date_reference_path, + ) + else: try: - ref_stat = os.stat(dropper_date_reference_path) os.utime(self._config["destination_path"], (ref_stat.st_atime, ref_stat.st_mtime)) except OSError: - logger.warning( - "Cannot set reference date using '%s', file not found", - dropper_date_reference_path, - ) + logger.warning("Cannot set reference date to destination file") monkey_options = build_monkey_commandline_explicitly( parent=self.opts.parent,