cybersecurity · intermediate · ~12 min · safe pentest lab
Detect specific substrings in untrusted input. Defence-in-depth on top of parameterised queries.
Flag input that contains classic SQL-injection metacharacters — a defence-in-depth detector that sits on top of parameterised queries (it does not replace them).
Implement int has_sqli_markers(const char *input) that returns 1 if input contains any of these markers, and 0 otherwise:
'--/*;UNION SELECT (case-insensitive)input: a fixed NUL-terminated string the grader provides (may be NULL).Returns 1 if any marker is present, 0 otherwise (NULL → 0).
has_sqli_markers("hello world") -> 0
has_sqli_markers("O'Brien") -> 1 (single quote)
has_sqli_markers("admin'--") -> 1 (quote + comment)
has_sqli_markers("DROP TABLE users;") -> 1 (';')
has_sqli_markers("/* comment */") -> 1 (block comment)
has_sqli_markers("Union SeLeCt 1,2") -> 1 (case-insensitive)
has_sqli_markers("union sneak") -> 0 ("UNION" without "SELECT")
has_sqli_markers(NULL) -> 0
NULL or empty input: return 0.UNION SELECT is matched case-insensitively; the other tokens match exactly.A metacharacter detector on the input layer adds defence in depth above prepared statements.
A fixed NUL-terminated string (may be NULL).
1 if any SQLi marker is present; otherwise 0.
Pure detection; UNION SELECT case-insensitive, other tokens exact; NULL-safe.
#include <stddef.h>
int has_sqli_markers(const char *input) {
/* TODO */
(void)input;
return 0;
}
Returning a count instead of a boolean. Forgetting NULL input. Case-sensitive match for UNION SELECT.
NULL input. Empty string. Input with quotes inside legitimate text (e.g. O'Brien) — still flagged because this is a flagger, not a smart parser.
O(n) per substring check; bounded constant number of checks.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.