The Java and Python runtimes fail to properly validate FTP URLs, which can potentially allow attackers to punch holes through firewalls to access local networks.

On Saturday, security researcher Alexander Klink disclosed an interesting attack where exploiting an XXE (XML External Entity) vulnerability in a Java application can be used to send emails.

XXE vulnerabilities can be exploited by tricking applications to parse specially crafted XML files that would force the XML parser to disclose sensitive information such as files, directory listings, or even information about processes running on the server.

Klink showed that the same type of vulnerabilities can be used to trick the Java runtime to initiate FTP connections to remote servers by feeding it FTP URLs in the form of ftp://user:password@host:port/file.ext.

However, it turns out that the built-in implementation of the FTP client in Java doesn't filter out special CR (carriage return) and LF (line feed) characters from URLs and actually interprets them.

By inserting such characters in the user or password portions of an FTP URL, the Java FTP client can be tricked to execute rogue commands and can even be tricked to speak SMTP (Simple Mail Transfer Protocol) because the syntax for SMTP and FTP is similar.

Klink showed that by exploiting an XXE vulnerability and this quirk in Java's FTP client implementation, an attacker could force a Java application to send emails to an SMTP server.

"This attack is particularly interesting in a scenario where you can reach an (unrestricted, maybe not even spam- or malware-filtering) internal mail server from the machine doing the XML parsing," Klink said in a blog post.

After seeing Klink's exploit, Timothy Morgan, a researcher with Blindspot Security, decided to disclose a similar attack that works against both Java's and Python's FTP implementations. But his attack is more serious because it can be used to punch holes through firewalls.

Morgan calls the attack "FTP protocol stream injection via malicious URLs" and it also involves injecting rogue FTP commands by exploiting the absence of CRLF filtering.

However, instead of injecting SMTP commands, Morgan abuses the FTP PORT command to trick the client into opening a data channel to a remote FTP server on a specific TCP port.

As the researcher points out, many Linux-based stateful packet inspection (SPI) firewalls, including commercial ones, support the classic mode of FTP translation and will automatically open a TCP port and forward it to an FTP client's LAN IP if they detect a PORT command in FTP traffic originating from that client.

This attack vector has been known for many years, which is why the developers of conntrack, a Linux set of tools that most firewalls use, have added an additional check. A port will only be opened if the PORT command appears at the very beginning of a TCP packet, to ensure that the client actually sent the command.

This poses a would-be attacker with two problems: first, discover the client's internal IP address in order to be able to spoof a PORT command, and then, align the TCP packets between the client and the server so that the spoofed PORT command falls at the beginning of a packet.

Morgan has found a way to do both of these things through his FTP protocol stream injection attack and claims to have developed a proof-of-concept exploit that he doesn't plan to release publicly until Oracle and Python correct their FTP client code.

"The entire attack (including the request used to determine the victim's internal IP) is typically accomplished with just three SSRF (Server Side Request Forgery) attacks to open up one TCP port," he said in a blog post Monday. "Each additional SSRF attack could open up one additional TCP port."

There are multiple ways to exploit this issue, including using it against users with Java installed on their computers. Users don't even need to execute a malicious Java applet, because the exploit can be delivered through a Java Web Start application.

"If a desktop user could be convinced to visit a malicious website while Java is installed, even if Java applets are disabled, they could still trigger Java Web Start to parse a JNLP file," Morgan said. "These files could contain malicious FTP URLs which trigger this bug."

Attackers could also target servers where Java applications run by abusing a man-in-the-middle position on the network or by exploiting SSRF or XXE vulnerabilities in those applications.

Morgan said he tested this attack against a custom Linux firewall running a recent kernel, as well as against firewalls from Palo Alto Networks and Cisco Systems that proved to be vulnerable under default settings.

"While testing of commercial firewalls has been very limited at this point, it seems likely that a significant percentage of production firewalls in the world are susceptible to attack through FTP protocol stream injections," he said.

The Java and Python developers have been notified of this problem, but until they fix their FTP client implementations, the researcher advises firewall vendors to disable classic mode FTP translation by default.

Users should uninstall Java from their systems, or at least disable the browser plug-in and disassociate the .jnlp file extension from the Java Web Start binary. Meanwhile, Java and Python applications should be audited for SSRF and XXE flaws. However, XML parsing in Java is currently vulnerable by default, making XXE vulnerabilities very common on that platform, Morgan said.