Reverse Code Engineering

Saturday, October 28, 2006

As I spent the whole holiday sick at home, and I got no chance whatsoever to go outside (except for the last couple of days maybe), I had enough time to finish off all the work and the things I was planning to do on my computer. Many projects from the old days, each in a separate folder, were waiting on my desktop for me to have enough time to resume digging into.

Out of boredom, I decided to dig up something I barely got into previously: Reverse Code Engineering; the art of breaking into executable files and modifying their machine code to obtain different execution behavior.

If you have ever installed or used unlicensed computer software before then you, probably, have used reverse-engineered programs at some point; the illegal ones of them ... Cracks!

Software Cracks are modified software executables that have been reverse-engineered by crackers to deactivate product registration, activation, limitations, and nagging screen routines. The cracked or “patched” program works as good as a full/registered version. It has been altered at the machine language level to do so.

Long before, I made a very basic, yet interesting, experiment in this regard. I coded and compiled a simple C++ program, then started analyzing and messing with its machine code using disassemblers and hex editors. The first program was actually something like this:



These four lines of C++ were compiled into some hundred lines of assembly. I used a disassembler to trace down the string “Password Correct” and look up the instructions that reference the memory location it was being held in.
I came up with the following machine code and assembly listing:



The CMP instruction was comparing a certain value in memory with 7Bh (which is equal to 123 in decimal). The second JNE (Jump if Not Equal) instruction made the program branch at that point to one of two paths, depending on the result of the previous comparison.

Using a hex editor, I replaced 75 (op-code for JNE) with 74 (op-code for JE) which is the opposite instruction. The program (now cracked :P) started to output “Password Correct” for any user-entered password EXCEPT 123 :D

That was the very first experiment, and I was really glad it worked!

In the previous days, I made more tests and came out with much complicated tricks. Instead of reversing IF statements, I tried jumping (using the JMP instruction) to the code right inside the IF statement, trying to bypass the evaluation of the condition, and it worked! :D

My friend Zeid had an interesting argument over jumping and bypassing sections of code. He said that jumping can’t always be the solution because sometimes you skip code that is essential for the proper execution of the program. That was definitely correct, but I actually found a way to do the jump safely!

The “dangerous” jump was tried on the following program:



My target was to make this program call the function secret() with x=17 immediately at startup, i.e. not to even request user input. I wanted to wipe out the user-input section and remove the limitation of x having to be less than 5.

What I did was overwriting the cout and cin assembly code sections with NOP (No Operation) instructions (a cool way I came up with to “comment-out” those sections :P) then replace some of those NOPs with the following assembly instructions:



Carefully tried that, and yup … it WORKED! :D

That was a “dangerous” code jump of my side, but I did good I guess! :P

Coding my own assembly into that EXE and observing how it worked was one of the most enjoyable things I’ve ever done in my life!

Next am trying to crack, e7m … “Reverse Engineer” some old DOS shareware, wait for the results ;)

Terror of the Python

Monday, October 16, 2006

Despite all the efforts I made to stop this blog from becoming a National Geographic affiliate, it looks like it was destined to. Animals keep popping around like we’re living in a jungle or something, or maybe am just paying unnecessary attention to the presence of non-human life forms around. Anyways, the last animal I met was actually in my friend’s apartment …


This is “Tshi-Tshi”, a Bull (Python) snake that my friend Basel bought as a pet. It’s a non-venomous snake that eats small mammals and lies down sleeping for most of the time. Basel said he always wanted to raise a snake; and since his family is absent for the time being, he brought Tshi-Tshi to share his place with him.

We had a good opportunity to get to know Tshi-Tshi since we gather frequently at Basel’s house. It is a cool animal once you get used to it. I like the way it sneaks on the floor and around the furniture to explore the area.
I also adore its tactical strategies; it lays low whenever possible, clings to heavy parts of the furniture, and never travels fast in an unexplored terrain. A good and strategic hunter for sure!

We also became familiar with Tshi-Tshi’s food, this thing …

This “mouse” thing makes an entertaining pet on its own. The only difference is that you don’t have to really “worry” about this pet; you can do whatever you want with it till your snake feels hungry next time. You only have to pay attention not to cripple the mouse in anyway because if you do so, your snake won't feel like eating it.

Tshi-Tshi has a poor appetite; it eats no more than a mouse per week. The mouse in the picture above was last week's meal and is now resting peacefully in Tshi-Tshi’s stomach. The snake’s eating process is very bizarre and worth watching. Basel has recorded some of its final seconds; you can check it out here …


We also recorded an attempt we made to feed Tshi-Tshi. In this video you can see how a mouse reacts when it figures that the long gigantic thing lying in front of it is a SNAKE! :D


The last video is a must-watch! This video shows the mouse performing stunning acrobatics while being held by the tail few centimeters above the snake’s head :D


Poor fellow … it freaked out to the most in its final moments …

May god rest its soul in peace! :P

Download from YouTube

Thursday, October 12, 2006

Most people don't know about this but videos can be actually downloaded from YouTube and other similar video sharing services seamlessly. The option is built-in there although those services decided not to feature it for obvious reasons.

I've been using YouTube to download some song clips, technology showcases and game trailers lately. YouTube has more videos than you can find anywhere else, and is one of the fastest growing web sites on the Internet. With these simple steps you'll be able to download whatever video(s) you like to your computer. Here is what you'll need:

The Video Encoder: FLV_File_Encoder.zip (1.46 MB)

Download this file and extract its contents to a new folder.

Instructions:

1. Copy the link of the page showing the video you want to download.

For example :
http://youtube.com/watch?v=MJDdJoBTwYE

2. Go to this web site or this one.

3. Paste the link in the text field and click Download.

5. A Download Link will appear in the Download section little below, right-click this link and click Save-As.

6. Name the file “Some Video.flv” including the double quotations. You can replace Some Video with whatever name you want, just make sure you include the .flv extension and the double quotes.

7. Click Save and wait till the download completes.

8. Move the FLV file(s) you downloaded to the encoder directory and double-click Encode All.

9. The encoder will encode every FLV file in the directory and produce equivalent AVI files which can be played by most media players. You can delete the FLV files afterwards.

Let me know if you had any trouble :)