# PWN [TOC] ## X86 Assembly ### Register ![](https://i.imgur.com/J166vrl.png) - [Ref](https://ithelp.ithome.com.tw/articles/10227112) - [J.J.'s Blogs](https://morosedog.gitlab.io/x86-20190702-x86-2/) ### Big endian and Little endian 通常來說, X86 是採取 Little endian 的讀取方法, PowerPC 則是 `Big endian` 。 ![](https://i.imgur.com/vvlGdmu.png) > 圖片取自 [Link](https://www.iar.com/kr/knowledge/learn/programming/migration-techniques-for-different-endianness/) 。 - Big endian: LSB -> MSB - Little endian: MSB -> LSB ## Buffer overflow ## Lazy-binding 當程式是利用 Dynamic linking 的方式做連結時,其函式位址會在執行週期才確定。這樣做的好處顯而易見: 程式引入的 library 的函式有千百個,但在執行周其中並不會都使用到,當函式被呼叫時再去載入它,就可以大幅提升執行效率。 > 判別是否為 Lazy-binding 的方法: > 當我們利用逆向工具查看組合語言時,如果有發現 call function 的形式如 `call puts@plt` ,就代表該函式會在執行期間才做載入。 - GOT: Global Offset Table > GOT 其實就是一個存放函式指標的陣列。 用來記錄在 ELF file 中用到的 Shared library 中符號的絕對地址, GOT 主要涵蓋以下內容: - .dynamic - .got 儲存全域變數的位址。 - .got.plt | Name | Description | | -------- | -------- | | address of .dynamic | 指向 GOT 的 .dynamic | | link_map | 一個鍊結串列,用來紀錄用到的 Library | | dl_runtime_resolve | 找出函式的位址 | - .data - PLT: Procedure Linkage Table ### 流程 ![](https://i.imgur.com/2heBuH3.png) - 程式碼中呼叫了 `func` - 跑去 PLT 執行 `func@plt` - PLT 中的 `func@plt` 會跳到 GOT 的 `.got.plt` 尋找 `func` 的位置 - 將 `func` 的 id 推入 Stack 中。 - 由於 `func` 是第一次呼叫,所以沒辦法順利在 `.got.plt` 找到函式位址,這時系統就會把 `func` 的位址寫進 `.got.plt` 當中。 - 如此一來,等到 `func` 第二次被呼叫時,就可以直接找到其位址。 ### Lazy-loading 造成的安全性問題 Lazy-binding 雖能夠大幅度的提升程式的執行效率,但也因為該機制需要 GOT 能夠被寫入,所以如果有有心人士將 PLT 的對應位置改成 system call 的位置,那呼叫 `plt function` 時便會變呼叫 `system call` ,這點需要特別注意。 ### Protection - CANARY - FORTIFY - NX - PIE - RELRO - disabled: 歡迎光臨 Myfone :3 - partial: 僅 `.got.plt` 可寫 - full: [READ-ONLY] ## 輔助工具 - pestudio - IDA - [gdb](https://github.com/ianchen0119/AwesomeCS/wiki/3-2-GNU-Debugger) - gdb-peda - [pwntools](https://github.com/Gallopsled/pwntools) ![](https://i.imgur.com/hjkb944.png) ## 平台 - [pwnable.tw](https://pwnable.tw/) ## References - [Lazy binding](https://rafaelchen.wordpress.com/2017/09/25/pwn%E7%9A%84%E4%BF%AE%E7%85%89%E4%B9%8B%E8%B7%AF-lazy-binding/) - [GOT和PLT原理簡析](https://www.cntofu.com/book/46/linked_and_loader/gothe_plt_yuan_li_jian_xi.md) - [Execution](https://www.slideshare.net/AngelBoy1/execution-50215114) > 很棒的投影片,看完會對 PWN 有初步的認知。