When you type a command with a “*“ in bash, bash expands it to the list of all files in the directory and passes them all as arguments to the program. For example, “rm *“ , will remove files in the current directory.
Most command line programs can take switches that affect how they work. For example, the ls command, when ran without any switches, looks like the output below.
Now let’s say you want to know what group and user owns these files. You can pass “-l” to the ls program to figure that out, which looks like this:
Notice there is a file named -l in our directory. Let’s try “ls *“ now and see what happens:
The last two outputs are similar, but the output of “ls *“ is different. It is missing the “-l” file, which was interpreted by ls as the “-l” switch. There’s no way for the ls program to tell that the “-l” came from the wildcard expansion and wasn’t actually what we in intended. It’s equivalent to running:
Misinterpreted filenames can lead to problems when someone runs a wildcard expansion on a folder they download from the Internet, for example, without first checking the filenames. Could this be used to attack someone’s computer? Can we make a program do something bad by having specially-named files in the directory? Yes, it turns out that we can.
This problem is well-known, but it still surprises a lot of people. There has been some discussion of it on the Full Disclosure mailing list.
Generally, we’re told that this is a “feature”, and that doing anything else would be even more surprising and difficult to understand to someone who really understands what’s going on. Even if that’s the case, if lots of people misunderstand it, and those people can be exploited as a result, then it should be considered a security vulnerability, either in the design or in the documentation.
The posting to Full Disclosure demonstrates how wildcard expansion can be abused to make a user delete files they didn’t intend to. That’s annoying, but isn’t too severe a problem. What we really want is to turn an improper use of “*“ into code execution, and that’s what we’ve done.
To show that it’s possible to turn this problem into an arbitrary code execution attack, we attack the “scp” command. The scp command provides the “-o” option, which passes a configuration option to ssh. Luckily, ssh has a configuration option that involves running a command. We can take advantage of this to get our script running.
Suppose we have control over the contents of a directory, and inside that directory our victim will run the following command. Imagine, for example, that the user just downloaded a web application’s source code from the attacker’s website and is uploading the files to their web server.
To exploit this command, in the directory we place three files:
“-o” - SCP will interpret this file as the “-o” switch.
“ProxyCommand sh supercool.sh %h %p” - SCP will interpret this file’s name as the argument to the “-o” switch.
“supercool.sh” - The script that will run, containing the attacker’s code.
“zzz.txt” - Another file in the directory which serves no purpose for the exploit.
It’s okay to have more files in the directory, so long as none of their names fall between “-o” and “ProxyCommand” in alphabetical order, or come before “-o” in alphabetical order (bash ignores the leading dash when sorting the names, and is case-insensitive). This limitation probably makes actual attacks difficult, since the very suspicious “ProxyCommand” file will show up near the top of the directory listing, if the user does look.
Inside “supercool.sh”, we have a script that will do what “ProxyCommand” is supposed to do, along with some malicious commands:
When the victim runs their scp command, it will appear successful:
But, when the user checks their /tmp/ directory, they’ll see our message:
You can download the entire proof of concept directory in a .zip file here. Be careful, and have fun!