Home php教程 php手册 浅析PHP程序中的目录遍历漏洞

浅析PHP程序中的目录遍历漏洞

Jun 06, 2016 pm 07:36 PM
php loopholes Table of contents program Traverse

目录 遍历 漏洞 在国内外有许多不同的叫法,比如也可以叫做信息泄露 漏洞 ,非授权文件包含 漏洞 .名称虽然多,可他们却有一个共同的成因,就是在 程序 中没有过滤用户输入的../和./之类的 目录 跳转符,导致恶意用户可以通过提交 目录 跳转来遍历 服务器 上的任

  目录遍历漏洞在国内外有许多不同的叫法,比如也可以叫做信息泄露漏洞,非授权文件包含漏洞.名称虽然多,可他们却有一个共同的成因,就是在程序中没有过滤用户输入的../和./之类的目录跳转符,导致恶意用户可以通过提交目录跳转来遍历服务器上的任意文件,其危害可想而知.这类漏洞大家比较熟悉的可能就是在一些邮件列表程序以及网络硬盘程序中,其实这类漏洞还广泛存在与一些国外的BLOG程序中,这类漏洞大概分两种下面就来通过实例来说明这类漏洞是如何产生以及该如何防范.

  首先,我们来看一个国外的BLOG,前几天从网上下了一个名为LoudBlog的BLOG程序,

  在它的index.php页面中看到如下代码:

<p><span>//</span><span>build an include-path from the url-request</span><span><br></span><span>  </span><span>else</span><span> {<br>  </span><span>$loadme</span><span>=</span><span>"</span><span>inc/backend_</span><span>"</span><span>.</span><span>$_GET</span><span>[</span><span>'</span><span>page</span><span>'</span><span>] </span><span>.</span><span>"</span><span>.php</span><span>"</span><span>;<br>  }<br>  </span><span>//</span><span>yee-hah! finally we do show real content on our page!</span><span><br></span><span>  </span><span>include</span><span> (</span><span>$loadme</span><span>);<br>  </span><span>?></span></p>
Copy after login

  这段程序很简单却包含了一个可怕的漏洞,变量$page是我们GET上去的,如果没有设置page参数,程序就自动包含inc/backend_postings.php这个文件,如果有page参数就把$page的值放到inc目录下以backend_前缀开头的文件形成一个新的文件.这里并没有对$page的值做任何的过滤,导致我们可以遍历所有文件了.

  这里要注意的是,我们提交的$page的值会自动的加上php后缀,所以我们阅读php文件是不会有效果的.当然我们可以读一些配置文件也是很有用的.下面就来测试一下,我们在inc目录外建立一个 TXT文件,内容为Wh0 !s H4K_BaN?我们提交如下URL看看结果:

  http://localhost/loudblog/loudblog/loudblog/index.php?page=/../../hello.txt%00这里要说的是由于变量会加上php后缀,所以我们要用%00来截断后缀这样才能正常显示文件内容。

  测试成功说明漏洞存在了,那我们接着读一些敏感文件吧,提交如下URL:

  http://localhost/loudblog/loudblog/loudblog/index.php?page=/../../../../../../conf/httpd.conf%00

  APACHE的配置文件也顺利读出来了,接下来就来看另外一种情况.

  这类漏洞主要是存在与基于PHP+TXT结构的程序中,漏洞代码也是来自于一个国外的BLOG,代码如下:

<p><span>$act</span><span>=</span><span>$_GET</span><span>[</span><span>'</span><span>act</span><span>'</span><span>];<br>  </span><span>if</span><span> (</span><span>$act</span><span>==</span><span>''</span><span>)<br>  {<br>  </span><span>include</span><span>(</span><span>"</span><span>blog.txt</span><span>"</span><span>);<br>  }<br>  </span><span>else</span><span><br>  {<br>  </span><span>include</span><span>(</span><span>"</span><span>act/</span><span>$act</span><span>.txt</span><span>"</span><span>);<br>  }<br>  </span><span>?></span><span><br>  </span><span>$blog_id</span><span>=</span><span>$_GET</span><span>[</span><span>'</span><span>blogid</span><span>'</span><span>];<br>  </span><span>if</span><span> (</span><span>$blog_id</span><span>==</span><span>''</span><span>)<br>  {<br>  </span><span>include</span><span>(</span><span>"</span><span>blog.txt</span><span>"</span><span>);<br>  }<br>  </span><span>else</span><span><br>  {<br>  </span><span>include</span><span>(</span><span>"</span><span>./blog_entries/</span><span>$blog_id</span><span>.txt</span><span>"</span><span>);<br>  }<br>  </span><span>?></span></p>
Copy after login

  从上面的代码可以清晰的看出问题所在,第一段程序获得$_GET[]提交的数据并赋值给$act,这里没有对act做任何的过滤,而在后面判断如果变量为空就把blog.txt包含进来,如果不为空就包含act目录下的$act.txt文件,不过只能读以.txt结尾的文件,读别的文件加上 txt后缀后会提示找不到文件,可以配合某些上传漏洞把文件包含进去,比如提交如下URL:

  index.php?act=blog&blogid=../../filename这样带到程序里就成了include("./filename.txt");包含近来的文件只要里面含有PHP代码就算后缀是TXT文件也会被执行,原理给上面的一样,我就不截图了.

  上面分别介绍了现在最主要的两种目录遍历漏洞,从表面上看基于TXT的PHP程序如果有这类漏洞似乎利用更方便一些,其实两者的危害性都是等价的.其实避免这类漏洞也是很简单的事情,象$blog-id这类数字形的参数只需用intval()函数强制整形化就可以了,对于字符形的参数我们可以自己写一个过滤函数把危险字符过滤掉,类似代码如下:

<p><span>function</span><span> fuckchar(</span><span>$var</span><span>){<br>  </span><span>$var</span><span>=</span><span>str_replace</span><span>(</span><span>"</span><span>..</span><span>"</span><span>,</span><span>""</span><span>,</span><span>$var</span><span>);<br>  </span><span>$var</span><span>=</span><span>str_replace</span><span>(</span><span>"</span><span>.</span><span>"</span><span>,</span><span>""</span><span>,</span><span>$var</span><span>);<br>  </span><span>$var</span><span>=</span><span>str_replace</span><span>(</span><span>"</span><span>/</span><span>"</span><span>,</span><span>""</span><span>,</span><span>$var</span><span>);<br>  </span><span>$var</span><span>=</span><span>str_replace</span><span>(</span><span>"</span><span>\</span><span>"</span><span>,</span><span>""</span><span>,</span><span>$var</span><span>);<br>  </span><span>$var</span><span>=</span><span>str_replace</span><span>(</span><span>"</span><span>"</span><span>,</span><span>""</span><span>,</span><span>$var</span><span>);<br>  }</span></p>
Copy after login

  大家可以自己测试一下这类漏洞,不管什么语言过滤的思路都是一样的,用GOOGLE搜索: powered by Loudblog可以找到一些这类程序,不过官方现在已经推出新版本了,更多的漏洞等待大家自己去发掘吧.

  当PHP配置文件中的allow_url_open打开的话,我们可以在自己的WEB服务器上建立一个同名文件里面包含shell命令,然后提交我们自己建立的shell文件让被攻击的服务器远程包含,可以以WEB权限执行命令,这样就是所谓的远程执行命令漏洞了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles