NET::Ftp allows command injection in filenames
The NET::Ftp library's gettextfile method uses Kernel#open with a filename that can start with a pipe character, allowing command execution. An attacker controlling an FTP server could supply a malicious filename to a client that downloads files, leading to remote code execution on the client.
Originally disclosed on HackerOne
- Fixed in 13 days
- Paid in 13 days
- Disclosed after 17 days
Hi
While using NET::Ftp I realised you could get command execution through "malicious" file names.
The problem lies in the gettextfile(remotefile, localfile = File.basename(remotefile)) method.
When looking at the source code, you'll note:
def gettextfile(remotefile, localfile = File.basename(remotefile),
&block) # :yield: line
f = nil
result = nil
if localfile
f = open(localfile, "w") # Vulnerable code here. open("| os command","w")
elsif !block_given?
result = String.new
end
The localfile value will trigger command execution if the value is | os command. In general use, most users would likely provide their own localfile value and would not rely on the default of File.basename(remotefile); however, in some situations, such as listing and downloading all files in a FTP share, the remotefile value would be controlled by the remote host and could thus be manipulated into causing RCE. Since the file path is simply a string returned by the server (either ls -l style for the LIST command, or filenames for NLIST), there is no need/guarantee that filename will be a valid filename.
I have attached a sample server that can be used to trigger this vulnerability, as well as a sample client which is vulnerable.
Usage:
Change the host and port values in both ftpserver.rb and client.rb
Start the server: ruby ftpserver.rb
Run the client: ruby client.rb
Observe that a new file has been created in the CWD of the client.rb. The file will be called pang and contain the output of the id command. As seen in screenshot1.png
The provided attack example is a little contrived and assumes the user is accepting the file names provided by the server, rather than their own. However, since there is no clear indication in the documentation or an expectation that filenames could lead to RCE, users may be caught unaware. It would probably be best to not use open in NET::Ftp, but rather something like File.open, maintaining both expected behaviour and security.
Impact
Remote code execution through command injection. As a user of the NET::Ftp is expecting normal file creation behaviour, they might not be sanitising file paths.
Attachments

Context
Ruby's standard library ships NET::Ftp, a client for the FTP protocol. Among its convenience methods is gettextfile, which downloads a remote file and writes it to a local path. The method has a default argument for the local path: if the caller does not supply one, the local filename is derived from the remote filename with File.basename(remotefile).
The report shows that this default path is dangerous. The local filename is passed to open, and open in Ruby treats a leading pipe character as a request to run a shell command. Because the remote filename comes from the FTP server, a malicious or compromised server can choose a filename that makes the client execute an arbitrary command.
Setup
The researcher wrote two small Ruby scripts, a sample server and a sample client, to demonstrate the issue. The only configuration needed is to set the host and port values in both scripts so that they match. The server is started first, then the client is run:
ruby ftpserver.rb
ruby client.rb
The client is written to use the vulnerable default, that is, it does not pass its own localfile argument and lets gettextfile derive the local name from the remote name supplied by the server.
Finding it
While using NET::Ftp, the researcher realised that command execution could be reached through "malicious" file names. The starting point is the signature of gettextfile:
def gettextfile(remotefile, localfile = File.basename(remotefile),
&block) # :yield: line
f = nil
result = nil
if localfile
f = open(localfile, "w") # Vulnerable code here. open("| os command","w")
elsif !block_given?
result = String.new
end
The comment in the snippet marks the line that matters: open(localfile, "w"). Ruby's open is not the same as File.open. When the string passed to it begins with |, open starts a subprocess and connects it to the returned IO object. So a localfile value of the form | os command runs that command instead of creating a file.
In ordinary use, most callers would pass their own localfile and would not rely on the default of File.basename(remotefile). The interesting case is a client that lists and downloads every file in an FTP share: there the remotefile value is controlled by the remote host, and the default turns that value into the local path. The remote filename is just a string returned by the server, either in ls -l style for the LIST command or as plain filenames for NLIST, so nothing guarantees that it is a valid filename at all.
Exploitation
Step 1: serve a crafted filename
The sample server supplies a filename that the client passes to gettextfile without a localfile argument. The client, following its normal listing-and-downloading behaviour, passes that name to gettextfile without a localfile argument.
Step 2: the default argument becomes the payload
The default argument derives the local name from the server-supplied remote name, and that string is handed to open(localfile, "w"). Because it starts with |, Ruby runs the remainder as a command and gives the caller an IO object connected to it.
Step 3: observe the result
After running the client, a new file appears in the client's current working directory. It is called pang and contains the output of the id command. The screenshot shows this file and its contents.

Root cause
The mistake is the choice of open rather than File.open for writing the downloaded content. open has the pipe behaviour described above, so any string that reaches it and starts with | is interpreted as a command. The default argument File.basename(remotefile) makes it easy for a remote party to supply that string, because the remote filename is attacker-controlled data that the library treats as a local path. The remote filename is just a string returned by the server, so nothing guarantees it is a valid filename.
The researcher notes that the documentation gives no clear indication, and users have no expectation, that filenames could lead to remote code execution, so callers are unlikely to be defending against it.
Impact
Remote code execution through command injection. The attack requires a client that accepts the file names provided by the server rather than supplying its own, for example when listing and downloading all files in a share. The researcher describes the example as a little contrived for that reason. The impact is on users of NET::Ftp who expect normal file creation behaviour and may therefore not be sanitising file paths.
Fix
The recommendation is not to use open in NET::Ftp but something like File.open instead, which keeps the expected behaviour while removing the command execution path.
Variants to test
- Any Ruby code that passes a server-controlled or user-controlled string to
openfor writing. A positive result is a file whose name begins with|causing a command to run, visible as output written to a file or as a side effect of the command. - FTP clients that download a whole directory listing using the default local filename. A positive result is a crafted name in the
LISTorNLISTresponse producing command execution on the client. - Other standard-library helpers that build a local path from a remote name. A positive result is the same pipe interpretation when the derived name reaches
open.
Hunting notes
This class of bug hides in convenience wrappers that accept a path and pass it to a general-purpose function with special syntax, here Ruby's open and its leading pipe. During review, look for default arguments derived from remote data, and for any call to open where the path is not fully controlled by the local user. The signal is a filename that is treated as trusted input simply because it arrived as a string from a server. To show impact, a minimal client and server pair that demonstrates the command running and its output landing in a file is enough, as in this report.
Timeline
The report was disclosed on 19 December 2017.
What happened, in plain words
Ruby is a programming language. It comes with a large collection of ready-made code called the standard library, so that programmers do not have to write common things from scratch. One of those ready-made pieces is NET::Ftp, a client for FTP.
FTP is an old protocol for moving files between two computers over a network. One computer runs an FTP server and holds the files; another computer runs an FTP client and asks the server for a list of files or for a copy of one. The client is the program that connects out, and the server is the program that answers.
NET::Ftp has a helper method called gettextfile. A method is a named block of code that does one job; you call it and give it the information it needs. gettextfile downloads a file from the server and saves it on the local computer. If the programmer does not say what to call the saved copy, the method makes up a name from the name the server gave.
That made-up name is the problem. Ruby has a function called open that is used to open files, but it has a hidden extra behaviour: if the name you give it starts with a pipe character, |, it does not open a file at all. It runs the rest of the string as a command on your computer, the same way you would type a command into a terminal.
Because the file name comes from the FTP server, a malicious or compromised server can send a file name that starts with | and contains a command. A client that downloads files without choosing its own local names will then run that command on the user's machine. This is called command injection: data that should be treated as plain text is instead treated as an instruction.
The impact is remote code execution: an attacker gets code running on the victim's computer. The recommendation is to use File.open instead of open.
Context
Ruby's standard library ships NET::Ftp, a client for the FTP protocol. Among its convenience methods is gettextfile, which downloads a remote file and writes it to a local path. The method has a default argument for the local path: if the caller does not supply one, the local filename is derived from the remote filename with File.basename(remotefile).
A default argument is a value a method uses when the caller leaves that slot empty. Think of a form where the "country" box is already filled in with your country: you can change it, but if you do not, the pre-filled value is used. Here the pre-filled value is built from the remote file name, which the server chooses.
File.basename is a small helper that takes a full path and returns only the last part. Given /home/ana/photos/cat.jpg, it returns cat.jpg. The idea is that a file downloaded as cat.jpg should be saved locally as cat.jpg too.
The report shows that this default path is dangerous. The local filename is passed to open, and open in Ruby treats a leading pipe character as a request to run a shell command. A shell is the program that reads typed commands and runs them; a shell command is one such instruction, for example id. Because the remote filename comes from the FTP server, a malicious or compromised server can choose a filename that makes the client execute an arbitrary command.
Setup
The researcher wrote two small Ruby scripts, a sample server and a sample client, to demonstrate the issue. The only configuration needed is to set the host and port values in both scripts so that they match. The host is the address of the machine to connect to, and the port is the numbered door on that machine where the FTP service listens. Both sides must agree on them, otherwise the client knocks on the wrong door.
The server is started first, then the client is run:
ruby ftpserver.rb
This line tells Ruby to run the file ftpserver.rb, the fake FTP server. It is started first because a client needs something to connect to; if the client runs first, there is nobody listening and the connection fails.
ruby client.rb
This line tells Ruby to run client.rb, the program that uses NET::Ftp the way a normal application would. Running it is what triggers the download and, with the crafted name, the command.
The client is written to use the vulnerable default, that is, it does not pass its own localfile argument and lets gettextfile derive the local name from the remote name supplied by the server. This is the key design choice of the demo: the client behaves like software that trusts the server's file names.
Finding it
While using NET::Ftp, the researcher realised that command execution could be reached through "malicious" file names. The starting point is the signature of gettextfile:
def gettextfile(remotefile, localfile = File.basename(remotefile),
&block) # :yield: line
f = nil
result = nil
if localfile
f = open(localfile, "w") # Vulnerable code here. open("| os command","w")
elsif !block_given?
result = String.new
end
Line by line:
def gettextfile(remotefile, localfile = File.basename(remotefile),starts the definition of the method. It takesremotefile, the name of the file on the server, andlocalfile, the name to save it as locally. The= File.basename(remotefile)part is the default: if the caller does not providelocalfile, Ruby fills it in with the last part of the remote name.&block) # :yield: lineaccepts an optional block, a piece of code the caller can hand in to be run for each line. The comment is documentation for programmers reading the code.f = nilandresult = nilprepare two empty variables, one for the opened file and one for the eventual result.if localfilechecks whether a local name is present. In practice it always is, because of the default.f = open(localfile, "w") # Vulnerable code here. open("| os command","w")is the line that matters. It opens the local name for writing,"w". The comment spells out the danger: if the name looks like| os command, this call runs a command instead.elsif !block_given?andresult = String.newhandle the case where no local name was given and no block was supplied, by collecting the content in memory instead.
The comment in the snippet marks the line that matters: open(localfile, "w"). Ruby's open is not the same as File.open. When the string passed to it begins with |, open starts a subprocess and connects it to the returned IO object. A subprocess is a separate program started by your program; an IO object is Ruby's handle for reading from or writing to something, here the pipe to that program. So a localfile value of the form | os command runs that command instead of creating a file.
A useful comparison is a mailroom. File.open is a clerk who only ever files envelopes in cabinets. open is a clerk who also accepts envelopes marked "deliver this instruction to the workshop", and does so without asking. If anyone can write the label on the envelope, anyone can send instructions to the workshop.
In ordinary use, most callers would pass their own localfile and would not rely on the default of File.basename(remotefile). The interesting case is a client that lists and downloads every file in an FTP share: there the remotefile value is controlled by the remote host, and the default turns that value into the local path. The remote filename is just a string returned by the server, either in ls -l style for the LIST command or as plain filenames for NLIST, so nothing guarantees that it is a valid filename at all.
LIST is the FTP command that returns a directory listing in the style of the Unix ls -l command, with permissions, sizes and dates. NLIST returns just the names. In both cases the client receives text from the server and has to decide what to do with it; the library treats part of that text as a local file name.
Exploitation
Step 1: serve a crafted filename
The sample server supplies a filename that the client passes to gettextfile without a localfile argument. The client, following its normal listing-and-downloading behaviour, passes that name to gettextfile without a localfile argument.
This step exists to put attacker-controlled text into the client. In a real attack the server would be someone else's machine, or a legitimate server that has been compromised; in the demo it is a script the researcher controls, which makes the test repeatable.
Step 2: the default argument becomes the payload
The default argument derives the local name from the server-supplied remote name, and that string is handed to open(localfile, "w"). Because it starts with |, Ruby runs the remainder as a command and gives the caller an IO object connected to it.
A payload is simply the part of an attack that does the damage, here the command hidden in the file name. The client never checks the name; it just passes it along, which is why the trick works.
Step 3: observe the result
After running the client, a new file appears in the client's current working directory. It is called pang and contains the output of the id command. The screenshot shows this file and its contents.
The current working directory is the folder the program was started from. id is a standard Unix command that prints which user the command is running as. Writing its output to a file proves the command really ran on the client machine, not just that a strange file name was accepted.

Root cause
The mistake is the choice of open rather than File.open for writing the downloaded content. open has the pipe behaviour described above, so any string that reaches it and starts with | is interpreted as a command. The default argument File.basename(remotefile) makes it easy for a remote party to supply that string, because the remote filename is attacker-controlled data that the library treats as a local path. The remote filename is just a string returned by the server, so nothing guarantees it is a valid filename.
Validation here would mean checking that the name is a plain file name and refusing anything with special meaning. The library does not do that.
The researcher notes that the documentation gives no clear indication, and users have no expectation, that filenames could lead to remote code execution, so callers are unlikely to be defending against it. This is a common theme in security: a feature that behaves surprisingly is dangerous even when the surprise is small, because nobody guards against a risk they do not know exists.
Impact
Remote code execution through command injection. The attack requires a client that accepts the file names provided by the server rather than supplying its own, for example when listing and downloading all files in a share. The researcher describes the example as a little contrived for that reason. The impact is on users of NET::Ftp who expect normal file creation behaviour and may therefore not be sanitising file paths.
Sanitising means cleaning input before using it, for example stripping characters that have special meaning. A user who believes a file name is just a file name has no reason to do that.
Fix
The recommendation is not to use open in NET::Ftp but something like File.open instead, which keeps the expected behaviour while removing the command execution path. The recommendation is to use File.open instead of open, which keeps the expected behaviour while removing the command execution path.
Variants to test
- Any Ruby code that passes a server-controlled or user-controlled string to
openfor writing. A positive result is a file whose name begins with|causing a command to run, visible as output written to a file or as a side effect of the command. - FTP clients that download a whole directory listing using the default local filename. A positive result is a crafted name in the
LISTorNLISTresponse producing command execution on the client. - Other standard-library helpers that build a local path from a remote name. A positive result is the same pipe interpretation when the derived name reaches
open.
These variants matter because the bug is not really about FTP. It is about any place where text from elsewhere is passed to a function that gives some characters special meaning.
Hunting notes
This class of bug hides in convenience wrappers that accept a path and pass it to a general-purpose function with special syntax, here Ruby's open and its leading pipe. A convenience wrapper is a short, friendly function built on top of a more powerful one; the friendliness hides the power. During review, look for default arguments derived from remote data, and for any call to open where the path is not fully controlled by the local user. The signal is a filename that is treated as trusted input simply because it arrived as a string from a server. To show impact, a minimal client and server pair that demonstrates the command running and its output landing in a file is enough, as in this report.
Timeline
The report was disclosed on 19 December 2017.
Glossary
- Ruby: a programming language with a large standard library.
- Standard library: code that ships with a language so programmers do not rewrite common tasks.
- FTP: an old protocol for listing and transferring files between computers.
- FTP server: the program that holds files and answers FTP requests.
- FTP client: the program that connects to a server and asks for files.
NET::Ftp: Ruby's built-in FTP client.- Method: a named block of code that performs one job when called.
gettextfile: theNET::Ftpmethod that downloads a file and saves it locally.- Default argument: a value a method uses when the caller does not supply one.
File.basename: a helper that returns only the last part of a path.open: Ruby's general opening function, which runs a command when the string starts with|.File.open: Ruby's function that only opens files, with no command behaviour.- Pipe character (
|): a symbol that, at the start of a string given toopen, means "run a command". - Shell: the program that reads and runs typed commands.
- Shell command: one instruction for the shell, such as
id. - Command injection: a bug where data meant as plain text is treated as a command.
- Remote code execution (RCE): an attacker getting code to run on someone else's computer.
- Payload: the part of an attack that does the damage.
- Host: the address of the machine to connect to.
- Port: the numbered door on a machine where a service listens.
- Subprocess: a separate program started by your program.
- IO object: Ruby's handle for reading from or writing to something.
LIST: the FTP command that returns a detailed directory listing.NLIST: the FTP command that returns just file names.- Current working directory: the folder a program was started from.
id: a Unix command that prints which user is running it.- Validation: checking input and refusing values that are not allowed.
- Sanitising: cleaning input before use, for example removing special characters.
- Severity: a rating of how serious a vulnerability is.
- Disclosed: made public, here on 19 December 2017.