Merge pull request #917 from VakarisZ/pba_path_fix

Bugfix: custom PBA upload path
This commit is contained in:
VakarisZ 2021-01-27 15:42:16 +02:00 committed by GitHub
commit e91dfaaaa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 14 deletions

View File

@ -1,7 +1,7 @@
import flask_restful import flask_restful
from flask import send_from_directory from flask import send_from_directory
from monkey_island.cc.services.post_breach_files import UPLOADS_DIR_NAME from monkey_island.cc.services.post_breach_files import ABS_UPLOAD_PATH
__author__ = 'VakarisZ' __author__ = 'VakarisZ'
@ -13,4 +13,4 @@ class PBAFileDownload(flask_restful.Resource):
# Used by monkey. can't secure. # Used by monkey. can't secure.
def get(self, path): def get(self, path):
return send_from_directory(UPLOADS_DIR_NAME, path) return send_from_directory(ABS_UPLOAD_PATH, path)

View File

@ -9,8 +9,7 @@ from werkzeug.utils import secure_filename
from monkey_island.cc.resources.auth.auth import jwt_required from monkey_island.cc.resources.auth.auth import jwt_required
from monkey_island.cc.services.config import ConfigService from monkey_island.cc.services.config import ConfigService
from monkey_island.cc.services.post_breach_files import ( from monkey_island.cc.services.post_breach_files import (
PBA_LINUX_FILENAME_PATH, PBA_UPLOAD_PATH, PBA_WINDOWS_FILENAME_PATH, ABS_UPLOAD_PATH, PBA_LINUX_FILENAME_PATH, PBA_WINDOWS_FILENAME_PATH)
UPLOADS_DIR)
__author__ = 'VakarisZ' __author__ = 'VakarisZ'
@ -19,9 +18,6 @@ LOG = logging.getLogger(__name__)
LINUX_PBA_TYPE = 'PBAlinux' LINUX_PBA_TYPE = 'PBAlinux'
WINDOWS_PBA_TYPE = 'PBAwindows' WINDOWS_PBA_TYPE = 'PBAwindows'
# This path is used by flask, which means that local directory is different from UPLOADS_DIR
FLASK_UPLOAD_PATH = PBA_UPLOAD_PATH[-1]
class FileUpload(flask_restful.Resource): class FileUpload(flask_restful.Resource):
""" """
@ -29,7 +25,7 @@ class FileUpload(flask_restful.Resource):
""" """
def __init__(self): def __init__(self):
# Create all directories on the way if they don't exist # Create all directories on the way if they don't exist
UPLOADS_DIR.mkdir(parents=True, exist_ok=True) ABS_UPLOAD_PATH.mkdir(parents=True, exist_ok=True)
@jwt_required @jwt_required
def get(self, file_type): def get(self, file_type):
@ -43,7 +39,7 @@ class FileUpload(flask_restful.Resource):
filename = ConfigService.get_config_value(copy.deepcopy(PBA_LINUX_FILENAME_PATH)) filename = ConfigService.get_config_value(copy.deepcopy(PBA_LINUX_FILENAME_PATH))
else: else:
filename = ConfigService.get_config_value(copy.deepcopy(PBA_WINDOWS_FILENAME_PATH)) filename = ConfigService.get_config_value(copy.deepcopy(PBA_WINDOWS_FILENAME_PATH))
return send_from_directory(FLASK_UPLOAD_PATH, filename) return send_from_directory(ABS_UPLOAD_PATH, filename)
@jwt_required @jwt_required
def post(self, file_type): def post(self, file_type):
@ -68,7 +64,7 @@ class FileUpload(flask_restful.Resource):
""" """
filename_path = PBA_LINUX_FILENAME_PATH if file_type == 'PBAlinux' else PBA_WINDOWS_FILENAME_PATH filename_path = PBA_LINUX_FILENAME_PATH if file_type == 'PBAlinux' else PBA_WINDOWS_FILENAME_PATH
filename = ConfigService.get_config_value(filename_path) filename = ConfigService.get_config_value(filename_path)
file_path = UPLOADS_DIR.joinpath(filename) file_path = ABS_UPLOAD_PATH.joinpath(filename)
try: try:
if os.path.exists(file_path): if os.path.exists(file_path):
os.remove(file_path) os.remove(file_path)
@ -87,7 +83,7 @@ class FileUpload(flask_restful.Resource):
:return: filename string :return: filename string
""" """
filename = secure_filename(request_.files['filepond'].filename) filename = secure_filename(request_.files['filepond'].filename)
file_path = UPLOADS_DIR.joinpath(filename).absolute() file_path = ABS_UPLOAD_PATH.joinpath(filename).absolute()
request_.files['filepond'].save(str(file_path)) request_.files['filepond'].save(str(file_path))
ConfigService.set_config_value((PBA_LINUX_FILENAME_PATH if is_linux else PBA_WINDOWS_FILENAME_PATH), filename) ConfigService.set_config_value((PBA_LINUX_FILENAME_PATH if is_linux else PBA_WINDOWS_FILENAME_PATH), filename)
return filename return filename

View File

@ -6,14 +6,17 @@ import monkey_island.cc.services.config
__author__ = "VakarisZ" __author__ = "VakarisZ"
from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Where to find file names in config # Where to find file names in config
PBA_WINDOWS_FILENAME_PATH = ['monkey', 'post_breach', 'PBA_windows_filename'] PBA_WINDOWS_FILENAME_PATH = ['monkey', 'post_breach', 'PBA_windows_filename']
PBA_LINUX_FILENAME_PATH = ['monkey', 'post_breach', 'PBA_linux_filename'] PBA_LINUX_FILENAME_PATH = ['monkey', 'post_breach', 'PBA_linux_filename']
UPLOADS_DIR_NAME = 'userUploads' UPLOADS_DIR_NAME = 'userUploads'
PBA_UPLOAD_PATH = ['monkey_island', 'cc', UPLOADS_DIR_NAME]
UPLOADS_DIR = Path(*PBA_UPLOAD_PATH)
ABS_UPLOAD_PATH = Path(MONKEY_ISLAND_ABS_PATH, 'cc', UPLOADS_DIR_NAME)
def remove_PBA_files(): def remove_PBA_files():
@ -27,7 +30,7 @@ def remove_PBA_files():
def remove_file(file_name): def remove_file(file_name):
file_path = os.path.join(UPLOADS_DIR, file_name) file_path = os.path.join(ABS_UPLOAD_PATH, file_name)
try: try:
if os.path.exists(file_path): if os.path.exists(file_path):
os.remove(file_path) os.remove(file_path)