Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
你 PWN 不動我@HackerSir 10th
Search
YJK
September 01, 2024
60
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
你 PWN 不動我@HackerSir 10th
YJK
September 01, 2024
More Decks by YJK
See All by YJK
CVE-2024-2961@HackerSir StudyGroup
yjk0805
0
40
From 0 to Shell PWN Basic
yjk0805
0
43
Master of ROP:PWN Advanced
yjk0805
0
71
Basic Reverse-逆要做什麼@THUHC
yjk0805
0
77
Reproducing Vulnerability in IoT@HackerSir StudyGroup
yjk0805
0
110
Reverse 0x1@HackerSir 10th
yjk0805
0
43
Reverse 0x2@HackerSir 10th
yjk0805
0
47
不要亂 PWN 我@HackerSir 10th
yjk0805
0
44
Assembly@HackerSir 10th
yjk0805
0
67
Featured
See All Featured
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
240
Embracing the Ebb and Flow
colly
88
5.2k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Designing for Performance
lara
611
70k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
280
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Transcript
你 PWN 不動我 YJK @ HackerSir
01 03 02 Shellcode ROP Format String Outline
Shellcode 01 What is Shellcode、Linux syscall、ret2sc
C compiled into machine code • 程式編譯過程 C Code Assembly
Machine code Compiler Assembler
Shellcode • 程式編譯過程 C Code Assembly Machine code Compiler Assembler
Shellcode • 撰寫 Machine code,利用漏洞執行 code • 自己寫參數後 call syscall
Syscall • System call • 跟 kernel 作溝通 • Linux
System Call Table
Syscall • System call • 跟 kernel 作溝通 • Linux
System Call Table • 常見 ◦ execve(“/bin/sh”,NULL,NULL) ◦ open、read、write
How to write Shellcode? • 自己寫 asm 並透過 pwntools 轉換
• Shellcode database • pwntools 的 shellcraft ◦ 要記得 context.arch
ret2sc • 沒有 backdoor 可以使用 ◦ 自己寫出 backdoor • 將
shellcode 寫到全域變數 • 透過 Overflow 跳過去
ORW • 只能使用 open、read、write • 可以透過 seccomp-tools 確認
Practice • ret2sc • orw
ret2sc
orw
ROP 02 Static、Dynamic Linking、Basic ROP
Demo • 編譯選項 ◦ gcc test.c -o dynamic ◦ gcc
test.c -o static -static
Demo • 編譯選項 ◦ gcc test.c -o dynamic ◦ gcc
test.c -o static -static • 觀察一下 ◦ file ◦ ls ◦ objdump ◦ gdb
file
ls
ASM
差異 • static 很大,dynamic 很小 • static linking 會將所有程式碼包進去 ◦
call 到任何外部 function 都會包進去 ▪ scanf、printf… • 浪費空間
ASM
Dynamic Linking • 一個程式會呼叫許多 library function • libc.so • .so、.dll
• 上週提到的 plt、got 就是如此 • 需要使用時再呼叫 libc • 可以重複利用,不浪費空間
ROP • ROP (Return Oriented Programming) • 重複利用編譯的程式碼繞過 NX •
透過多段可以執行的 gadget 串出 rop chain • 控制執行流程
ROP Gadgets • 片段可執行的程式 • 通常結尾為 ret 或是 jump <addr>
• 較常利用 ◦ 可以控制 register ◦ 可以寫入資料 ◦ syscall • 如何找到 gadget ◦ ROPgadget Tool
Control Register • pop rax; ret pop rax 0x3b ret
Control Register • rax = 0x3b pop rax 0x3b ret
Control Register • 繼續串 gadget pop rax 0x3b ret
ROP 原理 • Overflow 前 old rbp Return address
ROP 原理 • Overflow 並串成右方後 • 0x419afc:pop rax ; ret
• 0x40a48d:pop rsi ; ret • 0x402020:pop rdi ; ret AAAAAAAA 0x419afc 0x3b 0x40a48d 0 0x402020 0 ……
ROP 原理 • 0x419afc:pop rax ; ret AAAAAAAA 0x419afc 0x3b
0x40a48d 0 0x402020 0 ……
ROP 原理 • 0x419afc:pop rax ; ret • rax =
0x3b AAAAAAAA 0x419afc 0x3b 0x40a48d 0 0x402020 0 ……
ROP 原理 • 0x40a48d:pop rsi ; ret AAAAAAAA 0x419afc 0x3b
0x40a48d 0 0x402020 0 ……
ROP 原理 • 0x40a48d:pop rsi ; ret • rsi =
0 AAAAAAAA 0x419afc 0x3b 0x40a48d 0 0x402020 0 ……
ROP 原理 • 0x402020:pop rdi ; ret AAAAAAAA 0x419afc 0x3b
0x40a48d 0 0x402020 0 ……
ROP 原理 • 0x402020:pop rdi ; ret • rdi =
0 AAAAAAAA 0x419afc 0x3b 0x40a48d 0 0x402020 0 ……
ROP 原理 • 執行到最後 Register 會變成 • rax:0x3b • rsi:0
• rdi:0 AAAAAAAA 0x419afc 0x3b 0x40a48d 0 0x402020 0 ……
ROP 原理 • 執行到最後 Register 會變成 • rax:0x3b • rsi:0
• rdi:0 • 串成可以成功 ROP 的樣子 AAAAAAAA 0x419afc 0x3b 0x40a48d 0 0x402020 0 ……
Practice • Static ROP
Static ROP • static linking binary • 先將 ROP Gadget
全部找出來存到檔案 ◦ ROPgadget --binary <binary> > gadget • 利用 grep 找到想要的 gadget ◦ cat gadget | grep "pop rdi"
Static ROP • 常用 ◦ pop <reg>;ret 控制 register ◦
mov qword ptr [reg], reg ; ret; 寫入 memory ◦ syscall
Static ROP • 目標:execve(“/bin/sh”, argv, envp) • Linux System Call
Table NR Syscall references %rax arg0 (%rdi) arg1 (%rsi) arg2 (%rdx) 59 execve man/cs/ 0x3b const char *filename const char *const *argv const char *const *envp
整理 • execve 的 argv、envp 可以放 NULL • 用 mov
qword ptr [reg], reg ; ret; 寫 “/bin/sh” ◦ 用 gdb 開 vmmap 找可寫區域
整理 • execve 的 argv、envp 可以放 NULL • 用 mov
qword ptr [reg], reg ; ret; 寫 “/bin/sh” ◦ 用 gdb 開 vmmap 找可寫區域 • 用 pop <reg> ; ret ; 調整 register • 最後 syscall
整理 • syscall number = rax = 0x3b • rdi
= &"/bin/sh" (pointer to string "/bin/sh") • rsi = 0 • rdx = 0
整理 • rdi = &"/bin/sh" (pointer to string "/bin/sh")
整理 • syscall number = rax = 0x3b
整理 • rsi = 0 • rdx = 0
整理 • syscall
偷吃步 • 利用 ROPgadget 生成 ROP chain • ROPgadget --binary
<file> --ropchain • 有機會長出很白癡的 ROP chain
Static ROP
Format String 03
What is format string • printf、scanf 透過格式化傳遞參數 • scanf("%s", s);、printf("Hello,
%s\n", s); • 分別讀取格式化字串,讀取到 %s 將參數傳入解析
Format String Vulnerability • 控制格式化字串參數可以 leak 變數、stack… • 可以修改變數值 •
可以修改任意記憶體值 ◦ got hijacking
格式 • %[parameter][flags][field width][.precision][length]type
Parameter • %[parameter][flags][field width][.precision][length]type • 可以忽略 • n$ • 顯示第
n 個參數 • printf("%3$d %2$d %1$d\n", 1, 2, 3); • 3 2 1
Type • %[parameter][flags][field width][.precision][length]type • d/I、u:整數 • x/X:16 進位 •
o:10 進位 • c、s:字元、字串 • p:指標 • n:可以寫入變數
寫入資料 • %[parameter][flags][field width][.precision][length]type • printf("Hello%n\n", &a); ◦ a =
5
Vulnerability Sample
Practice • FormatString1 • FormatString2
FormatString1
FormatString2
題目自架 • HackerSir PWN Class
Reference • NTU Computer Security Fall 2019 • NCKUCTF (成大資安社)
• pwn.college-Format String Exploits
None