Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate ransomware target directories #1288

Merged
merged 13 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions monkey/common/common_consts/validation_formats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Defined in UI on ValidationFormats.js
IP_RANGE = "ip-range"
IP = "ip"
VALID_RANSOMWARE_TARGET_PATH_LINUX = "valid-ransomware-target-path-linux"
VALID_RANSOMWARE_TARGET_PATH_WINDOWS = "valid-ransomware-target-path-windows"
7 changes: 7 additions & 0 deletions monkey/monkey_island/cc/services/config_schema/ransomware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from common.common_consts.validation_formats import (
VALID_RANSOMWARE_TARGET_PATH_LINUX,
VALID_RANSOMWARE_TARGET_PATH_WINDOWS,
)

RANSOMWARE = {
"title": "Ransomware",
"type": "object",
Expand All @@ -20,6 +25,7 @@
"linux_target_dir": {
"title": "Linux target directory",
"type": "string",
"format": VALID_RANSOMWARE_TARGET_PATH_LINUX,
"default": "",
"description": "A path to a directory on Linux systems that contains "
"files that you will allow Infection Monkey to encrypt. If no "
Expand All @@ -28,6 +34,7 @@
"windows_target_dir": {
"title": "Windows target directory",
"type": "string",
"format": VALID_RANSOMWARE_TARGET_PATH_WINDOWS,
"default": "",
"description": "A path to a directory on Windows systems that contains "
"files that you will allow Infection Monkey to encrypt. If no "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {IP, IP_RANGE} from './ValidationFormats';
import {IP, IP_RANGE, VALID_RANSOMWARE_TARGET_PATH_LINUX, VALID_RANSOMWARE_TARGET_PATH_WINDOWS} from './ValidationFormats';

let invalidDirMessage = 'Invalid directory. Path should be absolute or begin with an environment variable.';

export default function transformErrors(errors) {
return errors.map(error => {
Expand All @@ -8,6 +10,10 @@ export default function transformErrors(errors) {
error.message = 'Invalid IP range, refer to description for valid examples.'
} else if (error.name === 'format' && error.params.format === IP) {
error.message = 'Invalid IP.'
} else if (error.name === 'format' && error.params.format === VALID_RANSOMWARE_TARGET_PATH_LINUX) {
error.message = invalidDirMessage
} else if (error.name === 'format' && error.params.format === VALID_RANSOMWARE_TARGET_PATH_WINDOWS) {
error.message = invalidDirMessage
}
return error;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@ const ipRegex = '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0
const cidrNotationRegex = '([0-9]|1[0-9]|2[0-9]|3[0-2])'
const hostnameRegex = '^([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*.?)*([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*)$'


const linuxAbsolutePathRegex = /^\// // path starts with `/`
const linuxPathStartsWithEnvVariableRegex = /^\$/ // path starts with `$`
const linuxPathStartsWithTildeRegex = /^~/ // path starts with `~`


const windowsAbsolutePathRegex = /^([A-Za-z]:(\\|\/))/ // path starts like `C:\` OR `C:/`
const windowsEnvVarNonNumeric = '[A-Za-z#\\$\'\\(\\)\\*\\+,\\-\\.\\?@\\[\\]_`\\{\\}~ ]'
const windowsPathStartsWithEnvVariableRegex = new RegExp(
`^%(${windowsEnvVarNonNumeric}+(${windowsEnvVarNonNumeric}|\\d)*)%`
) // path starts like `$` OR `%abc%`
const windowsUncPathRegex = /^\\{2}/ // Path starts like `\\`
const emptyRegex = /^$/


export const IP_RANGE = 'ip-range';
export const IP = 'ip';
export const VALID_RANSOMWARE_TARGET_PATH_LINUX = 'valid-ransomware-target-path-linux'
export const VALID_RANSOMWARE_TARGET_PATH_WINDOWS = 'valid-ransomware-target-path-windows'

export const formValidationFormats = {
[IP_RANGE]: buildIpRangeRegex(),
[IP]: buildIpRegex()
[IP]: buildIpRegex(),
[VALID_RANSOMWARE_TARGET_PATH_LINUX]: buildValidRansomwarePathLinuxRegex(),
[VALID_RANSOMWARE_TARGET_PATH_WINDOWS]: buildValidRansomwarePathWindowsRegex()
};

function buildIpRangeRegex(){
Expand All @@ -22,3 +41,21 @@ function buildIpRangeRegex(){
function buildIpRegex(){
return new RegExp('^'+ipRegex+'$')
}

function buildValidRansomwarePathLinuxRegex() {
return new RegExp([
emptyRegex.source,
linuxAbsolutePathRegex.source,
linuxPathStartsWithEnvVariableRegex.source,
linuxPathStartsWithTildeRegex.source
].join('|'))
}

function buildValidRansomwarePathWindowsRegex() {
return new RegExp([
emptyRegex.source,
windowsAbsolutePathRegex.source,
windowsPathStartsWithEnvVariableRegex.source,
windowsUncPathRegex.source
].join('|'))
}