Slide 1

Slide 1 text

From 0 to Shell:PWN Basic YJK @ HackerSir

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Environment ● ubuntu:22.04 ● 可以自己架環境 ● 內含 pwndbg、pwngdb、pwntools、常用工具... ● environment

Slide 4

Slide 4 text

01 03 02 04 Introduction ELF Buffer Overflow GOT Hijacking Outline 05 Shellcode

Slide 5

Slide 5 text

Introduction 01 What is Binary Exploitation (PWN)

Slide 6

Slide 6 text

Binary Exploitation (PWN) ● 透過程式漏洞控制執行流程觸發攻擊 ● PWN ○ 碰 or 胖 Start Input Output End

Slide 7

Slide 7 text

How to PWN ? ● 尋找漏洞 ○ 模糊測試 ○ 原始碼 ○ 組合語言 Start Input Output aborted segmentation fault

Slide 8

Slide 8 text

How to PWN ? ● 尋找漏洞 ○ 模糊測試 ○ 原始碼 ○ 組合語言 ● 利用漏洞 ○ 控制執行流程 ○ 覆蓋 return address ○ 修改變數

Slide 9

Slide 9 text

How to PWN ? ● 尋找漏洞 ○ 模糊測試 ○ 原始碼 ○ 組合語言 ● 利用漏洞 ○ 控制執行流程 ○ 覆蓋 return address ○ 修改變數 ○ Get Shell

Slide 10

Slide 10 text

ELF 02 Basic Concept、 Protections

Slide 11

Slide 11 text

ELF ● Executable and Linkable Format ● 可執行檔 ○ Windows:exe ○ Linux:ELF ● Section ○ 執行時會映射到記憶體上 ○ .text、.bss、.data、.rodata、.got、.plt…

Slide 12

Slide 12 text

Session ● .bss ○ 存放未初始化的全域變數 ● .data ○ 存放初始化的全域變數 ● .rodata ○ 存放可讀不可寫資料 ● .text ○ 存放編譯後的程式碼

Slide 13

Slide 13 text

Session

Slide 14

Slide 14 text

Protections ● PIE ○ Position-Independent Executable ● NX ○ No-Execute ● Stack Canary ○ stack protector ● RELRO ○ Relocation Read-Only

Slide 15

Slide 15 text

PIE ● Position-Independent Executable ● code & data section map 到位址時隨機 ● 開啟時,每次執行都不同,否則固定 address ● 需搭配 ASLR 使用

Slide 16

Slide 16 text

PIE ● Position-Independent Executable ● code & data section map 到位址時隨機 ● 開啟時,每次執行都不同,否則固定 address ● 需搭配 ASLR 使用 ● 關閉方式

Slide 17

Slide 17 text

ASLR ● Address Space Layout Randomization ● 針對 Process 的防護機制 ● 動態載入位址隨機 ○ library ○ stack ○ heap

Slide 18

Slide 18 text

ASLR ● 關閉方式 ○ 要在 root 權限下執行

Slide 19

Slide 19 text

NX ● Data Execution Prevention、No-execute ● 可寫的不可執行、可執行的不可寫 ● 防範 Shell Code 問題

Slide 20

Slide 20 text

NX ● Data Execution Prevention、No-execute ● 可寫的不可執行、可執行的不可寫 ● 防範 Shell Code 問題 ● 關閉方式

Slide 21

Slide 21 text

Stack Canary ● Stack Protector ● rbp 前塞 8 byte random 值,ret 驗證是否相同 ● 1st byte 為 null byte ● 每次執行的值不同 ● 防範 buffer overflow

Slide 22

Slide 22 text

Stack Canary ● Stack Protector ● rbp 前塞 8 byte random 值,ret 驗證是否相同 ● 1st byte 為 null byte ● 每次執行的值不同 ● 防範 buffer overflow ● 關閉方式

Slide 23

Slide 23 text

RELRO ● Relocation Read-Only ● No / Partial / Full ● 防範 Lazy Binding、GOT Hijacking 問題

Slide 24

Slide 24 text

RELRO ● 關閉方式

Slide 25

Slide 25 text

Tool-checksec ● 確認檔案保護 ● pwntools 裡面也有

Slide 26

Slide 26 text

Tool-checksec

Slide 27

Slide 27 text

Buffer Overflow 03 Overwrite Variable、Control Flow

Slide 28

Slide 28 text

Buffer Overflow ● 記憶體狀況 low address int num1 int num2 char buf[0x10] int num3 int num4 save rbp return address high address

Slide 29

Slide 29 text

Buffer Overflow ● 記憶體狀況 ● gets(buf) ● gets 不會檢查輸入長度 low address int num1 int num2 char buf[0x10] int num3 int num4 save rbp return address high address

Slide 30

Slide 30 text

Buffer Overflow ● 記憶體狀況 ● gets(buf) ● gets 不會檢查輸入長度 ● 正常輸入範圍 low address int num1 int num2 ‘A’ * 0x10 int num3 int num4 save rbp return address high address

Slide 31

Slide 31 text

Buffer Overflow ● 記憶體狀況 ● gets(buf) ● gets 不會檢查輸入長度 ● Overflow!!!! low address int num1 int num2 AAAAAAAAAAAAAAAA AAAA AAAA save rbp return address high address

Slide 32

Slide 32 text

Overwrite Variable ● 透過 buffer Overflow 控制變數 ● int num3 = 0x12345678 ● int num4 = 0x90abcdef low address int num1 int num2 AAAAAAAAAAAA 0x12345678 0x90abcdef save rbp return address high address

Slide 33

Slide 33 text

other vulnerable function ● gets ● scanf ● strcpy ● sprintf ● memcpy ● strcat ● 沒控制好 read 參數 ● 涉及 memory/IO 的 function 皆有可能有狀況

Slide 34

Slide 34 text

Offset ● 透過 gdb 執行程式查看記憶體狀況 ● 隨意輸入或是透過 cyclic、cyclic_find 確定位置 ● 計算 buffer 與目標位置距離

Slide 35

Slide 35 text

Offset ● 可以發現輸入覆蓋到比較的數值 ● 覆蓋的數值為 HHHHIIII ● offset 為 28

Slide 36

Slide 36 text

Overwrite Return Address ● 透過 Buffer Overflow 改變 return address ● 可以改寫到任意 address ● 必須關閉 PIE 或事先 Leak PIE

Slide 37

Slide 37 text

Overwrite Return Address …… AAAAAAAAAAAA AAAAAAAAAAAA AAAAAAAAAAAA AAAAAAAAAAAA shell()、win() …… Offset buffer Return Address

Slide 38

Slide 38 text

Get Address

Slide 39

Slide 39 text

Tool-pwntools

Slide 40

Slide 40 text

Tool-pwntools

Slide 41

Slide 41 text

Tool-pwntools

Slide 42

Slide 42 text

How to debug ?

Slide 43

Slide 43 text

How to debug ? ● 透過 gdb.attach 到執行的 process ● 開啟一個視窗,可以執行 gdb 的各種操作 ● 也可以透過 tmux ● context.terminal = ["tmux", "splitw", "-h"]

Slide 44

Slide 44 text

Practice ● bof ● ret2code

Slide 45

Slide 45 text

GOT Hijacking 04 Lazy Binding、GOT Hijacking

Slide 46

Slide 46 text

Lazy Binding ● 動態連結的程式,library function 有些不會被執行 ● 程式首次呼叫 function 尋找位置填入 GOT ● 後續呼叫直接從 GOT 獲取位置

Slide 47

Slide 47 text

GOT ● library 位址載入時才決定 ● 編譯時無法得知 library function 位址 ● GOT 儲存 library function 的指標陣列 ● 一開始不會得之真實位置,而是填入位於 plt 的 code

Slide 48

Slide 48 text

Lazy Binding

Slide 49

Slide 49 text

Lazy Binding

Slide 50

Slide 50 text

Lazy Binding

Slide 51

Slide 51 text

GOT Hijacking ● 由於 Lazy Binding,GOT 可寫 ● 如果可以覆蓋到 GOT 的值,下次呼叫到 function 時即 可控制將要執行的 function pointer ● Ex:陣列沒驗證輸入區域、記憶體

Slide 52

Slide 52 text

Practice ● GOT

Slide 53

Slide 53 text

Shellcode 05 What is Shellcode、Linux syscall、ret2sc

Slide 54

Slide 54 text

C compiled into machine code ● 程式編譯過程 C Code Assembly Machine code Compiler Assembler

Slide 55

Slide 55 text

Shellcode ● 程式編譯過程 C Code Assembly Machine code Compiler Assembler

Slide 56

Slide 56 text

Shellcode ● 撰寫 machine code,利用漏洞執行 code ● 自己寫參數後 call syscall

Slide 57

Slide 57 text

Syscall ● System call ● 跟 kernel 作溝通 ● Linux System Call Table

Slide 58

Slide 58 text

Syscall ● System call ● 跟 kernel 作溝通 ● Linux System Call Table ● 常見 ○ execve(“/bin/sh”,NULL,NULL) ○ open、read、write

Slide 59

Slide 59 text

How to write Shellcode? ● 自己寫 asm 並透過 pwntools 轉換 ● Shellcode database ● pwntools 的 shellcraft ○ 要記得 context.arch

Slide 60

Slide 60 text

Practice ● ret2sc ● orw

Slide 61

Slide 61 text

ret2sc ● 沒有 backdoor 可以使用 ○ 自己寫出 backdoor ● 將 shellcode 寫到全域變數 ● 透過 Overflow 跳過去

Slide 62

Slide 62 text

ORW ● 只能使用 open、read、write ● 可以透過 seccomp-tools 確認

Slide 63

Slide 63 text

Format String 00

Slide 64

Slide 64 text

What is format string ● printf、scanf 透過格式化傳遞參數 ● scanf("%s", s);、printf("Hello, %s\n", s); ● 分別讀取格式化字串,讀取到 %s 將參數傳入解析

Slide 65

Slide 65 text

Format String Vulnerability ● 控制格式化字串參數可以 leak 變數、stack… ● 可以修改變數值 ● 可以修改任意記憶體值 ○ got hijacking

Slide 66

Slide 66 text

格式 ● %[parameter][flags][field width][.precision][length]type

Slide 67

Slide 67 text

Parameter ● %[parameter][flags][field width][.precision][length]type ● 可以忽略 ● n$ ● 顯示第 n 個參數 ● printf("%3$d %2$d %1$d\n", 1, 2, 3); ● 3 2 1

Slide 68

Slide 68 text

Type ● %[parameter][flags][field width][.precision][length]type ● d/I、u:整數 ● x/X:16 進位 ● o:10 進位 ● c、s:字元、字串 ● p:指標 ● n:可以寫入變數

Slide 69

Slide 69 text

寫入資料 ● %[parameter][flags][field width][.precision][length]type ● printf("Hello%n\n", &a); ○ a = 5

Slide 70

Slide 70 text

Vulnerability Sample

Slide 71

Slide 71 text

Next ● ROP (Return Oriented Programming)

Slide 72

Slide 72 text

No content