Fixed configuration encryption/decryption to use b64 encoding

This commit is contained in:
VakarisZ 2021-06-01 14:15:58 +03:00
parent 7153b91c10
commit a94047d778
2 changed files with 12 additions and 9 deletions

View File

@ -4,11 +4,10 @@ from dataclasses import dataclass
import flask_restful import flask_restful
from flask import request from flask import request
from common.utils.exceptions import ( from common.utils.exceptions import ( # InvalidCredentialsError,
InvalidConfigurationError,
# InvalidCredentialsError,
NoCredentialsError,
FailedDecryption, FailedDecryption,
InvalidConfigurationError,
NoCredentialsError,
) )
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.utils.config_encryption import decrypt_config from monkey_island.cc.services.utils.config_encryption import decrypt_config
@ -32,7 +31,7 @@ class TempConfiguration(flask_restful.Resource):
def post(self): def post(self):
request_contents = json.loads(request.data) request_contents = json.loads(request.data)
try: try:
decrypt_config(request_contents["encrypted_config"], request_contents["password"]) decrypt_config(request_contents["config"], request_contents["password"])
self.import_config() self.import_config()
return ResponseContents().form_response() return ResponseContents().form_response()
# except InvalidCredentialsError: # except InvalidCredentialsError:

View File

@ -1,3 +1,4 @@
import base64
import io import io
import json import json
from typing import Dict from typing import Dict
@ -17,15 +18,18 @@ def encrypt_config(config: Dict, password: str) -> str:
plaintext_config_stream, ciphertext_config_stream, password, BUFFER_SIZE plaintext_config_stream, ciphertext_config_stream, password, BUFFER_SIZE
) )
ciphertext_config_bytes = str(ciphertext_config_stream.getvalue()) ciphertext_b64 = base64.b64encode(ciphertext_config_stream.getvalue())
return ciphertext_config_bytes
return str(ciphertext_b64)
def decrypt_config(enc_config: bytes, password: str) -> Dict: def decrypt_config(cyphertext: str, password: str) -> Dict:
if not password: if not password:
raise NoCredentialsError raise NoCredentialsError
ciphertext_config_stream = io.BytesIO(enc_config) cyphertext = base64.b64decode(cyphertext)
ciphertext_config_stream = io.BytesIO(cyphertext)
dec_plaintext_config_stream = io.BytesIO() dec_plaintext_config_stream = io.BytesIO()
len_ciphertext_config_stream = len(ciphertext_config_stream.getvalue()) len_ciphertext_config_stream = len(ciphertext_config_stream.getvalue())