Slide 1

Slide 1 text

Basic Reverse-逆要做什麼 YJK@THUHC

Slide 2

Slide 2 text

Slido

Slide 3

Slide 3 text

Whoami ● YJK ● 逢甲大學黑客社學術部長 ● AIS3 Junior 2024 助教 ● MyFirstCTF 2024 銅質獎 ● 2022 & 2023 T 貓盃資安基礎實務能力競賽 佳作 ● ICPC 2024 Taoyuan Regional High Honors ● 演算法 & Reverse & Pwn & IoT Security

Slide 4

Slide 4 text

Table of contents 01 03 02 04 Introduction How to Reverse IDA Dynamic Analysis & GDB 05 Other Language Reverse

Slide 5

Slide 5 text

Introduction 01

Slide 6

Slide 6 text

Before Reverse ● 基礎 C/C++ 語法 (迴圈、陣列、指標…) ● 一點點演算法知識 ● 基礎組合語言

Slide 7

Slide 7 text

Before Reverse ● 基礎 C/C++ 語法 (迴圈、陣列、指標…) ● 一點點演算法知識 ● 基礎組合語言

Slide 8

Slide 8 text

What is Reverse Engineering ● 逆向工程 ● 在沒有原始碼的時候分析程式行為 ● 盡可能推導出程式或產品的設計方式 ● PWN (Binary Exploitation) 的基礎

Slide 9

Slide 9 text

Why Reverse Engineering ● 惡意程式分析 (ex: WannaCry) ● 軟體分析 (ex: TikTok) ● 開發插件 ● 挖掘漏洞 (ex: IoT、browser) ● 遊戲外掛 ● 盜版軟體 ● Keygen

Slide 10

Slide 10 text

How to Reverse Engineering ● 假設現在有一個由 C/C++ 撰寫而成的程式 Source code Assembly code Preprocessor 前處理器 & Compiler 編譯器 Library/ Object Executable Machine code Assembler 組譯器 Linker 連結器

Slide 11

Slide 11 text

Reverse Engineering? ● Reverse 的過程 Source code Assembly code Preprocessor 前處理器 & Compiler 編譯器 Library/ Object Executable Machine code Assembler 組譯器 Linker 連結器 disassemble

Slide 12

Slide 12 text

Reverse Engineering? ● Reverse 的過程 Source code Assembly code Preprocessor 前處理器 & Compiler 編譯器 Library/ Object Executable Machine code Assembler 組譯器 Linker 連結器 disassemble decompile

Slide 13

Slide 13 text

Compiler ● 程式碼->執行檔的重要過程 ● 將高階語言轉換為較低階語言 ■ ex:一行的 scanf 會被編譯成多行的 ASM ● 未必直接生成 machine code ■ 可以先轉為 IL(中間語言),後續再做優化 ● C 語言所常用的 gcc 或是 C++ 的 g++ 就是 compiler

Slide 14

Slide 14 text

Interpreter ● 直譯器 ● 分析中間語言並做出相對應行為 ● 通常會將中間語言轉為當下架構的 instruction ● Python 即為常見的 interpreter 語言 ● Java 也為 interpreter ● 會先解析成 Bytecode 並執行

Slide 15

Slide 15 text

Interpreter ● 如果沒有經過其他加料的動作 (ex:加殼、混淆) ● 通常直譯語言逆向相對容易 ● 只要用對工具就可以看到跟 source code 相似度超高的程式 ● 中間語言跟一整個 CPU 架構相較起來很簡單 ■ 工具開發成本低

Slide 16

Slide 16 text

How to Reverse 02

Slide 17

Slide 17 text

How to Reverse Static Analysis Dynamic Analysis • 不執行程式 • Decompile 執行檔分析資訊 • 分析各函式及程式碼 • 執行程式並觀察程式行為 • 觀察執行程式可能改變的值 • 記憶體、變數、底層資訊

Slide 18

Slide 18 text

Useful Tool & Command objdump • disassemble 執行檔 • 顯示 object file 資訊 readelf • ELF 檔的資訊 • headers、segments… ltrace • 追蹤使用函式庫 strace • 追蹤 system call 和 signal strings • 列出檔案中所有可視字元 file • 查看檔案類型

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Static Analysis ● 靜態分析 ● 注重 code review ● 瞭解程式執行過程 ● 看懂自己不一定熟悉的語言執行過程

Slide 21

Slide 21 text

Disassemble vs Decompile ● Disassemble ■ 反組譯 ■ 執行檔 -> assembly ● Decompile ■ 反編譯 ■ 執行檔 -> source code

Slide 22

Slide 22 text

file ● 查看檔案類型 ● file [OPTION...] [FILE...]

Slide 23

Slide 23 text

IDA 03

Slide 24

Slide 24 text

IDA ● 曼特農夫人 ● 今天用的是 free 版 ● Pro 版是很強大的逆向工具 ● 反組譯成 asm ● 反編譯成 C 語言

Slide 25

Slide 25 text

How to use IDA ● 選取要 decompile 的檔案 ● 一路按下 OK 接下來按下鍵盤上的 F5 ● 神奇的事情發生了

Slide 26

Slide 26 text

IDA - Strings ● 功能跟 strings command 相同 ● 按下 shift + F12

Slide 27

Slide 27 text

IDA - XREF ● Cross Reference ● 查看 function 被呼叫的地方 ● 在追蹤流程的時候很方便

Slide 28

Slide 28 text

IDA - XREF ● Cross Reference ● 查看 function 被呼叫的地方 ● 在追蹤流程的時候很方便 ● 選取欲查看之 function ● 按下 x 或按右鍵選取 Jump to xref

Slide 29

Slide 29 text

IDA - 修改變數名稱 ● 選許要修改的變數 ● 按下 n 或是按下滑鼠右鍵選取 rename lvar

Slide 30

Slide 30 text

IDA - 型態轉換 ● 鼠標停在數字或變數上會發現它的型態 ● 數字也可能是字元

Slide 31

Slide 31 text

IDA - 型態轉換 ● 鼠標停在數字或變數上會發現它的型態 ● 數字也可能是字元 ● 對要轉換的目標值按右鍵 ● 選取想要顯示的型態

Slide 32

Slide 32 text

IDA - 誤判解決 ● IDA 可能會誤判 function 回傳型態、參數,可以自行修改 ● 選取要修改的 function ● 按下 y 或是滑鼠右鍵選取 set item type

Slide 33

Slide 33 text

How to solve a problem ● 先用 file command 確認檔案類型 ● 選定 decompile 軟體 ● Code review ● 將變數更改成可讀性高的名稱

Slide 34

Slide 34 text

Get Flag ● 執行程式依照逆向結果熟知流程拿到 Flag ● 直接發現有後門拿到 Flag ● 直接發現靜態分析可以拿到產出 Flag 的規則 ● ...

Slide 35

Slide 35 text

LAB ● flag-checker

Slide 36

Slide 36 text

Dynamic Analysis & GDB 04

Slide 37

Slide 37 text

Dynamic Analysis ● 將程式執行觀察程式行為並分析 ● 建議在虛擬環境 ■ 無法保證程式有無惡意行為

Slide 38

Slide 38 text

Why Dynamic Analysis ● 如果執行完某個函式發現有些數值變化,代表有可能是關鍵 ● 可以透過 debugger 跳過或繞過某些部分,甚至跳到重點 ● 設定中斷點,觀察暫存器、記憶體 ● 動態修改數值,使數值成立 ■ 可以執行到原本不該執行的程式

Slide 39

Slide 39 text

Dynamic Analysis Tool GDB • GNU Debugger • 互動式的 shell • CLI x64dbg • For Windows • GUI • x32dbg WinDbg • 微軟開發 • 對 Windows 相容性較高 • GUI

Slide 40

Slide 40 text

GDB ● GNU Debugger ● GNU 系統中的標準除錯器 ● 許多類 UNIX 系統都可以使用 ● 支援許多語言,包括 C、C++ 等

Slide 41

Slide 41 text

GDB Plugin • peda • pwndbg • gdbgui • gef • 可以直接觀察較多資訊

Slide 42

Slide 42 text

How to use GDB ● gdb ./[執行檔] ● 會發現 gdb 開始執行你的程式

Slide 43

Slide 43 text

GDB - run ● 執行程式 ● run、r

Slide 44

Slide 44 text

GDB - breakpoints ● 設定中斷點,可以是函式、記憶體位置、行數 ● break [中斷點]、b [中斷點] ● ex: b main

Slide 45

Slide 45 text

GDB - continue ● 程式中斷後再執行 ● continue、c

Slide 46

Slide 46 text

GDB - backtrace ● 追蹤 stack 的狀態 ● backtrace、bt

Slide 47

Slide 47 text

GDB - disassemble ● 反組譯執行的程式 ● disassemble

Slide 48

Slide 48 text

GDB - next instruction ● 下一步指令 ● ni

Slide 49

Slide 49 text

GDB - step instruction ● 下一步指令 ● si

Slide 50

Slide 50 text

GDB - finish ● 結束函式 ● finish、fini

Slide 51

Slide 51 text

GDB - information ● 顯示暫存器、函式、中斷點...資訊 ● info functions、breakpoints

Slide 52

Slide 52 text

GDB - Examine ● 列出記憶體資訊 ● x $rax、x 0x7fffffffd9b8

Slide 53

Slide 53 text

GDB - Examine ● 特定格式將記憶體印出 ● x/
● 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)…

Slide 54

Slide 54 text

GDB - Delete ● 刪除中斷點 ● delete、del、d 刪除全部 ● delete、del、d 刪除特定中斷點

Slide 55

Slide 55 text

GDB - jump ● 直接讓指令跳轉到指定位址 ● jump LOCATION

Slide 56

Slide 56 text

GDB - help ● 查看有什麼參數 ● help、help <指令>

Slide 57

Slide 57 text

初步使用 GDB ● 先將程式執行 ● b main ● r ● 後面再看該怎麼做 ■ ni、si...

Slide 58

Slide 58 text

LAB ● Guess

Slide 59

Slide 59 text

Other Language Reverse 05

Slide 60

Slide 60 text

Python reverse ● 實際上 python 可以編譯成 .pyc ■ 有需求時也會產生出 __pycache__ ● 內部為 Python Bytecode ● 可以透過 python -m compileall 編譯

Slide 61

Slide 61 text

Disassembler ● 在 disassemble/decompile 前要知道版本 (magic number、file) ● 可以使用 marshal 讀 code object ● 用對應版本的 dis module dissemble ■ dis.dis(data) ● 此時會獲得 bytecode

Slide 62

Slide 62 text

Disassembler ● 在 disassemble/decompile 前要知道版本 (magic number、file) ● 可以使用 marshal 讀 code object ● 用對應版本的 dis module dissemble ■ dis.dis(data) ● 此時會獲得 bytecode ● Demo:picoCTF-weirdSnake

Slide 63

Slide 63 text

Bytecode -> Python ● 可以間接當成 decompile ● pylingual (machine learning) ● uncompyle ● pycdc ● pylingual 會將程式繼續上傳幫助開發

Slide 64

Slide 64 text

LAB ● DDDDecrypted It

Slide 65

Slide 65 text

Pyinstaller ● 可以將 python 變成執行檔 ● 會將 cpython 和 script 一起包起來 ● 一樣可以整包解開 ■ 解開為 .pyc ● https://github.com/pyinstxtractor/pyinstxtractor-ng ● https://pyinstxtractor-web.netlify.app/

Slide 66

Slide 66 text

Pyinstaller ● 可以將 python 變成執行檔 ● 會將 cpython 和 script 一起包起來 ● 一樣可以整包解開 ■ 解開為 .pyc ● https://github.com/pyinstxtractor/pyinstxtractor-ng ● https://pyinstxtractor-web.netlify.app/ ● 從 entry point 分析

Slide 67

Slide 67 text

LAB ● Casino

Slide 68

Slide 68 text

Bonus:自動分析 00

Slide 69

Slide 69 text

符號執行 (symbolic execution)

Slide 70

Slide 70 text

符號執行 (symbolic execution)

Slide 71

Slide 71 text

壞處 ● 環境問題 ● Ex: 進入 libc 可能會掛在裡面 ● 路徑爆炸 ● 每個判斷式都要走兩條路 (2^n) ● 遞迴處理困難

Slide 72

Slide 72 text

angr ● 開源 binary 分析平台 ● 適用於多個架構 ● 有方法處理部分路徑爆炸問題

Slide 73

Slide 73 text

實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解

Slide 74

Slide 74 text

實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解

Slide 75

Slide 75 text

實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解 machine code angr Intermediate Language C pseudocode

Slide 76

Slide 76 text

實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解

Slide 77

Slide 77 text

實作 分析 binary 資訊 Address、Arch… instruction->IR 模擬 設定符號變數、solver (Claripy、z3)、限制 透過符號執行尋找解

Slide 78

Slide 78 text

問題 ● 跑著跑著就壞了 ● 跑很久都沒有解 ● 進入 libc function 分析可能就掛在裡面 ● 可能需要做剪枝

Slide 79

Slide 79 text

問題 ● 跑著跑著就壞了 ● 跑很久都沒有解 ● 進入 libc function 分析可能就掛在裡面 ● 可能需要做剪枝

Slide 80

Slide 80 text

Demo ● 2024 AIS3 pre-exam rage

Slide 81

Slide 81 text

No content