1ํธ์ ์ด์ด์ system call ํธ์ถ ๋ถ๋ถ๋ง์ ๋ฐ๋ก shellcode๋ฅผ ๋ง๋ค์ด๋ด ์๋ค.
์ ๋ ๊ฐ์ธ์ ์ผ๋ก ๋ ํธํ intel ๋ฌธ๋ฒ์ผ๋ก ์ฝ๋ฉํด ๋ณผ๊ฒ์.
hello.asm | → | hello.o | → | hello |
assembly source file | assembler (nasm) | object file | linker (ld) | executable file |
<hello.asm>
1 BITS 32
2
3 global _start
4
5 section .text
6 _start:
7 ; write(4, "hello world!" addr, 13)
8 mov eax, 4
9 mov ebx, 1
10 push 0x0a
11 push 0x21646c72
12 push 0x6f77206f
13 push 0x6c6c6568
14 mov ecx, esp
15 mov edx, 13
16 int 0x80
17
18 ; exit(0)
19 mov eax, 1
20 mov ebx, 0
21 int 0x80
BITS 32 : 32bit ํ๋ก๊ทธ๋จ ์์ ๋ช ์
global _start : Linker ๋ก ํ์ฌ๊ธ ์คํ ํ์ผ์ ๋ง๋ค๋ _start ํจ์๊ฐ entry point ์์ ๋ช ์
section .text : text์น์ ์ ๋ค์์ ์ ๋ ฅ
mov {destination}, {source} : ํ์์ ๊ฐ์ ์ ์์ ๋ณต์ฌ
push {data} : stack์ data๋ฅผ ์ ์ฅ
int 0x80 : system call ํธ์ถ
eds000n.github.io/syscalls-x86.html
system call write ํจ์๋ ์ด 4๊ฐ์ ๋ ์ง์คํฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
eax | ebx | ecx | edx |
writeํจ์์ syscall ๋ฒํธ | fd (ํ์ผ ๋์คํฌ๋ฆฝํฐ) | *buf (๋ฌธ์์ด์ ์ฃผ์) | len (๋ฌธ์์ด์ ๊ธธ์ด) |
4 | 1 | &(๋ฌธ์์ด) | 13 |
์ฌ๊ธฐ์ ecx์ ์ด๋ป๊ฒ ๋ฌธ์์ด์ ์ฃผ์๋ฅผ ๋ฃ๋์ง ๋ด ์๋ค. (์ฝ๋10~14)
- push๋ฅผ ํตํด "hello world!\n"๋ฅผ ์คํ์์ ์ฌ๋ฆฌ๊ธฐ
- mov ecx, esp๋ฅผ ํตํด ecx์ ํด๋น์คํ์ ์ฃผ์๋ฅผ ๋ณต์ฌ
1๋ฒ ๊ณผ์ ์ ํตํด stack์๋ "hello world!\n"๋ฌธ์์ด์ด ์ฌ๋ผ๊ฐ๊ฒ ๋๊ณ , esp๋ ์ด๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋ฉ๋๋ค.
(esp๋ ์ง์คํฐ๋ ์คํ ๋๋ถ๋ถ์ ์ฃผ์๋ฅผ ๋ด๊ณ ์์ต๋๋ค.)
=====================================
= "hello world\n" → 0x68656c6c6f20776f726c64210a
= little endian ๋ฐฉ์์ผ๋ก 4byte์ฉ ๋์ด์ pushํฉ๋๋ค.
=
= ← push
= 0x68656c6c | 0x6f20776f | 0x726c6421 | 0x0000000a
= ์๊ทธ๋ฐ๊ฑด์ง๋ ๋ค๋ค ์์ค๊ฑฐ๋ผ ์๊ฐํฉ๋๋ค.
= ๋ ์ฌ๋ถํ ์ ์ํ ์ฐธ์กฐ๋ฌธ์
======================================
์ด๋ ๊ฒ stack์ ์ฃผ์๊ฐ ๋ด๊ธด esp๋ฅผ ecx์ ๋ฃ์ด์ค ์ผ๋ก์จ, ๋ฌธ์์ด์ ์ฃผ์๊ฐ ๋ค์ด๊ฐ๊ฒ ๋ฉ๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก ํ๋ก๊ทธ๋จ์ ์ ์์ ์ธ ์ข ๋ฃ๋ฅผ ์ํด exit ํจ์๋ฅผ ์คํ์ํต๋๋ค.
eax | ebx |
exit ํจ์์ syscall ๋ฒํธ | error_code |
1 | 0 |
๋ง๋ค์ด์ง ํ๋ก๊ทธ๋จ์ ์คํ์์ผ๋ณด๋ฉด ๋ฐ๊ฐ๊ฒ ์ธ์์๊ฒ ์ธ์ฌํฉ๋๋ค.
์ด์ objdump๋ฅผ ์ด์ฉํ์ฌ ๊ธฐ๊ณ์ด์ฝ๋๋ฅผ ์ญ ๋ฝ์๋ผ ์ฐจ๋ก (๊ณ ๊ฒ์ด ๋ฐ๋ก shellcode)
์ ๋ ๊ฐ๋จํ python ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ดค๋๋ฐ ๊ด๋ จ ๋ชจ๋์ด ์๋์ง ์ฐพ์๋ด์ผ๊ฒ ๋ค์..^^;;
์๋นํ ๋ฌด์ํ๊ฒ ๋ง๋ค์ด๋์ ๋ถ๋๋ฝ์ง๋ง, ํน์ ์ฌ์ฉํ์ค๋ถ ๊ณ์๋ค๋ฉด.. ๊ฐํ ์ฌ๋ ค๋ณด๊ฒ ์ต๋๋ค.
import os
os.system("objdump -d hello > temp")
f = open('temp', 'r')
shellcode = ""
data = f.read().split('\n\n')
for i in range(2, len(data)):
tmp = data[i].split('\t')
tmp2 = ""
for j in range(len(tmp)/2):
tmp2 = tmp[j*2+1]
tmp2 = tmp2.split(' ')
code = ""
for k in range(len(tmp2)):
code += tmp2[k]
for k in range(len(code)/2):
shellcode += "\\x" + code[k*2:k*2+2]
os.system("rm temp")
print "shellcode len : " + str(len(shellcode)/4)
print shellcode
3๋ฒ์งธ ์ค objdump -d {ํ์ผ๋ช }์ ์ ์ ํ ๋ณ๊ฒฝํ์ฌ python2 ๋ก ์คํ์์ผ ๋ณด๋ฉด
์๋ง ์๋ก์ผ ์ ๋์ฌ๊ฒ๋๋ค.
shellcode len : 48
\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\x6a\x0a\x68\x72\x6c\x64\x21\x68\x6f\x20\x77\x6f\x68\x68\x65\x6c\x6c\x89\xe1\xba\x0d\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80
์ฒซ shellcode๋ฅผ ๋ง๋ค์ด๋ดค์ต๋๋ค
์ด์ ์ง์ ํ๋ฒ ์ฌ์ฉํด๋ณด๋ฌ ๊ฐ๋ณด์ฃ
๋ณ์ shellcode์ ๊ฐ์ ๋ง๋ shellcode๋ฅผ ๋ฃ์ด์ฃผ์๊ณ
์ปดํ์ผ์ ๊ฑฐ์น ํ๋ก๊ทธ๋จ์ ์คํ์ํค๋ฉด ์ฑ๊ณต์ ์ผ๋ก hello world!
์ฌ๊ธฐ์ ์ฃผ์ํด์ผํ ์ ์ gcc ์ปดํ์ผ ์ต์ ์ -z execstack ์ต์ ์ ๊ผญ ๋ฃ์ด์ฃผ์ ์ผ ํฉ๋๋ค.
๋ณ์ shellcode๊ฐ stack์์ ์ฌ๋ผ๊ฐ ์๊ธฐ ๋๋ฌธ์ ํด๋น ์ต์ ์ด ์๋ค๋ฉด ๋ณดํธ๊ธฐ๋ฒ์ ์ํด ์คํ์ด ๋์ง ์์ต๋๋ค.
GNU_STACK์ ํ๋๊ทธ๋ฅผ ์ ๋ณด๋ฉด ๋ณดํธ๊ธฐ๋ฒ์ ๊บผ์ฃผ์์๋ E(์คํ๊ถํ)๊ฐ ์ถ๊ฐ ๋์์์ ๋ณผ์ ์์ฃ ?
์ด๋ ๊ฒ ์ฑ๊ณต์ ์ผ๋ก shellcode๋ฅผ ๋ง๋ค์ด๋ณด์์ต๋๋ค... ๋ผ๊ณ ํ ์ค ์์๋ค๋ฉด ๊ฒฝ๊ธฐ๋ ์ค์ฐ.
๋ง๋ shellcode๋ฅผ ์ค์ ๊ณต๊ฒฉ์ ํ๋ฒ ์ฌ์ฉํด ๋ณด์๋ฉด ์๋ง ์๋์ด ๋์ง ์์๊ฒ๋๋ค.
๊ทธ ์ด์ ๋ shellcode์ 0x00๋๋ฌธ์ธ๋ฐ์.
๋ค๋ค ์์๋ค์ํผ 0x00์ ๋ง๋๋ฉด ํ๋ก๊ทธ๋จ์ด ๋ฌธ์์ด์ ๋์ผ๋ก ๊ฐ์ฃผํ๊ธฐ ๋๋ฌธ์ ๋ค ์ฝ๋๊ฐ ๋ฌด์๋ฉ๋๋ค.
ํ๋ฒ ๋ณผ๊น์
๊ธธ์ด๊ฐ 48์ด ์๋, 2๋ผ๊ณ ๋์ค์ฃ ?
์ด๋ฅผ ์ฐ์งค๊ณ ...
to be continued..
'system hacking ๐ฑ > shellocde' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
shellcode (0x2f ์์ ๊ธฐ) (0) | 2020.12.19 |
---|---|
shellcode (4) (0) | 2020.12.19 |
shellcode (3) (0) | 2020.12.18 |
shellcode (1) (0) | 2020.12.17 |