Execution

A
Execution
Angelboy @ bamboofox
Who am I
• Angelboy
• 中央⼤大學碩⼠士班 ADLab
• Bamboofox / HITCON 成員
• 擅⻑⾧長領域 : PWN
Outline
• How programs get run
• Lazy binding
• Global Offset Table
• GOT Hijacking
• We focus on ELF (Executable and Linking
Format)
• What’s happened when we execute an elf file.
• ex : ./hello
How programs get run
Overview of the workflow (static linking)
./hello
fork()
execve(“./hello”,*argv[],*envp[])
sys_execve()
do_execve() search_binary_handler()
load_elf_binary()
_start
main
kernel mode
user mode
Overview of the workflow (dynamic linking)
./hello
fork()
execve(“./hello”,*argv[],*envp[])
sys_execve()
do_execve() search_binary_handler()
load_elf_binary()
ld.so
_start
__libc_start_main _init
kernel mode
user mode
main
• sys_execve()
• 檢查參數 ex: argv , envp
• do_execve()
• 搜尋執⾏行檔位置
• 讀取執⾏行檔前 128 byte 獲取執⾏行檔的資訊
• ex: magic number
How programs get run
• search_binary_handler()
• 利⽤用前⾯面所獲取的資訊來呼叫相對應的 handler
• ex : load_script() 、 load_elf_binary()
How programs get run
• load_elf_binary()
• 檢查及獲取 program header 資訊
• 如果是 dynamic linking 則利⽤用 .interp 這個 section 來確定
loader 路徑
• 將 program header 紀錄的位置 mapping 到 memory 中,ex :
code segment 位置
• 將 sys_execve 的 return address 改為 loader (ld.so) 的entry
point
• static linking 下則會是 elf 的 entry point
How programs get run
• How program maps to virtual memory.
• 在 program header 中
• 記錄著哪些 segment 應該 mapping 到什麼位
置,以及該 segment 的讀寫執⾏行權限
• 記錄哪些 section 屬於哪些 segment
• 當 program mapping 記憶體時會根據權限的
不同來分成好幾個 segment
How programs get run
• How program maps to virtual memory.
How programs get run
權限
mapping 位置
segment 中有哪些 section
• How program maps to virtual memory.
How programs get run
other section
.data
.bss
.got.plt
.rodata
.text
.init
ELF Header
In disk In memory
kernel space
CODE VMA
DATA VMA
HEAP
STACK
R or RX
RW
0x08048000
0x0804a000
0x0804b000
0xbffff4b0
• ld.so
• 載⼊入 elf 所需的 shared library
• 這部分會記錄在 elf 中的 DT_NEED 中
• 初始化 GOT
• 其他相關初始化的動作
• ex : 將 symbol table 合併到 global symbol table
等等
How programs get run
• ld.so 其實是個很複雜的程式,要認真全部講完,
⼀一兩天的時間絕對不夠⽤用
• 有興趣可參考 elf/rtld.c
• 這邊最重要的是在執⾏行程式時,要知道不是從
main 開始的
How programs get run
How programs get run
• _start
• 將下列項⺫⽬目傳給 libc_start_main
• 環境變數起始位置
• .init
• 呼叫 main 之前的初始化⼯工作
• .fini
• 程式結束前的收尾⼯工作
How programs get run
• _libc_start_main
• 執⾏行 .init
• 執⾏行 main
• 執⾏行 .fini
• 執⾏行 exit
Lazy binding
• Dynamic linking 的程式在執⾏行過程中,有些
library 的函式可能到結束都不會執⾏行到
• 所以 ELF 採取 Lazy binding 的機制,在第⼀一次
call library 函式時,才會去尋找函式真正的位置
進⾏行 binding
Global Offset Table
• library 的位置再載⼊入後才決定,因此無法在
compile 後,就知道 library 中的 function 在哪,
該跳去哪
• GOT 為⼀一個函式指標陣列,儲存其他 library 中,
function 的位置,但因 Lazy binding 的機制,並
不會⼀一開始就把正確的位置填上,⽽而是填上⼀一段
plt 位置的 code
18
Global Offset Table
• 當執⾏行到 library 的 function 時才會真正去尋找
function ,最後再把 GOT 中的位置填上真正
function 的位置
19
Global Offset Table
• 分成兩部分
• .got
• 保存全域變數引⽤用位置
• .got.plt
• 保存函式引⽤用位置
20
Global Offset Table
• .got.plt
• 前三項有特別⽤用途
• address of .dynamic
• link_map
• ⼀一個將有引⽤用到的 library 所串成的 linked list
• dl_runtime_resolve
• ⽤用來找出函式位置的函式
• 後⾯面則是程式中 .so 函式引⽤用位置
21
Global Offset Table
• layout
.dynamic
.got
.got.plt
.data
addr of .dynamic
link_map
dl_resolve
printf
put
…
22
.
.
.
call foo@plt
…
Lazy binding
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
23
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
Lazy binding
24
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
因 foo 還沒 call 過
所以 foo 在 .got.plt 中所存的值
會是.plt中的下⼀一⾏行指令位置
所以看起來會像沒有 jmp 過
Lazy binding
25
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
Lazy binding
26
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
Lazy binding
27
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
push link_map
Lazy binding
28
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
jmp dl_runtime_resolve
dl_runtime_resolve (link_map,index)
Lazy binding
29
.
.
.
call foo@plt
…
.text
.got.plt
printf
foo@plt+6
bar
…
..
..
call _fix_up
..
..
ret 0xc
dl_resolve
Lazy binding
30
.
.
.
call foo@plt
…
.text
.got.plt
printf
foo@plt+6
bar
…
..
..
call _fix_up
..
..
ret 0xc
dl_resolve
foo 找到 foo 在 library 的位置後
會填回 .got.plt
Lazy binding
31
.
.
.
call foo@plt
…
Lazy binding
.text
.got.plt
printf
foo@plt+6
bar
…
..
..
call _fix_up
..
..
ret 0xc
dl_resolve
foo
return to foo
32
• 第⼆二次 call foo 時
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
Lazy binding
33
foo
• 第⼆二次 call foo 時
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
Lazy binding
34
foo
Jmp to foo function
How to find the GOT
• objdump -R elf or readelf -r elf
• 為了實作 Lazy binding 的機制 GOT 位置必須是
可寫⼊入的
• 但如果程式有存在任意更改位置的漏洞,便可改
寫 GOT ,造成程式流程的改變
• 也就是控制 eip
GOT Hijacking
• 第⼆二次 call foo 時
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
GOT Hijacking
37
foo
• 第⼆二次 call foo 時
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
GOT Hijacking
38
system
• 第⼆二次 call foo 時
.
.
.
call foo@plt
…
.text
jmp *(foo@GOT)
push index
jmp PLT0
foo@plt
.got.plt
printf
foo@plt+6
bar
…
push *(GOT + 4)
jmp *(GOT + 8)
PLT0
GOT Hijacking
39
system
Jmp to system function
Reference
• Glibc cross reference
• Linux Cross Reference
• 程式設計師的⾃自我修養
Q & A
1 of 41

Recommended

Heap exploitation by
Heap exploitationHeap exploitation
Heap exploitationAngel Boy
35.2K views100 slides
Linux binary Exploitation - Basic knowledge by
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
3K views105 slides
Linux Binary Exploitation - Stack buffer overflow by
Linux Binary Exploitation - Stack buffer overflowLinux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflowAngel Boy
2.6K views92 slides
Pwning in c++ (basic) by
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)Angel Boy
30.6K views83 slides
Linux Binary Exploitation - Return-oritend Programing by
Linux Binary Exploitation - Return-oritend ProgramingLinux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend ProgramingAngel Boy
2.6K views91 slides
Tcache Exploitation by
Tcache ExploitationTcache Exploitation
Tcache ExploitationAngel Boy
2.8K views63 slides

More Related Content

What's hot

Sigreturn Oriented Programming by
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented ProgrammingAngel Boy
26.4K views34 slides
Play with FILE Structure - Yet Another Binary Exploit Technique by
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniqueAngel Boy
38.4K views81 slides
Linux Binary Exploitation - Heap Exploitation by
Linux Binary Exploitation - Heap Exploitation Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation Angel Boy
1.9K views132 slides
Return to dlresolve by
Return to dlresolveReturn to dlresolve
Return to dlresolveAngel Boy
17.8K views41 slides
ROP 輕鬆談 by
ROP 輕鬆談ROP 輕鬆談
ROP 輕鬆談hackstuff
26K views82 slides
Windows 10 Nt Heap Exploitation (English version) by
Windows 10 Nt Heap Exploitation (English version)Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)Angel Boy
44.1K views189 slides

What's hot(20)

Sigreturn Oriented Programming by Angel Boy
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented Programming
Angel Boy26.4K views
Play with FILE Structure - Yet Another Binary Exploit Technique by Angel Boy
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit Technique
Angel Boy38.4K views
Linux Binary Exploitation - Heap Exploitation by Angel Boy
Linux Binary Exploitation - Heap Exploitation Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation
Angel Boy1.9K views
Return to dlresolve by Angel Boy
Return to dlresolveReturn to dlresolve
Return to dlresolve
Angel Boy17.8K views
ROP 輕鬆談 by hackstuff
ROP 輕鬆談ROP 輕鬆談
ROP 輕鬆談
hackstuff26K views
Windows 10 Nt Heap Exploitation (English version) by Angel Boy
Windows 10 Nt Heap Exploitation (English version)Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)
Angel Boy44.1K views
TDOH x 台科 pwn課程 by Weber Tsai
TDOH x 台科 pwn課程TDOH x 台科 pwn課程
TDOH x 台科 pwn課程
Weber Tsai2.6K views
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt by Anne Nicolas
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven RostedtKernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Anne Nicolas5.6K views
Linux 4.x Tracing: Performance Analysis with bcc/BPF by Brendan Gregg
Linux 4.x Tracing: Performance Analysis with bcc/BPFLinux 4.x Tracing: Performance Analysis with bcc/BPF
Linux 4.x Tracing: Performance Analysis with bcc/BPF
Brendan Gregg10.7K views
ret2dl resolve by sounakano
ret2dl resolveret2dl resolve
ret2dl resolve
sounakano585 views
Part II: LLVM Intermediate Representation by Wei-Ren Chen
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
Wei-Ren Chen868 views
Master Canary Forging: 新しいスタックカナリア回避手法の提案 by 小池 悠生 - CODE BLUE 2015 by CODE BLUE
Master Canary Forging: 新しいスタックカナリア回避手法の提案 by 小池 悠生 - CODE BLUE 2015Master Canary Forging: 新しいスタックカナリア回避手法の提案 by 小池 悠生 - CODE BLUE 2015
Master Canary Forging: 新しいスタックカナリア回避手法の提案 by 小池 悠生 - CODE BLUE 2015
CODE BLUE5.3K views
Windows 10 Nt Heap Exploitation (Chinese version) by Angel Boy
Windows 10 Nt Heap Exploitation (Chinese version)Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)
Angel Boy7.8K views
Memory Mapping Implementation (mmap) in Linux Kernel by Adrian Huang
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux Kernel
Adrian Huang2.1K views
ELFの動的リンク by 7shi
ELFの動的リンクELFの動的リンク
ELFの動的リンク
7shi 18K views
Linux 4.x Tracing Tools: Using BPF Superpowers by Brendan Gregg
Linux 4.x Tracing Tools: Using BPF SuperpowersLinux 4.x Tracing Tools: Using BPF Superpowers
Linux 4.x Tracing Tools: Using BPF Superpowers
Brendan Gregg210.2K views
re:Invent 2019 BPF Performance Analysis at Netflix by Brendan Gregg
re:Invent 2019 BPF Performance Analysis at Netflixre:Invent 2019 BPF Performance Analysis at Netflix
re:Invent 2019 BPF Performance Analysis at Netflix
Brendan Gregg5.5K views
FISL XIV - The ELF File Format and the Linux Loader by John Tortugo
FISL XIV - The ELF File Format and the Linux LoaderFISL XIV - The ELF File Format and the Linux Loader
FISL XIV - The ELF File Format and the Linux Loader
John Tortugo2.8K views

Similar to Execution

Vim hacks by
Vim hacksVim hacks
Vim hacksXuYj
1.4K views243 slides
現代 IT 人一定要知道的 Ansible 自動化組態技巧 by
現代 IT 人一定要知道的 Ansible 自動化組態技巧現代 IT 人一定要知道的 Ansible 自動化組態技巧
現代 IT 人一定要知道的 Ansible 自動化組態技巧Chu-Siang Lai
28.1K views56 slides
Yet another introduction to Git - from the bottom up by
Yet another introduction to Git - from the bottom upYet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom upWen-Tien Chang
26.2K views71 slides
Automate with Ansible basic (3/e) by
Automate with Ansible basic (3/e)Automate with Ansible basic (3/e)
Automate with Ansible basic (3/e)Chu-Siang Lai
612 views50 slides
用Raspberry PI學Linux驅動程式 by
用Raspberry PI學Linux驅動程式用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式Stanley Ho
588 views89 slides
Erlang Practice by
Erlang PracticeErlang Practice
Erlang Practicelitaocheng
2.5K views50 slides

Similar to Execution(20)

Vim hacks by XuYj
Vim hacksVim hacks
Vim hacks
XuYj1.4K views
現代 IT 人一定要知道的 Ansible 自動化組態技巧 by Chu-Siang Lai
現代 IT 人一定要知道的 Ansible 自動化組態技巧現代 IT 人一定要知道的 Ansible 自動化組態技巧
現代 IT 人一定要知道的 Ansible 自動化組態技巧
Chu-Siang Lai28.1K views
Yet another introduction to Git - from the bottom up by Wen-Tien Chang
Yet another introduction to Git - from the bottom upYet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom up
Wen-Tien Chang26.2K views
Automate with Ansible basic (3/e) by Chu-Siang Lai
Automate with Ansible basic (3/e)Automate with Ansible basic (3/e)
Automate with Ansible basic (3/e)
Chu-Siang Lai612 views
用Raspberry PI學Linux驅動程式 by Stanley Ho
用Raspberry PI學Linux驅動程式用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式
Stanley Ho588 views
Erlang Practice by litaocheng
Erlang PracticeErlang Practice
Erlang Practice
litaocheng2.5K views
Lua 语言介绍 by gowell
Lua 语言介绍Lua 语言介绍
Lua 语言介绍
gowell1.2K views
Python 入门 by kuco945
Python 入门Python 入门
Python 入门
kuco945855 views
Introduce to Linux command line by Wen Liao
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command line
Wen Liao16.6K views
Android C Library: Bionic 成長計畫 by Kito Cheng
Android C Library: Bionic 成長計畫Android C Library: Bionic 成長計畫
Android C Library: Bionic 成長計畫
Kito Cheng5.7K views
Openshift by mtchang by Chang Mt
Openshift by mtchangOpenshift by mtchang
Openshift by mtchang
Chang Mt949 views
Learning python in the motion picture industry by will zhou by Will Zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
Will Zhou908 views
Python 于 webgame 的应用 by 勇浩 赖
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
勇浩 赖4.5K views
Strace debug by luo jing
Strace debugStrace debug
Strace debug
luo jing780 views
为啥别读HotSpot VM的源码(2012-03-03) by Kris Mok
为啥别读HotSpot VM的源码(2012-03-03)为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)
Kris Mok13.6K views
Effective linux.1.(commandline) by wang hongjiang
Effective linux.1.(commandline)Effective linux.1.(commandline)
Effective linux.1.(commandline)
wang hongjiang1.1K views
程式設計師的自我修養 Chapter 10 記憶體 by Shu-Yu Fu
程式設計師的自我修養 Chapter 10 記憶體程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體
Shu-Yu Fu2.8K views
Continuous Delivery with Ansible x GitLab CI by Chu-Siang Lai
Continuous Delivery with Ansible x GitLab CIContinuous Delivery with Ansible x GitLab CI
Continuous Delivery with Ansible x GitLab CI
Chu-Siang Lai1.5K views
Git 版本控制系統 -- 從微觀到宏觀 by Wen-Tien Chang
Git 版本控制系統 -- 從微觀到宏觀Git 版本控制系統 -- 從微觀到宏觀
Git 版本控制系統 -- 從微觀到宏觀
Wen-Tien Chang23.6K views

Execution

  • 2. Who am I • Angelboy • 中央⼤大學碩⼠士班 ADLab • Bamboofox / HITCON 成員 • 擅⻑⾧長領域 : PWN
  • 3. Outline • How programs get run • Lazy binding • Global Offset Table • GOT Hijacking
  • 4. • We focus on ELF (Executable and Linking Format) • What’s happened when we execute an elf file. • ex : ./hello How programs get run
  • 5. Overview of the workflow (static linking) ./hello fork() execve(“./hello”,*argv[],*envp[]) sys_execve() do_execve() search_binary_handler() load_elf_binary() _start main kernel mode user mode
  • 6. Overview of the workflow (dynamic linking) ./hello fork() execve(“./hello”,*argv[],*envp[]) sys_execve() do_execve() search_binary_handler() load_elf_binary() ld.so _start __libc_start_main _init kernel mode user mode main
  • 7. • sys_execve() • 檢查參數 ex: argv , envp • do_execve() • 搜尋執⾏行檔位置 • 讀取執⾏行檔前 128 byte 獲取執⾏行檔的資訊 • ex: magic number How programs get run
  • 8. • search_binary_handler() • 利⽤用前⾯面所獲取的資訊來呼叫相對應的 handler • ex : load_script() 、 load_elf_binary() How programs get run
  • 9. • load_elf_binary() • 檢查及獲取 program header 資訊 • 如果是 dynamic linking 則利⽤用 .interp 這個 section 來確定 loader 路徑 • 將 program header 紀錄的位置 mapping 到 memory 中,ex : code segment 位置 • 將 sys_execve 的 return address 改為 loader (ld.so) 的entry point • static linking 下則會是 elf 的 entry point How programs get run
  • 10. • How program maps to virtual memory. • 在 program header 中 • 記錄著哪些 segment 應該 mapping 到什麼位 置,以及該 segment 的讀寫執⾏行權限 • 記錄哪些 section 屬於哪些 segment • 當 program mapping 記憶體時會根據權限的 不同來分成好幾個 segment How programs get run
  • 11. • How program maps to virtual memory. How programs get run 權限 mapping 位置 segment 中有哪些 section
  • 12. • How program maps to virtual memory. How programs get run other section .data .bss .got.plt .rodata .text .init ELF Header In disk In memory kernel space CODE VMA DATA VMA HEAP STACK R or RX RW 0x08048000 0x0804a000 0x0804b000 0xbffff4b0
  • 13. • ld.so • 載⼊入 elf 所需的 shared library • 這部分會記錄在 elf 中的 DT_NEED 中 • 初始化 GOT • 其他相關初始化的動作 • ex : 將 symbol table 合併到 global symbol table 等等 How programs get run
  • 14. • ld.so 其實是個很複雜的程式,要認真全部講完, ⼀一兩天的時間絕對不夠⽤用 • 有興趣可參考 elf/rtld.c • 這邊最重要的是在執⾏行程式時,要知道不是從 main 開始的 How programs get run
  • 15. How programs get run • _start • 將下列項⺫⽬目傳給 libc_start_main • 環境變數起始位置 • .init • 呼叫 main 之前的初始化⼯工作 • .fini • 程式結束前的收尾⼯工作
  • 16. How programs get run • _libc_start_main • 執⾏行 .init • 執⾏行 main • 執⾏行 .fini • 執⾏行 exit
  • 17. Lazy binding • Dynamic linking 的程式在執⾏行過程中,有些 library 的函式可能到結束都不會執⾏行到 • 所以 ELF 採取 Lazy binding 的機制,在第⼀一次 call library 函式時,才會去尋找函式真正的位置 進⾏行 binding
  • 18. Global Offset Table • library 的位置再載⼊入後才決定,因此無法在 compile 後,就知道 library 中的 function 在哪, 該跳去哪 • GOT 為⼀一個函式指標陣列,儲存其他 library 中, function 的位置,但因 Lazy binding 的機制,並 不會⼀一開始就把正確的位置填上,⽽而是填上⼀一段 plt 位置的 code 18
  • 19. Global Offset Table • 當執⾏行到 library 的 function 時才會真正去尋找 function ,最後再把 GOT 中的位置填上真正 function 的位置 19
  • 20. Global Offset Table • 分成兩部分 • .got • 保存全域變數引⽤用位置 • .got.plt • 保存函式引⽤用位置 20
  • 21. Global Offset Table • .got.plt • 前三項有特別⽤用途 • address of .dynamic • link_map • ⼀一個將有引⽤用到的 library 所串成的 linked list • dl_runtime_resolve • ⽤用來找出函式位置的函式 • 後⾯面則是程式中 .so 函式引⽤用位置 21
  • 22. Global Offset Table • layout .dynamic .got .got.plt .data addr of .dynamic link_map dl_resolve printf put … 22
  • 23. . . . call foo@plt … Lazy binding .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 23
  • 24. . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 Lazy binding 24
  • 25. . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 因 foo 還沒 call 過 所以 foo 在 .got.plt 中所存的值 會是.plt中的下⼀一⾏行指令位置 所以看起來會像沒有 jmp 過 Lazy binding 25
  • 26. . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 Lazy binding 26
  • 27. . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 Lazy binding 27
  • 28. . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 push link_map Lazy binding 28
  • 29. . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 jmp dl_runtime_resolve dl_runtime_resolve (link_map,index) Lazy binding 29
  • 31. . . . call foo@plt … .text .got.plt printf foo@plt+6 bar … .. .. call _fix_up .. .. ret 0xc dl_resolve foo 找到 foo 在 library 的位置後 會填回 .got.plt Lazy binding 31
  • 32. . . . call foo@plt … Lazy binding .text .got.plt printf foo@plt+6 bar … .. .. call _fix_up .. .. ret 0xc dl_resolve foo return to foo 32
  • 33. • 第⼆二次 call foo 時 . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 Lazy binding 33 foo
  • 34. • 第⼆二次 call foo 時 . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 Lazy binding 34 foo Jmp to foo function
  • 35. How to find the GOT • objdump -R elf or readelf -r elf
  • 36. • 為了實作 Lazy binding 的機制 GOT 位置必須是 可寫⼊入的 • 但如果程式有存在任意更改位置的漏洞,便可改 寫 GOT ,造成程式流程的改變 • 也就是控制 eip GOT Hijacking
  • 37. • 第⼆二次 call foo 時 . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 GOT Hijacking 37 foo
  • 38. • 第⼆二次 call foo 時 . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 GOT Hijacking 38 system
  • 39. • 第⼆二次 call foo 時 . . . call foo@plt … .text jmp *(foo@GOT) push index jmp PLT0 foo@plt .got.plt printf foo@plt+6 bar … push *(GOT + 4) jmp *(GOT + 8) PLT0 GOT Hijacking 39 system Jmp to system function
  • 40. Reference • Glibc cross reference • Linux Cross Reference • 程式設計師的⾃自我修養
  • 41. Q & A