Jim Lawless' Blog


Obfuscated Perl

Originally published on: Tue, 07 Jul 2009 11:33:26 +0000

In 1997, I entered a contest held by the publication The Perl Journal. It was their 1st annual Obfuscated Perl coding contest.

My entry in the category for simply printing "The Perl Journal" took 2nd place. ( You can now find this entry in the O'Reilly book Games, Diversions, and Perl Culture ).

I would like to briefly explain the genesis of my entry.

I had thought about listing my entry here, but I'm pretty certain that I don't own any kind of publication rights. One can still find it with a little Googling. However, the first two phases of the entry are still mine.

The program began with an idea that I had that involved allowing the Perl program read data from its own set of comments. The code opens itself as a file, loads its own source ( including the comments ).

To confuse the reader, I small p-machine and interpreted a short program to read from the comments and display The Perl Journal.


# Jim Lawless jimbo@radiks.net
# This program is an entry for the
# obfuscated Perl contest.

open(i,$0);@x=<i>;$y=join("",@x);close(i);
@z=split(/\ /,$y);
$c{"]"}='$d++;';
$c{">"}='$e++;';
$c{"?"}='print substr($z[$d],$e,1);';
$c{"["}='$d--;';
$c{"<"}='$e--;';
$c{"."}='print " ";';

$d=0; $e=0;

$f="]]]]]]?>?[[[[>>>?]]].]]]]]]]]]<<<<?>?>?>?[[[[[[[[.[[[[[<<<?" .
   "]]](fix this)]>>>>?]]]]]]]]<?[[[?]]]]]<?[[[[[[[[[[[[[<?>>?";

for($i=0;$i<length($f);$i++) {
   eval($c{substr($f,$i,1)});
}

The associative array %c holds the code for the six instruction machine.

The symbolic instructions are as follows:

  1. ] Increment variable $d
  2. > Increment variable $e
  3. ? Display the character in array $z, element $d, at position $e
  4. [ Decrement variable $d
  5. < Decrement variable $e
  6. . Print a space

The p-machine program that performed the output was embedded in the Perl code as the string:


$f="]]]]]]?>?[[[[>>>?]]].]]]]]]]]]<<<<?>?>?>?[[[[[[[[.[[[[[<<<?" .
   "]]](fix this)]>>>>?]]]]]]]]<?[[[?]]]]]<?[[[[[[[[[[[[[<?>>?";

Once I had the p-machine interpreter, I needed to make the code more unreadable. I took half of the top portion of the code and half of the lower portion and then interlaced the characters of each. I added a for loop to the final code that separated the two halves, concatenated them, then called eval() to interpret the original program.


   for($i=0;$i<368;$i+=2){$s=substr($v,$i,1);$q=$q .
   (($s eq '|') ? "'" : $s);$s=substr($v,$i+1,1);$r=$r .
   (($s eq '|') ? "'" : $s);}eval( $q . $r);print "\n";

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: A GUI for urlx
Next post:Removing IE Popups in C


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

An Interview with Tom Zimmer: Forth System Developer

A Simple ROT13 Macro

A Quine in C

Throwaway Software: HangUp

Shrouding CSharp and Java Source Code with AWK

Choose your own Adventure with Sinatra

Internet Protocols and Rhino JavaScript

Removing IE Popups in C

Blogoversary

Stacking Images with PerlMagick


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer