2. Outline
• Name Mangling
• Virtual function table
• Vtable Hijacking
• Vector & String
• New & delete
• Copy constructor & assignment operator
2
3. Outline
• Name Mangling
• Virtual function table
• Vtable Hijacking
• Vector & String
• New & delete
• Copy constructor & assignment operator
3
4. Name Mangling
• C++ 為了了 Overloading 時,可以讓 compiler 和
linker 可以辨別出相同 function 名稱,參參數不同的
function 引⽤用的機制
• 使 programer 在不同的 namespace 底下可以有
著多個相同名稱的 function
4
5. Name Mangling
• ⽽而在 compiler 和 linker 處理理 symbol 時,就會使
⽤用該機制讓每個 function 名對應到⼀一個修飾過後
的名稱
• C++ 中全域變數和靜態變數也有相同的機制
5
6. Name Mangling
• 在 gdb 中可以使⽤用下列列指令讓 function 好看⼀一點
• set print asm-demangle on
6
7. Outline
• Name Mangling
• Virtual function table
• Vector & String
• New & delete
• Copy constructor & assignment operator
7
8. Virtual function table
• Virtual function is a key mechanism to support
polymorphism in C++
• For each class with virtual functions, depending
on the class inheritance hierarchy, the compiler
will create one or more associated virtual
function table
8
9. Virtual function table
9
writable section
(heap)
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
compiler generates
the table for all class
10. Virtual function table
10
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
new a Person and
a Stu object
11. Virtual function table
11
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
ddaa->speak()
12. Virtual function table
12
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
vfptr == *ddaa
取 vfptr
13. Virtual function table
13
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
call *vfptr
(Person::speak(ddaa))
14. Virtual function table
14
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
meh->speak()
15. Virtual function table
15
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
vfptr == *meh
取 vfptr
16. Virtual function table
16
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
call *vfptr
(Stu::speak(meh))
17. Virtual function table
17
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
meh->pwn()
18. Virtual function table
18
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
vfptr == *meh
取 vfptr
19. Virtual function table
19
writable section
(heap)
ddaa
meh
vfptr
a
vfptr
b
typeinfo
Person::speak()
Person::phd()
typeinfo
Stu::speak()
Person::phd()
Stu::pwn()
read-only section
vtable for Person
vtable for Stu
call *(vfptr+0x10)
(Stu::pwn(meh))
20. Vtable Hijacking
• Need other vulnerabilities
• Use-after-free, Heap overflow ….
• Force the table and Hijack the vfptr
• Because the vfptr is writable
20
29. Vector & String
• Vector
• member
• _M_start : vector 起始位置
• vector::begin()
• _M_finish : vector 結尾位置
• vector::end()
• _M_end_of_storage :容器最後位置
• if _M_finish == _M_end_of_storage in push_back
• It will alloca a new space for the vector
• 以這個來來判斷空間是否⾜足夠放元素
29
30. Vector & String
• Vector
• member function
• push_back : 在 vector 最後加入新元素
• pop_back : 移除 vector 最後⼀一個元素
• insert :插入元素到 vector 第 n 個位置
• erase :移除 vector 中第 n 個元素
• ……
30
74. Copy constructor &
assignment operator
74
push_back(student)
id = 1337
name orange
vector
id = 1337
name copy the value
to vector
using
shadow copy
75. Copy constructor &
assignment operator
75
~Stu()
id = 1337
name orange
vector
id = 1337
name 因 student 的
life time 結束
所以
call destructor