4

Im trying to run this shellcode but it throws me: "Segmentation fault" error The shellcode is the following:

shellcode.asm:

global _start
_start:

jmp short ca
doit:
pop ebx
xor eax, eax
cdq
mov byte [ebx+7], al
mov long [ebx+8], ebx
mov long [ebx+12], eax
lea ecx, [ebx+8]
mov byte al, 0x0b

int 0x80
ca:
call doit
db '/bin/sh'

i compile it with : 'nasm -f elf shellcode.asm' and link it with: ' ld -m elf_i386 -s -o shellcode shellcode.o

I think the error is when I use mov [ebx+x], al/eax/ebx because when I erase it from the code y get no error

Thank you

1 Answer 1

6

Your problem is that the .text section is not writable by default. The easiest thing to do is put your code into a new custom section that is marked as writable. Add this line at the top of your asm file:

section .shellcode  progbits alloc exec write align=16

You could also pass the -N switch to the linker.

Alternatively, you could rewrite the shellcode so that it uses the stack to create the arguments.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you , it works. Yes, if I use the stack, it works properly, but I wanted to know why cant I use mov [], x instructions.
I've been looking for this answer for hours. Thank you!
Thanks for this. The exec section didn't work but -N did. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.