Segmentation fault error when exe C

2019-08-05 12:13发布

So after I compile and execute my program I get the following error message that reads: "Segmentation fault", and the strace error message reads:

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

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

Assembly 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'

Assemble it with: nasm -f elf r3v.asm Link it with: ld -o r3v r3v.o Dump it with: objdump -d r3v Extract the shell code into a 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 Execute it with: ./shelltest and the output reads "Segmentation fault".

标签: c shellcode
1条回答
【Aperson】
2楼-- · 2019-08-05 12:31

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

To fix this change your declaration of code to be a const char

i.e

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

This will allow the compiler put it into executable memory and thus allow it to be run.

查看更多
登录 后发表回答