Skip to content

Instantly share code, notes, and snippets.

@rexim
Created February 11, 2022 19:30
Show Gist options
  • Save rexim/c172f43ffdc73a14e8b0f83f4cbe0f5a to your computer and use it in GitHub Desktop.
Save rexim/c172f43ffdc73a14e8b0f83f4cbe0f5a to your computer and use it in GitHub Desktop.
BITS 64
%define SYS_exit 60
%define SYS_read 0
%define SYS_write 1
%define SYS_rt_sigaction 13
%define SYS_rt_sigreturn 15
%define STDIN 0
%define STDOUT 1
%define SIGINT 2
%define sigaction_size 32
%define sigaction_handler_offset 0
%define sigaction_restorer_offset 16
%define sigaction_flags_offset 8
%define SA_RESTORER 0x04000000
segment .text
handler:
mov rax, SYS_write
mov rdi, STDOUT
mov rsi, handler_msg ;Ctrl+C
mov rdx, handler_msg_sz
syscall
ret
restorer:
mov rax, SYS_write
mov rdi, STDOUT
mov rsi, restorer_msg ;I'm a restorer!
mov rdx, restorer_msg_sz
syscall
mov rax, SYS_rt_sigreturn
syscall
global _start
_start:
mov QWORD [sa+sigaction_handler_offset], handler
mov QWORD [sa+sigaction_flags_offset], SA_RESTORER
; mov QWORD [sa+sigaction_restorer_offset], restorer
mov rax, SYS_rt_sigaction
mov rdi, SIGINT
mov rsi, sa
xor rdx, rdx
mov r10, 8
syscall
cmp rax, 0
jl sigaction_failed
mov rax, SYS_read
mov rdi, STDIN
mov rsi, buf
mov rdx, buf_sz
syscall
mov rax, SYS_write
mov rdi, STDOUT
mov rsi, ok
mov rdx, ok_sz
syscall
mov rax, SYS_exit
mov rdi, 0
syscall
sigaction_failed:
mov rax, SYS_write
mov rdi, STDOUT
mov rsi, sigaction_failed_msg
mov rdx, sigaction_failed_msg_sz
syscall
mov rax, SYS_exit
mov rdi, 1
syscall
ret
segment .data
ok: db "OK!", 10
ok_sz: equ $-ok
handler_msg: db "Ctrl+C", 10
handler_msg_sz: equ $-handler_msg
restorer_msg: db "I'm a restorer!", 10
restorer_msg_sz: equ $-restorer_msg
sigaction_failed_msg: db "ERROR: Failed to setup sigaction for whatever reason", 10
sigaction_failed_msg_sz: equ $-sigaction_failed_msg
segment .bss
sa: resb sigaction_size
buf_sz: equ 256
buf: resb buf_sz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment