Shellcode 1 -> execve(/bin/sh) STACK PUSH
Last updated
Last updated
Now that we have covered various techniques to make shellcode, let's actually look at an example
execve() is a syscall in Linux that takes in 3 arguments and executes a command
These parameters/arguments will go in specific registers:
So, 59 is the rax, filename goes in rdi (/bin/sh), paramter to use with /bin/sh goes in rsi and environment variable (we really don't care about this right now) goes in rdx.
In Linux everything is a file. So is /bin/sh
Note that execve takes in a string as an input for RDI
In Linux system calls, strings are always null terminated. So we will somehow be pushing "/bin/sh\x00" onto the stack.
Cool, so we are clear that we have to push /bin/sh on the stack, make RDI reference this. Then we need to have RSI reference address of /bin//sh in memory which can be done with rsp. We can also have RDX reference this too.
so, the stack looks like:
Also, length of "/bin/sh" is 7 bytes. We need to make this 8 bytes to push absolute value on the stack. This is why instead of "/bin/sh" we will be pushing "/bin//sh" (same effect, 8 bytes instruction)
Since stack grows from high to low memory, we need to push in this "/bin//sh" in reverse order.
With explanation, I wrote my code above which as you can see is working to give shell.
But upon inspecting this in GDB we can see that a lot of 0s are there so this might not work in C.
Let's remove those 0s.
OBJDUMP shows no 0s now
we can extract this and test in our skeleton C code using objdump comand line fu in the previous article