http://www.perlmonks.org?node_id=342355

It's a nasty wet snowy day here in Durango. I don't usually amuse myself with Perl outside of work, but I've been thinking that one of the first Perl scripts I ever wrote would make a fun obfu node. So I cleaned it up and started making it unreadable and all that...

...but that obfu stuff is *hard*!

And the more I struggled with my pitiful obfu, the more impressed I became with the good monks here who contribute the obfu nodes that I enjoy so much. Which led me to thinking things like this:

What editors do you use? How do you make that stuff look that good?
What's your motivation to do obfu stuff?
How much practice do you put in?

Do you have a Philosophy of Obfu? I realize that obfuscated code has been around for a long time, but much of the stuff I see here is downright elegant.

So, having punted on *my* first obfu, I wanted to see what the monks here would tell me about how and why they obfuscate code.

Replies are listed 'Best First'.
Re: Principles of obfuscation?
by gmax (Abbot) on Apr 03, 2004 at 21:35 UTC

    See Trying to learn something about obfuscation for some interesting links.

    Then, before rushing to create your obfuscation masterpiece, try to understand some of the works from other people.

    Camel Code has a fine spoiler that you can use as your first lesson, but if it looks too hard, you may start with this step-by-step deconstruction of one of my nightmarish compositions, or a more demanding twist.

    As for the editor, I use Vim, but for obfuscated scripts, as you can see from my notes, the best editor is Perl itself, since I use it to create garbled code from clean lines.

    My motivation to do obfu stuff is to challenge myself doing something harder than usual. A side effect of my obfu works is that I became more confident about some less than ordinary pieces of Perl syntax.

    Have fun!

     _  _ _  _  
    (_|| | |(_|><
     _|   
    
      JaVE does camilizes with any text and any image that you provide.

      And there is also a similar aproach as a PM: Text::JavE. So you should only have to think in the perl code part.

         _                                              _
       .\_/. -=======================================- .\_/.
       ||a||  |  JavE (Java Ascii Versatile Editor) |  ||a||
       `/_\' -=======================================- `/_\'
      
      -=-_-=-_-=-_-=-_-=-_-=-_-=-_-=-_-=-_-=-_-=-_-=-_-=-_-=-_-=-_-
      
Re: Principles of obfuscation?
by davido (Cardinal) on Apr 04, 2004 at 07:18 UTC
    I've written a few obfus. I'm not qualified to win any Obfuscation contests, but I still enjoy the thought-process.

    To me, an obfu is simply a puzzle; a little doodle combined with a brainteaser.

    My Obfus come to me a lot of ways. Sometimes I'll be reading the Perl PODs and think to myself, "I wonder if I can use this to do something tricky..." Other times I'll see something on Perlmonks that grabs my attention and makes me investigate deeper. And other times I'll see something in the real world that motivates me to write an obfu.

    In every case, the challenge to me is to take a particular theme, whether it be a little programming trick I've discovered, or a real-world theme I want to play with, and to design something nifty around that theme.

    • In Morse Code JAPH I was just starting out with Perl. I thought hashes were pretty cool, slices were even moreso, and as it happens, I had just stumbled across something about Morse Code online. That was the inspiration I needed to start fiddling. The result was as simple as you might expect from a complete novice. But it was a learning opportunity for me.

    • In Chomped JAPH I had just read about the fact that chomp has a little known and seldom used return value. I set out to find an unconventional use of that return value.

    • In ASCII Art JAPH I had just dug out an old book of mine that contained a BASIC program for outputting ASCII art. That got me thinking that "Just Another Perl Hacker" doesn't have to be plain old boring text.

    • In Tortise JAPH I had just read a webpage that reminded me of Logo. I started thinking, I wonder how concisely I could write a little application that reads a Logo-like script and uses it to move an ascii-art-drawing turtle around the screen. My "logo like scripting language" turned out to be similar only in the most basic conceptual sense. But it was fun to implement.

    • With The Matrix Reloaded: JAPH I had just seen a movie and started thinking of how to "do that".

    There are about 30 others of mine that you can find with Super Search if you're interested. Some are better than others. None are at pure and clever as some of the "award winners", but they all started with some sort of an epiphany in my mind where I said to myself, "What if...?"

    Basically they were all opportunities for me to dig into some new concept and explore it a little deeper. Some say you don't learn anything from an Obfu. I disagree. Sure, they don't teach good programming style, but they can be a chance to tinker with and become intimately familiar with a new syntax, construct, or concept.


    Dave

Re: Principles of obfuscation?
by eyepopslikeamosquito (Archbishop) on Apr 04, 2004 at 04:28 UTC

    I think creating obfus satisfies a basic creative urge we all have; more normal people perhaps satisfy this urge through art or music or even conventional computer programing. I also find creating obfus to be an excellent form of stress relief. My personal obfu style, perhaps driven by stress, is to try to be humorous bordering on the absurd. My main motivation is not to make people spend hours figuring out how an obfu works, but simply to make them smile.

    See also Learning from Obfuscation and domm's YAPC talk.

Re: Principles of obfuscation?
by BUU (Prior) on Apr 03, 2004 at 22:20 UTC
    I think the best obfuscations come from finding a bizarre feature of perl that either looks differently or acts differently then you would expect. For example, an obfu by Abigail (as I recall) Basically looked like this:
    $; ={just=>another=>perl=>hacker} ; $"=$/; print %$;
    Or something to that affect, taking advantage of the $; variable.
Re: Principles of obfuscation?
by teamster_jr (Curate) on Apr 05, 2004 at 09:05 UTC
    I approach obfuscation more as an excersise in terseness than perhaps unreadability (although that is a byproduct).
    I do them as a mental excercise to keep my brain actually having to think about things i don't normally have to - i agree with djantzen about it being partly "mental masturbation", but at the same time if people want to do that then who's to stop them? And by writing hard code in a terse manner i now understand functions i wouldn't necessarily have used or certainly understand as well (vec, pack/unpack, splice, map, grep etc etc)
    Also it's nice to make something that people are surprised and pleased that it can actually be done in that space.

    I started off doing them entirely for email signatures, so i almost always use a 288 char limit - 4 lines of 72 chars each.
    I use Vim and a script which formats the code into 4*72 lines, removes whitespace and counts chars for me
    (you can see it in my scratchpad - usage sizer <name of script to check> <char counting 0|1> <chomp whitespace> <width>).

    The process usually starts with an idea - something i've seen that i think i can do in that number of chars (or more likely can't do - but want to try). I'm starting to do bigger ones - like Balls! as the problems i approach get harder to fit in.
    For example: The first one i posted here was this - a pair of sigs to generate and then solve a maze. This became an obsession and i kept working at it till i got it into 4 lines.
    After the idea i tend to write it in semi-long hand, then start shortening it - spotting shorter ways to do things etc etc.

    er, that's it really!

    apologies for the rambling post!
    al

Re: Principles of obfuscation?
by djantzen (Priest) on Apr 04, 2004 at 02:06 UTC

    My Philosophy of Obfu: I have better things to do.

    Not to be wet blanket on this discussion, but I've never understood why obfuscated code is interesting. Generally speaking, it relies on little understood quirks of a language, or worse, bugs, that someone with the appropriate pathology has devoted their time to committing to memory. My goal as a programmer is to write *good* code -- by which I mean correct, robust, extensible, and maintainable -- and I've never seen anything in any obfu anywhere that helped me to write good code.

    In short, like my chemistry major roommate opined about my philosophy studies in college, it's mental masturbation. FWIW, I don't get Geek Code either.


    "The dead do not recognize context" -- Kai, Lexx
      I've seen some really slick stuff done in obfuscation, and for that, I have to give the artists credit.

      I have no interest in decoding it. I thoguht I did, but it runs counter to my upbringing. In fact, I look at obfuscation without documentation as to how it works and often thing "Well what is the point?", as what is presented is often denied knowledge. There may be a really clever trick inside, but most won't find it, and in essense that's just hiding the cool tricks. (If the tricks are cool ... I don't consider the evil corners of Perl near-bugs cool in the least .. but rather, signs the language or interpreter needs improvement to be more clear)

      Anyhow, some tricks involved in obfuscation bring programming back to it's earlier roots, when things weren't always written for you and trivial, and you have to invent hacks or neat ways of solving problems. I think a lot of teamster_jr's maze solving stuff, for instance, is pretty darn cool.

      But I do agree, while these things are cool in obfuscated state, the ability to obfuscate isn't the interesting one, it's the ability to get a lot done in a short amount of code and the algorithms they contain.

      That being said, I'd rather read and write clean interesting code myself, as to me elegance can only be found in clean code and the Zen-like simplicity of a well tailored algorithmic solution. Of course, even that is blowing smoke, I seldom achieve that either -- I just don't like to add fuel to the bigotry that is "Perl is line noise!" any more than I have to.

      It's sort of like those bad movies where there are good wizards and bad wizards, and the good wizards refuse to cast the "Black" spells. I am sort of refusing to cast those spells, because the power would corrupt me and I might enjoy it.

      Or take Star Wars, if you will. Yoda isn't going to cast lightening from his fingertips, that's not what Yoda does. But I'm still blowing smoke, I am no Jedi Master, sitting in a swamp down in Dagobah, where it bubbles all the time like a giant carbonated soda.

Re: Principles of obfuscation?
by jynx (Priest) on Apr 12, 2004 at 21:45 UTC
    Back when i was a more active member of this community i wrote a document on a few good things to keep in mind about obfuscations that links to quite a few other good sources of information on the topic. It's not definitive (and probably nothing is), but it is a decent start.

    If you're interested, you can check it out here.

    jynx