Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shellcode to execute a shell results in a segmentation fault

Tags:

c

shellcode

I am trying to run this code to get a shell but I am getting a segmentation fault even with ASLR disabled. I am running this code on my AMD Ryzen 3 computer with Ubuntu 20.04 64bit version.

I am compiling with the following command:

$ gcc -O0 -fno-stack-protector -z execstack getshell.c -o getshell

File getshell.c is as following:

#include <stdio.h>

unsigned char shellcode[] = \
"\x48\x31\xf6\x56\x48\xbf\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x57\x54\x5f\x6a\x3b\x58\x99\x0f\x05";
int main()
{
    int (*ret)() = (int(*)())shellcode;
    ret();
}

Edit: I found this piece of code here

like image 394
Taimoor Zaeem Avatar asked Oct 25 '25 05:10

Taimoor Zaeem


1 Answers

unsigned char __attribute__((section(".text#"))) shellcode[]

works for me (mind the #)

# is a trick - it comments part of the emitted assembly code by gcc.

like image 132
0___________ Avatar answered Oct 26 '25 18:10

0___________



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!