Search code examples
gccassemblyx86nasmshellcode

NASM automatically escaping shellcode initialized in .data


I am attempting to write a piece of assembly code which will perform operations on some shell code I have initialized in .data

My initialization is as as follows:

section .data
        shellcode: db "\x31\xc9\xf7\xe1\x51\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\xb0\x0b\xcd\x80", 0

When I attempt to load the array holding the shell code into a register, it is automatically escaped, breaking my program:

enter image description here

enter image description here

I assembled and linked as follows:

nasm -f elf32 -g encryptor_assembly.asm -o encryptor_assembly.o
gcc -m32 -g encryptor_assembly.o -o encryptor_assembly

Is there a way to avoid this from happening? Thanks for your help.


Solution

  • Nasm only interpretes C-style escape sequences inside character strings delimited with backticks. To fix your code, replace the double quotes with backticks like this:

    shellcode: db `\x31\xc9\xf7\xe1\x51\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\xb0\x0b\xcd\x80`, 0
    

    Refer to the manual for details.