forked from p15670423/monkey
Add support for strings to be encrypted
This commit is contained in:
parent
3124f1eb5e
commit
fac6f970bb
|
@ -869,6 +869,7 @@ SCHEMA = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# This should be used for config values of array type (array of strings only)
|
||||||
ENCRYPTED_CONFIG_ARRAYS = \
|
ENCRYPTED_CONFIG_ARRAYS = \
|
||||||
[
|
[
|
||||||
['basic', 'credentials', 'exploit_password_list'],
|
['basic', 'credentials', 'exploit_password_list'],
|
||||||
|
@ -877,6 +878,12 @@ ENCRYPTED_CONFIG_ARRAYS = \
|
||||||
['internal', 'exploits', 'exploit_ssh_keys']
|
['internal', 'exploits', 'exploit_ssh_keys']
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# This should be used for config values of string type
|
||||||
|
ENCRYPTED_CONFIG_STRINGS = \
|
||||||
|
[
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class ConfigService:
|
class ConfigService:
|
||||||
default_config = None
|
default_config = None
|
||||||
|
@ -913,8 +920,11 @@ class ConfigService:
|
||||||
config = mongo.db.config.find_one({'name': 'initial' if is_initial_config else 'newconfig'}, {config_key: 1})
|
config = mongo.db.config.find_one({'name': 'initial' if is_initial_config else 'newconfig'}, {config_key: 1})
|
||||||
for config_key_part in config_key_as_arr:
|
for config_key_part in config_key_as_arr:
|
||||||
config = config[config_key_part]
|
config = config[config_key_part]
|
||||||
if should_decrypt and (config_key_as_arr in ENCRYPTED_CONFIG_ARRAYS):
|
if should_decrypt:
|
||||||
config = [encryptor.dec(x) for x in config]
|
if config_key_as_arr in ENCRYPTED_CONFIG_ARRAYS:
|
||||||
|
config = [encryptor.dec(x) for x in config]
|
||||||
|
elif config_key_as_arr in ENCRYPTED_CONFIG_STRINGS:
|
||||||
|
config = encryptor.dec(config)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -1071,7 +1081,7 @@ class ConfigService:
|
||||||
"""
|
"""
|
||||||
Same as decrypt_config but for a flat configuration
|
Same as decrypt_config but for a flat configuration
|
||||||
"""
|
"""
|
||||||
keys = [config_arr_as_array[2] for config_arr_as_array in ENCRYPTED_CONFIG_ARRAYS]
|
keys = [config_arr_as_array[2] for config_arr_as_array in (ENCRYPTED_CONFIG_ARRAYS + ENCRYPTED_CONFIG_STRINGS)]
|
||||||
for key in keys:
|
for key in keys:
|
||||||
if isinstance(flat_config[key], collections.Sequence) and not isinstance(flat_config[key], string_types):
|
if isinstance(flat_config[key], collections.Sequence) and not isinstance(flat_config[key], string_types):
|
||||||
# Check if we are decrypting ssh key pair
|
# Check if we are decrypting ssh key pair
|
||||||
|
@ -1085,18 +1095,25 @@ class ConfigService:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _encrypt_or_decrypt_config(config, is_decrypt=False):
|
def _encrypt_or_decrypt_config(config, is_decrypt=False):
|
||||||
for config_arr_as_array in ENCRYPTED_CONFIG_ARRAYS:
|
for config_arr_as_array in (ENCRYPTED_CONFIG_ARRAYS + ENCRYPTED_CONFIG_STRINGS):
|
||||||
config_arr = config
|
config_arr = config
|
||||||
|
prev_config_arr = None
|
||||||
|
|
||||||
for config_key_part in config_arr_as_array:
|
for config_key_part in config_arr_as_array:
|
||||||
|
prev_config_arr = config_arr
|
||||||
config_arr = config_arr[config_key_part]
|
config_arr = config_arr[config_key_part]
|
||||||
|
|
||||||
for i in range(len(config_arr)):
|
if isinstance(config_arr, collections.Sequence) and not isinstance(config_arr, string_types):
|
||||||
# Check if array of shh key pairs and then decrypt
|
for i in range(len(config_arr)):
|
||||||
if isinstance(config_arr[i], dict) and 'public_key' in config_arr[i]:
|
# Check if array of shh key pairs and then decrypt
|
||||||
config_arr[i] = ConfigService.decrypt_ssh_key_pair(config_arr[i]) if is_decrypt else \
|
if isinstance(config_arr[i], dict) and 'public_key' in config_arr[i]:
|
||||||
ConfigService.decrypt_ssh_key_pair(config_arr[i], True)
|
config_arr[i] = ConfigService.decrypt_ssh_key_pair(config_arr[i]) if is_decrypt else \
|
||||||
else:
|
ConfigService.decrypt_ssh_key_pair(config_arr[i], True)
|
||||||
config_arr[i] = encryptor.dec(config_arr[i]) if is_decrypt else encryptor.enc(config_arr[i])
|
else:
|
||||||
|
config_arr[i] = encryptor.dec(config_arr[i]) if is_decrypt else encryptor.enc(config_arr[i])
|
||||||
|
else:
|
||||||
|
prev_config_arr[config_arr_as_array[-1]] =\
|
||||||
|
encryptor.dec(config_arr) if is_decrypt else encryptor.enc(config_arr)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def decrypt_ssh_key_pair(pair, encrypt=False):
|
def decrypt_ssh_key_pair(pair, encrypt=False):
|
||||||
|
|
Loading…
Reference in New Issue