Bibek Paudel’s weblog

void man(Computing, South_Asia)

Code Obfuscation

with 11 comments

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 sapiens [1] who has neither girlfriend nor time. Why, only today there was a discussion going on at InRev [2] about how less frequently the software team went to toilets. Once you are into the programming spirit, everything else becomes moh-maya [3]. 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 JAPH [5] 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.

Written by Bibek Paudel

April 6, 2009 at 1:24 pm

Posted in code, guest

11 Responses

Subscribe to comments with RSS.

  1. Nice post, but it would’ve been even nicer if you used a fixed-width font and turned off the spell checker for the code. :-)

    Mauro

    April 7, 2009 at 7:50 pm

  2. Oh, I’m embarrassed now, on how I missed such distinct red underlines :) Thanks for pointing out. In fact, since hosted wordpress blogs don’t allow custom CSS, and i don’t like the default style that comes with this theme for .. segments, I chose to take screenshots of the code and put them instead. Otherwise, it would have been very hard to adjust all the spaces in the code. I’ve provided the code for download as separate files instead.

    I’m sorry for the trouble and mistakes. Thanks for pointing out though.

    Bibek

    Bibek Paudel

    April 7, 2009 at 11:33 pm

  3. lol the code are nice one hehe :)

    just one comment though – real programmers use comment and do so elaborately in _real_ world.

    ishwor

    April 8, 2009 at 4:49 pm

  4. This is weak dude and can’t believe this came from a software architect. The only thing interesting I found about the author is that he is a “Harry Potter” fan.

    Security via obscurity is stupid.
    Obfuscation goes anti-FOSS.
    Non egomaniac programmers who like to share their code comment and make the code neat.

    dude

    April 17, 2009 at 4:35 pm

  5. Yooo..

    I kno it has nothing to do with what you wrote, but have you ever heard of http://www.bluestickers.info/ringtones.php . They seems to promise free ringtones

    PS. Dont be an ass, this is NOT spam ;)

    brillreishKab

    April 17, 2009 at 5:12 pm

  6. Nake sure that also hard to customize code

    Deep

    May 8, 2009 at 9:01 am

  7. Отличная статья.Респект автору.

    Ferinannnd

    May 24, 2009 at 10:23 pm

  8. Занятно пишете, жизненно. Все-таки, для того, чтобы делать по-настоящему интересный блог, нужно не только сообщать о чем-то, но и делать это в интересной форме:)

    Avertedd

    May 26, 2009 at 11:45 am

  9. Побольше б таких штук

    Svetllana

    June 1, 2009 at 11:28 pm

  10. Always remember one thing while writing code. The guy who’s gonna debug it might have your phone number. [ He can bug you to Death]

    Rahil

    June 8, 2009 at 11:32 pm

  11. visit us!
    newsbox.cc
    newsbox.us
    nbstatus.wordpress.com
    NOW!

    tigniclenny

    June 12, 2009 at 8:24 am


Leave a Reply