Lesson 2 of 7
Command injection
Listen to an AI summary of Command injection
What is a command
The command below runs mogrify, a program that changes a picture:
mogrify -resize 200x200 photo.jpg
- mogrifythe program
- -resize 200x200the new size
- photo.jpgthe file to change
photo.jpg smaller.What is a shell
A shell is the program that reads a typed command and runs the command. Code can pass a command to a shell as a string, and the shell runs the command the same way.
The shell is an interpreter. The shell reads some characters as instructions. A semicolon ends one command. The shell then runs the text after the semicolon as a second command.
The sports shop resizes a photo
The sports shop lets a customer upload a profile photo. The browser sends the photo and the photo's file name in a request. The Python code below makes the photo smaller. Look at where name comes from and where name goes:
On the last line, subprocess.run is the Python function that starts a program. The words shell=True tell Python to pass the string to a shell.
A file name of photo.jpg makes the command mogrify -resize 200x200 photo.jpg. The shell runs the command and the photo gets smaller.
A file name with a semicolon
The person who sends the request chooses the file name. An attacker sends a photo with the file name photo.jpg; rm orders.db. The program rm deletes a file, and orders.db is the file that holds the shop's orders.
mogrify -resize 200x200 photo.jpg; rm orders.db
- mogrify -resize 200x200 written by the programmer
- photo.jpg; rm orders.dbthe file name from the request
The shell reads the semicolon and runs two commands. The first command makes photo.jpg smaller. The second command is rm orders.db, which deletes the shop's orders.
What is command injection
Command injection is untrusted data joined into a command, so that the shell runs a command an attacker wrote. The full name is OS command injection (CWE-78), and OS is short for operating system, such as Windows.
The line that reads the file name is a source, and the line that starts the shell is a sink.
What is an argument
An argument is an extra word after the name of a program in a command. An argument tells the program what to work on. The command mogrify -resize 200x200 photo.jpg has three arguments, which are -resize, 200x200 and photo.jpg. The shell cuts a command into arguments at each space.
A list in place of a string
Python can start a program with no shell. The code gives subprocess.run a list, with the program first and each argument as an item of the list. Compare the two versions:
# Weakness: one string, and a shell reads the string subprocess.run("mogrify -resize 200x200 " + name, shell=True) # Safer: a list, and no shell subprocess.run(["mogrify", "-resize", "200x200", name])
In the safer version, name is one item of the list, so the file name is always one argument. No shell reads the file name, so a semicolon is one more character in the name. mogrify reads photo.jpg; rm orders.db as the name of one photo, and no second command runs.
What is argument injection
One problem remains in the safer version. Many programs read an argument that starts with a dash, the character -, as an option. -resize is an option of mogrify.
Argument injection (CWE-88) is untrusted data that starts with a dash, so a program reads the data as an option.
An attacker sends a photo with the file name -version. mogrify reads the file name as an option and not as a file. An option of another program can do more harm, such as write a file or run a second program.
Stopping argument injection
The fix is an allowlist. The sports shop permits letters, digits and full stops in a file name. The shop refuses a file name with any other character, so no file name starts with a dash.
Many programs also read the argument -- as the end of the options. The command rm -- -old.jpg deletes the file named -old.jpg.
Removing characters from the input
Some code removes the semicolon from the file name first. Removing characters is a weak defence. The shell reads other characters as instructions too, such as &&, | and $(. A list of characters to remove can miss one.
Removing the characters a shell reads also leaves the dash, so argument injection still works.
Examples of defences
- A library function in place of a command. The Python library Pillow resizes a photo with no command.
- A list of arguments and no shell, with the name of the program fixed in the code.
- An allowlist check on every argument from a request, and
--before the argument where the program supports--.
Names in other languages
Each row gives names in another language that start a program:
- JavaScript on a server
exec(passes a string to a shell.execFile(takes a list of arguments and starts no shell.- PHP
system(,exec(andshell_exec(each pass a string to a shell.- Java
Runtime.exec(starts a program with no shell, and cuts one string into arguments at each space. The code starts a shell when the program named is a shell, such assh.- Ruby
system(passes one string to a shell. Given the program and each argument separately,system(starts no shell.
Examples of what to look for
- A new line that joins data from a request into a command, such as
"mogrify -resize 200x200 " + name. - A new line that passes a string to a shell, such as
subprocess.run(command, shell=True)oros.system(command)in Python. - An item from a request in a list of arguments, with no
--before the item and no allowlist check. - The name of the program read from a request.
- A line that removes characters such as
;from the input as the only defence.
Check your understanding
State what the shell does with a semicolon in a command.
Show the answer
The shell ends one command at the semicolon. The shell then runs the text after the semicolon as a second command.
Which of these two lines passes the file name as a separate argument? Line A is
subprocess.run("mogrify -resize 200x200 " + name, shell=True). Line B issubprocess.run(["mogrify", "-resize", "200x200", name]).Show the answer
Line B. In line B,
nameis one item of a list, so the file name is always one argument.Give one reason why removing some characters from the input is a weak defence.
Show the answer
The shell reads many characters as instructions, and a list of characters to remove can miss one.
Practise it
Challenges with a weakness this lesson teaches.
- Thumbnail workerGiftwrap, BeginnerOpen
- Statement exportBrightledger, IntermediateOpen