Skip to content

Commit 79011a2

Browse files
DanielMadsenDKDaniel Madsen
andauthored
Added CommandInjectionChecker for checking for command injection (#2276)
* Add SQLInjectionValidator script for user input validation against SQL injection * Add CommandInjectionChecker utility for detecting command injection attacks --------- Co-authored-by: Daniel Madsen <daniel@madsen.local>
1 parent f409e84 commit 79011a2

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* CommandInjectionChecker
3+
*
4+
* A ServiceNow Script Include that provides security utilities for detecting
5+
* command injection attack patterns in user input strings.
6+
*
7+
* PURPOSE:
8+
* This utility class is designed to identify potentially malicious command
9+
* injection payloads by analyzing input strings for common shell metacharacters
10+
* and command substitution patterns. It serves as a first-line defense against
11+
* command injection attacks in server-side scripts.
12+
*
13+
* USAGE:
14+
* var checker = new CommandInjectionChecker();
15+
* var isSuspicious = checker.containsCommandInjection(userInput);
16+
* if (isSuspicious) {
17+
* gs.warn('Potential command injection detected in input');
18+
* }
19+
*
20+
* SECURITY NOTES:
21+
* - This checker detects PATTERNS, not guaranteed exploits
22+
* - Use this as ONE layer of defense, not the only layer
23+
* - Always validate and sanitize user input at multiple levels
24+
* - Consider using parameterized queries and proper escaping
25+
* - Log suspicious inputs for security auditing
26+
* - Never trust user input, even after this check passes
27+
*
28+
*/
29+
var CommandInjectionChecker = Class.create();
30+
31+
CommandInjectionChecker.prototype = {
32+
initialize: function() {
33+
34+
// Regex pattern to detect command-injection attempts.
35+
// This pattern matches the following items in the input string:
36+
// 1) Command separators:
37+
// - semicolon: `;`
38+
// - single pipe: `|` (common shell pipe)
39+
// - logical OR chaining: `||`
40+
// - logical AND chaining: `&&`
41+
// Note: this pattern matches both single `|` and the double `||`, and it
42+
// matches `&&` for logical AND chaining.
43+
//
44+
// 2) Command substitution starts:
45+
// - `$(` e.g. `$(command)`
46+
// - `${` some shells/templating forms use this
47+
// - `$[` other less-common forms or obfuscation using bracket notation
48+
//
49+
// 3) Backtick execution:
50+
// - `` ` `` (backtick command substitution)
51+
//
52+
// 4) Escaped separators (literal backslash before separator):
53+
// - `\;` (escaped semicolon)
54+
// - `\|` (escaped pipe)
55+
//
56+
// 5) Line control characters that can be used to inject or terminate commands:
57+
// - newline: `\n`
58+
// - carriage return: `\r`
59+
//
60+
// 6) Null byte:
61+
// - `\x00` (NULL byte — written as `\x00` in the regex; commonly shown as `\0`)
62+
this.injectionPattern = /[;\n\r\x00`]|(\|\||&&)|\$\(|\$\{|\$\[|\\;|\\\||\|/;
63+
64+
// Type metadata for ServiceNow framework
65+
this.type = 'CommandInjectionChecker';
66+
},
67+
68+
containsCommandInjection: function(input) {
69+
// INPUT VALIDATION
70+
// Handle null or undefined input - return false (safe default)
71+
if (input === null || input === undefined) {
72+
gs.debug('CommandInjectionChecker: Null or undefined input provided');
73+
return false;
74+
}
75+
// Handle empty string - return false (safe default)
76+
if (input.length === 0) {
77+
return false;
78+
}
79+
// INJECTION DETECTION
80+
// Test the input against the compiled regex pattern
81+
// Reset the regex lastIndex to ensure proper matching
82+
this.injectionPattern.lastIndex = 0;
83+
84+
// Perform the pattern match
85+
var hasInjectionPattern = this.injectionPattern.test(input);
86+
// LOGGING FOR SECURITY AUDITING
87+
if (hasInjectionPattern) {
88+
// Log suspicious input for security monitoring
89+
// Truncate very long inputs to prevent log flooding
90+
var truncatedInput = input.length > 100 ?
91+
input.substring(0, 100) + '...' :
92+
input;
93+
94+
gs.warn('CommandInjectionChecker: Potential command injection detected in input: ' +
95+
truncatedInput);
96+
}
97+
return hasInjectionPattern;
98+
},
99+
type: 'CommandInjectionChecker'
100+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The CommandInjectionChecker is a ServiceNow Script Include designed to detect potential command injection attacks in user-supplied strings. It scans for common shell metacharacters and patterns (e.g., ;, |, &, $(), `) using regex, providing a boolean result to flag suspicious inputs. This utility enhances security in server-side automation without relying on external libraries, following ServiceNow best practices for input validation.
2+
3+
Example on how to use it:
4+
5+
var checker = new CommandInjectionChecker();
6+
7+
// Safe input
8+
if (!checker.containsCommandInjection('Normal text')) {
9+
gs.info('Input is clean.');
10+
}
11+
12+
// Suspicious input
13+
if (checker.containsCommandInjection('ls; rm -rf /')) {
14+
gs.error('Command injection detected - blocking action.');
15+
}

0 commit comments

Comments
 (0)