1

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

g0tr00t
  • 69
  • 1
  • 6
  • 3
    Seeing as nobody can know what you did exactly (you posted no code), I doubt anybody will have an idea on how to fix your problem. – StoryTeller - Unslander Monica Mar 16 '15 at 12:15
  • Post your code. Your entire error. – Jared Burrows Mar 16 '15 at 12:17
  • We need to establish terminology first. What is "shell code" in your opinion? What is "extract the hex into C script"? What is "C script"? Usually we have "shell script" and "C code". – Ivan Aksamentov - Drop Mar 16 '15 at 12:21
  • 1
    this is a runtime error , first try to find till what part of your code it runs fine with `printf` or `exit(0)` then check your dynamic arrays , pointers , indexes , frees and everything related to memory...I guess that you have an evil wild pointer so look for him or post your code – Alireza Soori Mar 16 '15 at 12:23
  • @StoryTeller sorry man Im new to the whole forum Qs and As thing, still practising on how to ask questions in a more detailed and understanding format... – g0tr00t Mar 23 '15 at 03:32
  • @g0tr00t, no need to apologise. Just remember that you should help the people here help you. If you do your own investigation and post all relevant info, someone will come along soon enough with the help you need. – StoryTeller - Unslander Monica Mar 23 '15 at 13:06

1 Answers1

2

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.

Simon Gibbons
  • 6,969
  • 1
  • 21
  • 34
  • I made the change and it had taken away the "segmentation fault" message and replaced it with the desired output :) – g0tr00t Mar 16 '15 at 13:34
  • an old discussion for me it is working without the const but I don't understand why it is not working when I write const *char ? – DeathNet123 Jun 25 '22 at 20:19