Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Basic Reverse-逆要做什麼@THUHC
Search
YJK
January 13, 2025
0
7
Basic Reverse-逆要做什麼@THUHC
YJK
January 13, 2025
Tweet
Share
More Decks by YJK
See All by YJK
Reproducing Vulnerability in IoT@HackerSir StudyGroup
yjk0805
0
5
你 PWN 不動我@HackerSir 10th
yjk0805
0
4
Reverse 0x1@HackerSir 10th
yjk0805
0
7
Reverse 0x2@HackerSir 10th
yjk0805
0
4
不要亂 PWN 我@HackerSir 10th
yjk0805
0
6
Assembly@HackerSir 10th
yjk0805
0
5
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Practical Orchestrator
shlominoach
186
10k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Language of Interfaces
destraynor
156
24k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.2k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
How to train your dragon (web standard)
notwaldorf
91
5.8k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.2k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Transcript
Basic Reverse-逆要做什麼 YJK@THUHC
Slido
Whoami • YJK • 逢甲大學黑客社學術部長 • AIS3 Junior 2024 助教
• MyFirstCTF 2024 銅質獎 • 2022 & 2023 T 貓盃資安基礎實務能力競賽 佳作 • ICPC 2024 Taoyuan Regional High Honors • 演算法 & Reverse & Pwn & IoT Security
Table of contents 01 03 02 04 Introduction How to
Reverse IDA Dynamic Analysis & GDB 05 Other Language Reverse
Introduction 01
Before Reverse • 基礎 C/C++ 語法 (迴圈、陣列、指標…) • 一點點演算法知識 •
基礎組合語言
Before Reverse • 基礎 C/C++ 語法 (迴圈、陣列、指標…) • 一點點演算法知識 •
基礎組合語言
What is Reverse Engineering • 逆向工程 • 在沒有原始碼的時候分析程式行為 • 盡可能推導出程式或產品的設計方式
• PWN (Binary Exploitation) 的基礎
Why Reverse Engineering • 惡意程式分析 (ex: WannaCry) • 軟體分析 (ex:
TikTok) • 開發插件 • 挖掘漏洞 (ex: IoT、browser) • 遊戲外掛 • 盜版軟體 • Keygen
How to Reverse Engineering • 假設現在有一個由 C/C++ 撰寫而成的程式 Source code
Assembly code Preprocessor 前處理器 & Compiler 編譯器 Library/ Object Executable Machine code Assembler 組譯器 Linker 連結器
Reverse Engineering? • Reverse 的過程 Source code Assembly code Preprocessor
前處理器 & Compiler 編譯器 Library/ Object Executable Machine code Assembler 組譯器 Linker 連結器 disassemble
Reverse Engineering? • Reverse 的過程 Source code Assembly code Preprocessor
前處理器 & Compiler 編譯器 Library/ Object Executable Machine code Assembler 組譯器 Linker 連結器 disassemble decompile
Compiler • 程式碼->執行檔的重要過程 • 將高階語言轉換為較低階語言 ▪ ex:一行的 scanf 會被編譯成多行的 ASM
• 未必直接生成 machine code ▪ 可以先轉為 IL(中間語言),後續再做優化 • C 語言所常用的 gcc 或是 C++ 的 g++ 就是 compiler
Interpreter • 直譯器 • 分析中間語言並做出相對應行為 • 通常會將中間語言轉為當下架構的 instruction • Python
即為常見的 interpreter 語言 • Java 也為 interpreter • 會先解析成 Bytecode 並執行
Interpreter • 如果沒有經過其他加料的動作 (ex:加殼、混淆) • 通常直譯語言逆向相對容易 • 只要用對工具就可以看到跟 source code
相似度超高的程式 • 中間語言跟一整個 CPU 架構相較起來很簡單 ▪ 工具開發成本低
How to Reverse 02
How to Reverse Static Analysis Dynamic Analysis • 不執行程式 •
Decompile 執行檔分析資訊 • 分析各函式及程式碼 • 執行程式並觀察程式行為 • 觀察執行程式可能改變的值 • 記憶體、變數、底層資訊
Useful Tool & Command objdump • disassemble 執行檔 • 顯示
object file 資訊 readelf • ELF 檔的資訊 • headers、segments… ltrace • 追蹤使用函式庫 strace • 追蹤 system call 和 signal strings • 列出檔案中所有可視字元 file • 查看檔案類型
Static Analysis Tool IDA Pro • 非常貴、很強大 • IDA Free
Ghidra • Java • 開源 • NSA 開發 Radare2 • 開源 • CLI binary ninja • 比 IDA Free 可以逆更多架構 • cloud version dnSpy • .NET 架構 Apktool • .apk Bytecode-Viewer • .class
Static Analysis • 靜態分析 • 注重 code review • 瞭解程式執行過程
• 看懂自己不一定熟悉的語言執行過程
Disassemble vs Decompile • Disassemble ▪ 反組譯 ▪ 執行檔 ->
assembly • Decompile ▪ 反編譯 ▪ 執行檔 -> source code
file • 查看檔案類型 • file [OPTION...] [FILE...]
IDA 03
IDA • 曼特農夫人 • 今天用的是 free 版 • Pro 版是很強大的逆向工具
• 反組譯成 asm • 反編譯成 C 語言
How to use IDA • 選取要 decompile 的檔案 • 一路按下
OK 接下來按下鍵盤上的 F5 • 神奇的事情發生了
IDA - Strings • 功能跟 strings command 相同 • 按下
shift + F12
IDA - XREF • Cross Reference • 查看 function 被呼叫的地方
• 在追蹤流程的時候很方便
IDA - XREF • Cross Reference • 查看 function 被呼叫的地方
• 在追蹤流程的時候很方便 • 選取欲查看之 function • 按下 x 或按右鍵選取 Jump to xref
IDA - 修改變數名稱 • 選許要修改的變數 • 按下 n 或是按下滑鼠右鍵選取 rename
lvar
IDA - 型態轉換 • 鼠標停在數字或變數上會發現它的型態 • 數字也可能是字元
IDA - 型態轉換 • 鼠標停在數字或變數上會發現它的型態 • 數字也可能是字元 • 對要轉換的目標值按右鍵 •
選取想要顯示的型態
IDA - 誤判解決 • IDA 可能會誤判 function 回傳型態、參數,可以自行修改 • 選取要修改的
function • 按下 y 或是滑鼠右鍵選取 set item type
How to solve a problem • 先用 file command 確認檔案類型
• 選定 decompile 軟體 • Code review • 將變數更改成可讀性高的名稱
Get Flag • 執行程式依照逆向結果熟知流程拿到 Flag • 直接發現有後門拿到 Flag • 直接發現靜態分析可以拿到產出
Flag 的規則 • ...
LAB • flag-checker
Dynamic Analysis & GDB 04
Dynamic Analysis • 將程式執行觀察程式行為並分析 • 建議在虛擬環境 ▪ 無法保證程式有無惡意行為
Why Dynamic Analysis • 如果執行完某個函式發現有些數值變化,代表有可能是關鍵 • 可以透過 debugger 跳過或繞過某些部分,甚至跳到重點 •
設定中斷點,觀察暫存器、記憶體 • 動態修改數值,使數值成立 ▪ 可以執行到原本不該執行的程式
Dynamic Analysis Tool GDB • GNU Debugger • 互動式的 shell
• CLI x64dbg • For Windows • GUI • x32dbg WinDbg • 微軟開發 • 對 Windows 相容性較高 • GUI
GDB • GNU Debugger • GNU 系統中的標準除錯器 • 許多類 UNIX
系統都可以使用 • 支援許多語言,包括 C、C++ 等
GDB Plugin • peda • pwndbg • gdbgui • gef
• 可以直接觀察較多資訊
How to use GDB • gdb ./[執行檔] • 會發現 gdb
開始執行你的程式
GDB - run • 執行程式 • run、r
GDB - breakpoints • 設定中斷點,可以是函式、記憶體位置、行數 • break [中斷點]、b [中斷點] •
ex: b main
GDB - continue • 程式中斷後再執行 • continue、c
GDB - backtrace • 追蹤 stack 的狀態 • backtrace、bt
GDB - disassemble • 反組譯執行的程式 • disassemble
GDB - next instruction • 下一步指令 • ni
GDB - step instruction • 下一步指令 • si
GDB - finish • 結束函式 • finish、fini
GDB - information • 顯示暫存器、函式、中斷點...資訊 • info functions、breakpoints
GDB - Examine • 列出記憶體資訊 • x $rax、x 0x7fffffffd9b8
GDB - Examine • 特定格式將記憶體印出 • x/<fmt> <address> • fmt
= count + size + format • ex: x/2gx $rax • size: b (byte)、h (halfword)、w (word)、g (giant, 8 bytes) • format: x (hex)、d (decimal)、c (char)、s (string)…
GDB - Delete • 刪除中斷點 • delete、del、d 刪除全部 • delete、del、d
<id> 刪除特定中斷點
GDB - jump • 直接讓指令跳轉到指定位址 • jump LOCATION
GDB - help • 查看有什麼參數 • help、help <指令>
初步使用 GDB • 先將程式執行 • b main • r •
後面再看該怎麼做 ▪ ni、si...
LAB • Guess
Other Language Reverse 05
Python reverse • 實際上 python 可以編譯成 .pyc ▪ 有需求時也會產生出 __pycache__
• 內部為 Python Bytecode • 可以透過 python -m compileall <file> 編譯
Disassembler • 在 disassemble/decompile 前要知道版本 (magic number、file) • 可以使用 marshal
讀 code object • 用對應版本的 dis module dissemble ▪ dis.dis(data) • 此時會獲得 bytecode
Disassembler • 在 disassemble/decompile 前要知道版本 (magic number、file) • 可以使用 marshal
讀 code object • 用對應版本的 dis module dissemble ▪ dis.dis(data) • 此時會獲得 bytecode • Demo:picoCTF-weirdSnake
Bytecode -> Python • 可以間接當成 decompile • pylingual (machine learning)
• uncompyle • pycdc • pylingual 會將程式繼續上傳幫助開發
LAB • DDDDecrypted It
Pyinstaller • 可以將 python 變成執行檔 • 會將 cpython 和 script
一起包起來 • 一樣可以整包解開 ▪ 解開為 .pyc • https://github.com/pyinstxtractor/pyinstxtractor-ng • https://pyinstxtractor-web.netlify.app/
Pyinstaller • 可以將 python 變成執行檔 • 會將 cpython 和 script
一起包起來 • 一樣可以整包解開 ▪ 解開為 .pyc • https://github.com/pyinstxtractor/pyinstxtractor-ng • https://pyinstxtractor-web.netlify.app/ • 從 entry point 分析
LAB • Casino
Bonus:自動分析 00
符號執行 (symbolic execution)
符號執行 (symbolic execution)
壞處 • 環境問題 • Ex: 進入 libc 可能會掛在裡面 • 路徑爆炸
• 每個判斷式都要走兩條路 (2^n) • 遞迴處理困難
angr • 開源 binary 分析平台 • 適用於多個架構 • 有方法處理部分路徑爆炸問題
實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解
實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解
實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解
machine code angr Intermediate Language C pseudocode
實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解
實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解
問題 • 跑著跑著就壞了 • 跑很久都沒有解 • 進入 libc function 分析可能就掛在裡面
• 可能需要做剪枝
問題 • 跑著跑著就壞了 • 跑很久都沒有解 • 進入 libc function 分析可能就掛在裡面
• 可能需要做剪枝
Demo • 2024 AIS3 pre-exam rage
None