๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

DreamHack: System Hacking/F Stage 2

Background: x86 Assembly Essential Part(2) ์Šคํƒ, ํ”„๋กœ์‹œ์ €, ์‹œ์Šคํ…œ ์ฝœ

์Šคํƒ๐Ÿงฑ(push, pop)

- push val : val์„ ์Šคํƒ ์ตœ์ƒ๋‹จ์— ์Œ“์Œ

 

์—ฐ์‚ฐ
rsp -= 8
[rsp] = val

 

- pop reg : ์Šคํƒ ์ตœ์ƒ๋‹จ์˜ ๊ฐ’์„ ๊บผ๋‚ด์„œ reg์— ๋Œ€์ž…

 

์—ฐ์‚ฐ
rsp += 8
reg = [rsp-8]

 

์ฃผ์˜ํ•  ์ โš ๏ธ

1) ์Šคํƒ์€ ํ™•์žฅ๋  ๋•Œ ์ฃผ์†Œ๊ฐ’์ด ๋‚ฎ์•„์ง€๊ณ , ์ถ•์†Œ๋  ๋•Œ ์ฃผ์†Œ๊ฐ’์ด ๋†’์•„์ง„๋‹ค๋Š” ๊ฑธ ์œ ๋…ํ•˜์ž(์Šคํƒ์€ ์•„๋ž˜๋กœ ์ž๋ž€๋‹ค)

2) push์™€ pop ๋ช…๋ น์–ด๋Š” ํ˜„์žฌ rsp๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ณณ์„ ๊ธฐ์ค€์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค๋Š” ๊ฒƒ์„ ์œ ๋…ํ•˜์ž

3) pop ๋ช…๋ น์–ด๋Š” ์Šคํƒ ์ตœ์ƒ๋‹จ์˜ ๊ฐ’์„ ๊บผ๋‚ธ ๋’ค, ๋Œ€์ž…๊นŒ์ง€ ํ•œ๋‹ค๋Š” ๊ฑธ ์žŠ์ง€ ๋ง์ž

 

ํ”„๋กœ์‹œ์ €๐Ÿ“œ(call, leave, ret)

ํ”„๋กœ์‹œ์ €๋ฅผ ๋ถ€๋ฅด๋Š” ํ–‰์œ„๋ฅผ ํ˜ธ์ถœ(Call)์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ํ”„๋กœ์‹œ์ €์—์„œ ๋Œ์•„์˜ค๋Š” ๊ฒƒ์„ ๋ฐ˜ํ™˜(Return)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

x64์–ด์…ˆ๋ธ”๋ฆฌ์–ธ์–ด์—๋Š” ํ”„๋กœ์‹œ์ €์˜ ํ˜ธ์ถœ๊ณผ ๋ฐ˜ํ™˜์„ ์œ„ํ•œ call, leave, ret ๋ช…๋ น์–ด๊ฐ€ ์žˆ๋‹ค.

 

CALL๐Ÿ“œ

- call addr : addr์— ์œ„์น˜ํ•œ ํ”„๋กœ์‹œ์ ธ ํ˜ธ์ถœ

 

์—ฐ์‚ฐ
push return_address
jmp addr

 

์˜ˆ์ œ
[Register]
rip = 0x400000
rsp = 0x7fffffffc400 

[Stack]
0x7fffffffc3f8 | 0x0
0x7fffffffc400 | 0x0 <= rsp

[Code]
0x400000 | call 0x401000  <= rip
0x400005 | mov esi, eax
...
0x401000 | push rbp

 

๊ฒฐ๊ณผ
[Register]
rip = 0x401000
rsp = 0x7fffffffc3f8

[Stack]
0x7fffffffc3f8 | 0x400005  <= rsp
0x7fffffffc400 | 0x0

[Code]
0x400000 | call 0x401000
0x400005 | mov esi, eax
...
0x401000 | push rbp  <= rip

 

*์˜ˆ์ œ์™€ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด call 0x401000์ด ์‹คํ–‰๋˜์ž

1) ์Šคํƒ์— ๋Œ์•„๊ฐ€์•ผ ํ•  ์ฃผ์†Œ(๋‹ค์Œ ๋ช…๋ น์–ด๊ฐ€ ์กด์žฌํ•˜๋Š” ์ฃผ์†Œ)์ธ 0x400005๊ฐ€ ์Šคํƒ์— push(== 1. rsp -= 8  2. push val) ๋˜๊ณ 

2) rip๊ฐ€ call๋œ ์ฃผ์†Œ์ธ 0x401000์œผ๋กœ ์˜ฎ๊ฒจ๊ฐ”์Œ(jmp)์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

LEAVE๐Ÿ“œ

- leave์Šคํƒํ”„๋ ˆ์ž„ ์ •๋ฆฌ (์Šคํƒ ํ”„๋ ˆ์ž„ == ํ•จ์ˆ˜๋ณ„๋กœ ์„œ๋กœ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์Šคํƒ์˜ ์˜์—ญ์„ ๋ช…ํ™•ํžˆ ๊ตฌ๋ถ„ํ•ด์ฃผ๋Š” ๊ฒƒ.

                                                                       -> ์ด๋Š”, ํ•จ์ˆ˜๋งˆ๋‹ค ์‚ฌ์šฉํ•˜๋Š” ์Šคํƒ์˜ ์˜์—ญ์ด ๋‹ค๋ฆ„์„ ์˜๋ฏธํ•œ๋‹ค)

 

์—ฐ์‚ฐ
mov rsp, rbp
pop rbp

 

์˜ˆ์ œ
[Register]
rsp = 0x7fffffffc400
rbp = 0x7fffffffc480

[Stack]
0x7fffffffc400 | 0x0 <= rsp
...
0x7fffffffc480 | 0x7fffffffc500 <= rbp
0x7fffffffc488 | 0x31337 

[Code]
leave

๊ฒฐ๊ณผ
[Register]
rsp = 0x7fffffffc488
rbp = 0x7fffffffc500

[Stack]
0x7fffffffc400 | 0x0
...
0x7fffffffc480 | 0x7fffffffc500
0x7fffffffc488 | 0x31337 <= rsp
...
0x7fffffffc500 | 0x7fffffffc550 <= rbp

 

*์˜ˆ์ œ์™€ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด leave๊ฐ€ ์‹คํ–‰๋˜์ž

1) mov rsp, rbp๋ฅผ ํ†ตํ•ด, rsp์˜ ๊ฐ’์ด rbp์˜ ๊ฐ’์ธ 0x7fffffffc480์œผ๋กœ ๋ฐ”๋€ ํ›„

2) pop rbp๋ฅผ ํ†ตํ•ด, rsp๊ฐ€ ํ˜„์žฌ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ์ธ '0x7fffffffc480'์˜ ๊ฐ’์—์„œ 8 ์ฆ๊ฐ€ํ•œ ํ›„(x64 ๋ ˆ์ง€์Šคํ„ฐ์ธ rsp ๊ธฐ์ค€, pop์„ ์‹คํ–‰ํ•˜๋ฉด rsp์˜ ์ฃผ์†Œ๊ฐ€ 8 ์ฆ๊ฐ€ํ•˜๋ฉฐ rsp๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋˜ ๊ฐ’์„ [ํ”ผ์—ฐ์‚ฐ์ž]์— ์ €์žฅํ•œ๋‹ค)

3) rsp๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋˜ ์ฃผ์†Œ์ธ '0x7fffffffc480'์˜ ๋ฐ์ดํ„ฐ์ธ '0x7fffffffc500'์ด rbp์— ๋Œ€์ž…๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค

 

RET๐Ÿ“œ

- ret : return address๋กœ ๋ฐ˜ํ™˜

 

์—ฐ์‚ฐ
pop rip

 

์˜ˆ์ œ
[Register]
rip = 0x401008
rsp = 0x7fffffffc3f8

[Stack]
0x7fffffffc3f8 | 0x400005    <= rsp
0x7fffffffc400 | 0

[Code]
0x400000 | call 0x401000
0x400005 | mov esi, eax
...
0x401000 | mov rbp, rsp  
...
0x401007 | leave
0x401008 | ret  <= rip

๊ฒฐ๊ณผ
[Register]
rip = 0x400005
rsp = 0x7fffffffc400

[Stack]
0x7fffffffc3f8 | 0x400005
0x7fffffffc400 | 0x0    <= rsp

[Code]
0x400000 | call 0x401000
0x400005 | mov esi, eax   <= rip
...
0x401000 | mov rbp, rsp  
...
0x401007 | leave
0x401008 | ret

 

*์˜ˆ์ œ์™€ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด,

1) pop rip๊ฐ€ ์ˆ˜ํ–‰๋˜์–ด

2) rsp์˜ ๊ฐ’์ด 8 ์ฆ๊ฐ€๋˜๊ณ 

3) rsp๊ฐ€ ์›๋ž˜ ๊ฐ€๋ฆฌํ‚ค๋˜ ์ฃผ์†Œ์ธ '0x7fffffffc3f8'์— ์ €์žฅ๋ผ์žˆ๋˜ ๋ฐ์ดํ„ฐ์ธ '0x400005'๊ฐ€ rip์— ๋Œ€์ž…๋˜์–ด

4) rip๊ฐ€ call ๋˜๊ธฐ ์ „ ํ•จ์ˆ˜์˜ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด return ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค

 

์ด๋ฅผ ๋” ์ž˜ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, 

1. ์Šคํƒ ํ”„๋ ˆ์ž„

2. ํ•จ์ˆ˜ ํ”„๋กค๋กœ๊ทธ, ์ฝœ, ์—ํ•„๋กœ๊ทธ

๋ฅผ ๊ฒ€์ƒ‰ํ•ด ์Šค์Šค๋กœ ๋” ๊ณต๋ถ€ํ•ด๋ด์•ผ ํ•œ๋‹ค

 

์‹œ์Šคํ…œ ์ฝœ๐Ÿ“Ÿ

์„œ๋ก 

์šด์˜์ฒด์ œ๋Š” ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ํ•˜๋“œ์›จ์–ด ๋ฐ ์†Œํ”„ํŠธ์›จ์–ด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋“ค์„ ์ œ์–ดํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ดํ‚น์œผ๋กœ๋ถ€ํ„ฐ ์ด ๋ง‰๊ฐ•ํ•œ ๊ถŒํ•œ์„ ๋ณดํ˜ธํ•˜๊ธฐ ์œ„ํ•ด ์ปค๋„ ๋ชจ๋“œ์™€ ์œ ์ € ๋ชจ๋“œ๋กœ ๊ถŒํ•œ์„ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.

 

- ์ปค๋„ ๋ชจ๋“œ๋Š” ์šด์˜์ฒด์ œ๊ฐ€ ์ „์ฒด ์‹œ์Šคํ…œ์„ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด ์‹œ์Šคํ…œ ์†Œํ”„ํŠธ์›จ์–ด์— ๋ถ€์—ฌํ•˜๋Š” ๊ถŒํ•œ์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ์‹œ์Šคํ…œ, ์ž…๋ ฅ/์ถœ๋ ฅ, ๋„คํŠธ์›Œํฌ ํ†ต์‹ , ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๋“ฑ ๋ชจ๋“  ์ €์ˆ˜์ค€์˜ ์ž‘์—…์€ ์‚ฌ์šฉ์ž ๋ชจ๋ฅด๊ฒŒ ์ปค๋„ ๋ชจ๋“œ์—์„œ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ปค๋„ ๋ชจ๋“œ์—์„œ๋Š” ์‹œ์Šคํ…œ์˜ ๋ชจ๋“  ๋ถ€๋ถ„์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด์ปค๊ฐ€ ์ปค๋„ ๋ชจ๋“œ๊นŒ์ง€ ์ง„์ž…ํ•˜๊ฒŒ ๋˜๋ฉด ์‹œ์Šคํ…œ์€ ๊ฑฐ์˜ ๋ฌด๋ฐฉ๋น„ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

- ์œ ์ € ๋ชจ๋“œ๋Š” ์šด์˜์ฒด์ œ๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ถ€์—ฌํ•˜๋Š” ๊ถŒํ•œ์ž…๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ด์šฉํ•˜์—ฌ ์›นํŽ˜์ด์ง€๋ฅผ ๋ณด๊ฑฐ๋‚˜, ์œ ํŠœ๋ธŒ๋ฅผ ์‹œ์ฒญํ•˜๋Š” ๊ฒƒ, ๊ฒŒ์ž„์„ ํ•˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋Š” ๊ฒƒ ๋“ฑ์€ ๋ชจ๋‘ ์œ ์ € ๋ชจ๋“œ์—์„œ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ๋ฆฌ๋ˆ…์Šค์—์„œ ๋ฃจํŠธ ๊ถŒํ•œ์œผ๋กœ ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ํŒจํ‚ค์ง€๋ฅผ ๋‚ด๋ ค ๋ฐ›๋Š” ํ–‰์œ„ ๋“ฑ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์œ ์ € ๋ชจ๋“œ์—์„œ๋Š” ํ•ดํ‚น์ด ๋ฐœ์ƒํ•ด๋„, ํ•ด์ปค๊ฐ€ ์œ ์ € ๋ชจ๋“œ์˜ ๊ถŒํ•œ๊นŒ์ง€ ๋ฐ–์— ํš๋“ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด์ปค๋กœ ๋ถ€ํ„ฐ ์ปค๋„์˜ ๋ง‰๊ฐ•ํ•œ ๊ถŒํ•œ์„ ๋ณดํ˜ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์‹œ์Šคํ…œ ์ฝœ(system call, syscall)

์œ ์ € ๋ชจ๋“œ์—์„œ ์ปค๋„ ๋ชจ๋“œ์˜ ์‹œ์Šคํ…œ ์†Œํ”„ํŠธ์›จ์–ด์—๊ฒŒ ์–ด๋–ค ๋™์ž‘์„ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
๋„์›€์ด ํ•„์š”ํ•˜๋‹ค๋Š” ์š”์ฒญ์„ ์‹œ์Šคํ…œ ์ฝœ์ด๋ผ๊ณ  ํ•จ. 
์œ ์ € ๋ชจ๋“œ์˜ ์†Œํ”„ํŠธ์›จ์–ด๊ฐ€ ํ•„์š”ํ•œ ๋„์›€์„ ์š”์ฒญํ•˜๋ฉด, ์ปค๋„์ด ์š”์ฒญํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์œ ์ €์—๊ฒŒ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์คŒ.

 

๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” x64์•„ํ‚คํ…์ณ์—์„œ rax๋กœ ๋ฌด์Šจ ์š”์ฒญ์ธ์ง€ ๋‚˜ํƒ€๋‚ด๊ณ ์•„๋ž˜์˜ ์ˆœ์„œ๋Œ€๋กœ ํ•„์š”ํ•œ ์ธ์ž๋ฅผ ์ „๋‹ฌ
์ธ์ž ์ˆœ์„œ: rdi → rsi → rdx → rcx → r8 → r9 → stack

 

ํ•ด์„

 

syscall ํ…Œ์ด๋ธ”

syscall          rax      arg0 (rdi)                                arg1 (rsi)                                      arg2 (rdx)
read 0x00 unsigned int fd char *buf size_t count
write 0x01 unsigned int fd const char *buf size_t count
open 0x02 const char *filename int flags umode_t mode
close 0x03 unsigned int fd    
mprotect 0x0a unsigned long start size_t len unsigned long prot
connect 0x2a int sockfd struct sockaddr * addr int addrlen
execve 0x3b const char *filename const char *const *argv const char *const *envp


syscall table์„ ๋ณด๋ฉด, rax๊ฐ€ 0x1์ผ ๋•Œ, ์ปค๋„์— write ์‹œ์Šคํ…œ์ฝœ์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ rdi, rsi, rdx๊ฐ€ 0x1, 0x401000, 0xb ์ด๋ฏ€๋กœ ์ปค๋„์€ write(0x1, 0x401000, 0xb)๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

writeํ•จ์ˆ˜์˜ ๊ฐ ์ธ์ž๋Š” ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ, ์ถœ๋ ฅ ๋ฒ„ํผ, ์ถœ๋ ฅ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 0x1์€ stdout์ด๋ฉฐ, ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ™”๋ฉด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 0x401000์—๋Š” Hello World๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๊ณ , ๊ธธ์ด๋Š” 0xb๋กœ ์ง€์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ํ™”๋ฉด์— Hello World๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 

 

'DreamHack: System Hacking > F Stage 2' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Quiz: x86 Assembly 2  (0) 2023.03.31
Quiz: x86 Assembly 1  (0) 2023.03.31
Background: x86 Assembly Essential Part(1) ์–ด์…ˆ๋ธ”๋ฆฌ ๋ช…๋ น์–ด  (0) 2023.03.31
Quiz: Linux Memory Layout  (0) 2023.03.31
Background: Linux Memory Layout  (0) 2023.03.31