x86 Assembly๐Ÿค–: Essential Part(2)

 

1. x86-64 ์–ด์…ˆ๋ธ”๋ฆฌ ๋ช…๋ น์–ด Pt.2

Opcode : ์Šคํƒ ๐Ÿงฑ

x64 ์•„ํ‚คํ…์ณ์—์„œ๋Š” ๋‹ค์Œ์˜ ๋ช…๋ น์–ด๋กœ ์Šคํƒ์„ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

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

์—ฐ์‚ฐ)

rsp -= 8

[rsp] = val

 

์˜ˆ์ œ )

[Register]
rsp = 0x7fffffffc400

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

[Code]
push 0x31337

๊ฒฐ๊ณผ )

[Register]
rsp = 0x7fffffffc3f8

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

 

 

 

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

์—ฐ์‚ฐ)

reg = [rsp]

rsp += 8

 

์˜ˆ์ œ)

[Register]
rax = 0
rsp = 0x7fffffffc3f8

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

[Code]
pop rax

๊ฒฐ๊ณผ )

[Register]
rax = 0x31337
rsp = 0x7fffffffc400

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

 

 

 

 

Opcode : ํ”„๋กœ์‹œ์ € ๐Ÿ“œ

ํ”„๋กœ์‹œ์ €(Procedure)

: ํŠน์ • ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ ์กฐ๊ฐ

  • ๋ฐ˜๋ณต๋˜๋Š” ์—ฐ์‚ฐ์„ ํ”„๋กœ์‹œ์ € ํ˜ธ์ถœ๋กœ ๋Œ€์ฒด -> ์ฝ”๋“œ์˜ ๊ธธ์ด๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค
  • ๊ธฐ๋Šฅ ๋ณ„๋กœ ์ฝ”๋“œ ์กฐ๊ฐ์— ์ด๋ฆ„์„ ๋ถ™์ž„ -> ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ธ๋‹ค

ํ˜ธ์ถœ(Call)

: ํ”„๋กœ์‹œ์ €๋ฅผ ๋ถ€๋ฅด๋Š” ํ–‰์œ„

๋ฐ˜ํ™˜(Return)

: ํ”„๋กœ์‹œ์ €์—์„œ ๋Œ์•„์˜ค๋Š” ๊ฒƒ

 

โ–ก ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ํ”„๋กœ์‹œ์ €๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋‚˜์„œ ์›๋ž˜์˜ ์‹คํ–‰ ํ๋ฆ„์œผ๋กœ ๋Œ์•„์™€์•ผ ํ•˜๋ฏ€๋กœ,

call ๋‹ค์Œ์˜ ๋ช…๋ น์–ด ์ฃผ์†Œ(return address, ๋ฐ˜ํ™˜์ฃผ์†Œ)๋ฅผ ์Šคํƒ์— ์ €์žฅํ•˜๊ณ  ํ”„๋กœ์‹œ์ €๋กœ rip๋ฅผ ์ด๋™์‹œํ‚จ๋‹ค.

 

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

 

 

โ–ฃ 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

 

 

 

โ–ฃ 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

 

 

 

 

โ–ฃ ret : return address๋กœ ๋ฐ˜ํ™˜

 

์—ฐ์‚ฐ )

pop rip

 

์˜ˆ์ œ )

[Register]
rip = 0x401000
rsp = 0x7fffffffc3f8

[Stack]
0x7fffffffc3f8 | 0x400005    <= rsp

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

๊ฒฐ๊ณผ )

[Register]
rip = 0x400005
rsp = 0x7fffffffc3f8

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

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

 

 

 

์Šคํƒ ํ”„๋ ˆ์ž„์˜ ํ• ๋‹น๊ณผ ํ•ด์ œ

 

 

 

 


Quiz : x86 Assembly 1

 

end๋กœ ์ ํ”„ํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋์„ ๋•Œ, 0x400000 ๋ถ€ํ„ฐ 0x400019๊นŒ์ง€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ€์‘๋˜๋Š” ์•„์Šคํ‚ค ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ์–ด๋А ๋ฌธ์ž์—ด์ด ๋‚˜์˜ค๋Š”๊ฐ€?

A) Welcome to assembly world!

์ด์ „ ๋ฆฌ๋ทฐ ํ€ด์ฆˆ์™€ ๊ฐ™์€ ๋ฌธ์ œ์ด๋ฏ€๋กœ ์„ค๋ช…์€ ์ƒ๋žตํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

 

 

 

Quiz : x86 Assembly 2

 

๋‹ค์Œ ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?

main๋ถ€ํ„ฐ ์‚ดํŽด๋ณด์ž.

push rbp : rbp๋ฅผ ์Šคํƒ ์ตœ์ƒ๋‹จ์— ์Œ“์Œ

mov rbp, rsp : rsp์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ rbp์— ๋Œ€์ž…

mov rdi, 0x400500 : 0x400500๋ฅผ rdi์— ๋Œ€์ž…  (rdi = 0x400500)

call 0x400497 <write_n> : write_nํ•จ์ˆ˜๋กœ ์ด๋™

 

write_nํ•จ์ˆ˜ ์ฝ”๋“œ

push rbp : rbp๋ฅผ ์Šคํƒ ์ตœ์ƒ๋‹จ์— ์Œ“์Œ

mov rbp, rsp : rsp์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ rbp์— ๋Œ€์ž…

mov QWORD PTR[rbp-0x8], rdi : rdi์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ rbp-0x8์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ์— ๋Œ€์ž…

 

mov DWORD PTR[rbp-0xc], esi : esi์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ rbp-0xc๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ์— ๋Œ€์ž…

 

* QWORD PTR : 8byte

DWORD PTR : 4byte

 

xor rdx, rdx : ๊ฐ™์€ ๊ฒƒ์„ ์—ฐ์‚ฐํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ๋ชจ๋“  ๋น„ํŠธ๊ฐ€ 0์ด ๋˜๋ฏ€๋กœ rdx์˜ ๊ฐ’์€ 0์ด๋‹ค.

mov edx, DWORD PTR[rbp-0xc]  : rbp-0xc๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ฃผ์†Œ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์€ edx์— ๋Œ€์ž… (์ด์ „์— rbp-0xc๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ๋Š” esi์— ์ €์žฅ๋จ)

 

mov rsi, QWORD PTR[rbp-0x8] : rbp-0x8๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ฃผ์†Œ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์€ rsi์— ๋Œ€์ž… (์ด์ „์— rbp-0x8๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ๋Š” rdi์— ์ €์žฅ๋จ)

 

mov rdi, 0x1 : 0x1์˜ ๊ฐ’์„ rdi์— ๋Œ€์ž…

mov rax, 0x1 : 0x1์˜ ๊ฐ’์„ rax์— ๋Œ€์ž…

 

syscall : rax๋ฅผ ์š”์ฒญ (rax๋Š” 0x1์˜ ๊ฐ’์„ ๊ฐ€์ง)

syscall์—์„œ rax๊ฐ€ 0x1๊ฐ’์„ ๊ฐ€์ง€๋ฉด ์ปค๋„์— write ์‹œ์Šคํ…œ์ฝœ์„ ์š”์ฒญํ•œ๋‹ค.

rdi, rsi, rdx๊ฐ€ ๊ฐ๊ฐ 0x1, 0x400500, 0์ด๋ฏ€๋กœ ์ปค๋„์€ write(0x1, 0x400500, 0)์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

x86 ์•„ํ‚คํ…์ฒ˜๋Š” ๋ฆฌํ‹€ ์—”๋””์–ธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—

0x400500 -> 0x7233346479203730๊ณผ ๊ฐ™์ด ์ €์žฅ๋œ๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 0x400508 -> 0x2064336275363f00 ๊ณผ ๊ฐ™์ด ์ €์žฅ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ๋Š” 0x72333464792037302064336275363f00์ด๋‹ค.

์ด๋ฅผ ์•„์Šคํ‚ค ์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋ฉด r34dy 70 d3bu6?๋ผ๋Š” ํ…์ŠคํŠธ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค!