๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • Tried. Failed. Logged.
๐Ÿ‘จ๐Ÿผ‍๐Ÿ’ป๊ฐœ๋ฐœ/์–ด์…ˆ๋ธ”๋ฆฌ์–ด

์–ด์…ˆ๋ธ”๋ฆฌ์–ด - ๋ฆฌ๋ˆ…์Šค syscall๋กœ open, read, write ์‚ฌ์šฉ

by Janger 2023. 3. 24.
728x90

 

๋ฆฌ๋ˆ…์Šค ์•„ํ‚คํ…์ฒ˜ ํ™•์ธ ๋ช…๋ น์–ด

 

uname -a

 

 

syscall ํ…Œ์ด๋ธ”

 

 

https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md#x86_64-64_bit 

 

Chromium OS Docs - Linux System Call Table

Linux System Call Table These are the system call numbers (NR) and their corresponding symbolic names. These vary significantly across architectures/ABIs, both in mappings and in actual name. This is a quick reference for people debugging things (e.g. secc

chromium.googlesource.com

 

 

์˜ˆ์‹œ 1. Hello world! ํ™”๋ฉด ์ถœ๋ ฅ(write syscall ์‚ฌ์šฉ)

 

section .data
	txt db "Hello world!"

section .text
	global _start

_start:
	mov rax, 0x01 ; write(syscall)
	mov rdi, 0x01 ; stdout
	mov rsi, txt  ; buf
	mov rdx, 0xC  ; buf size
	syscall       ; syscall

 

nasm -f elf64 hello.asm
ld -o hello hello.o
./hello

 

 

์˜ˆ์‹œ 2. /tmp/flag ํŒŒ์ผ ์—ด๊ณ (open), ์ฝ๊ณ (read), ์ถœ๋ ฅ(write)

 

section .data
	path db "/tmp/flag"

section .text
	global _start

_start:
	; open
	mov rax, 0x02  ; open(2)
	mov rdi, path ; /tmp/flag
	xor rsi, rsi  ; 0(O_RDONLY)
	xor rdx, rdx
	syscall		 ; ์‹œ์Šคํ…œ์ฝœ

	; read
	mov rdi, rax  ; fd(File Descriptor)
	mov rsi, rsp	; buf
	sub rsi, 0x50 ; 0x50๋งŒํผ ๊ณต๊ฐ„ ํ• ๋‹น
	mov rdx, 0x50 ; 0x50๋งŒํผ ์ฝ์–ด์˜จ๋‹ค.
	xor rax, rax ; read(0)
	syscall		; ์‹œ์Šคํ…œ์ฝœ

	; write
	mov rdi, 1 ; fd = stdout
	mov rax, 0x01 ; write(1)
	syscall		; ์‹œ์Šคํ…œ์ฝœ

 

nasm -f elf64 orw.asm
ld -o orw orw.o
./orw

 

.text ์„น์…˜ ์ถ”์ถœํ•˜๊ธฐ

 

$ objcopy --dump-section .text=write.bin write.o
$ xxd write.bin
00000000: 48b8 6865 6c6c 6f0a 0000 5048 89e6 6a01  H.hello...PH..j.
00000010: 5f6a 065a 6a01 580f 05                   _j.Zj.X..

 

 

 

 

์ฐธ๊ณ : 

https://ye0ye0.tistory.com/11

 

์–ด์…ˆ๋ธ”๋ฆฌ์–ด๋กœ Hello world ์ถœ๋ ฅํ•˜๊ณ  ๋””๋ฒ„๊น…ํ•ด๋ณด๊ธฐ

์นผ๋ฆฌ๋ฆฌ๋ˆ…์Šค ํ„ฐ๋ฏธ๋„์ฐฝ์„ ๋„์šด ํ›„ nano helloworld.s ๋ฅผ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค. s๋Š” ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œํŒŒ์ผ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅํ•œ ํ›„ section .data msg db "hello word" section .text global_start _start: mov rax, 1//mov๋ฅผ ํ†ตํ•ด rax์— 1๊ฐ’

ye0ye0.tistory.com

 

https://learn.dreamhack.io/50#6

 

๋กœ๊ทธ์ธ | Dreamhack

 

dreamhack.io

 

 

728x90