cybersecurity · intermediate · ~15 min
Spot characters that break out of a shell command.
Flag input that carries characters able to break out of a shell command — an audit signal for command injection.
Implement int has_shell_meta(const char *s) that returns 1 if s contains any shell metacharacter, else 0.
The metacharacters are:
; | & $ ` ( ) < > \n
s: a NUL-terminated string the grader passes.Returns int: 1 if any listed metacharacter is present, else 0.
has_shell_meta("ls; rm -rf /") -> 1
has_shell_meta("a|b") -> 1
has_shell_meta("report.txt") -> 0
execv with an argv array).A NUL-terminated string s.
An int: 1 if s contains any shell metacharacter, else 0.
Detection only — do not sanitise.
#include <string.h>
int has_shell_meta(const char *s) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.