Top Banner
GDB & PWNTOOLS INTRO --BY TA 瓈方 1091 電腦攻擊與防禦
33

1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

Feb 18, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

G D B & P W N T O O L S I N T R O - - B Y T A 瓈方

1091 電腦攻擊與防禦

Page 2: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

OUTLINE

➢ What is pwn?

➢ Flow

➢ Useful Tools

➢ GDB

➢ PWNTOOLS

2

Page 3: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

What is PWN

➢ 碰(O) / 胖(X)

➢ pwn own

➢ pwn = binary exploitation

• 利用binary的漏洞,在執行期間控制其Control flow

以達到特定行為 (ex get shell in CTF)

3

Page 4: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

Flow

1. Reverse Engineering (逆向工程) : 尋找漏洞

• 通常只會拿到binary,而非程式原始碼

2. Exploitation (漏洞利用)

4

Page 5: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

Useful Tools

1. Reverse Engineering (逆向工程) : 尋找漏洞

➢ 靜態分析

• objdump

• ida pro

• Ghidra

5

➢ 動態分析

• GDB

• Ollydbg

• Windbg

Page 6: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

Objdump

➢ dump出執行檔中的組合語言

$ objdump –M intel –d <執行檔>

• -M intel : 設定組合語言的syntax為intel,

default是AT&T

• 後面接 | less或 | grep更方便使用

6

Page 7: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

Objdump

➢ Ex.

7

Page 8: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- installation

➢ Origin GDB

$ sudo apt-get update

$ sudo apt-get install gdb

➢ 好用插件

• gef / pwndbg / gdb-peda

8

Page 9: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ run <arg1> <arg2> ...

• r < file.txt : 把檔案內容當作input

• r <<< $(cmd) : 把cmd執行結果當作input

9

Page 10: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ disas main : disassemble main function

➢ break main : 下斷點在main function

➢ break *0x4011fb : 下斷點在0x4011fb

➢ info breakpoint : 查看現在所有斷點

➢ delete 2 : 刪除第2個斷點

➢ disable/enable 2 : 暫停/恢復第2個斷點

10

Page 11: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ Ex.

11

Page 12: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ continue : 繼續執行

➢ ni / n : step over, 遇到function不會跟進去

• ni : 是針對assembly

• n : 是針對source code

➢ si / s : step in, 遇到function會跟進去

12

Page 13: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ x/10gx <address> : 查看address中的內容

• b/h/w/g : 代表取1/2/4/8 bytes

• x : 以hex形式印出,可替代為

- i : 以指令形式印出

- u : 以unsigned int的形式印出

- S : 以字串形式印出

• 10 : 從address開始印出10個

13

Page 14: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ Ex.

• Note: $<Register> 代表暫存器中的值

14

Page 15: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ set *<address>=<value> : 將address中的值設成value

• * 代表設定4 byte

可取代成{char/short/long},分別代表1/2/8 bytes,

也可以取代成{int}, 代表value為int形式

• Ex

15

Page 16: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ attach <pid> : attach一個正在執行的process,

• 需要root權限

• $ echo 0 > /proc/sys/kernel/yama/ptrace_scope

16

Page 17: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GDB -- Basic Command

➢ set follow-fork-mode <parent|child>

• fork之後(eg,system),要繼續debug parent還是child process

17

Page 18: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GEF -- Installation

➢ GEF

18

$ wget -O ~/.gdbinit-gef.py –q

https://github.com/hugsy/gef/raw/master/gef.py

$ echo source ~/.gdbinit-gef.py >> ~/.gdbinit

Page 19: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GEF -- Useful feature

➢ checksec : 查看binary有哪些保護機制

➢ vmmap : 查看process mapping狀況

➢ pattern create/search : 可以算overflow offset

➢ ropper : 列出rop gadget

➢ search-pattern : 在process memory中找特定字串

19

Page 20: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GEF -- Useful feature

➢ checksec

• 查看binary有哪些保護機制

20

Page 21: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GEF -- Useful feature

➢ vmmap

• 查看process的mapping狀況

21

Page 22: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GEF -- Useful feature

➢ pattern create/search

• 算overflow的大小時很好用

• 如果發現死在ret

22→代表要塞18byte的junk

Page 23: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GEF -- Useful feature

➢ ropper

• 可以找ROP gadget

23

Page 24: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

GEF -- Useful feature

➢ search-pattern <str/addr>

• 可以拿來找字串或地址

24

Page 25: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

Flow

1. Reverse Engineering (逆向工程) : 尋找漏洞

• 通常只會拿到binary,而非程式原始碼

2. Exploitation (漏洞利用)

• pwntools : python exploit library

25

Page 26: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

PWNTOOLS

➢ from pwn import *

26

Page 27: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

PWNTOOLS

➢ recv / send

27

Page 28: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

PWNTOOLS

➢ Payload construct :

28

Page 29: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

PWNTOOLS

➢ Shellcode

• 記得先指定架構,或是asm()也可以帶參數

29

Page 30: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

PWNTOOLS

➢ ELF

• 尋找特定Function或library function

30

Page 31: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

PWNTOOLS

➢ ROP chain

• 也可以用ropper或ROPgadget找

31

Page 32: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

ROPgadget

➢ $ ROPgadget --binary <binary>

32

Page 33: 1091 電腦攻擊與防禦 - staff.csie.ncu.edu.tw

33

THE END