commit fbbe017fd505216116090d9e5805ef86634fed75
Author: afify <hassan@afify.dev>
Date: Fri, 4 Mar 2022 21:26:34 +0300
init
Diffstat:
7 files changed, 149 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,15 @@
+ISC License
+
+© 2022 Hassan Afify <hassan at afify dot dev>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/Makefile b/Makefile
@@ -0,0 +1,37 @@
+# See LICENSE file for copyright and license details.
+
+SRC = execve.s hi.s
+BIN = ${SRC:%.s=%}
+OBJ = ${SRC:%.s=%.o}
+
+ASM = nasm
+LNK = ld
+AFLAGS = -f elf64 -w+all -D$$(uname)
+LFLAGS = -m elf_x86_64 -s
+
+all: options ${BIN}
+
+options:
+ @echo ${BIN} build options:
+ @echo "AFLAGS = ${AFLAGS}"
+ @echo "LFLAGS = ${LFLAGS}"
+ @echo "ASM = ${ASM}"
+ @echo "LNK = ${LNK}"
+
+${OBJ}: %.o: %.s
+ ${ASM} ${AFLAGS} -o $@ $<
+
+${BIN}: %: %.o
+ ${LNK} ${LFLAGS} -o $@ $<
+
+clean:
+ rm -rf *.o ${BIN}
+
+raw:
+ printf '\\x'
+ printf '\\x' && objdump -d ./execve | grep "^ " | cut -f2 | tr -d ' ' | tr -d '\n' | sed 's/.\{2\}/&\\x /g'| head -c-3 | tr -d ' ' && echo ' '
+
+x:
+ objdump -d ./execve|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'
+
+.PHONY: all options clean
diff --git a/execve.s b/execve.s
@@ -0,0 +1,19 @@
+; See LICENSE file for copyright and license details.
+BITS 64
+%include "syscalls.s"
+
+section .text
+ global _start
+
+_start:
+ mov rbx, 0x0068732F6E69622F
+ push rbx
+ mov rax, SYS_execve
+ mov rdi, rsp ;const char *filename
+ xor rsi, rsi ;const char *const argv[]
+ xor rdx, rdx ;const char *const envp[]
+ syscall
+
+ mov rax, SYS_exit
+ mov rdi, 0
+ syscall
diff --git a/hi.s b/hi.s
@@ -0,0 +1,19 @@
+; See LICENSE file for copyright and license details.
+BITS 64
+%include "syscalls.s"
+
+section .text
+ global _start
+
+_start:
+ push 0x34333231
+ mov dword [rsp+4], 0x38373635
+ mov rax, SYS_write
+ mov rdi, 1 ;unsigned int fd
+ mov rsi, rsp ;const char *buf
+ mov rdx, 8 ;size_t count
+ syscall
+
+ mov rax, SYS_exit
+ mov rdi, 0
+ syscall
diff --git a/linux_x86_64.csv b/linux_x86_64.csv
@@ -0,0 +1,4 @@
+%rax,System call,%rdi,%rsi,%rdx,%r10,%r8,%r9
+1,sys_write,unsigned int fd,const char *buf,size_t count
+59,sys_execve,const char *filename,const char *const argv[],const char *const envp[]
+60,sys_exit,int error_code
diff --git a/macros.s b/macros.s
@@ -0,0 +1,40 @@
+; See LICENSE file for copyright and license details.
+
+%ifndef MACROS_S
+%define MACROS_S
+
+%define EXIT_SUCCESS 0
+%define EXIT_FAILURE 1
+%define STDOUT 1
+%define STDERR 2
+
+%macro CHECK_BSD 0
+%ifdef OpenBSD
+section .note.openbsd.ident note
+ dd 8, 4, 1
+ db "OpenBSD", 0
+ dd 0
+%elifdef NetBSD
+section .note.openbsd.ident note
+ dd 7, 4, 1
+ db "NetBSD", 0, 0
+ dd 0
+%endif
+%endmacro
+
+%macro EEXIT 1
+ mov rax, SYS_exit
+ mov rdi, %1
+ syscall
+%endmacro
+
+%macro DIE 2
+ mov rax, SYS_write
+ mov rdi, STDERR
+ mov rsi, %1
+ mov rdx, %2
+ syscall
+ EEXIT EXIT_FAILURE
+%endmacro
+
+%endif ;MACROS_S
diff --git a/syscalls.s b/syscalls.s
@@ -0,0 +1,15 @@
+; See LICENSE file for copyright and license details.
+
+%ifndef SYSCALLS_S
+%define SYSCALLS_S
+
+%ifdef Linux
+ %define SYS_exit 60
+ %define SYS_write 1
+ %define SYS_gettimeofday 96
+ %define SYS_execve 59
+%else
+ %fatal "OS not supported"
+%endif
+
+%endif ;SYSCALLS_S