Page 2 of 2

Re: change the return address to point to shellcode

Posted: Thu Oct 09, 2014 1:23 pm
by iocoder
I don't use gdb so I don't have answer for the gdb question.

For compiling the shellcode, it is very simple. You write your code in some specific language, run the compiler, then it is compiled :D

After having the compiled shellcode, you can inject it into the program through a user-input buffer, then execute it using stack-based exploitation by filling in a stack buffer (that is located inside the stack frame of bang()) until buffer overflows happens... if you keep inserting values into the buffer after the buffer overflow has occured, you will certainly reach the location of the pushed EIP. Modify it to refer to your shellcode.

Re: change the return address to point to shellcode

Posted: Thu Oct 09, 2014 1:24 pm
by iansjack
OK, I've found the code that the OP is trying to run online. It's an example of a buffer overflow exploit (although the source I found had it as a FreeBSD script).

Now if you want to keep on helping a would be script-kiddie that's your affair; me - I'm out.

Re: change the return address to point to shellcode

Posted: Thu Oct 09, 2014 1:37 pm
by hadi
iocoder,

i dont want to use it with buffer, keeping buffering the program, my idea here change the return address with out buffering the program.

Re: change the return address to point to shellcode

Posted: Thu Oct 09, 2014 2:03 pm
by iocoder
hadi wrote:iocoder,

i dont want to use it with buffer, keeping buffering the program, my idea here change the return address with out buffering the program.
So you just want to do it with gdb? I think this is straightforward!

Just set a breakpoint inside your bang(), run the program so the debugger stops at the breakpoint. Then tell gdb to print the value of EBP register (p $ebp), and then tell gdb to modify the value at EBP+4 to "shellcode" (set {int} x = shellcode, where x is ebp+4). Continue debugging and see shellcode() executing :D

Re: change the return address to point to shellcode

Posted: Thu Oct 09, 2014 4:48 pm
by sortie
I'm taking the liberty to lock this, as opposed to other methods.

hadi: You don't appear skilled enough to do this and your question is unanswerable. You ask us to help inject some shellcode (but in hex?) into some code that's obviously not vulnerable as far as I see, and with gdb? I have no idea what you are trying to do. What you are asking appears malicious and raises ethical questions whether we should help you (on the other hand, if you can gdb attach to a process, you are already on the other side of the airtight hatchway). You appear to have solved the first part of your problem by using mysterious shell code and gdb, now you need help solving the second impossible part.

There is however value in understanding actual attacks in depth and discussing them, this is not bad at all. That doesn't seem to be point of this topic here, or am I misunderstanding you?

A real attack takes advantage of a lot of specifics about a program, the architecture, the ABI, the standard library and the compiler. You need to supply some of these details if you want help doing an attack. Not all code is attackable, such code is called secure. You can however determine whether code is exploitable/malfunctioning easily, making an actual attack is much harder. For instance:

Code: Select all

void vulnerable()
{
    char buffer[16];
    gets(buffer);
}
This function is vulnerable if stdin contains a line longer than 16 characters including the NUL byte. This is often rather exploitable as stdin is rarely fully controlled. If we assume something like a naive i386 operating system, you'll find the return pointer just after the buffer on the stack. You can fill the buffer with malicious instructions that doesn't contain NUL bytes or newline bytes. You can then guess the stack location and make the return pointer point to the buffer. If you do that, then suddenly the program is running your instructions resulting in remote code execution. If you are clever, you can make the process execve a shell and if you control stdin, you must got shell access to that user.

Of course, this attack is seriously mitigated on any decent operating system. For instance, the operating system could enforce a W^X policy (a page cannot be executable and writeable). This makes that naive impossible. You then have to make the return pointer point to existing code in the process (return to libc attack). This can be mitigated again by employing address space randomization that makes it very hard to guess the right addresses, but you might get it right if you try enough times. This particular attack can be made even harder if the compiler uses stack smash protection where a magic value is placed on the stack, if you corrupt that, then the program aborts. Of course, you could be lucky and guess that as well. Even better operating systems don't even have the gets function in the first place, but it could happen in many other cases, such as bad strcpy use.

This is why actually exploiting a vulnerability requires knowing a lot of specifics about the program. Your question was too simple and wasn't answerable. Additionally, you didn't know enough basic things about this topic, this is not the best forum for you to use. I'm very much willing to discuss this seriously, but you should give a more suitable forum and read the GDB documentation.

Edit: Looking around, I find this that might be of interest.