👨🏼‍💻개발/어셈블리어

어셈블리어 - 리눅스 syscall로 open, read, write 사용

Janger 2023. 3. 24. 23:21
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