首页 > 解决方案 > PHP 断言:在错误消息中显示文件内容

问题描述

我正在研究一些 web 服务器的漏洞利用,我发现了一个断言,我可以在其中注入一些 php 代码。我发现下面的代码

assert(file_get_contents('file.txt') !== null)

像我预期的那样工作:file_get_contents()执行,其结果在断言中传递。但是,如果我让它像这样失败

assert(file_get_contents('file.txt') === null)

结果file_get_contents()不会被解释,因此不会显示在错误消息中。

Warning: assert(): assert(file_get_contents('file.txt') === null) failed in /Applications/MAMP/htdocs/test/assert.php on line 3 

有没有人知道我如何获取文件内容,假设我只能在 assert 中做到这一点?

标签: phpfileassert

解决方案


对于想知道如何做到这一点的人,我找到了一个解决方案。file_get_contents()不直接显示文件的内容,您必须将echo其显示出来。

但是,该功能readfile()做得很好,它只是直接显示文件的内容。

所以只需像这样调用断言:

assert(readfile('file.txt') === null)

推荐阅读