I am trying to output a file in Assembly using cat
with the execve
syscall, but I am having a bit of trouble and I don't know why.
section .text
global _start
_start:
mov al, 59 ; Syscall number
xor rdx, rdx ; No enviornment variables
push rdx
; Add arg1
mov rdi, "/bin/cat"
push rdi
mov rdi, rsp
push rdx
; Add arg2
mov si, "xt"
push rsi
mov rsi, "//file.t"
push rsi
mov rsi, rsp
; Call execve
syscall
I checked to make sure /file.txt
exists. Then looked at the stack with GDB before the syscall is done, and everything also looks correct:
0x00007fffc25f8628│+0x0000: "//file.txt" ← $rsp, $rsi
0x00007fffc25f8630│+0x0008: 0x0000000000007478 ("xt"?)
0x00007fffc25f8638│+0x0010: 0x0000000000000000
0x00007fffc25f8640│+0x0018: "/bin/cat" ← $rdi
How can I call execve
correctly?
Here is an 64-bit example, written in nasm, that runs an external command with arguments and this works for me.
; ---------------------------------------------------------
; Executes external command
; ---------------------------------------------------------
SECTION .data
; Important: The strings must be null-terminated
bin db '/usr/bin/nano',0x0
arg1 db '-w', 0x0
arg2 db '/etc/fstab', 0x0
args dq arg1, arg2, 0x0
SECTION .text
global _start
_start:
; Invoke SYS_EXECVE
mov rax, 59
mov rdi, bin
mov rsi, args
xor rdx, rdx ; envp[] is not used
syscall
; Quit
mov rax, 60
mov rdi, 0
syscall
Best regards.