# 資安系列讀書會part 4筆記 時間:2023/11/24 講者:suijyou [簡報連結](https://discord.com/channels/869131755335057499/1154469880758804510/1177563632993841213) # Why do reverse? **逆向工程**(reverse engineering) 而順向工程就是我們編寫程式(c->assembly->machine code->binary) 當我們遇到沒有source code或是當我們想修改遊戲、找漏洞時就需要它 # tool for reverse - [PE Bear傳送門](https://hshrzd.wordpress.com/pe-bear/) - [JAVA傳送門](https://www.oracle.com/java/technologies/downloads/#jdk17-windows) (在安裝Ghidra前記得裝java喔!!) - [Ghidra傳送門](https://github.com/NationalSecurityAgency/ghidra/releases) - Windbg:動態分析工具 常用指令 - g (go to) - bp (break point) - p (單步步過) - t (單步步入) - dd (display dword) - db (display bytes + ascii) - du (display unicode) - ed (edit dword) - lm (查看記憶體映射) - k (查看 stack 堆疊) # x86_64 Assembly - 通用暫存器 (general register) - RAX、RCX、RDX、R8 ~ R15 - 特殊用途暫存器 - RSP 指向 Stack 的頂端 - RBP 指向 Stack 的底端 - RIP 指向下一行要執行指令的位址 如果有遇到不會的組合語言,可以上c9x.me查詢 ## calling convention - 約定了呼叫函數時如何傳遞參數 - 函數執行前會先設定參數 - x64 Windows Function(rcx, rdx, r8, r9) - x64 Linux Function(rdi, rsi, rdx, rcx, r8, r9) ## Stack - stack的最終樣貌 ![image](https://hackmd.io/_uploads/B1JCaZA4T.png) ## endian - 分為 little endian 及 big endian - Big endian比較方便人類閱讀 ![image](https://hackmd.io/_uploads/H1kRAZ0NT.png) - little endian比較方便 CPU 拿取資料 ![image](https://hackmd.io/_uploads/H1dh0ZRNa.png) [參考連結](https://blog.gtwang.org/programming/difference-between-big-endian-and-little-endian-implementation-in-c/) ## PE format - Image Base 程式記憶體的基值,EXE、ELF 檔皆有 - VA (Virtual Address) 其實你在記憶體看到的都是歸屬於 VA - PA (Physical Address) OS 會將 process 的 PA 給區分開來,已解決 VA 記憶體位置重疊的問題 - RVA (Relative Virtual Address) 方便 PE 結構算 offset 的 address **image base + RVA_offset = VA** - entry point (程式進入點) main() 其實不是真正的 entry point,而真正的entry point 是進到系統預設給的 win_API **entry point (VA) = image base + entry point RVA** ## protector - ASLR - **全名: Address Space Layout Randomization** - 特徵: - base 開頭的 1 個 bytes 恆為 0x7f - base 最後的 1.5 個 bytes 不會被 random - 長度為 6 bytes - 功用: 使 library 的 base address 隨機化 # 我們該如何開始逆向工程 - 第零步: 可以先執行程式,看你要不要順便關 ASLR - 第一步: 先找 entry point - 第二步: 依照 calling convention 找 main() - 第三步: 找相關重點程式,開逆 - 第四步: 可以試試動態分析 - 第五步: 遇到不知道的函式,可以上網找找 document 關鍵字: msdn <你要的 function> 關鍵字: ma7.org <你要的 function> - 第六步: 逆到完...