6 Apr 2009 bibekpaudel   » (Apprentice)

Code Obfuscation


By Binit Thapa

“Real Programmers don’t comment their code. If it was hard to write, it should be hard to read.”

Obfuscation: confusion resulting from failure to understand, mystification. Try telling someone you’re a programmer and he knows you’re an ugly, boring specimen of homo sapiens1 who has neither girlfriend nor time. Why, only today there was a discussion going on at InRev2 about how less frequently the software team went to toilets. Once you are into the programming spirit, everything else becomes moh-maya3. When you misplace something, you miss find and grep4. And when you’ve got par with all the games you have, you start writing code for fun. Maybe a small useful tool or maybe a piece of code that only bows to its creator. While this technique is not new, it’s so unique and diverse that it can easily cause a mild heart-attack if you’re not a really good programmer. Here I’ll present some of the best ones, which I’ve collected over time. But I won’t explain those to you as I’ve never fully understood them myself.

C program

C program

This is a valid C program that generates a nice poem when compiled. Author: James O. Coplien

The next one is prime number test by Abigail. This prints “prime” as 7 is a prime number.

perl -wle ‘print “Prime” if (1 x shift) !~ /^1?$|^(11+?)\1+$/’ 7

Next ones are taken from JAPH5 section of CPAN.

camel-code

camel-code

tree-code

star-wars-code

star-wars-code

love-code

love-code

The examples above were scripts with some meaningful output. There’s also poetry which may not produce any output but compiles perfectly and looks nice. These are some of those, all compiling perfectly in Perl.

poem-code-1

poem-code-2

While most of these are written in C/C++ and in Perl, hackers have created plenty in almost all languages. Code obfuscation is not only for fun. This technique has been used in making code very difficult to reverse engineer and hence secure. This is specially helpful Javascript where code is to be presented to the world.
I am signing off with some good references. Other examples are left as an exercise to the reader, etc.

Notes
1 homo sapiens - the set containing both programmers and Muggles
2 InRev - the company I work with
3 moh-maya - loving the materialistic things
4 find and grep - unix tools to search tokens in files.
5 JAPH - Just a Perl Hacker, scripts by Perl Hackers.

References
1. Wiki explains http://en.wikipedia.org/wiki/Obfuscated_code
2. The International Obfuscated C Code Contest - http://www.ioccc.org/
3. The Perl Poetry - http://docstore.mik.ua/orelly/perl/prog3/ch27_02.htm
4. JAPHs in CPAN http://www.cpan.org/misc/japh

Download Code
C-program, Camel-code, tree-code, star-wars-code, love-code, poem-1, poem-2

About the writer: Binit Thapa is the Chief Software Architect of the Bangalore-based startup InRev. He has industry experience in scripting tools, Linux and Unix development environment, SOAP/XML and data storage technologies. He is a graduate of Bachelor of Engineering (IT) from NIT Durgapur, India.

Note: I would like to thank InRev, Binit Thapa and Bhupendra Khanal for making this guest-article possible. I welcome other guest articles on topics coherent with the theme of this blog.

Syndicated 2009-04-06 07:39:35 from Bibek Paudel's weblog

Latest blog entries     Older blog entries

New Advogato Features

FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!