-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdetect_shell_injection_test.py
More file actions
428 lines (276 loc) · 13.6 KB
/
detect_shell_injection_test.py
File metadata and controls
428 lines (276 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import pytest
from .detect_shell_injection import detect_shell_injection
def is_shell_injection(command: str, user_input: str):
assert (
detect_shell_injection(command, user_input) == True
), f"command: {command}, userInput: {user_input}"
def is_not_shell_injection(command: str, user_input: str):
assert (
detect_shell_injection(command, user_input) == False
), f"command: {command}, userInput: {user_input}"
def test_single_characters_ignored():
is_not_shell_injection("ls `", "`")
is_not_shell_injection("ls *", "*")
is_not_shell_injection("ls a", "a")
def test_no_user_input():
is_not_shell_injection("ls", "")
is_not_shell_injection("ls", " ")
is_not_shell_injection("ls", " ")
is_not_shell_injection("ls", " ")
def test_user_input_not_in_command():
is_not_shell_injection("ls", "$(echo)")
def test_user_input_longer_than_command():
is_not_shell_injection("`ls`", "`ls` `ls`")
def test_detects_command_substitution():
is_shell_injection("ls $(echo)", "$(echo)")
is_shell_injection('ls "$(echo)"', "$(echo)")
is_shell_injection(
'echo $(echo "Inner: $(echo "This is nested")")',
'$(echo "Inner: $(echo "This is nested")")',
)
is_not_shell_injection("ls '$(echo)'", "$(echo)")
is_not_shell_injection(
'ls \'$(echo "Inner: $(echo "This is nested")")\'',
'$(echo "Inner: $(echo "This is nested")")',
)
def test_detects_backticks():
is_shell_injection("echo `echo`", "`echo`")
def test_checks_unsafely_quoted():
is_shell_injection("ls '$(echo)", "$(echo)")
def test_single_quote_between_single_quotes():
is_shell_injection("ls ''single quote''", "'single quote'")
# Test not ported : Requires `quote` function
# t.test("it ignores escaped backticks", async () => {
# const domain = "www.example`whoami`.com";
# const args = ["--domain", domain];
# isNotShellInjection(`binary ${quote(args)}`, domain);
# });
def test_no_special_chars_inside_double_quotes():
is_shell_injection('ls "whatever$"', "whatever$")
is_shell_injection('ls "whatever!"', "whatever!")
is_shell_injection('ls "whatever`"', "whatever`")
def test_no_semi_colon():
is_shell_injection("ls whatever;", "whatever;")
is_not_shell_injection('ls "whatever;"', "whatever;")
is_not_shell_injection("ls 'whatever;'", "whatever;")
def test_rm_rf_executed_by_using_semi_colon():
is_shell_injection("ls; rm -rf", "; rm -rf")
def test_rm_rf_is_flagged_as_shell_injection():
is_shell_injection("rm -rf", "rm -rf")
def test_shell_injection_with_chained_commands_using_and():
is_shell_injection("ls && rm -rf /", "&& rm -rf /")
def test_shell_injection_with_or_logic():
is_shell_injection("ls || echo 'malicious code'", "|| echo 'malicious code'")
def test_detects_redirection_attempts():
is_shell_injection("ls > /dev/null", "> /dev/null")
is_shell_injection("cat file.txt > /etc/passwd", "> /etc/passwd")
def test_detects_append_redirection_attempts():
is_shell_injection("echo 'data' >> /etc/passwd", ">> /etc/passwd")
def test_detects_pipe_character_as_shell_injection():
is_shell_injection("cat file.txt | grep 'password'", "| grep 'password'")
def test_allows_safe_use_of_pipe_character_within_quotes():
is_not_shell_injection("echo '|'", "|")
def test_detects_nested_command_substitution():
is_shell_injection("echo $(cat $(ls))", "$(cat $(ls))")
def test_allows_safe_commands_within_single_quotes():
is_not_shell_injection("echo 'safe command'", "safe command")
def test_detects_unsafe_use_of_variables():
is_shell_injection("echo $USER", "$USER")
is_shell_injection("echo ${USER}", "${USER}")
is_shell_injection('echo "${USER}"', "${USER}")
def test_allows_safe_use_of_variables_within_quotes():
is_not_shell_injection("echo '$USER'", "$USER")
def test_detects_subshell_execution_with_backticks_inside_double_quotes():
is_shell_injection('ls "$(echo `whoami`)"', "`whoami`")
def test_detects_code_injection_with_newline_characters():
is_shell_injection("echo 'safe'\necho 'malicious'", "\necho 'malicious'")
def test_detects_attempts_to_escape_out_of_quotes():
is_shell_injection('echo "safe"; echo "malicious"', '"; echo "malicious"')
def test_correctly_handles_whitespace_in_inputs():
is_not_shell_injection("ls", " ")
is_shell_injection("ls ; rm -rf /", "; rm -rf /")
def test_detects_file_manipulation_commands():
is_shell_injection("touch /tmp/malicious", "touch /tmp/malicious")
is_shell_injection("mv /tmp/safe /tmp/malicious", "mv /tmp/safe /tmp/malicious")
def test_allows_commands_with_constants_that_resemble_user_input():
is_not_shell_injection("echo 'userInput'", "userInput")
def test_recognizes_safe_paths_that_include_patterns_similar_to_user_input():
is_not_shell_injection(
"ls /constant/path/without/user/input/", "/constant/path/without/user/input/"
)
def test_acknowledges_safe_use_of_special_characters_when_properly_encapsulated():
is_not_shell_injection('echo ";"', ";")
is_not_shell_injection('echo "&&"', "&&")
is_not_shell_injection('echo "||"', "||")
def test_treats_encapsulated_redirection_and_pipe_symbols_as_safe():
is_not_shell_injection("echo 'data > file.txt'", "data > file.txt")
is_not_shell_injection("echo 'find | grep'", "find | grep")
def test_recognizes_safe_inclusion_of_special_patterns_within_quotes_as_non_injections():
is_not_shell_injection("echo '$(command)'", "$(command)")
def test_considers_constants_with_semicolons_as_safe_when_non_executable():
is_not_shell_injection("echo 'text; more text'", "text; more text")
def test_acknowledges_commands_that_look_dangerous_but_are_safe_due_to_quoting():
is_not_shell_injection("echo '; rm -rf /'", "; rm -rf /")
is_not_shell_injection("echo '&& echo malicious'", "&& echo malicious")
def test_recognizes_commands_with_newline_characters_as_safe_when_encapsulated():
is_not_shell_injection("echo 'line1\nline2'", "line1\nline2")
def test_accepts_special_characters_in_constants_as_safe_when_no_execution():
is_not_shell_injection("echo '*'", "*")
is_not_shell_injection("echo '?'", "?")
is_not_shell_injection("echo '\\' ", "\\")
def test_does_not_flag_command_with_matching_whitespace_as_injection():
is_not_shell_injection(
"ls -l", " "
) # A single space is just an argument separator, not user input
def test_ignores_commands_where_multiple_spaces_match_user_input():
is_not_shell_injection(
"ls -l", " "
) # Multiple spaces between arguments should not be considered injection
def test_does_not_consider_leading_whitespace_in_commands_as_user_input():
is_not_shell_injection(
" ls -l", " "
) # Leading spaces before the command are not user-controlled
def test_treats_trailing_whitespace_in_commands_as_non_injection():
is_not_shell_injection("ls -l ", " ") # Trailing space after the command is benign
def test_recognizes_spaces_between_quotes_as_non_injective():
is_not_shell_injection(
"echo ' ' ", " "
) # Space within quotes is part of the argument, not a separator
def test_handles_spaces_within_quoted_arguments_correctly():
is_not_shell_injection(
"command 'arg with spaces'", " "
) # Spaces within a quoted argument should not be flagged
def test_correctly_interprets_spaces_in_mixed_argument_types():
is_not_shell_injection(
"command arg1 'arg with spaces' arg2", " "
) # Mixed argument types with internal spaces are safe
def test_ignores_spaces_in_commands_with_concatenated_arguments():
is_not_shell_injection(
"command 'arg1'arg2'arg3'", " "
) # Lack of spaces in concatenated arguments is intentional and safe
def test_does_not_flag_spaces_in_commands_with_no_arguments():
is_not_shell_injection("command", " ") # No arguments mean spaces are irrelevant
def test_considers_spaces_in_environment_variable_assignments_as_safe():
is_not_shell_injection(
"ENV_VAR='value' command", " "
) # Spaces around environment variable assignments are not injections
def test_new_lines_in_commands_are_considered_injections():
is_shell_injection("ls \nrm", "\nrm")
is_shell_injection("ls \nrm -rf", "\nrm -rf")
def test_new_lines_alone_are_not_considered_injections():
is_not_shell_injection("ls\n\n", "\n\n")
def test_certain_commands_are_always_flagged_as_dangerous():
is_shell_injection("/bin/rm -rf", "/bin/rm -rf")
is_shell_injection("rm -rf", "rm -rf")
is_shell_injection("rm -rf /", "rm -rf /")
is_shell_injection("sleep 10", "sleep 10")
is_shell_injection("sleep 10 &", "sleep 10 &")
is_shell_injection("shutdown -h now", "shutdown -h now")
is_shell_injection("halt", "halt")
is_shell_injection("poweroff", "poweroff")
is_shell_injection("reboot", "reboot")
is_shell_injection("reboot -f", "reboot -f")
is_shell_injection("ifconfig", "ifconfig")
is_shell_injection("ifconfig -a", "ifconfig -a")
is_shell_injection("kill", "kill")
is_shell_injection("killall", "killall")
is_shell_injection("killall -9", "killall -9")
is_shell_injection("chmod", "chmod")
is_shell_injection("chmod 777", "chmod 777")
is_shell_injection("chown", "chown")
is_shell_injection("chown root", "chown root")
def test_rm_being_part_of_other_commands():
is_shell_injection('find /path/to/search -type f -name "pattern" | xargs rm', "rm")
is_shell_injection(
'find /path/to/search -type f -name "pattern" -exec rm {} \\;', "rm"
)
is_shell_injection("ls .|rm", "rm")
def test_ignores_dangerous_commands_if_part_of_string():
is_not_shell_injection("binary sleepwithme", "sleepwithme")
is_not_shell_injection("binary rm-rf", "rm-rf")
is_not_shell_injection("term", "term")
is_not_shell_injection("rm /files/rm.txt", "rm.txt")
def test_does_not_flag_domain_name_as_argument_unless_contains_backticks():
is_not_shell_injection("binary --domain www.example.com", "www.example.com")
is_not_shell_injection(
"binary --domain https://www.example.com", "https://www.example.com"
)
is_shell_injection(
"binary --domain www.example`whoami`.com", "www.example`whoami`.com"
)
is_shell_injection(
"binary --domain https://www.example`whoami`.com",
"https://www.example`whoami`.com",
)
def test_flags_colon_if_used_as_command():
is_shell_injection(":|echo", ":|")
is_shell_injection(":| echo", ":|")
is_shell_injection(": | echo", ": |")
def test_detects_shell_injection():
is_shell_injection("/usr/bin/kill", "/usr/bin/kill")
def test_detects_shell_injection_with_uppercase_path():
is_shell_injection("/usr/bIn/kill", "/usr/bIn/kill")
def test_detects_shell_injection_with_uppercase_command():
is_shell_injection("/bin/CAT", "/bin/CAT")
def test_detects_shell_injection_with_uppercase_path_and_command():
is_shell_injection("/bIn/LS -la", "/bIn/LS -la")
def test_shell_injection_with_multiple_slashes():
is_shell_injection("//bin/ls", "//bin/ls")
is_shell_injection("///bin/ls", "///bin/ls")
def test_shell_injection_with_dotdot():
is_shell_injection("../bin/ls", "../bin/ls")
is_shell_injection("../../bin/ls", "../../bin/ls")
is_shell_injection("/../bin/ls", "/../bin/ls")
is_shell_injection("/./bin/ls", "/./bin/ls")
def test_shell_injection_with_tilde():
is_shell_injection("echo ~", "~")
is_shell_injection("ls ~/.ssh", "~/.ssh")
def test_no_shell_injection_with_tilde():
is_not_shell_injection("~", "~")
is_not_shell_injection("ls ~/path", "path")
def test_real_case():
is_shell_injection(
"command -disable-update-check -target https://examplx.com|curl+https://cde-123.abc.domain.com+%23 -json-export /tmp/5891/8526757.json -tags microsoft,windows,exchange,iis,gitlab,oracle,cisco,joomla -stats -stats-interval 3 -retries 3 -no-stdin",
"https://examplx.com|curl+https://cde-123.abc.domain.com+%23",
)
def test_false_positive_with_email():
is_not_shell_injection(
"echo token | docker login --username john.doe@acme.com --password-stdin hub.acme.com",
"john.doe@acme.com",
)
def test_at_sign_with_shell_syntax():
is_shell_injection("'echo \"${array[@]}\"'", "${array[@]}")
is_shell_injection("echo $@", "$@")
def test_allows_comma_separated_list():
is_not_shell_injection(
"command -tags php,laravel,drupal,phpmyadmin,symfony -stats",
"php,laravel,drupal,phpmyadmin,symfony",
)
def test_it_flags_comma_in_loop():
is_shell_injection(
"""command for (( i=0, j=10; i<j; i++, j-- ))
do
echo "$i $j"
done""",
"for (( i=0, j=10; i<j; i++, j-- ))",
)
def test_carriage_return_in_user_input_is_flagged():
is_shell_injection("ls \rrm", "\rrm")
is_shell_injection("ls \rrm -rf", "\rrm -rf")
def test_form_feed_in_user_input_is_flagged():
is_shell_injection("ls \frm", "\frm")
is_shell_injection("ls \frm -rf", "\frm -rf")
def test_carriage_return_in_user_input_is_flagged_when_userinput_is_command():
is_shell_injection("sleep\r10", "sleep\r10")
is_shell_injection("shutdown\r-h\rnow", "shutdown\r-h\rnow")
def test_form_feed_in_user_input_is_flagged_when_userinput_is_command():
is_shell_injection("sleep\f10", "sleep\f10")
is_shell_injection("shutdown\f-h\fnow", "shutdown\f-h\fnow")
def test_carriage_return_as_separator_between_commands():
is_shell_injection("ls\rrm", "rm")
is_shell_injection("echo test\rrm -rf /", "rm")
is_shell_injection("rm\rls", "rm")
def test_form_feed_as_separator_between_commands():
is_shell_injection("ls\frm", "rm")
is_shell_injection("echo test\frm -rf /", "rm")
is_shell_injection("rm\fls", "rm")