PHP fopen() CRLF Injection

From: Ulf Harnhammar (
Date: 09/09/02

Date: Mon, 9 Sep 2002 23:23:01 +0200 (CEST)
From: Ulf Harnhammar <>

PHP fopen() CRLF Injection

VENDOR: The PHP Group <>
VULNERABLE VERSIONS: 4.1.2, 4.2.2, 4.2.3, latest CVS, possibly others
IMMUNE VERSIONS: none, but workarounds exist
SEVERITY: medium


"PHP is a widely-used Open Source general-purpose scripting language
that is especially suited for Web development and can be embedded
into HTML. Its syntax draws upon C, Java, and Perl, and is easy
to learn. PHP runs on many different platforms and can be used
as a standalone executable or as a module under a variety of Web
servers. It has excellent support for databases, XML, LDAP, IMAP,
Java, various Internet protocols, and general data manipulation,
and is extensible via its powerful API."

(direct quote from the program's project page at Freshmeat)

PHP is published under the terms of The PHP License. It is installed
on millions of web servers.


fopen(), file() and other functions in PHP have a vulnerability
that makes it possible to add extra HTTP headers to HTTP
queries. Attackers may use it to escape certain restrictions,
like what host to access on a web server. In some cases, this
vulnerability even opens up for arbitrary net connections, turning
some PHP scripts into proxies and open mail relays.


PHP has several functions that take filenames as one of their
arguments: fopen(), file() and some others. If allow_url_fopen is
set to On in php.ini, those functions also accept URLs instead of
regular files, and they connect to the server in question with the
correct protocol. This functionality is vulnerable to some CRLF
Injection attacks.

1) We start with the simple attacks. Let's say that this PHP snippet
is saved as snippet.php:


echo '<pre>';


echo '</pre>';


If an attacker surfs to:

(should be on one line)

this HTTP query will be sent to

GET /api?sunnan=visby&vind=gotland HTTP/1.0
User-Agent: Ulf/0.0
Cookie: user=ulf

User-Agent: PHP/4.1.2

As you can see, the real headers from PHP are sent as well, but
the web server ignores them, as we send two CRLFs before them to
indicate that the headers are over.

Using this technique, we can add arbitrary user agents, referers and
cookies. We can also break out of restrictions and access
instead of the site that snippet.php tries to restrict us
to, if and are virtual hosts on the same machine.

2) If the PHP script is even worse, like this one called dotcom.php:


$fp = fopen($url, 'r');


we can connect to arbitrary ports and send (almost) arbitrary
commands, thus turning the dotcom.php script into a proxy and an
open mail relay.

If we surf to:

(should be on one line)

the PHP interpreter will connect to on port 25,
and send the following commands:

GET / HTTP/1.0
HELO my.own.machine
MAIL FROM:<me@my.own.machine>
i will never say the word PROCRASTINATE again

User-Agent: PHP/4.1.2

Both PHP and the MTA will complain, but the mail is still sent.


For more information about this group of problems, read my "CRLF
Injection" paper, which is available at


All contact methods I could find were very public, like mailing
lists and bug tracking systems. I ended up entering this security
hole into their bug tracking system (as number 19160) on the 28th
of August. The PHP developers are working on fixing this bug, but
nothing have been committed to their CVS yet. I am releasing this
anyway, as it is already public in their bug tracking system and
as Matthew Murphy has published a related hole in PHP recently,
thus making it more likely that some blackhat will find this too.


One solution is to make sure that all variables that are used in this
type of URL are clean, by including this command in your PHP scripts:

$var = preg_replace('/\\s+/', '', $var);

Another solution: if your scripts don't need to access URLs
like files, you can switch off that functionality by setting
allow_url_fopen to Off in php.ini.

// Ulf Harnhammar