OllyDbg

OllyDbg (http://www.ollydbg.de/) is an application debugger with support for limited code analysis. A lot of professionals swear by OllyDbg, and as a debugger it's easy to see why. From a code analysis standpoint, it's lacking when compared with IDA Pro, but as a debugger, IDA Pro can't hold a candle to OllyDbg. By using the two programs together, you get the best of both worlds. Whenever I'm tasked with quickly reverse engineering a binary, I usually have IDA Pro open in one window and OllyDbg open in another.

If you've ever used a graphical debugger before, then OllyDbg's interface will already be somewhat familiar to you. It has all the standard windows and tracing features that you would expect from a modern debugger.

Compared with IDA Pro, navigating the disassembly is a bit limited but it has just about everything you need. To see the basic options for navigation, go to the CPU window and right-click; then select "Go to". Here previous and next work just like forward and back do in IDA Pro. You can also choose to go back and forth through the function calls the process has made by selecting "Next procedure" and "Previous procedure." You can specify a location to return to at any point by right-clicking and selecting "New origin here." Then when you are ready to return to this point, right-click and select Go to → Origin.

The ability to edit a running program's memory, code, and register values is probably the thing that OllyDbg does best. To edit code or register values, double-click on the entry you wish to change. For code sections, it will actually let you specify the new value as an assembly mnemonic. This makes patching a running executable very easy. If you want to edit data, you will need to select the region you wish to edit and then right-click and choose Binary → Edit.

Earlier, while using IDA Pro, we found some bugs in the FreeCiv server that would let us trick the server into giving us lots of gold and giving an AI player lots of debt. In this section, I'm going to show you how to use OllyDbg to implement this hack.

Open civclient.exe in OllyDbg; after it has paused the executable, right-click on the CPU window and choose "Go to" → Expression. In the box, type 4A2A40 and press Enter. You should now see the code you need to edit. Start by selecting the first line you want to edit (xor ecx, ecx). Right-click on it and choose Binary → "Fill with NOPs". You should now see it replaced with two NOP instructions. The next thing to do would be to insert the replacement instruction, but if you try, you will find out that the new instruction needs five bytes and the old only took two. We must use the padding at the end of the function to steal the three bytes we need.

First we will need to copy the binary section for the bottom half of the function so we can move it and create the room we need. Do this by selecting all the lines starting from mov edx, 8 and ending with retn. Then right-click and choose Binary → "Binary copy". Now select mov edx, 8, right-click, and choose Binary → "Fill with NOPs". We now have room enough to edit the first instruction. Double-click on the first NOP and enter the replacement instruction.

We now need to move the remaining code down three bytes. To fit it in, we will need to take up some of the pad space at the end of the function. After retn, you should see some instructions used for alignment. Right-click on the first line after the retn and using the same procedure as before, fill it with NOP instructions. We now have room enough to paste the remaining code back. Select all the lines from the first remaining NOP instructions to the last NOP instructions you just created. Right-click and choose Binary → "Binary paste" from the drop-down menu to paste the code back. Now that you've pasted that data back, double-click on mov edx, 8 and change the 8 to a 1.

If you were to run this now, it would crash because the binary paste we did made an exact copy of the code that used to be there. The problem here is that most compilers generate relative call instructions, so if the function call that is made at the end of this routine is run now, it would be accessing a location that was three bytes off from where it should be. To fix this, you need to patch the call instruction and subtract 3 from the offset to which it is calling. Once that is done, you should have a working exploit.