Skip to content

Commit b1ab252

Browse files
authored
Merge pull request #1288 from guardicore/ransomware-target-dir-validators
Validate ransomware target directories
2 parents b17b85d + 4bec957 commit b1ab252

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Defined in UI on ValidationFormats.js
22
IP_RANGE = "ip-range"
33
IP = "ip"
4+
VALID_RANSOMWARE_TARGET_PATH_LINUX = "valid-ransomware-target-path-linux"
5+
VALID_RANSOMWARE_TARGET_PATH_WINDOWS = "valid-ransomware-target-path-windows"

monkey/monkey_island/cc/services/config_schema/ransomware.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from common.common_consts.validation_formats import (
2+
VALID_RANSOMWARE_TARGET_PATH_LINUX,
3+
VALID_RANSOMWARE_TARGET_PATH_WINDOWS,
4+
)
5+
16
RANSOMWARE = {
27
"title": "Ransomware",
38
"type": "object",
@@ -27,6 +32,7 @@
2732
"linux_target_dir": {
2833
"title": "Linux target directory",
2934
"type": "string",
35+
"format": VALID_RANSOMWARE_TARGET_PATH_LINUX,
3036
"default": "",
3137
"description": "A path to a directory on Linux systems that contains "
3238
"files that you will allow Infection Monkey to encrypt. If no "
@@ -35,6 +41,7 @@
3541
"windows_target_dir": {
3642
"title": "Windows target directory",
3743
"type": "string",
44+
"format": VALID_RANSOMWARE_TARGET_PATH_WINDOWS,
3845
"default": "",
3946
"description": "A path to a directory on Windows systems that contains "
4047
"files that you will allow Infection Monkey to encrypt. If no "

monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationErrorMessages.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {IP, IP_RANGE} from './ValidationFormats';
1+
import {IP, IP_RANGE, VALID_RANSOMWARE_TARGET_PATH_LINUX, VALID_RANSOMWARE_TARGET_PATH_WINDOWS} from './ValidationFormats';
2+
3+
let invalidDirMessage = 'Invalid directory. Path should be absolute or begin with an environment variable.';
24

35
export default function transformErrors(errors) {
46
return errors.map(error => {
@@ -8,6 +10,10 @@ export default function transformErrors(errors) {
810
error.message = 'Invalid IP range, refer to description for valid examples.'
911
} else if (error.name === 'format' && error.params.format === IP) {
1012
error.message = 'Invalid IP.'
13+
} else if (error.name === 'format' && error.params.format === VALID_RANSOMWARE_TARGET_PATH_LINUX) {
14+
error.message = invalidDirMessage
15+
} else if (error.name === 'format' && error.params.format === VALID_RANSOMWARE_TARGET_PATH_WINDOWS) {
16+
error.message = invalidDirMessage
1117
}
1218
return error;
1319
});

monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationFormats.js

+38-1
Original file line numberDiff line numberDiff line change
@@ -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
22
const cidrNotationRegex = '([0-9]|1[0-9]|2[0-9]|3[0-2])'
33
const hostnameRegex = '^([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*.?)*([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*)$'
44

5+
6+
const linuxAbsolutePathRegex = /^\// // path starts with `/`
7+
const linuxPathStartsWithEnvVariableRegex = /^\$/ // path starts with `$`
8+
const linuxPathStartsWithTildeRegex = /^~/ // path starts with `~`
9+
10+
11+
const windowsAbsolutePathRegex = /^([A-Za-z]:(\\|\/))/ // path starts like `C:\` OR `C:/`
12+
const windowsEnvVarNonNumeric = '[A-Za-z#\\$\'\\(\\)\\*\\+,\\-\\.\\?@\\[\\]_`\\{\\}~ ]'
13+
const windowsPathStartsWithEnvVariableRegex = new RegExp(
14+
`^%(${windowsEnvVarNonNumeric}+(${windowsEnvVarNonNumeric}|\\d)*)%`
15+
) // path starts like `$` OR `%abc%`
16+
const windowsUncPathRegex = /^\\{2}/ // Path starts like `\\`
17+
const emptyRegex = /^$/
18+
19+
520
export const IP_RANGE = 'ip-range';
621
export const IP = 'ip';
22+
export const VALID_RANSOMWARE_TARGET_PATH_LINUX = 'valid-ransomware-target-path-linux'
23+
export const VALID_RANSOMWARE_TARGET_PATH_WINDOWS = 'valid-ransomware-target-path-windows'
724

825
export const formValidationFormats = {
926
[IP_RANGE]: buildIpRangeRegex(),
10-
[IP]: buildIpRegex()
27+
[IP]: buildIpRegex(),
28+
[VALID_RANSOMWARE_TARGET_PATH_LINUX]: buildValidRansomwarePathLinuxRegex(),
29+
[VALID_RANSOMWARE_TARGET_PATH_WINDOWS]: buildValidRansomwarePathWindowsRegex()
1130
};
1231

1332
function buildIpRangeRegex(){
@@ -22,3 +41,21 @@ function buildIpRangeRegex(){
2241
function buildIpRegex(){
2342
return new RegExp('^'+ipRegex+'$')
2443
}
44+
45+
function buildValidRansomwarePathLinuxRegex() {
46+
return new RegExp([
47+
emptyRegex.source,
48+
linuxAbsolutePathRegex.source,
49+
linuxPathStartsWithEnvVariableRegex.source,
50+
linuxPathStartsWithTildeRegex.source
51+
].join('|'))
52+
}
53+
54+
function buildValidRansomwarePathWindowsRegex() {
55+
return new RegExp([
56+
emptyRegex.source,
57+
windowsAbsolutePathRegex.source,
58+
windowsPathStartsWithEnvVariableRegex.source,
59+
windowsUncPathRegex.source
60+
].join('|'))
61+
}

0 commit comments

Comments
 (0)