From 320167224864227f6adac60bd25ddad534bc1a90 Mon Sep 17 00:00:00 2001 From: shreyamalviya Date: Wed, 19 May 2021 15:59:38 +0530 Subject: [PATCH] Move `is_windows_os` to data_dir_generator.py and add user write permissions to newly created data directory --- .../cc/environment/data_dir_generator.py | 18 ++++++++++++------ monkey/monkey_island/cc/environment/os.py | 5 ----- 2 files changed, 12 insertions(+), 11 deletions(-) delete mode 100644 monkey/monkey_island/cc/environment/os.py diff --git a/monkey/monkey_island/cc/environment/data_dir_generator.py b/monkey/monkey_island/cc/environment/data_dir_generator.py index 877faba3c..a809f4360 100644 --- a/monkey/monkey_island/cc/environment/data_dir_generator.py +++ b/monkey/monkey_island/cc/environment/data_dir_generator.py @@ -1,26 +1,28 @@ import os +import sys import ntsecuritycon import win32api import win32con import win32security -from monkey_island.cc.environment.os import is_windows_os from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR +is_windows_os = sys.platform.startswith("win") + def create_data_dir(data_dir: str) -> None: if not os.path.isdir(data_dir): os.makedirs(data_dir, mode=0o700) - if is_windows_os(): # `mode=0o700` doesn't work on Windows - set_data_dir_security_to_read_by_owner(data_dir_path=data_dir) + if is_windows_os: # `mode=0o700` doesn't work on Windows + set_data_dir_security_to_read_by_owner(data_dir_path=data_dir) def create_default_data_dir() -> None: if not os.path.isdir(DEFAULT_DATA_DIR): os.mkdir(DEFAULT_DATA_DIR, mode=0o700) - if is_windows_os(): # `mode=0o700` doesn't work on Windows - set_data_dir_security_to_read_by_owner(data_dir_path=DEFAULT_DATA_DIR) + if is_windows_os: # `mode=0o700` doesn't work on Windows + set_data_dir_security_to_read_by_owner(data_dir_path=DEFAULT_DATA_DIR) def set_data_dir_security_to_read_by_owner(data_dir_path: str) -> None: @@ -30,7 +32,11 @@ def set_data_dir_security_to_read_by_owner(data_dir_path: str) -> None: data_dir_path, win32security.DACL_SECURITY_INFORMATION ) dacl = win32security.ACL() - dacl.AddAccessAllowedAce(win32security.ACL_REVISION, ntsecuritycon.FILE_GENERIC_READ, user) + dacl.AddAccessAllowedAce( + win32security.ACL_REVISION, + ntsecuritycon.FILE_GENERIC_READ | ntsecuritycon.FILE_GENERIC_WRITE, + user, + ) security_descriptor.SetSecurityDescriptorDacl(1, dacl, 0) win32security.SetFileSecurity( data_dir_path, win32security.DACL_SECURITY_INFORMATION, security_descriptor diff --git a/monkey/monkey_island/cc/environment/os.py b/monkey/monkey_island/cc/environment/os.py deleted file mode 100644 index 195e54fd3..000000000 --- a/monkey/monkey_island/cc/environment/os.py +++ /dev/null @@ -1,5 +0,0 @@ -import sys - - -def is_windows_os(): - return sys.platform.startswith("win")