Argument injection without a shell
Skipping the shell is necessary and not sufficient. A fixed program can still be steered by arguments that mean “read this file” or “run this option.”
Diagram
From a request field to an argument a program will honor
Step 1 of 4
Program
You call one binary you chose, with shell execution off. That part is right.
program = "convert" # You chose this program. # The caller does not choose it.
The binary is fixed and the shell is off.
A request field is placed where an option is parsed.
The program does something the feature never named.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
No shell is the first step
- What fails
- A shell parses metacharacters. Turning the shell off removes that parser. It does not remove the program’s own parser.
- Who is affected
- People whose files and privileges the application account can reach.
- What you would notice
- A review that stops at “we used execFile” and never reads the argument list.
- What you change
- Keep the shell off, and still allow-list every argument that is not a constant.
Options are a language
- What fails
- Many programs treat a leading dash as an option, and some options name files or other programs. Caller data in that position is still control.
- Who is affected
- The same people, because the program runs with the app’s rights.
- What you would notice
- A filename or a label from the request placed before a double-dash separator, or placed where a path option is expected.
- What you change
- Pass constants for options. If the program supports end-of-options, use it. Reject values that are not in the set you meant.
Paths you generated
- What fails
- Even a data argument that is a path should be a path you created, inside a directory you chose, not the raw upload name.
- Who is affected
- Owners of files outside that directory.
- What you would notice
- The client’s original filename appearing in the argument vector.
- What you change
- Write uploads to a generated name. Pass that path. Do not pass the client name to the binary.
The account beside the call
- What fails
- A correct argument list run as an administrator is still a large failure if any argument is ever wrong.
- Who is affected
- Everyone who shares that host.
- What you would notice
- The service account is an admin, or the feature shells out where a library would do.
- What you change
- Prefer a library. When you must exec, use an account that can read only the scratch directory.
Turning the shell off is not the last step
Command injection is taught as “the shell parsed metacharacters.” That lesson is real, and the first change is to call a fixed program with an argument array and the shell disabled. Stop there and you miss the program’s own interface. Image tools, archive tools, and git all have options that read files, write files, or fetch URLs. If a request field occupies that option, the caller is still steering the process.
Read the manual page for the binary you shell out to, in the defensive sense: which arguments are data, which are control, and whether a value that looks like an option is consumed as one. Then make the control arguments constants in your source code.
Allow-list the shape
A format, a mode, or a protocol belongs in a set you wrote. Compare the field to that set and reject anything else before you build the argument vector. Do not filter out “bad characters” and pass the remainder. The program’s grammar is not your filter’s grammar.
Put caller data, when it is truly data, in one position. Use the program’s end-of-options marker if it has one, so a value that starts with a dash is still data. Prefer not to pass caller strings at all. A generated id and a file you created give the program less to interpret.
What the account can already do
Assume an argument will be wrong once. The damage is whatever the user can do. An application user that can write the web root or read every home directory makes a small argument bug into a host bug. A user that can only see a scratch directory makes the same bug a failed conversion.
You would notice a service account in the administrators group, a client filename in the argument list, and no allow-list next to the call. The finding names the binary, the field, and the user. It does not include an argument string that performs the failure.
What you change
Replace the shell string. Allow-list non-constant arguments. Generate paths. Drop privileges. If a library can do the job inside the process, delete the exec. Every exec is a second product you now have to review when the binary updates.
Test that an unexpected format never starts the process, and that a normal conversion writes only inside the scratch directory. The people affected are whoever shares the application user’s rights. Say that in the ticket so the privilege change is in scope, not only the argument list.
Check yourself
Recognition and the fix. Nothing here is a procedure.
The code calls a fixed converter with the shell disabled, and it passes the caller’s format string through as an argument the converter treats as an option. What failed?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.