Command and template injection
OS command and template injection (SSTI) share the SQL principle that input becomes code, but the consequence is often RCE. In one case input lands in a string for the shell, in the other it becomes the template itself. The defence: do not build a command or template from input, call the program without a shell using an argument array, and pass input only as a variable value.
Pentest · Injections
A service converts a file with an external utility. What if the file name contains a semicolon?
The same principle "input becomes code" works not only in SQL. When an application builds a string for a command shell or for a template engine, the user’s input can change the command or expression the server executes. The consequences are often heavier than with SQL injection: execution of arbitrary commands on the server (RCE). The lesson covers OS command injection and template injection (SSTI) and what they have in common.
OS command injection. The application calls a utility through a shell:
exec("convert " + name + " out.png");
A shell understands special characters: ;, |, &&, $(…), backticks, a line break. If name equals a.png; id, the shell executes two commands, and the second runs as the application’s process. A safe probe for a test environment: ; sleep 5 gives a delay and ; id shows a line with uid= in the response. For the "blind" case, when no output is visible, a delay or a request to a controlled test host remains.
A string for the shell or an argument array
exec("convert " + name)
Everything goes as one string to a shell, which parses special characters in the input.
execFile("convert", [name])
Arguments are passed as an array without a shell: ; stays part of the file name.
Also: generate the file name on the server, check permitted formats against a list, and -- before the argument stops a name that starts with a hyphen from becoming a flag (argument injection).
A real case. CVE-2023-2868 in the Barracuda Email Security Gateway appliance (firmware 5.1.3.001–9.2.0.006): according to the NVD description, a specially formatted file name inside a TAR attachment reached the Perl operator qx and the system executed a command with the appliance’s rights. So an email with an attachment gave remote command execution. The vulnerability is in the CISA KEV catalogue; according to the vendor as cited by Rapid7 analysts, it had been exploited since October 2022. The same rule: a name from an attachment is input and must not be passed to a shell.
SSTI (Server-Side Template Injection). Template engines (Jinja2, Twig, FreeMarker, Velocity, Thymeleaf) insert values into a ready template: input is only data there. The flaw appears when input becomes the template itself:
# safe: the input is a value
render_template("hello.html", name=name)
# vulnerable: the input is the template text
render_template_string("Hello " + name)
A probe: send {{7*7}}. If the raw server response (not just the DOM in the browser) already contains Hello 49, the server evaluated the expression, and it is SSTI, not XSS (evaluation only in the DOM comes from a client-side template framework: that is CSTI, a kind of XSS): with HTML reflection you would see literally {{7*7}}. Other engines take ${7*7} or <%= 7*7 %>. To identify the engine a second evaluation is used:
| Expression | Twig | Jinja2 |
|---|---|---|
{{7*7}} | 49 | 49 |
{{7*'7'}} | 49 | 7777777 |
Jinja2 (Python) repeats the string, Twig (PHP) turns "7" into a number. The following steps (access to the engine’s objects and command execution) are practised in a training lab on the PortSwigger Web Security Academy, while in a real test one stops here: a safe probe is enough as proof. The method was described by James Kettle (PortSwigger, 2015), who showed vulnerable cases in enterprise products (Alfresco, XWiki): when users edit a template "for convenience", the input is the code.
Fixes and limits.
- Do not build a template from input; pass input only as a variable value. If users need formatting, take "logic-less" languages (Markdown, Mustache) with sanitization;
- A template engine sandbox helps as an extra layer, but historically it was bypassed, so it is not the main fix;
- For commands: no shell, an argument array, an allowlist of values, a process with minimal rights.
Other injections of the same kind. NoSQL ({"$ne": null} instead of a password in JSON), LDAP, XPath, headers (CRLF in a header value), logs (forged lines). The principle is one: the command structure must not depend on input, use an API that takes data separately.
Common mistakes.
- Stripping
;and|from input:&,$(), a line break remain; - Mixing up SSTI with XSS: reflecting
{{7*7}}without evaluation is not SSTI; - Proving the flaw with destructive commands instead of
id,sleepor a calculation.
Check yourself
A service calls exec("ping " + host). The developer removed the ; character from host. Is it safe?
Answer
No. A shell has other separators: |, &&, $(…), backticks, a line break. The reliable option is a call without a shell with an argument array and a check that host is a host name or an IP address.
A search answers Nothing found for {{7*7}}. Is this SSTI?
Answer
No, the expression is shown as text: it was not evaluated. If the raw server response said "for 49", the server evaluated the expression and it would be SSTI. Reflected text without execution is checked for XSS separately.
id for commands, {{7*7}} and a second check for the engine in SSTI, with no destructive actions. The fix is a call without a shell with an argument array and an allowlist; I treat a template sandbox only as an extra layer.