Skip to content
Advertisement

Why does my shellcode segfault when executed from C, but not as a stand-alone executable?

I’m trying to execute a shell with shellcode. I’ve made this code in a 64-bits machine:

JavaScript

After using nasm and linking with ld if i execute the file this works fine. The problem is if i get the shellcode from this and tried to execute it with this program:

JavaScript

It gives me a segmentation fault error. I can’t see what’s wrong here. Any help would be appreciated.

EDIT: Already tried the gcc -z execstack to make the stack executable, still gives a segmentation fault error

Advertisement

Answer

It is normal, because your shellcode is not setting the registers rsi and rdx, and when your C program executes the shellcode will have garbage in the registers rdi and rdx. It is because the syscall execve needs more arguments.

JavaScript

As extra information, the segmentation fault is because after your execve syscall you will get an error in rax and you will move 60 to the last 8 bits of rax and call to this syscall that doesn’t exist.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement