| Gabri3l
Tutorial #2
Beginner Tutorial: Internal Keygen and Patching |
| The Target: LC 5 http://www.atstake.com/products/lc/ |
| The Tools: OllyDbg 1.09d, HexWorkshop 4.1, Diablos2002 Universal Patcher |
| The Protection: Serial Protection |
| Other Information: This is a tutorial to introduce the beginner to internal keygens and introductory patching. This will take you through the steps of finding the serial and constructing an internal keygen and creating a patch for the original file. |
|
Intro: |
|
All the tools you will need can be found online: Let us first get set up and ready to crack. I like to open the folder
with my target in it. In this case c:\program files\@stake\ and make
a quick backup that we can work on without fear of screwing up the
file. I named mine lc52.exe and I will refer to it as such through
the tutorial. Note: I used three seperate computers to create this tutorial. This is why my serial changes halway through. It is also why my Addresses in Olly change. If your Addresses in Olly do not match mine, it is not a problem. The code will be the same. |
|
Body: |
|
Knowing that we will not need to unpack the executable lets us do a little examination of the protection scheme. Open up LC52.exe. It goes directly into the nag screen. From the previous tutorial we know that it is checking for a registration key. Press Register and we are presented with a new window holding a unique serial number and a box asking for our unlock code. Enter anything in as the unlock code and press Okay. We get a message box saying "You have entered an invalid code. Please try again.". Remeber to write this message down as we will be searching for it later. Press Okay and the program returns to the window asking for our unlock code. Searching For A Serial: Press the Run button. The LC5 nag screen pops up, press the Register button. In the window asking for our unlock code enter any serial. Let's try 1234567890. Press Okay. Olly breaks on our TEST EAX, EAX. First thing you notice is the registers on the right hand side. There are a string of numbers and letters in ECX and EDX that look suspiciously like an unlock code. It was that easy! Before we continue, return the program to the state of unregistered. Go the the START MENU in Windows and select the RUN command. Type in REGEDIT and press Okay. In REGEDIT's left window, click on the [+] in front of HKEY_CURRENT_USER. Then click on the [+] in front of SOFTWARE. Click on the [+] in front of @STAKE and then the [+] in front of LC5. Select the Registration folder. In the right hand window you will see Unlock Code. Double click it, and erase the Value Data in the new window. Press Okay and close REGEDIT.
Creating An Internal Keygen: Before we begin let's evaluate what we need to do to create an internal keygen. We already know where to find the unlock code so we need to solve how to display the code. A good idea would be, rather than displaying an error message, we display the unlock code. Let's reexamine the code where we see the test and where we see the "You have entered an invalid..." string pushed for the message box. Now that we know what we need to consider, let's move onto how to implement the unlock code into the messagebox. We know where the error message is pushed onto the stack so let's start by changing the code to push our code instead. We can see our code in both the ECX and EDX registers. However before we get to the PUSH, EAX is zeroed out and our code is overwritten in memory. So let's address that issue first. We only need our code in one register so we can keep XOR EAX, EAX. To stop our code from being overwritten we need to change MOV DWORD PTR SS:[ESP+14],ECX, MOV DWORD PTR SS:[ESP+1C],ECX and, MOV BYTE PTR SS:[ESP+24],CL. Select the first one with your mouse and right click. Choose Assemble from the menu. This will bring up a box where you can edit the code. We are now going to make our messsage box display the unlock code. Select PUSH lc5.004D62D4 and select Assemble. When the box comes up, enter PUSH EDX and press Assemble. Now press the Run button and...
Modifying the Internal Keygen: A Quick Overview of the Messagebox Function: Knowing how a MessageBox is created gives us the knowledge of how to manipulate it. (For more information on internal functions of Windows research Win32 API). What we want to do is find where the program calls MessageBox and where the Arguments are passed. Open your new LC5modified.exe up in Ollydbg. Right click in the code area and choose Search For, select All Intermodular Calls. A new window opens up with all the functions called by the program. We will sort these by Destination by clicking the Destination bar at the top. As you can see they are now in alphabetical order. Scroll down until you find MessageBoxA. Select one of the MessageBoxA lines and right click. Now select Set Breakpoint On Every Call To MessageBoxA: Our objective is to let the user know that the code they are seeing is the Unlock Code. A good place to do so would be in the Title of the message box. Let's examine how we are going to change the Title. We need to find a place in code to create our new text string. Because we cannot write new code we are going to have to overwrite old code. We then need to change the code for the MessageBox call to PUSH our string as the Title Argument. Let's begin by creating a new string. Because we have already modified this program, we know of a string that is not being used: "You have entered an invalid code. Please try again". If we refer back to the original code of the serial test we can see this string being pushed onto the stack: PUSH lc5.004D63D4. We now know that the string is located at 004D63D4. We will change it later, let's first try and get the program to actually push this value in. Looking at the code we see that this part of the code jumps to MessageBoxA. In the Arguments for MessageBoxA we see EDI is pushed as the Title. And the value for Title is moved into EDI just before the call to the MessageBox. We need to change the program to move our string into EDI rather than ECX+43. I am going to show you the very ugly, unaccepted, however it works, way to change the program. Begin by selecting MOV EDI,DWORD PTR DS:[ECX+4C], select Assemble (or press spacebar), and in the box type MOV EDI, 4D63D4. Press Assemble. Do you see why this method is ugly and unaccepted? Because our code to move the string into EDI is longer than the original code, we have just overwritten our jump to MessageBox. "Well, just write it underneath". That would seem the logical option, but we will be destroying the function call to GetModuleFileNameA. Let's find out how many times GetModuleFileName is called before we do any overwriting. Select the first line of the function: 68 04010000 PUSH 104. At the bottom of the code window we see: Jump from 004BD56F. There is only one jump to this function and it is directly above our jump to MessageBoxA. Set a breakpoint on JE SHORT lc5modif.005BD576 and press Run. Go through the registration process and when we reach our breakpoint (do not step past it or the program will quit) we see in the bottom code window that the Jump is not even taken. If we insert our new jump overtop of PUSH 104 the program will call a MessageBox instead of GetModuleFileName but we do not even see the program calling GetModuleFileName anyway so let's try it out. Select PUSH 104 and choose Assemble. Enter JMP SHORT 4BD590 (Your number may need to be different, check the Address of PUSH EBX, that is the Address you want to jump to). Your code should look like this: It worked! Go through the registration process a few times to make sure that the program does not crash. For future information: The correct way to modify this program would be to jump to a code cave, move our serial into EDI and jump back to the MessageBox function. You can try it out on your own if you are interested, I may cover code caves in another tutorial. Now we want to save our modifications before we do anything else. Just as before, select Copy to Executable, and then All Modifications. A box pops up asking us if we want to copy the selction, choose Copy All. A new window will open with the program in it and our changes included. Right click and select Save File. Save it as LC5Final.exe. You can close Olly, we are almost done. The last thing we need to do is change the text string "You have entered..." Changing The Text String: You will end up at 000D63D4. Sound familiar, that is the same value you pushed into EDI just a few minutes ago. We have found the correct string. We can only overwrite the current string, we cannot use any more or any less space. By placing your cursor just before the Y in the right hand window you can type just as you would in any text editor. Experiment around until you find something that will completely overwrite the string. Keep in mind the string includes a period at the end. Here is my modified string:
Creating a Patch: There are two ways to make a patch. Code it completely yourself, or use a program that will help you create it. There are many different patching programs to choose from. I am going to use Diablos2002 Universal Patcher, or DUP. It is newer, easy to use, and allows you to customize how and what you want to patch.
Once you have filled in the boxes, choose the Offset Patch tab at the top. Select, for the Original File, LC5.exe and, for the Patched File, LC5Final.exe. Press the Compare button and it will create a list of the different bytes between the two. Last thing you need to do is press the Create Patch button and choose a name for your patch. You are done! Backup LC5.exe and run the patch. Then try registering with an invalid serial... It works! You have successfully modified a program to create an display a valid serial. You also learned how to create a patch so you can apply the modifications on other computers. You can now apply the techniques you learned to other reversing projects. |
|
Conclusion: |
| I used this particular program purely as a demonstration
for internal keygens and patching. If like the program and are going to
use it please purchase it.
Thanks to all the people who take time to write tutorials. Without
the teaching's of others we would all lack knowledge. Thanks to Exetools, Woodmann, and ARTeam for being a great place of learning. If you have any suggestions, comments or corrections email me: Gabri3l2003[at]yahoo.com |