文件包含漏洞及简单绕过

文件包含漏洞

DVWA靶场对文件包含漏洞的解释

File Inclusion靶场对文件包含漏洞的解释:

About

Some web applications allow the user to specify input that is used directly into file streams or allows the user to upload files to the server. At a later time the web application accesses the user supplied input in the web applications context. By doing this, the web application is allowing the potential for malicious file execution.

一些web应用允许用户指定输入然后将该输入直接用于文件流中或者允许用户上传文件到服务器

然后web应用访问了位于服务端目录下的用户提供的输入.

诚如是,则web应用就有执行恶意文件的风险

If the file chosen to be included is local on the target machine, it is called "Local File Inclusion (LFI). But files may also be included on other machines, which then the attack is a "Remote File Inclusion (RFI).

如果选择服务端的文件进行包含,则称为"本地文件包含"(LFI)(这里本地是相对于服务端而言的)

但是文件也可以被包含自其他计算机,此时称为"远程文件包含"(RFI)

When RFI is not an option. using another vulnerability with LFI (such as file upload and directory traversal) can often achieve the same effect.

Note, the term “file inclusion” is not the same as “arbitrary file access” or “file disclosure”.

当RFI不可选的时候,使用LFI也十有八九可以达到相同的攻击目的

注意,"文件包含"与"任意文件访问"和"文件曝光???"不是一个事情

Objective

Read all five famous quotes from ../hackable/flags/fi.php using only the file inclusion.

目标:仅使用文件包含方法读取…/hackable/flags/fi.php`目录下的五个文件

include.php
file1.php
file2.php
file3.php
file4.php

其中远程包含漏洞需要php.ini的allow_url_fopen和allow_url_include都打开才可以发生

image-20220318110220724

漏洞发生原因

后端代码

index.php

$vulnerabilityFile = '';
switch( $_COOKIE[ 'security' ] ) {
	case 'low'://从low等级入手
		$vulnerabilityFile = 'low.php';
		break;
	case 'medium':
		$vulnerabilityFile = 'medium.php';
		break;
	case 'high':
		$vulnerabilityFile = 'high.php';
		break;
	default:
		$vulnerabilityFile = 'impossible.php';
		break;
}//该switch规定了当前难度级别应该包含哪一个php文件

require_once DVWA_WEB_PAGE_TO_ROOT . "vulnerabilities/fi/source/{$vulnerabilityFile}";

// if( count( $_GET ) )
if( isset( $file ) )//如果$file非空即已经赋值过了,那么就包含$file值中的文件
	include( $file );
else {
	header( 'Location:?page=include.php' );//否则还是显示include.phpp页面
	exit;
}

dvwaHtmlEcho( $page );

关键在于$file是从哪里来的?

在index.php中我们并没有发现$file的身影.那么可能的来源只有low.php从这个文件引进来

low.php

<?php

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

?>

发现$file确实是从这里包含进来的,其值是通过get请求获取到的

回到前端

image-20220318094740728

刚进入File inclusion靶场时的url是http://192.168.171.1/dvwa/vulnerabilities/fi/?page=include.php

这是由于我们还没有指定$file的值,刚才分析过的后端代码中没有该值会执行else中的header( 'Location:?page=include.php' );

这里?page=include.php显然就是get请求了

然后在前端网页上随便点击一个File 1

image-20220318095232962

get请求相应变成了?page=file1.php

前端上给出的只有三个文件,但是目标是访问到五个文件,这两个文件就是开发者不愿意让我们看到的

image-20220318095404836

但是我们可以通过修改get请求比如这样:

image-20220318095529006

就访问到了这个前端没有告诉我们但是后端切实存在的file4

继续推广,能不能访问网站的上级目录,甚至根目录,甚至网站主机的目录呢?

将page的值改为一个桌面目录下的文件发现是可以执行的

image-20220318100103829

将page的值改成http网页也是可以执行的

image-20220318100249306

防御与绕过

DVWA File inclusion medium

相对于low等级,medium的后端对于page的值做出了一些简单的过滤

<?php

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

// Input validation
$file = str_replace( array( "http://", "https://" ), "", $file );//将http://和https://都用空字符串替换
$file = str_replace( array( "../", "..\\" ), "", $file );//将上级目录跳转符号用空字符串替换

?> 

两个替换的绕过对策

绝对目录绕过
page=C:\Users\86135\Desktop\testShell\test.php
双写绕过

由于str_replace只会执行一次,也就是说hhttp://ttp://经过str_replace函数作用之后会变成http://

image-20220318100955422

DVWA File inclusion high

<?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;
}

?> 
fnmatch防御函数
fnmatch(模式,字符串)

如果字符串匹配给定的模式则返回true否则false

fnmatch( "file*", $file )

这里模式使用了通配符*,意思是$file的值必须以"file"开头,后面是啥我不管

if( !fnmatch( "file*", $file ) && $file != "include.php" )

如果$file不匹配file*模式并且$file不是include.php则满足if条件,报告文件不存在

明显网站开发者的的意图是限定我们只能包含当前文件夹下的file1.php,file2.php,filea.php这样的文件,然后没有指定包含什么文件时默认包含include.php

但是开发者忽略了一个同名的==file伪协议==

file://伪协议绕过

image-20220318103047852

为啥叫"伪"协议

真的协议长这样:

https://
ftp://

file://不是一个正儿八经的应用层协议却也有一个冒号两个斜杠这种形式,所以叫做伪协议

image-20220318103327845

在page的值中首先加上file://然后后面随便写本地文件就可以绕过对file*的模式匹配

pikachu 本地包含

if(isset($_GET['submit']) && $_GET['filename']!=null){
    $filename=$_GET['filename'];
    include "include/$filename";//变量传进来直接包含,没做任何的安全限制
//     //安全的写法,使用白名单,严格指定包含的文件名
//     if($filename=='file1.php' || $filename=='file2.php' || $filename=='file3.php' || $filename=='file4.php' || $filename=='file5.php'){
//         include "include/$filename";

//     }
}
相对路径绕过

这里include "include/$filename";限制的include必须是include/文件夹下的文件,但是果真如此吗?

如果一个目录这样写include/../不就相当于跳转到include 的平级目录了吗

如果一个目录这样写include/../../就跳转到了include 的上级目录里

filename=../../../index.php  跳转到include文件夹的上级的上级文件夹下的index.php文件

这样写就跳转到了pikachu平台的主页image-20220318105757363

绝对防御

DVWA File inclusion impossible

<?php

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

// Only allow include.php or file{1..3}.php
if( $file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php" ) {
	// This isn't the page we want!
	echo "ERROR: File not found!";
	exit;
}

?>

这里直接采取了白名单过滤,只允许包含指定的这四个文件.

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰球球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值