Segmentation fault error when exe C

冷暖自知 提交于 2019-12-11 12:16:38

问题


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".


回答1:


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.



来源:https://stackoverflow.com/questions/29076525/segmentation-fault-error-when-exe-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!