A few weeks ago I started taking a stab at an Active X exploit in Enjoy SAPGui that I saw on FD. MC found the software and had me download; he wanted to 0wn it at work. We have a plethora of old dell laptops which actually make great victims but I think his is getting old. He was having some CRC errors when trying to extract the installation files from the 600 mb rar file. He passed it off to me and went to lunch! I extracted it and re read the advisory and figured since it was a stack overflow so it would be straight forward. So, I fuzz it using ComRaider in order to find the exact spot it breaks and then start crafting my POC which I completely ripped the template from MC. Using the create_pattern within the REX API from Metasploit 3.0 I find my offset and start going to work. After about an hour of tinkering with it and getting discouraged I discovered it wasn’t going to be as easy or as forgiving as I had hoped. As it would turn out, it required me to overwrite the structured exception handler… Shit, I knew of this but not how to do it. MC quickly hacked out the missing components and pushed out the module. Fortunately I was able to analyze what he did and ask a few questions. In typical fashion MC didn’t answer my questions but he guided me in a topic for research later that evening.
If you’re unfamiliar with Structured Exception Handlers, or their exploitation you can learn a lot by checking out the following article. Alternatively, Uninformed journal has an excellent paper on preventing s eh overwrites. The paper has an excellent intro to structured exception handlers.
http://www.microsoft.com/msj/0197/Exception/Exception.aspx
So what exactly is the structured exception handler? I think the definition in the shellcoders handbook was the best definition “An exception handler is a piece of code that deals with problems that arise when something goes wrong within a running process.” When something goes bad, e.g. an exception occurs, the exception handler is used to handle it. The exception handler in a structure on the stack and is basically a linked list. Then the exception occurs, the linked list is walked until a suitable handler is found. In the event a handler is not locate the thread or process is terminated. This is why if you are fuzzing an active x controller and send it 1000 “\x41’s” and it overflows at 800, internet explorer crashes! This works well for a Denial of Service code, but isn’t very effective if we want remote code execution.
As those who were paying attention will notice, the exception handler is loaded on the stack at thread startup. When we overflow our programs buffer and end up with a stack overflow we also overflow the exception handler. Basically our stack window in OLLY will look like this (minus the shortjmp code and the jmb ebx address which will we get into momentarily)

Currently The data in the “Pointer to next S EH record is our EXCEPTION_REGISTRATION and the “SE HANDLER” is where the address our EXCEPTION_REGISTRATION IS Pointed. EBX points to our EXCEPTION_REGISTRATION structure. What we need now is an address that contains a JMP or CALL EBX. When we CALL or JMP into EBX, we go right into our “SE Handler” and if we don’t handle that correctly the thread is terminated. What we need to do is place a JMP instruction. This will JMP over our “SE Handler” and into our shell code. In the particular exploit I was playing with MC used \xeb\x06 + \x90\x90. I had seen this done before and used before but I never fully understood what it all was doing. This compelled me to do research and then write a paper, and subsequently this blog.
My research bought me to a few websites, as well as the shell coder’s handbook. The first website I encountered was: http://mirror.href.com/thestarman/asm/2bytejumps.htm The website was great in breaking everthing out for me about relative jumps. Basically what I needed to get out of it was relative jumps (also known as short jumps) always have the first byte EB and the second is a relative offset 00h to 7FH is a forward jump and 80h to Ffh is for a backward jump. If your overwrite keeps overwriting the stack well past the exception handler structure you can simply write your shellcode in after handler overflow and jump forward to it. In the event you don’t have room, you will need to jump back! So, that took care of the Jumps. I hope I broke some stuff down to better understandings. I’m a horrible writer, but researching the issue definitely helped me understand it better!