I am writing shellcode on Ubuntu 11.10 x86 and the registers prior to the int 0x80 syscall look like this:
eax 0x66
ecx 0x8e60558
edx 0x0
ebx 0x3
which is set up for the connect() syscall. The value in the ecx register is an argument array that contains:
0x8e60558: 0x00000009 0x8e60583 0x00000010
where 0x00000009 is the file descriptor, 0x8e60583 is the server struct pointer that points to:
0x8e60583: 0x00000002 0x0000115c 0x7f000001
which is:
[address]: [AF_INET=2] [PORT=4444] [IP=127.0.0.1]
I know that the file descriptor is correct and all of the constant values set up in the registers like storing 0x66 in eax (socketcall is syscall #102) are correct to the best of my knowledge yet when I run the code, which should return the connected socket FD in the eax register, it returns:
eax: 0xffffff9b
which is obviously wrong. What I have I done incorrectly?
EDIT: Changed endianess of inet_address.
Your problem is that you are encoding the parameters to the connect syscall as little endian while some of them should be big-endian, additionally both the sin_family and sin_port members are encoded as 32bit, they should be 16 bit and that struct appears to require padding to 16 bytes.
PS You may want to use an assembler, you can always use
objdump -x -D $binaryto get the opcodes. Additionally I compile withgcc -c -x assembler-with-cpp -o hello-net.o hello-net.S && ld -o hello-net hello-net.oto be able to use the preprocessor as well.PS2: You may want to try executing your code with
strace, that shows the actual syscalls that you’re making.E.g. this test-program works for me (x86_64):
The same thing, but modified to get it working on x86 (32bit):
Edit: Expanded the first paragraph to mention two other possible errors and added a 32 bit (x86) sample that works for me.