Segmentation Error Error When EXE C

So, after compiling and executing my program, I get the following error message that reads: “Segmentation Error” and strace error message:

--- SIGSEGV (Segmentation fault) @ 0 (0) --- +++ killed by SIGSEGV +++ Segmentation fault 

Question: any ideas how I can fix this error and display a message in the shell code?

Build Code:

 ;r3v.asm ;r3v3rs3c - 3x_z3r0 [SECTION .text] global _start _start: jmp short ender starter: xor eax, eax xor ebx, ebx xor edx, edx xor ecx, ecx mov al, 4 mov bl, 1 pop ecx mov dl, 18 int 0x80 xor ebx, ebx int 0x80 ender: call starter db 'r3v3rs3c' 

Build it with: nasm -f elf r3v.asm Link it with: ld -o r3v r3v.o Reset it with: objdump -d r3v Extract the shell code into the test program:

 /*shelltest.c r3v3s3c - 3x_z3r0*/ char code[] = "\xeb\x15\x31\xc0\x31\xdb\x31\xd2\x31\xc9\xb0\x04\xb3\x01\x59\xb2\x12\xcd\x80\31\xdb\xcd\x80\xe8\xe6\xff\xff\xff\x72\x33\x76\x33\x72\x73\x33\x63"; ; int main(int argc, char **argv) { int (*exeshell)(); exeshell = (int (*)()) code; (int)(*exeshell)(); } 

Then I compile with: gcc shelltest.c -o shelltest Run it with: ./ shelltest and the output shows “Segmentation Error”.

+1
source share
1 answer

Currently, your string code will be placed in the memory part of the program, which is declared not executable, since you declare the array mutable (not const). When you try to run it as a function, your OS will see that you are trying to run code in a memory area that cannot be executed, and it will kill your program with segfault.

To fix this change, your code declaration must be const char

i.e

 const char code[] = "\xeb......." 

This will allow the compiler to put it in executable memory and thus allow it to start.

+2
source

All Articles