From bb798898c1918e4a6d250e167671c59ce9ded2bb Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 4 Apr 2022 13:03:50 +0530 Subject: [PATCH] Agent: Catch subprocess exceptions in utils/*/users.py --- monkey/infection_monkey/utils/linux/users.py | 25 +++++++++++++------ .../infection_monkey/utils/windows/users.py | 9 ++++--- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/monkey/infection_monkey/utils/linux/users.py b/monkey/infection_monkey/utils/linux/users.py index df01f9f18..112670dbb 100644 --- a/monkey/infection_monkey/utils/linux/users.py +++ b/monkey/infection_monkey/utils/linux/users.py @@ -44,9 +44,12 @@ class AutoNewLinuxUser(AutoNewUser): logger.debug( "Trying to add {} with commands {}".format(self.username, str(commands_to_add_user)) ) - _ = subprocess.check_output( - commands_to_add_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT - ) + try: + _ = subprocess.check_output( + commands_to_add_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT + ) + except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err: + logger.error(f"An exception occurred when creating a new linux user: {str(err)}") def __enter__(self): return self # No initialization/logging on needed in Linux @@ -55,7 +58,12 @@ class AutoNewLinuxUser(AutoNewUser): command_as_new_user = shlex.split( "sudo -u {username} {command}".format(username=self.username, command=command) ) - return subprocess.call(command_as_new_user, timeout=SHORT_REQUEST_TIMEOUT) + try: + return subprocess.call(command_as_new_user, timeout=SHORT_REQUEST_TIMEOUT) + except subprocess.TimeoutExpired as err: + logger.error( + f"An exception occurred when running a command as a new linux user: {str(err)}" + ) def __exit__(self, _exc_type, value, traceback): # delete the user. @@ -65,6 +73,9 @@ class AutoNewLinuxUser(AutoNewUser): self.username, str(commands_to_delete_user) ) ) - _ = subprocess.check_output( - commands_to_delete_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT - ) + try: + _ = subprocess.check_output( + commands_to_delete_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT + ) + except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err: + logger.error(f"An exception occurred when deleting the new linux user: {str(err)}") diff --git a/monkey/infection_monkey/utils/windows/users.py b/monkey/infection_monkey/utils/windows/users.py index 9e3af52a1..498e747c5 100644 --- a/monkey/infection_monkey/utils/windows/users.py +++ b/monkey/infection_monkey/utils/windows/users.py @@ -50,9 +50,12 @@ class AutoNewWindowsUser(AutoNewUser): windows_cmds = get_windows_commands_to_add_user(self.username, self.password, True) logger.debug("Trying to add {} with commands {}".format(self.username, str(windows_cmds))) - _ = subprocess.check_output( - windows_cmds, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT - ) + try: + _ = subprocess.check_output( + windows_cmds, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT + ) + except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err: + logger.error(f"An exception occurred when creating a new windows user: {str(err)}") def __enter__(self): try: