title: Linux 64-bit shellcode
body: <p>I'm trying to write my first "Hello world" shellcode on my Ubuntu 64-bit, but it doesn't work. </p>
  
  <p>I have file <strong>hello.asm</strong>:</p>
  
  <pre><code>; 64-bit "Hello World!" in Linux NASM
  
  global _start            ; global entry point export for ld
  
  section .text
  _start:
  
      ; sys_write(stdout, message, length)
  
      mov    rax, 1        ; sys_write
      mov    rdi, 1        ; stdout
      mov    rsi, message    ; message address
      mov    rdx, length    ; message string length
      syscall
  
      ; sys_exit(return_code)
  
      mov    rax, 60        ; sys_exit
      mov    rdi, 0        ; return 0 (success)
      syscall
  
  section .data
      message: db 'Hello, world!',0x0A    ; message and newline
      length:    equ    $-message        ; NASM definition pseudo-instruction
  </code></pre>
  
  <p>I used this commands:</p>
  
  <pre><code>nasm -felf64 hello.asm -o hello.o
  ld -o hello hello.o
  objdump -d hello
  </code></pre>
  
  <p>I put shellcode from objdump into my C program:</p>
  
  <pre><code>char code[] = "\xb8\x01\x00\x00\x00\xbf\x01\x00\x00\x00\x48\xbe\xd8\x00\x60\x00\x00\x00\x00\x00\xba\x0e\x00\x00\x00\x0f\x05\xb8\x3c\x00\x00\x00\xbf\x00\x00\x00\x00\x0f\x05";
  
  int main(int argc, char **argv)
  {
      int (*func)();
      func = (int (*)()) code;
      (int)(*func)();
      return 0;
  }
  </code></pre>
  
  <p>and compile it in gcc, but after run I've got "Segmentation fault (core dumped)".</p>
  
  <p>I have no idea what I'm doing wrong. Assembler code seems to work, because when i run <strong>./hello</strong> it prints "Hello world". </p>