1

I wrote a very simple assembly program (x64, linux) which reads the contents from /etc/passwd (doesn't really matter from where) and writes it to stdout. I compile it with nasm (nasm -f elf64 foo.asm -o bar). I receive the following error: ./bar: 24: Syntax error: EOF in backquote substitution

Here is my code:

global _start
section .data
    fn:  db  '/etc/passwd',0

section .bss

section .text
_start:
    ; open the file
    xor rax,rax
    add al, 2
    lea rdi, [fn]
    xor rsi, rsi
    syscall

    ; read the file, use some area
    ; in the stack to store the contents
    mov rdi, rax
    sub sp, 0xfff
    lea rsi, [rsp]
    xor rdx, rdx
    mov dx, 0x200
    xor rax, rax
    syscall

    ; write to stdout
    xor rdi, rdi
    add dil, 1
    mov rdx, rax
    xor rax, rax
    add al,1 
    syscall
    ; exit
    xor rax,rax
    add al, 60
    syscall

Is is also possible to get more info about the error? the program compiles with no errors in nasm. Thanks :)!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Inter Sys
  • 129
  • 7
  • 2
    `nasm` produces object file, you need to link it, e.g. `nasm -f elf64 foo.asm && ld foo.o -o bar` – Jester Oct 23 '20 at 11:20
  • 1
    Oh my god, stupid of me. You can add this answer seperately, because it solved my problem. Thanks buddy! – Inter Sys Oct 23 '20 at 12:07
  • 1
    Not the problem, but you forgot `default rel`; you definitely want that if you're using `lea rdi, [fn]`, especially if this is supposed to be shellcode which needs to be position independent. Also, `lea rsi, [rsp]` is just an inefficient way to write `mov rsi, rsp`. (Many modern CPUs can run `mov` even more efficiently than `lea`, because `mov` doesn't do math.) – Peter Cordes Oct 23 '20 at 23:39
  • 1
    Also, `mov dil, 1` saves 1 byte vs. `add dil,1`. But both of those ways suck compared to `lea edi, [rsi + 1]`, since you still have RSI=0 from the first system call. And since you need both RDI and RAX to be `1` for `write` to stdout, just copy one to the other. See also [Tips for golfing in x86/x64 machine code](https://codegolf.stackexchange.com/q/132981) – Peter Cordes Oct 23 '20 at 23:44
  • Thanks a lot @PeterCordes :) always a pleasure to learn new things. – Inter Sys Oct 24 '20 at 14:17
  • How did you even try to run `bar`, anyway? If I run `./foo` (after NASM overwrote an already-executable file called `foo` so I didn't need chmod), I get `bash: ./foo: cannot execute binary file: Exec format error`. Did you try to `source` it, like `. ./foo`? Your "EOF in backquote substitution" error sounds like a shell was trying to parse it as a shell script. – Peter Cordes Oct 24 '20 at 23:33

0 Answers0